antlr / antlr-php-runtime

PHP Runtime for ANTLR4
BSD 3-Clause "New" or "Revised" License
81 stars 19 forks source link

Create Member Variables (unable to use `$` literal) #21

Closed jonathan-shahen closed 2 years ago

jonathan-shahen commented 2 years ago

Here is an example use case (limited depth of nested expressions):

grammar T;

@members {
  private $depth = 0;
}

parse
 : expr EOF
 ;

expr
 : {++depth <= 3}? '(' expr ')' 
 | INT
 ;

INT
 : [0-9]+
 ;

You can ignore the Semantic Predicate, because we cannot even declar the member variable due to the $ being used. The error message is: attribute references not allowed in lexer actions: $depth.

If we replace ~$depth = 0;withdepth = 0;, then it compiles the php class, but the member variable is malformed (php variables require the$`).

Is there a work around here to get this to work?

I found a work around for referencing variables in actions ($parser->var ==> $this->var), which is not the best work around but I will leave that for a separate ticket.

ANTLR Version: 4.9.2 Runtime: 0.5.1 (had to downgrade to get working, see https://github.com/antlr/antlr-php-runtime/issues/19)

Any help would be appreciated :)

jonathan-shahen commented 2 years ago

I am willing to contribute to this project. Just need to know if this a bug/problem, and where I can go about adding either an escape-character (\$) or use another symbol % for referencing attributes.

marcospassos commented 2 years ago

Hi @jonathan-shahen, it's not related to the PHP Runtime itself but the parser generator. It should be fixed there, more specifically here.

marcospassos commented 2 years ago

Hey @jonathan-shahen, is there any question left? May I close this issue?