antlr / antlr/antlr4

Go Target:how Exit when Error occurred

Open
#2,158 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

I have search [antlr4-discussion google group](https://groups.google.com/forum/#!forum/antlr-discussion) and stackoverflow ,this is no way to solve this.

1. I use the example [JSON.g4](https://github.com/antlr/grammars-v4/tree/master/json).
2. execute antlr4
antlr4 -Dlanguage=Go -o parser JSON.g4
3. the main.go
```
package main

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)
}
```
4.
`go run main.go`
I want the the error message and hope exit when error occurred this is the output:
```
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
```

Contributor guide

Open the contributing guide

Research direction

Start with main.go and the JSON.g4 example, then inspect how the generated Go parser and Go runtime report the trailing '!'. Run `go run main.go` to reproduce the diagnostic and tree-walking output. Done means the error is exposed to the caller and the requested behavior of stopping after an error is defined and demonstrated.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.