Merge pull request #1570 from sharwell/fix-1543

Disable label checks for left recursive rules
This commit is contained in:
Terence Parr 2017-01-03 17:04:26 -08:00 committed by GitHub
commit 81f52adf82
2 changed files with 51 additions and 0 deletions

View File

@ -9,6 +9,7 @@ package org.antlr.v4.test.tool;
import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -321,6 +322,49 @@ public class TestSymbolIssues extends BaseJavaToolTest {
testErrors(test, false);
}
// https://github.com/antlr/antlr4/issues/1543
@Test public void testLabelsForTokensWithMixedTypesLRWithLabels() {
String[] test = {
"grammar L;\n" +
"\n" +
"expr\n" +
" : left=A '+' right=A #primary\n" +
" | left=expr '-' right=expr #sub\n" +
" ;\n" +
"\n" +
"A: 'a';\n" +
"B: 'b';\n" +
"C: 'c';\n",
""
};
testErrors(test, false);
}
// https://github.com/antlr/antlr4/issues/1543
@Test
@Ignore
public void testLabelsForTokensWithMixedTypesLRWithoutLabels() {
String[] test = {
"grammar L;\n" +
"\n" +
"expr\n" +
" : left=A '+' right=A\n" +
" | left=expr '-' right=expr\n" +
" ;\n" +
"\n" +
"A: 'a';\n" +
"B: 'b';\n" +
"C: 'c';\n",
"error(" + ErrorType.LABEL_TYPE_CONFLICT.code + "): L.g4:5:7: label left=expr type mismatch with previous definition: left=A\n" +
"error(" + ErrorType.LABEL_TYPE_CONFLICT.code + "): L.g4:5:19: label right=expr type mismatch with previous definition: right=A\n"
};
testErrors(test, false);
}
@Test public void testCharsCollision() throws Exception {
String[] test = {
"lexer grammar L;\n" +

View File

@ -20,6 +20,7 @@ import org.antlr.v4.tool.LexerGrammar;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.GrammarAST;
import org.antlr.v4.tool.LabelType;
import org.antlr.v4.tool.LeftRecursiveRule;
import java.util.Collection;
import java.util.HashMap;
@ -127,6 +128,12 @@ public class SymbolChecks {
public void checkForLabelConflicts(Collection<Rule> rules) {
for (Rule r : rules) {
checkForAttributeConflicts(r);
if (r instanceof LeftRecursiveRule) {
// Label conflicts for left recursive rules need to be checked
// prior to the left recursion elimination step.
continue;
}
Map<String, LabelElementPair> labelNameSpace =
new HashMap<String, LabelElementPair>();
for (int i = 1; i <= r.numberOfAlts; i++) {