Fixes #773. rule[arg] in non-lr rule alt didnt translate right

This commit is contained in:
Terence Parr 2015-05-22 16:49:31 -07:00
parent b7e5bfcbf8
commit 3e5fc6972a
2 changed files with 26 additions and 4 deletions

View File

@ -370,13 +370,16 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
}
StringBuilder buf = new StringBuilder();
for (int i=tokenStartIndex; i<=tokenStopIndex; i++) {
int i=tokenStartIndex;
while ( i<=tokenStopIndex ) {
if ( ignore.contains(i) ) {
i++;
continue;
}
Token tok = tokenStream.get(i);
// Compute/hold any element options
StringBuilder elementOptions = new StringBuilder();
if (!noOptions.contains(i)) {
GrammarAST node = t.getNodeWithTokenIndex(tok.getTokenIndex());
@ -402,7 +405,16 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
}
}
buf.append(tok.getText());
buf.append(tok.getText()); // add actual text of the current token to the rewritten alternative
i++; // move to the next token
// Are there args on a rule?
if ( tok.getType()==RULE_REF && i<=tokenStopIndex && tokenStream.get(i).getType()==ARG_ACTION ) {
buf.append('['+tokenStream.get(i).getText()+']');
i++;
}
// now that we have the actual element, we can add the options.
if (elementOptions.length() > 0) {
buf.append('<').append(elementOptions).append('>');
}

View File

@ -96,7 +96,6 @@ public class TestLeftRecursionToolIssues extends BaseTest {
testErrors(new String[]{grammar, expected}, false);
}
/** Reproduces https://github.com/antlr/antlr4/issues/855 */
@Test public void testLeftRecursiveRuleRefWithArg3() throws Exception {
String grammar =
@ -109,7 +108,6 @@ public class TestLeftRecursionToolIssues extends BaseTest {
testErrors(new String[]{grammar, expected}, false);
}
/** Reproduces https://github.com/antlr/antlr4/issues/822 */
@Test public void testIsolatedLeftRecursiveRuleRef() throws Exception {
String grammar =
@ -120,4 +118,16 @@ public class TestLeftRecursionToolIssues extends BaseTest {
"error(" + ErrorType.NONCONFORMING_LR_RULE.code + "): T.g4:2:0: rule a is left recursive but doesn't conform to a pattern ANTLR can handle\n";
testErrors(new String[]{grammar, expected}, false);
}
/** Reproduces https://github.com/antlr/antlr4/issues/773 */
@Test public void testArgOnPrimaryRuleInLeftRecursiveRule() throws Exception {
String grammar =
"grammar T;\n" +
"val: dval[1]\n" +
" | val '*' val\n" +
" ;\n" +
"dval[int x]: '.';\n";
String expected = ""; // dval[1] should not be error
testErrors(new String[]{grammar, expected}, false);
}
}