Merge pull request #514 from sharwell/fix-500
Add compile error 159: RESERVED_RULE_NAME (fixes #500)
This commit is contained in:
commit
08300b457f
|
@ -67,6 +67,11 @@ public class SymbolChecks {
|
|||
|
||||
public ErrorManager errMgr;
|
||||
|
||||
protected final Set<String> reservedNames = new HashSet<String>();
|
||||
{
|
||||
reservedNames.add("EOF");
|
||||
}
|
||||
|
||||
public SymbolChecks(Grammar g, SymbolCollector collector) {
|
||||
this.g = g;
|
||||
this.collector = collector;
|
||||
|
@ -94,6 +99,7 @@ public class SymbolChecks {
|
|||
if ( g.rules!=null ) {
|
||||
for (Rule r : g.rules.values()) nameToRuleMap.put(r.name, r);
|
||||
}
|
||||
checkReservedNames(g.rules.values());
|
||||
checkActionRedefinitions(collector.namedActions);
|
||||
checkForTokenConflicts(collector.tokenIDRefs); // sets tokenIDs
|
||||
checkForLabelConflicts(g.rules.values());
|
||||
|
@ -257,6 +263,14 @@ public class SymbolChecks {
|
|||
}
|
||||
}
|
||||
|
||||
protected void checkReservedNames(@NotNull Collection<Rule> rules) {
|
||||
for (Rule rule : rules) {
|
||||
if (reservedNames.contains(rule.name)) {
|
||||
errMgr.grammarError(ErrorType.RESERVED_RULE_NAME, g.fileName, ((GrammarAST)rule.ast.getChild(0)).getToken(), rule.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CAN ONLY CALL THE TWO NEXT METHODS AFTER GRAMMAR HAS RULE DEFS (see semanticpipeline)
|
||||
|
||||
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
|
||||
|
|
|
@ -883,6 +883,23 @@ public enum ErrorType {
|
|||
* @since 4.2.1
|
||||
*/
|
||||
FRAGMENT_ACTION_IGNORED(158, "fragment rule '<arg>' contains an action or command which can never be executed", ErrorSeverity.WARNING),
|
||||
/**
|
||||
* Compiler Error 159.
|
||||
*
|
||||
* <p>cannot declare a rule with reserved name '<em>rule</em>'</p>
|
||||
*
|
||||
* <p>A rule was declared with a reserved name.</p>
|
||||
*
|
||||
* <p>The following rule produces this error.</p>
|
||||
*
|
||||
* <pre>
|
||||
* EOF : ' ' // error 159 (EOF is a reserved name)
|
||||
* ;
|
||||
* </pre>
|
||||
*
|
||||
* @since 4.2.1
|
||||
*/
|
||||
RESERVED_RULE_NAME(159, "cannot declare a rule with reserved name '<arg>'", ErrorSeverity.ERROR),
|
||||
|
||||
/*
|
||||
* Backward incompatibility errors
|
||||
|
|
|
@ -706,6 +706,11 @@ public class Grammar implements AttributeResolver {
|
|||
}
|
||||
|
||||
public void setTokenForType(int ttype, String text) {
|
||||
if (ttype == Token.EOF) {
|
||||
// ignore EOF, it will be reported as an error separately
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ttype>=typeToTokenList.size() ) {
|
||||
Utils.setSize(typeToTokenList, ttype+1);
|
||||
}
|
||||
|
|
|
@ -502,4 +502,25 @@ public class TestToolSyntaxErrors extends BaseTest {
|
|||
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a regression test for antlr/antlr4#500 "Array Index Out Of
|
||||
* Bounds".
|
||||
* https://github.com/antlr/antlr4/issues/500
|
||||
*/
|
||||
@Test public void testTokenNamedEOF() {
|
||||
String grammar =
|
||||
"lexer grammar A;\n" +
|
||||
"WS : ' ';\n" +
|
||||
" EOF : 'a';\n";
|
||||
String expected =
|
||||
"error(" + ErrorType.RESERVED_RULE_NAME.code + "): A.g4:3:1: cannot declare a rule with reserved name 'EOF'\n";
|
||||
|
||||
String[] pair = new String[] {
|
||||
grammar,
|
||||
expected
|
||||
};
|
||||
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue