-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
46 lines (31 loc) · 1.12 KB
/
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
package cql
import (
"github.com/antlr4-go/antlr/v4"
parser "github.com/hawyar/cql/internal"
)
func Parse(chars antlr.CharStream) (*AST, []Err) {
if chars.Size() == 0 {
return nil, []Err{{0, 0, "empty input"}}
}
lexer := parser.NewcqlLexer(chars)
// lexer.RemoveErrorListeners()
// lexer.AddErrorListener()
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
parseErr := &ErrorListener{}
parser := parser.NewcqlParser(stream)
parser.GetInterpreter().SetPredictionMode(antlr.PredictionModeLL)
parser.BuildParseTrees = true
parser.RemoveErrorListeners()
parser.AddErrorListener(parseErr)
// LibraryDefintion, Definition, or Statement followed by EOF otherwise panic
entry := parser.GetTokenStream().LT(1)
if entry.GetTokenType() != 1 {
return nil, []Err{{entry.GetLine(), entry.GetColumn(), "unexpected token: " + entry.GetText() + "expected LibraryDefinition, Definition, or Statement"}}
}
listener := NewCQLListener()
antlr.ParseTreeWalkerDefault.Walk(listener, parser.Library())
if len(parseErr.SyntaxErrors()) > 0 {
return nil, parseErr.SyntaxErrors()
}
return listener.AST(), nil
}