type filterListener struct {
*parser.BaseFilterListener
errorListener ErrorListener
}
// errorListener is custom implmentation of error listener
// ExitStart is called when exiting the start production.
func (s *filterListener) ExitStart(c *parser.StartContext) {
fmt.Printf("ExitStart: %v\n", c.GetText())
}
// ExitComparison is called when exiting the comparison production.
func (s *filterListener) ExitComparison(c *parser.ComparisonContext) {
fmt.Printf("ExitComparison: %v\n", c.GetText())
}
// ExitComparison is called when exiting the comparison production.
func (s *filterListener) ExitComparisonAND(c *parser.ComparisonANDContext) {
fmt.Printf("ExitComparison: %v\n", c.GetText())
}
// ExitComparison is called when exiting the comparison production.
func (s *filterListener) ExitComparisonOR(c *parser.ComparisonORContext) {
fmt.Printf("ExitComparison: %v\n", c.GetText())
}
// ExitNestedExpression is called when exiting the nestedExpression production.
func (s *filterListener) ExitNestedExpression(c *parser.NestedExpressionContext) {
fmt.Printf("ExitNestedExpression: %v\n", c.GetText())
}
// Parse
func Parse(input string) (string, string, error) {
// Setup the input.
is := antlr.NewInputStream(input)
// Create the Lexer.
lexer := parser.NewFilterLexer(is)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// Create the Parser.
p := parser.NewFilterParser(stream)
// Finally parse the expression (by walking the tree).
listener := filterListener{}
// Add err listener for the parser.
p.AddErrorListener(&listener.errorListener)
antlr.ParseTreeWalkerDefault.Walk(&listener, p.Start())
// return the err message on encountering
// err while parsing.
if listener.errorListener.err != nil {
fmt.Printf("failed to run")
return "", "", listener.errorListener.err
}
return "", "", nil
}
func main() {
qs := []string{
`test123`,
}
for _, q := range qs {
if _, _, err := Parse(q); err != nil {
fmt.Printf("*********************\n\n failed - %s : %v\n\n*************************** \n", q, err)
}
fmt.Printf("*********************\n\n completed*************************** \n")
}
}