forked from jasder/antlr
Add compiler warning 157: UNRECOGNIZED_ASSOC_OPTION (fixes #489)
This commit is contained in:
parent
96c22611c4
commit
2b3ef648f8
|
@ -152,6 +152,9 @@ public class LeftRecursiveRuleTransformer {
|
|||
RuleCollector ruleCollector = new RuleCollector(g);
|
||||
ruleCollector.visit(t, "rule");
|
||||
BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
|
||||
// disable the assoc element option checks because they are already
|
||||
// handled for the pre-transformed rule.
|
||||
basics.checkAssocElementOption = false;
|
||||
basics.visit(t, "rule");
|
||||
|
||||
// track recursive alt info for codegen
|
||||
|
|
|
@ -101,6 +101,15 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
|
|||
public RuleCollector ruleCollector;
|
||||
public ErrorManager errMgr;
|
||||
|
||||
/**
|
||||
* When this is {@code true}, the semantic checks will report
|
||||
* {@link ErrorType#UNRECOGNIZED_ASSOC_OPTION} where appropriate. This may
|
||||
* be set to {@code false} to disable this specific check.
|
||||
*
|
||||
* <p>The default value is {@code true}.</p>
|
||||
*/
|
||||
public boolean checkAssocElementOption = true;
|
||||
|
||||
/**
|
||||
* This field is used for reporting the {@link ErrorType#MODE_WITHOUT_RULES}
|
||||
* error when necessary.
|
||||
|
@ -477,6 +486,17 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
|
|||
GrammarAST ID,
|
||||
GrammarAST valueAST)
|
||||
{
|
||||
if (checkAssocElementOption && ID != null && "assoc".equals(ID.getText())) {
|
||||
if (elem.getType() != ANTLRParser.ALT) {
|
||||
Token optionID = ID.token;
|
||||
String fileName = optionID.getInputStream().getSourceName();
|
||||
g.tool.errMgr.grammarError(ErrorType.UNRECOGNIZED_ASSOC_OPTION,
|
||||
fileName,
|
||||
optionID,
|
||||
currentRuleName);
|
||||
}
|
||||
}
|
||||
|
||||
if ( elem instanceof TerminalAST ) {
|
||||
return checkTokenOptions((TerminalAST)elem, ID, valueAST);
|
||||
}
|
||||
|
|
|
@ -826,6 +826,32 @@ public enum ErrorType {
|
|||
* @since 4.2.1
|
||||
*/
|
||||
INVALID_ESCAPE_SEQUENCE(156, "invalid escape sequence", ErrorSeverity.ERROR),
|
||||
/**
|
||||
* Compiler Warning 157.
|
||||
*
|
||||
* <p>rule '<em>rule</em>' contains an 'assoc' element option in an
|
||||
* unrecognized location</p>
|
||||
*
|
||||
* <p>
|
||||
* In ANTLR 4.2, the position of the {@code assoc} element option was moved
|
||||
* from the operator terminal(s) to the alternative itself. This warning is
|
||||
* reported when an {@code assoc} element option is specified on a grammar
|
||||
* element that is not recognized by the current version of ANTLR, and as a
|
||||
* result will simply be ignored.
|
||||
* </p>
|
||||
*
|
||||
* <p>The following rule produces this warning.</p>
|
||||
*
|
||||
* <pre>
|
||||
* x : 'x'
|
||||
* | x '+'<assoc=right> x // warning 157
|
||||
* |<assoc=right> x '*' x // ok
|
||||
* ;
|
||||
* </pre>
|
||||
*
|
||||
* @since 4.2.1
|
||||
*/
|
||||
UNRECOGNIZED_ASSOC_OPTION(157, "rule '<arg>' contains an 'assoc' terminal option in an unrecognized location", ErrorSeverity.WARNING),
|
||||
|
||||
/*
|
||||
* Backward incompatibility errors
|
||||
|
|
|
@ -451,4 +451,26 @@ public class TestToolSyntaxErrors extends BaseTest {
|
|||
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test ensures the {@link ErrorType#UNRECOGNIZED_ASSOC_OPTION} warning
|
||||
* is produced as described in the documentation.
|
||||
*/
|
||||
@Test public void testUnrecognizedAssocOption() {
|
||||
String grammar =
|
||||
"grammar A;\n" +
|
||||
"x : 'x'\n" +
|
||||
" | x '+'<assoc=right> x // warning 157\n" +
|
||||
" |<assoc=right> x '*' x // ok\n" +
|
||||
" ;\n";
|
||||
String expected =
|
||||
"warning(" + ErrorType.UNRECOGNIZED_ASSOC_OPTION.code + "): A.g4:3:10: rule 'x' contains an 'assoc' terminal option in an unrecognized location\n";
|
||||
|
||||
String[] pair = new String[] {
|
||||
grammar,
|
||||
expected
|
||||
};
|
||||
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue