Open wxd237 opened 6 years ago
Can you provide error message? What did you meet In your code had some problems, I don't know you knew or not.
main
call str
never used.os
and never usedHere is how I handle errors in my project: First, I define an error listener and count the number of errors. Later on I check i the number of errors is more than one.
type CqlErrorListener struct { errors int }
func (l *CqlErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) { l.errors += 1 }
func (l CqlErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts antlr.BitSet, configs antlr.ATNConfigSet) { l.errors += 1 } func (l CqlErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa antlr.DFA, startIndex, stopIndex int, conflictingAlts antlr.BitSet, configs antlr.ATNConfigSet) { l.errors += 1 } func (l CqlErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa antlr.DFA, startIndex, stopIndex, prediction int, configs antlr.ATNConfigSet) { l.errors += 1 }
Later on in main code I have: p.AddErrorListener(errorListener) lexer.AddErrorListener(errorListener) if errorListener.errors > 0 { panic("Found errors in input") }
I have search antlr4-discussion google group and stackoverflow ,this is no way to solve this.
import ( "github.com/antlr/antlr4/runtime/Go/antlr" "./parser" "fmt" )
type TreeShapeListener struct { *parser.BaseJSONListener }
func NewTreeShapeListener() *TreeShapeListener { return new(TreeShapeListener) }
func (this *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) { fmt.Println(ctx.GetText()) }
func main() { str:=
{"a":1}!
input := new(antlr.FileStream) input.InputStream = antlr.NewInputStream(str) lexer := parser.NewJSONLexer(input) stream := antlr.NewCommonTokenStream(lexer,0) p := parser.NewJSONParser(stream) p.AddErrorListener(antlr.NewDiagnosticErrorListener(true)) p.BuildParseTrees = true tree := p.Json() antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree) }root@iZj6ca7mzaes6eiy2p6fnsZ:~/grammars-v4/json# go run main.go line 1:7 token recognition error at: '!' {"a":1} {"a":1} {"a":1} "a":1 1