forked from MontFerret/ferret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
31 lines (24 loc) · 848 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
//go:generate antlr -Xexact-output-dir -o fql -package fql -visitor -Dlanguage=Go antlr/FqlLexer.g4 antlr/FqlParser.g4
package parser
import (
"github.com/MontFerret/ferret/pkg/parser/fql"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
type Parser struct {
tree *fql.FqlParser
}
func New(query string) *Parser {
input := antlr.NewInputStream(query)
lexer := fql.NewFqlLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
p := fql.NewFqlParser(stream)
p.BuildParseTrees = true
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
return &Parser{tree: p}
}
func (p *Parser) AddErrorListener(listener antlr.ErrorListener) {
p.tree.AddErrorListener(listener)
}
func (p *Parser) Visit(visitor fql.FqlParserVisitor) interface{} {
return visitor.VisitProgram(p.tree.Program().(*fql.ProgramContext))
}