Open RossPatterson opened 2 years ago
Adding a new attribute could, of course, be a breaking change for some existing grammars.
Seaching Google for "$lexer" and "antlr" finds only hits for using a generated lexer in languages that require or prefer $whatever
variables (e.g., Perl, PHP). Seaching for just "$lexer" doesn't find anything else of significance.
Grepping the grammars-v4
corpus for "$lexer" finds only _scripts/templates/PHP/Test.php
, which is a case of using the generated lexer. Doing the same for the antlr4
repo finds only runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.php.stg
and doc/php-target.md
, which are similiar cases, and runtime/Dart/lib/src/atn/src/lexer_atn_simulator.dart
, runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.php.stg
, tool/src/org/antlr/v4/parse/ANTLRParser.g
, tool/src/org/antlr/v4/parse/ATNBuilder.g
, and tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g
, which are false positives (i.e., substring matches).
That's not an exhaustive check, but it is indicative.
For the same reason that parsers need a
self
-like attribute in semantic predicates(i.e.,self
vs.this
vs. ...), lexers do as well. Commit 38c4e2b0028d2a305d0cf05ce5669d23d67ea88e, back in 2014, added$parser
for this purpose, but attempting to use it in a lexer generates errorATTRIBUTE_IN_LEXER_ACTION
"attribute references not allowed in lexer actions: $parser".The obvious choice for such an attribute is
$lexer
.