1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-01-26 03:51:57 +02:00
ferret/pkg/parser/parser.go
2021-04-08 23:37:16 -04:00

41 lines
1.2 KiB
Go

//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"
resources "github.com/antlr/antlr4/doc/resources"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
type Parser struct {
tree *fql.FqlParser
}
func New(query string) *Parser {
input := antlr.NewInputStream(query)
// converts tokens to upper case, so now it doesn’t matter
// in which case the tokens were entered
upper := resources.NewCaseChangingStream(input, true)
lexer := fql.NewFqlLexer(upper)
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))
}
func (p *Parser) Walk(listener fql.FqlParserListener) {
antlr.ParseTreeWalkerDefault.Walk(listener, p.tree.Program())
}