Error in case of mode conflicts with token. fixed #996

This commit is contained in:
Ivan Kochurkin 2015-10-06 17:39:01 +03:00
parent 3bdc4b7ee6
commit a87ac8664b
4 changed files with 37 additions and 8 deletions

View File

@ -116,10 +116,25 @@ public class TestSymbolIssues extends BaseTest {
"warning(" + ErrorType.TOKEN_NAME_REASSIGNMENT.code + "): E.g4:3:4: token name A is already defined\n"
};
static String[] F = {
// INPUT
"lexer grammar F;\n" +
"A: 'a';\n" +
"mode M1;\n" +
"A1: 'a';\n" +
"mode M2;\n" +
"A2: 'a';\n" +
"M1: 'b';\n",
// YIELDS
"error(" + ErrorType.MODE_CONFLICTS_WITH_TOKEN.code + "): F.g4:3:0: mode M1 conflicts with token with same name\n"
};
@Test public void testA() { super.testErrors(A, false); }
@Test public void testB() { super.testErrors(B, false); }
@Test public void testD() { super.testErrors(D, false); }
@Test public void testE() { super.testErrors(E, false); }
@Test public void testF() { super.testErrors(F, false); }
@Test public void testStringLiteralRedefs() throws Exception {
String grammar =

View File

@ -128,6 +128,8 @@ public class SemanticPipeline {
collector.tokenIDRefs, collector.terminals);
}
symcheck.checkForModeConflicts(g);
assignChannelTypes(g, collector.channelDefs);
// CHECK RULE REFS NOW (that we've defined rules in grammar)

View File

@ -31,14 +31,8 @@
package org.antlr.v4.semantics;
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.tool.Alternative;
import org.antlr.v4.tool.Attribute;
import org.antlr.v4.tool.AttributeDict;
import org.antlr.v4.tool.ErrorManager;
import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LabelElementPair;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.GrammarAST;
import java.util.Collection;
@ -269,6 +263,18 @@ public class SymbolChecks {
}
}
public void checkForModeConflicts(Grammar g) {
if (g.isLexer()) {
LexerGrammar lexerGrammar = (LexerGrammar)g;
for (String modeName : lexerGrammar.modes.keySet()) {
if (g.getTokenType(modeName) != Token.INVALID_TYPE) {
Rule rule = ((Collection<Rule>)lexerGrammar.modes.get(modeName)).iterator().next();
g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_TOKEN, g.fileName, rule.ast.parent.getToken(), modeName);
}
}
}
}
// CAN ONLY CALL THE TWO NEXT METHODS AFTER GRAMMAR HAS RULE DEFS (see semanticpipeline)
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {

View File

@ -955,6 +955,12 @@ public enum ErrorType {
* <p>custom channels are not supported in combined grammars</p>
*/
CHANNELS_BLOCK_IN_COMBINED_GRAMMAR(164, "custom channels are not supported in combined grammars", ErrorSeverity.ERROR),
/**
* Compiler Error 165.
*
* <p>mode <em>name</em> conflicts with token with same name</p>
*/
MODE_CONFLICTS_WITH_TOKEN(165, "mode <arg> conflicts with token with same name", ErrorSeverity.ERROR),
NONCONFORMING_LR_RULE(169, "rule <arg> is left recursive but doesn't conform to a pattern ANTLR can handle", ErrorSeverity.ERROR),