tweak to michaelpj's PR; alter name. Indicate this fixes #1048.

This commit is contained in:
parrt 2016-03-29 15:28:36 -07:00
parent f2cbc6eb7c
commit eeea98c00b
2 changed files with 12 additions and 12 deletions

View File

@ -30,12 +30,6 @@
package org.antlr.v4.analysis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
@ -57,6 +51,12 @@ import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** Using a tree walker on the rules, determine if a rule is directly left-recursive and if it follows
* our pattern.
*/
@ -68,7 +68,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> binaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> ternaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> suffixAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
public List<LeftRecursiveRuleAltInfo> prefixAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
public List<LeftRecursiveRuleAltInfo> prefixAndOtherAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
/** Pointer to ID node of ^(= ID element) */
public List<Pair<GrammarAST,String>> leftRecursiveRuleRefLabels =
@ -187,7 +187,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
LeftRecursiveRuleAltInfo a =
new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
a.nextPrec = nextPrec;
prefixAlts.add(a);
prefixAndOtherAlts.add(a);
//System.out.println("prefixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
}
@ -223,7 +223,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
// We keep other alts with prefix alts since they are all added to the start of the generated rule, and
// we want to retain any prior ordering between them
prefixAlts.add(a);
prefixAndOtherAlts.add(a);
// System.out.println("otherAlt " + alt + ": " + altText);
}
@ -255,7 +255,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
ruleST.add("opAlts", altST);
}
ruleST.add("primaryAlts", prefixAlts);
ruleST.add("primaryAlts", prefixAndOtherAlts);
tool.log("left-recursion", ruleST.render());
@ -439,7 +439,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
"binaryAlts=" + binaryAlts +
", ternaryAlts=" + ternaryAlts +
", suffixAlts=" + suffixAlts +
", prefixAlts=" + prefixAlts +
", prefixAndOtherAlts=" +prefixAndOtherAlts+
'}';
}
}

View File

@ -168,7 +168,7 @@ public class LeftRecursiveRuleTransformer {
// track recursive alt info for codegen
r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts);
r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts);
if (r.recPrimaryAlts.isEmpty()) {
tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name);
}