lexical.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xsql
  15. import (
  16. "bufio"
  17. "bytes"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "io"
  20. "strconv"
  21. "strings"
  22. )
  23. type Scanner struct {
  24. r *bufio.Reader
  25. }
  26. func NewScanner(r io.Reader) *Scanner {
  27. return &Scanner{r: bufio.NewReader(r)}
  28. }
  29. func (s *Scanner) Scan() (tok ast.Token, lit string) {
  30. ch := s.read()
  31. if isWhiteSpace(ch) {
  32. //s.unread()
  33. return s.ScanWhiteSpace()
  34. } else if isLetter(ch) {
  35. s.unread()
  36. return s.ScanIdent()
  37. } else if isQuotation(ch) {
  38. s.unread()
  39. return s.ScanString()
  40. } else if isDigit(ch) {
  41. s.unread()
  42. return s.ScanNumber(false, false)
  43. } else if isBackquote(ch) {
  44. return s.ScanBackquoteIdent()
  45. }
  46. switch ch {
  47. case eof:
  48. return ast.EOF, ast.Tokens[ast.EOF]
  49. case '=':
  50. return ast.EQ, ast.Tokens[ast.EQ]
  51. case '!':
  52. _, _ = s.ScanWhiteSpace()
  53. if r := s.read(); r == '=' {
  54. return ast.NEQ, ast.Tokens[ast.NEQ]
  55. } else {
  56. s.unread()
  57. }
  58. return ast.EQ, ast.Tokens[ast.EQ]
  59. case '<':
  60. _, _ = s.ScanWhiteSpace()
  61. if r := s.read(); r == '=' {
  62. return ast.LTE, ast.Tokens[ast.LTE]
  63. } else {
  64. s.unread()
  65. }
  66. return ast.LT, ast.Tokens[ast.LT]
  67. case '>':
  68. _, _ = s.ScanWhiteSpace()
  69. if r := s.read(); r == '=' {
  70. return ast.GTE, ast.Tokens[ast.GTE]
  71. } else {
  72. s.unread()
  73. }
  74. return ast.GT, ast.Tokens[ast.GT]
  75. case '+':
  76. return ast.ADD, ast.Tokens[ast.ADD]
  77. case '-':
  78. _, _ = s.ScanWhiteSpace()
  79. if r := s.read(); r == '-' {
  80. s.skipUntilNewline()
  81. return ast.COMMENT, ""
  82. } else if r == '>' {
  83. return ast.ARROW, ast.Tokens[ast.ARROW]
  84. } else if r == '.' {
  85. _, _ = s.ScanWhiteSpace()
  86. if r1 := s.read(); isDigit(r1) {
  87. s.unread()
  88. return s.ScanNumber(true, true)
  89. } else {
  90. s.unread()
  91. }
  92. s.unread()
  93. } else {
  94. s.unread()
  95. }
  96. return ast.SUB, ast.Tokens[ast.SUB]
  97. case '/':
  98. _, _ = s.ScanWhiteSpace()
  99. if r := s.read(); r == '*' {
  100. if err := s.skipUntilEndComment(); err != nil {
  101. return ast.ILLEGAL, ""
  102. }
  103. return ast.COMMENT, ""
  104. } else {
  105. s.unread()
  106. }
  107. return ast.DIV, ast.Tokens[ast.DIV]
  108. case '.':
  109. if r := s.read(); isDigit(r) {
  110. s.unread()
  111. return s.ScanNumber(true, false)
  112. }
  113. s.unread()
  114. return ast.DOT, ast.Tokens[ast.DOT]
  115. case '%':
  116. return ast.MOD, ast.Tokens[ast.MOD]
  117. case '&':
  118. return ast.BITWISE_AND, ast.Tokens[ast.BITWISE_AND]
  119. case '|':
  120. return ast.BITWISE_OR, ast.Tokens[ast.BITWISE_OR]
  121. case '^':
  122. return ast.BITWISE_XOR, ast.Tokens[ast.BITWISE_XOR]
  123. case '*':
  124. return ast.ASTERISK, ast.Tokens[ast.ASTERISK]
  125. case ',':
  126. return ast.COMMA, ast.Tokens[ast.COMMA]
  127. case '(':
  128. return ast.LPAREN, ast.Tokens[ast.LPAREN]
  129. case ')':
  130. return ast.RPAREN, ast.Tokens[ast.RPAREN]
  131. case '[':
  132. return ast.LBRACKET, ast.Tokens[ast.LBRACKET]
  133. case ']':
  134. return ast.RBRACKET, ast.Tokens[ast.RBRACKET]
  135. case ':':
  136. return ast.COLON, ast.Tokens[ast.COLON]
  137. case '#':
  138. return ast.HASH, ast.Tokens[ast.HASH]
  139. case ';':
  140. return ast.SEMICOLON, ast.Tokens[ast.SEMICOLON]
  141. }
  142. return ast.ILLEGAL, ""
  143. }
  144. func (s *Scanner) ScanIdent() (tok ast.Token, lit string) {
  145. var buf bytes.Buffer
  146. buf.WriteRune(s.read())
  147. for {
  148. if ch := s.read(); ch == eof {
  149. break
  150. } else if !isLetter(ch) && !isDigit(ch) && ch != '_' {
  151. s.unread()
  152. break
  153. } else {
  154. buf.WriteRune(ch)
  155. }
  156. }
  157. switch lit = strings.ToUpper(buf.String()); lit {
  158. case "SELECT":
  159. return ast.SELECT, lit
  160. case "AS":
  161. return ast.AS, lit
  162. case "FROM":
  163. return ast.FROM, lit
  164. case "WHERE":
  165. return ast.WHERE, lit
  166. case "AND":
  167. return ast.AND, lit
  168. case "OR":
  169. return ast.OR, lit
  170. case "GROUP":
  171. return ast.GROUP, lit
  172. case "HAVING":
  173. return ast.HAVING, lit
  174. case "ORDER":
  175. return ast.ORDER, lit
  176. case "BY":
  177. return ast.BY, lit
  178. case "DESC":
  179. return ast.DESC, lit
  180. case "ASC":
  181. return ast.ASC, lit
  182. case "FILTER":
  183. return ast.FILTER, lit
  184. case "INNER":
  185. return ast.INNER, lit
  186. case "LEFT":
  187. return ast.LEFT, lit
  188. case "RIGHT":
  189. return ast.RIGHT, lit
  190. case "FULL":
  191. return ast.FULL, lit
  192. case "CROSS":
  193. return ast.CROSS, lit
  194. case "JOIN":
  195. return ast.JOIN, lit
  196. case "ON":
  197. return ast.ON, lit
  198. case "CASE":
  199. return ast.CASE, lit
  200. case "WHEN":
  201. return ast.WHEN, lit
  202. case "THEN":
  203. return ast.THEN, lit
  204. case "ELSE":
  205. return ast.ELSE, lit
  206. case "END":
  207. return ast.END, lit
  208. case "IN":
  209. return ast.IN, lit
  210. case "NOT":
  211. return ast.NOT, lit
  212. case "BETWEEN":
  213. return ast.BETWEEN, lit
  214. case "LIKE":
  215. return ast.LIKE, lit
  216. case "CREATE":
  217. return ast.CREATE, lit
  218. case "DROP":
  219. return ast.DROP, lit
  220. case "EXPLAIN":
  221. return ast.EXPLAIN, lit
  222. case "DESCRIBE":
  223. return ast.DESCRIBE, lit
  224. case "SHOW":
  225. return ast.SHOW, lit
  226. case "STREAM":
  227. return ast.STREAM, lit
  228. case "STREAMS":
  229. return ast.STREAMS, lit
  230. case "TABLE":
  231. return ast.TABLE, lit
  232. case "TABLES":
  233. return ast.TABLES, lit
  234. case "WITH":
  235. return ast.WITH, lit
  236. case "BIGINT":
  237. return ast.XBIGINT, lit
  238. case "FLOAT":
  239. return ast.XFLOAT, lit
  240. case "DATETIME":
  241. return ast.XDATETIME, lit
  242. case "STRING":
  243. return ast.XSTRING, lit
  244. case "BYTEA":
  245. return ast.XBYTEA, lit
  246. case "BOOLEAN":
  247. return ast.XBOOLEAN, lit
  248. case "ARRAY":
  249. return ast.XARRAY, lit
  250. case "STRUCT":
  251. return ast.XSTRUCT, lit
  252. case "DATASOURCE":
  253. return ast.DATASOURCE, lit
  254. case "KEY":
  255. return ast.KEY, lit
  256. case "FORMAT":
  257. return ast.FORMAT, lit
  258. case "CONF_KEY":
  259. return ast.CONF_KEY, lit
  260. case "TYPE":
  261. return ast.TYPE, lit
  262. case "TRUE":
  263. return ast.TRUE, lit
  264. case "FALSE":
  265. return ast.FALSE, lit
  266. case "STRICT_VALIDATION":
  267. return ast.STRICT_VALIDATION, lit
  268. case "TIMESTAMP":
  269. return ast.TIMESTAMP, lit
  270. case "TIMESTAMP_FORMAT":
  271. return ast.TIMESTAMP_FORMAT, lit
  272. case "RETAIN_SIZE":
  273. return ast.RETAIN_SIZE, lit
  274. case "SHARED":
  275. return ast.SHARED, lit
  276. case "SCHEMAID":
  277. return ast.SCHEMAID, lit
  278. case "KIND":
  279. return ast.KIND, lit
  280. case "DD":
  281. return ast.DD, lit
  282. case "HH":
  283. return ast.HH, lit
  284. case "MI":
  285. return ast.MI, lit
  286. case "SS":
  287. return ast.SS, lit
  288. case "MS":
  289. return ast.MS, lit
  290. }
  291. return ast.IDENT, buf.String()
  292. }
  293. func (s *Scanner) ScanString() (tok ast.Token, lit string) {
  294. var buf bytes.Buffer
  295. ch := s.read()
  296. buf.WriteRune(ch)
  297. escape := false
  298. for {
  299. ch = s.read()
  300. if ch == '"' && !escape {
  301. buf.WriteRune(ch)
  302. break
  303. } else if ch == eof {
  304. return ast.BADSTRING, buf.String()
  305. } else if ch == '\\' && !escape {
  306. escape = true
  307. buf.WriteRune(ch)
  308. } else {
  309. escape = false
  310. buf.WriteRune(ch)
  311. }
  312. }
  313. r, err := strconv.Unquote(buf.String())
  314. if err != nil {
  315. return ast.ILLEGAL, "invalid string: " + buf.String()
  316. }
  317. return ast.STRING, r
  318. }
  319. func (s *Scanner) ScanDigit() (tok ast.Token, lit string) {
  320. var buf bytes.Buffer
  321. ch := s.read()
  322. buf.WriteRune(ch)
  323. for {
  324. if ch := s.read(); isDigit(ch) {
  325. buf.WriteRune(ch)
  326. } else {
  327. s.unread()
  328. break
  329. }
  330. }
  331. return ast.INTEGER, buf.String()
  332. }
  333. func (s *Scanner) ScanNumber(startWithDot bool, isNeg bool) (tok ast.Token, lit string) {
  334. var buf bytes.Buffer
  335. if isNeg {
  336. buf.WriteRune('-')
  337. }
  338. if startWithDot {
  339. buf.WriteRune('.')
  340. }
  341. ch := s.read()
  342. buf.WriteRune(ch)
  343. isNum := false
  344. for {
  345. if ch := s.read(); isDigit(ch) {
  346. buf.WriteRune(ch)
  347. } else if ch == '.' {
  348. isNum = true
  349. buf.WriteRune(ch)
  350. } else {
  351. s.unread()
  352. break
  353. }
  354. }
  355. if isNum || startWithDot {
  356. return ast.NUMBER, buf.String()
  357. } else {
  358. return ast.INTEGER, buf.String()
  359. }
  360. }
  361. func (s *Scanner) ScanBackquoteIdent() (tok ast.Token, lit string) {
  362. var buf bytes.Buffer
  363. for {
  364. ch := s.read()
  365. if isBackquote(ch) || ch == eof {
  366. break
  367. } else {
  368. buf.WriteRune(ch)
  369. }
  370. }
  371. return ast.IDENT, buf.String()
  372. }
  373. func (s *Scanner) skipUntilNewline() {
  374. for {
  375. if ch := s.read(); ch == '\n' || ch == eof {
  376. return
  377. }
  378. }
  379. }
  380. func (s *Scanner) skipUntilEndComment() error {
  381. for {
  382. if ch1 := s.read(); ch1 == '*' {
  383. // We might be at the end.
  384. star:
  385. ch2 := s.read()
  386. if ch2 == '/' {
  387. return nil
  388. } else if ch2 == '*' {
  389. // We are back in the state machine since we see a star.
  390. goto star
  391. } else if ch2 == eof {
  392. return io.EOF
  393. }
  394. } else if ch1 == eof {
  395. return io.EOF
  396. }
  397. }
  398. }
  399. func (s *Scanner) ScanWhiteSpace() (tok ast.Token, lit string) {
  400. var buf bytes.Buffer
  401. for {
  402. if ch := s.read(); ch == eof {
  403. break
  404. } else if !isWhiteSpace(ch) {
  405. s.unread()
  406. break
  407. } else {
  408. buf.WriteRune(ch)
  409. }
  410. }
  411. return ast.WS, buf.String()
  412. }
  413. func (s *Scanner) read() rune {
  414. ch, _, err := s.r.ReadRune()
  415. if err != nil {
  416. return eof
  417. }
  418. return ch
  419. }
  420. func (s *Scanner) unread() {
  421. _ = s.r.UnreadRune()
  422. }
  423. var eof = rune(0)
  424. func isWhiteSpace(r rune) bool {
  425. return (r == ' ') || (r == '\t') || (r == '\r') || (r == '\n')
  426. }
  427. func isLetter(ch rune) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') }
  428. func isDigit(ch rune) bool { return ch >= '0' && ch <= '9' }
  429. func isQuotation(ch rune) bool { return ch == '"' }
  430. func isBackquote(ch rune) bool { return ch == '`' }