manager.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2021 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. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. )
  19. var (
  20. Language = &ParseTree{}
  21. parserFuncRuntime *funcRuntime
  22. )
  23. type ParseTree struct {
  24. Handlers map[ast.Token]func(*Parser) (ast.Statement, error)
  25. Tokens map[ast.Token]*ParseTree
  26. Keys []string
  27. }
  28. func (t *ParseTree) Handle(tok ast.Token, fn func(*Parser) (ast.Statement, error)) {
  29. // Verify that there is no conflict for this token in this parse tree.
  30. if _, conflict := t.Tokens[tok]; conflict {
  31. panic(fmt.Sprintf("conflict for token %s", tok))
  32. }
  33. if _, conflict := t.Handlers[tok]; conflict {
  34. panic(fmt.Sprintf("conflict for token %s", tok))
  35. }
  36. if t.Handlers == nil {
  37. t.Handlers = make(map[ast.Token]func(*Parser) (ast.Statement, error))
  38. }
  39. t.Handlers[tok] = fn
  40. t.Keys = append(t.Keys, tok.String())
  41. }
  42. func (pt *ParseTree) Parse(p *Parser) (ast.Statement, error) {
  43. tok, _ := p.scanIgnoreWhitespace()
  44. p.unscan()
  45. if f, ok := pt.Handlers[tok]; ok {
  46. return f(p)
  47. }
  48. return nil, nil
  49. }
  50. func init() {
  51. Language.Handle(ast.SELECT, func(p *Parser) (ast.Statement, error) {
  52. return p.Parse()
  53. })
  54. Language.Handle(ast.CREATE, func(p *Parser) (statement ast.Statement, e error) {
  55. return p.ParseCreateStmt()
  56. })
  57. Language.Handle(ast.SHOW, func(p *Parser) (statement ast.Statement, e error) {
  58. return p.parseShowStmt()
  59. })
  60. Language.Handle(ast.EXPLAIN, func(p *Parser) (statement ast.Statement, e error) {
  61. return p.parseExplainStmt()
  62. })
  63. Language.Handle(ast.DESCRIBE, func(p *Parser) (statement ast.Statement, e error) {
  64. return p.parseDescribeStmt()
  65. })
  66. Language.Handle(ast.DROP, func(p *Parser) (statement ast.Statement, e error) {
  67. return p.parseDropStmt()
  68. })
  69. }