antlr / antlr/antlr4

[Go target] Generated parser contains interfaces without requisite methods for tree navigation

Open
#3,926 5 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

This is a placeholder for a problem someone noted in StackOverflow ([so74022012](https://stackoverflow.com/q/74022012/4779853). It might be somewhere in the antlr/antlr4 issues, but I cannot find it.

The Go target doesn't generate code with accessor functions using the RHS symbol name as is done with other targets. Consequently, code that makes navigations using the accessor functions have to be type casted in order to go from one node to another in the parse tree.

#### Grammar
[abb](https://github.com/antlr/grammars-v4/blob/master/abb/abbParser.g4)

```
module : moduleData EOF ;
moduleData : MODULE moduleName NEWLINE dataList NEWLINE* ENDMODULE ;
```

#### Generated Java
```
public static class ModuleContext extends ParserRuleContext {
public ModuleDataContext moduleData() { ... }
}
public static class ModuleDataContext extends ParserRuleContext {
public TerminalNode MODULE() { return getToken(abbParser.MODULE, 0); }
public ModuleNameContext moduleName() { ... }
}
```
##### Navigation in Java
```
module.moduleData().moduleName() ...
```

#### Generated Go
```
func (s *ModuleContext) ModuleData() IModuleDataContext { ... }
type IModuleDataContext interface {
antlr.ParserRuleContext

// GetParser returns the parser.
GetParser() antlr.Parser

// IsModuleDataContext differentiates from other interfaces.
IsModuleDataContext()
}
type ModuleDataContext struct {
*antlr.BaseParserRuleContext
parser antlr.Parser
}
```
##### Navigation in Go
```
module.(*parser.ModuleContext).moduleData().(*parser.ModuleDataContext).moduleName()
```
In the generated Go, the accessors return interfaces (unlike that in Java or C#), and the interfaces do not have methods declared for the accessors contained in that interface.

Contributor guide

Open the contributing guide

Research direction

Start with the abb grammar in grammars-v4/abb/abbParser.g4 and compare the generated Java and Go parser examples in the issue. Trace the Go target's generated context interfaces and accessor declarations; done means parse-tree navigation can expose the requisite child accessors without the shown type casts.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.