antlr / antlr/antlr4

Golang target @ member variables are global

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

Description

In the Java language target, variables declared in the @lexer::members section get declared within the lexer class. In the golang target the variables get declared as global variables. For example:

// TestLexer.g4
lexer grammar TestLexer;

@lexer::members {
var depth int
}

OPAR : '(' { depth++ } ;
CPAR : ')' { depth-- } ;

It is not clear if this is by design or not (but it would seem not). I am not familiar with the Antlr internals, but it seems the change is only related to the Go.stg template file. From this:

type struct {
*antlr.BaseLexer
channelNames []string
modeNames []string
// TODO: EOF string
}

To this (moving the template section for @lexer::members):

type struct {
*antlr.BaseLexer
channelNames []string
modeNames []string



// TODO: EOF string
}

And access the values via the receiver (aka l.depth).

My application was requiring recursive substitution - hence the ability to have lexer-instance specific values is important
(same issues apply to generated parsers).

Any pointers to operator error, or work-arounds highly appreciated.

Contributor guide

Open the contributing guide

Research direction

Start with the Go.stg template and the TestLexer.g4 example. Generate the lexer and inspect how @lexer::members declarations are emitted and accessed; done means member variables are scoped to each generated lexer instance, with the same behavior applying to generated parsers.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.