简体   繁体   中英

AntLR4 in Go: Invalid type assertion: listener

I generated Parser for the Go Language from Antlr4 grammar.

Grammar is here: https://raw.githubusercontent.com/antlr/grammars-v4/master/solidity/Solidity.g4

I generate parser as follows: java -jar $PWD/antlr-4.7.1-complete.jar -Dlanguage=Go -o parser syntax/Solidity.g4

The generated solidity_parser.go file has the following error whereever listener.(SolidityListener) shows up:

Invalid type assertion: listener.(SolidityListener) (non-interface type antlr.ParseTreeListener on left)

Code snippet: func (s *SourceUnitContext) EnterRule(listener antlr.ParseTreeListener) { if listenerT, ok := listener.(SolidityListener); ok { // <-- here is the error listenerT.EnterSourceUnit(s) } } func (s *SourceUnitContext) EnterRule(listener antlr.ParseTreeListener) { if listenerT, ok := listener.(SolidityListener); ok { // <-- here is the error listenerT.EnterSourceUnit(s) } }

What could be the problem?

找不到报关单

I met the same problem. The reason is go cannot find the interface definition, just run go get github.com/antlr/antlr4/runtime/Go/antlr would solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM