-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally report unused lexer/parser rules #1069
Comments
Yes, it is especially usefull in grammar developing stage. Moreover, parsing from custom rule is rarely used feature. Maybe additional attribute should be added to parser rule? Something like |
What about such syntax for root rules? parserRule1 options { root=true; }
: parserRule2
| TOKEN
...
; If at least one rule this "root" option is enabled, warnings will be generated for unreachable parser rules like this: parserRule1 options { root=true; } // No warning, root.
: parserRule2
: parserRule3
;
parserRule2 // Warning: this rule doesn't have "root" option and references.
: TOKEN1
;
parserRule3 // No warning because it has reference from parserRule1.
: parserRule4
| TOKEN2
;
parserRule5 options { root=true; } // No warning here because it's root.
: TOKEN3
; But if grammar has no root nodes, warnings will not be generated (for back-compatibility issues with old format). In most cases only one root rule required in grammar development. But the root node brings logical clarity. |
UPDATE: this option is not actual now since we have Another idea: put "root" modifier before rule declaration (similary to token "fragment" modifier): root parserRule1
: parserRule2
| TOKEN
...
; Moreover it can be extended to "caseSensitive" or "caseInsensitive" modifier in mode (implemented in this PR): caseInsensitive mode PHP;
Abstract: 'abstract';
Array: 'array';
As: 'as'; |
If roots exist, they will be available in Parser class: public override List<ParseRuleContext> Roots { get; } Similarly to If no root option is detected, |
I think this is a matter for dev tools, like the intellij plugin, not antlr itself so i'm closing. |
But dev tools don't have info about root rules, they highlight everything unused. It's untidy. |
Sam Harwell points out this is dubious given that all parse rules can be considered roots from which a caller can start parsing from, and some rules deliberately left unused (his example https://github.com/tunnelvisionlabs/antlr4-grammar-postgresql/blob/master/src/com/tunnelvisionlabs/postgresql/PostgreSqlLexer.g4#L609-L636).
However it may be helpful to allow them to be reported. Definition of unused rule is any parse/lex rule unreferenced by any other. Put this onto stdout and let the user decide what to do with it.
The text was updated successfully, but these errors were encountered: