lexical.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 "CREATE":
  213. return ast.CREATE, lit
  214. case "DROP":
  215. return ast.DROP, lit
  216. case "EXPLAIN":
  217. return ast.EXPLAIN, lit
  218. case "DESCRIBE":
  219. return ast.DESCRIBE, lit
  220. case "SHOW":
  221. return ast.SHOW, lit
  222. case "STREAM":
  223. return ast.STREAM, lit
  224. case "STREAMS":
  225. return ast.STREAMS, lit
  226. case "TABLE":
  227. return ast.TABLE, lit
  228. case "TABLES":
  229. return ast.TABLES, lit
  230. case "WITH":
  231. return ast.WITH, lit
  232. case "BIGINT":
  233. return ast.XBIGINT, lit
  234. case "FLOAT":
  235. return ast.XFLOAT, lit
  236. case "DATETIME":
  237. return ast.XDATETIME, lit
  238. case "STRING":
  239. return ast.XSTRING, lit
  240. case "BYTEA":
  241. return ast.XBYTEA, lit
  242. case "BOOLEAN":
  243. return ast.XBOOLEAN, lit
  244. case "ARRAY":
  245. return ast.XARRAY, lit
  246. case "STRUCT":
  247. return ast.XSTRUCT, lit
  248. case "DATASOURCE":
  249. return ast.DATASOURCE, lit
  250. case "KEY":
  251. return ast.KEY, lit
  252. case "FORMAT":
  253. return ast.FORMAT, lit
  254. case "CONF_KEY":
  255. return ast.CONF_KEY, lit
  256. case "TYPE":
  257. return ast.TYPE, lit
  258. case "TRUE":
  259. return ast.TRUE, lit
  260. case "FALSE":
  261. return ast.FALSE, lit
  262. case "STRICT_VALIDATION":
  263. return ast.STRICT_VALIDATION, lit
  264. case "TIMESTAMP":
  265. return ast.TIMESTAMP, lit
  266. case "TIMESTAMP_FORMAT":
  267. return ast.TIMESTAMP_FORMAT, lit
  268. case "RETAIN_SIZE":
  269. return ast.RETAIN_SIZE, lit
  270. case "SHARED":
  271. return ast.SHARED, lit
  272. case "SCHEMAID":
  273. return ast.SCHEMAID, lit
  274. case "DD":
  275. return ast.DD, lit
  276. case "HH":
  277. return ast.HH, lit
  278. case "MI":
  279. return ast.MI, lit
  280. case "SS":
  281. return ast.SS, lit
  282. case "MS":
  283. return ast.MS, lit
  284. }
  285. return ast.IDENT, buf.String()
  286. }
  287. func (s *Scanner) ScanString() (tok ast.Token, lit string) {
  288. var buf bytes.Buffer
  289. ch := s.read()
  290. buf.WriteRune(ch)
  291. escape := false
  292. for {
  293. ch = s.read()
  294. if ch == '"' && !escape {
  295. buf.WriteRune(ch)
  296. break
  297. } else if ch == eof {
  298. return ast.BADSTRING, buf.String()
  299. } else if ch == '\\' && !escape {
  300. escape = true
  301. buf.WriteRune(ch)
  302. } else {
  303. escape = false
  304. buf.WriteRune(ch)
  305. }
  306. }
  307. r, err := strconv.Unquote(buf.String())
  308. if err != nil {
  309. return ast.ILLEGAL, "invalid string: " + buf.String()
  310. }
  311. return ast.STRING, r
  312. }
  313. func (s *Scanner) ScanDigit() (tok ast.Token, lit string) {
  314. var buf bytes.Buffer
  315. ch := s.read()
  316. buf.WriteRune(ch)
  317. for {
  318. if ch := s.read(); isDigit(ch) {
  319. buf.WriteRune(ch)
  320. } else {
  321. s.unread()
  322. break
  323. }
  324. }
  325. return ast.INTEGER, buf.String()
  326. }
  327. func (s *Scanner) ScanNumber(startWithDot bool, isNeg bool) (tok ast.Token, lit string) {
  328. var buf bytes.Buffer
  329. if isNeg {
  330. buf.WriteRune('-')
  331. }
  332. if startWithDot {
  333. buf.WriteRune('.')
  334. }
  335. ch := s.read()
  336. buf.WriteRune(ch)
  337. isNum := false
  338. for {
  339. if ch := s.read(); isDigit(ch) {
  340. buf.WriteRune(ch)
  341. } else if ch == '.' {
  342. isNum = true
  343. buf.WriteRune(ch)
  344. } else {
  345. s.unread()
  346. break
  347. }
  348. }
  349. if isNum || startWithDot {
  350. return ast.NUMBER, buf.String()
  351. } else {
  352. return ast.INTEGER, buf.String()
  353. }
  354. }
  355. func (s *Scanner) ScanBackquoteIdent() (tok ast.Token, lit string) {
  356. var buf bytes.Buffer
  357. for {
  358. ch := s.read()
  359. if isBackquote(ch) || ch == eof {
  360. break
  361. } else {
  362. buf.WriteRune(ch)
  363. }
  364. }
  365. return ast.IDENT, buf.String()
  366. }
  367. func (s *Scanner) skipUntilNewline() {
  368. for {
  369. if ch := s.read(); ch == '\n' || ch == eof {
  370. return
  371. }
  372. }
  373. }
  374. func (s *Scanner) skipUntilEndComment() error {
  375. for {
  376. if ch1 := s.read(); ch1 == '*' {
  377. // We might be at the end.
  378. star:
  379. ch2 := s.read()
  380. if ch2 == '/' {
  381. return nil
  382. } else if ch2 == '*' {
  383. // We are back in the state machine since we see a star.
  384. goto star
  385. } else if ch2 == eof {
  386. return io.EOF
  387. }
  388. } else if ch1 == eof {
  389. return io.EOF
  390. }
  391. }
  392. }
  393. func (s *Scanner) ScanWhiteSpace() (tok ast.Token, lit string) {
  394. var buf bytes.Buffer
  395. for {
  396. if ch := s.read(); ch == eof {
  397. break
  398. } else if !isWhiteSpace(ch) {
  399. s.unread()
  400. break
  401. } else {
  402. buf.WriteRune(ch)
  403. }
  404. }
  405. return ast.WS, buf.String()
  406. }
  407. func (s *Scanner) read() rune {
  408. ch, _, err := s.r.ReadRune()
  409. if err != nil {
  410. return eof
  411. }
  412. return ch
  413. }
  414. func (s *Scanner) unread() {
  415. _ = s.r.UnreadRune()
  416. }
  417. var eof = rune(0)
  418. func isWhiteSpace(r rune) bool {
  419. return (r == ' ') || (r == '\t') || (r == '\r') || (r == '\n')
  420. }
  421. func isLetter(ch rune) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') }
  422. func isDigit(ch rune) bool { return ch >= '0' && ch <= '9' }
  423. func isQuotation(ch rune) bool { return ch == '"' }
  424. func isBackquote(ch rune) bool { return ch == '`' }