manager.go 2.3 KB

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