lexical.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2021-2023 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. "io"
  19. "strconv"
  20. "strings"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  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(ch == '\'')
  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 "OVER":
  217. return ast.OVER, lit
  218. case "PARTITION":
  219. return ast.PARTITION, lit
  220. case "TRUE":
  221. return ast.TRUE, lit
  222. case "FALSE":
  223. return ast.FALSE, lit
  224. case "DD":
  225. return ast.DD, lit
  226. case "HH":
  227. return ast.HH, lit
  228. case "MI":
  229. return ast.MI, lit
  230. case "SS":
  231. return ast.SS, lit
  232. case "MS":
  233. return ast.MS, lit
  234. }
  235. return ast.IDENT, buf.String()
  236. }
  237. func (s *Scanner) ScanString(isSingle bool) (tok ast.Token, lit string) {
  238. var buf bytes.Buffer
  239. ch := s.read()
  240. if ch == '\'' && isSingle {
  241. buf.WriteRune('"')
  242. } else {
  243. buf.WriteRune(ch)
  244. }
  245. escape := false
  246. for {
  247. ch = s.read()
  248. if ch == '"' && !escape {
  249. if !isSingle {
  250. buf.WriteRune(ch)
  251. break
  252. } else {
  253. escape = false
  254. buf.WriteRune('\\')
  255. buf.WriteRune(ch)
  256. }
  257. } else if ch == '\'' && !escape && isSingle {
  258. buf.WriteRune('"')
  259. break
  260. } else if ch == eof {
  261. return ast.BADSTRING, buf.String()
  262. } else if ch == '\\' && !escape {
  263. escape = true
  264. nextCh := s.read()
  265. if nextCh == '\'' && isSingle {
  266. buf.WriteRune(nextCh)
  267. } else {
  268. buf.WriteRune(ch)
  269. s.unread()
  270. }
  271. } else {
  272. escape = false
  273. buf.WriteRune(ch)
  274. }
  275. }
  276. r, err := strconv.Unquote(buf.String())
  277. if err != nil {
  278. return ast.ILLEGAL, "invalid string: " + buf.String()
  279. }
  280. if isSingle {
  281. return ast.SINGLEQUOTE, r
  282. }
  283. return ast.STRING, r
  284. }
  285. func (s *Scanner) ScanDigit() (tok ast.Token, lit string) {
  286. var buf bytes.Buffer
  287. ch := s.read()
  288. buf.WriteRune(ch)
  289. for {
  290. if ch := s.read(); isDigit(ch) {
  291. buf.WriteRune(ch)
  292. } else {
  293. s.unread()
  294. break
  295. }
  296. }
  297. return ast.INTEGER, buf.String()
  298. }
  299. func (s *Scanner) ScanNumber(startWithDot bool, isNeg bool) (tok ast.Token, lit string) {
  300. var buf bytes.Buffer
  301. if isNeg {
  302. buf.WriteRune('-')
  303. }
  304. if startWithDot {
  305. buf.WriteRune('.')
  306. }
  307. ch := s.read()
  308. buf.WriteRune(ch)
  309. isNum := false
  310. for {
  311. if ch := s.read(); isDigit(ch) {
  312. buf.WriteRune(ch)
  313. } else if ch == '.' {
  314. isNum = true
  315. buf.WriteRune(ch)
  316. } else {
  317. s.unread()
  318. break
  319. }
  320. }
  321. if isNum || startWithDot {
  322. return ast.NUMBER, buf.String()
  323. } else {
  324. return ast.INTEGER, buf.String()
  325. }
  326. }
  327. func (s *Scanner) ScanBackquoteIdent() (tok ast.Token, lit string) {
  328. var buf bytes.Buffer
  329. for {
  330. ch := s.read()
  331. if isBackquote(ch) || ch == eof {
  332. break
  333. }
  334. buf.WriteRune(ch)
  335. }
  336. return ast.IDENT, buf.String()
  337. }
  338. func (s *Scanner) skipUntilNewline() {
  339. for {
  340. if ch := s.read(); ch == '\n' || ch == eof {
  341. return
  342. }
  343. }
  344. }
  345. func (s *Scanner) skipUntilEndComment() error {
  346. for {
  347. if ch1 := s.read(); ch1 == '*' {
  348. // We might be at the end.
  349. star:
  350. ch2 := s.read()
  351. if ch2 == '/' {
  352. return nil
  353. } else if ch2 == '*' {
  354. // We are back in the state machine since we see a star.
  355. goto star
  356. } else if ch2 == eof {
  357. return io.EOF
  358. }
  359. } else if ch1 == eof {
  360. return io.EOF
  361. }
  362. }
  363. }
  364. func (s *Scanner) ScanWhiteSpace() (tok ast.Token, lit string) {
  365. var buf bytes.Buffer
  366. for {
  367. if ch := s.read(); ch == eof {
  368. break
  369. } else if !isWhiteSpace(ch) {
  370. s.unread()
  371. break
  372. } else {
  373. buf.WriteRune(ch)
  374. }
  375. }
  376. return ast.WS, buf.String()
  377. }
  378. func (s *Scanner) read() rune {
  379. ch, _, err := s.r.ReadRune()
  380. if err != nil {
  381. return eof
  382. }
  383. return ch
  384. }
  385. func (s *Scanner) unread() {
  386. _ = s.r.UnreadRune()
  387. }
  388. var eof = rune(0)
  389. func isWhiteSpace(r rune) bool {
  390. return (r == ' ') || (r == '\t') || (r == '\r') || (r == '\n')
  391. }
  392. func isLetter(ch rune) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') }
  393. func isDigit(ch rune) bool { return ch >= '0' && ch <= '9' }
  394. func isQuotation(ch rune) bool { return ch == '"' || ch == '\'' }
  395. func isBackquote(ch rune) bool { return ch == '`' }