manager.go 2.5 KB

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