-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
47 lines (38 loc) · 882 Bytes
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package sqlparser
import "sync"
// parserPool is a pool for parser objects.
var parserPool = sync.Pool{
New: func() interface{} {
return &yyParserImpl{}
},
}
var zeroParser yyParserImpl
func yyParsePooled(yylex yyLexer) int {
parser := parserPool.Get().(*yyParserImpl)
defer func() {
*parser = zeroParser
parserPool.Put(parser)
}()
return parser.Parse(yylex)
}
// Parse parses an statement into an AST.
func Parse(statement string) (*AST, error) {
// yyErrorVerbose = true
// yyDebug = 4
if len(statement) == 0 {
return &AST{}, nil
}
lexer := &Lexer{}
lexer.errors = make(map[int]error)
lexer.input = []byte(statement)
lexer.readByte()
yyParsePooled(lexer)
if lexer.syntaxError != nil {
return nil, lexer.syntaxError
}
if len(lexer.errors) != 0 {
lexer.ast.Errors = lexer.errors
return lexer.ast, lexer.errors[0]
}
return lexer.ast, nil
}