forked from jasder/antlr
Track 'prefix' and 'other' alternatives together
These alternatives are not treated differently after they are created, and we want to retain their alternative ordering.
This commit is contained in:
parent
b1b2579621
commit
5967674531
|
@ -30,6 +30,12 @@
|
||||||
|
|
||||||
package org.antlr.v4.analysis;
|
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.CommonToken;
|
||||||
import org.antlr.runtime.Token;
|
import org.antlr.runtime.Token;
|
||||||
import org.antlr.runtime.TokenStream;
|
import org.antlr.runtime.TokenStream;
|
||||||
|
@ -51,12 +57,6 @@ import org.stringtemplate.v4.ST;
|
||||||
import org.stringtemplate.v4.STGroup;
|
import org.stringtemplate.v4.STGroup;
|
||||||
import org.stringtemplate.v4.STGroupFile;
|
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
|
/** Using a tree walker on the rules, determine if a rule is directly left-recursive and if it follows
|
||||||
* our pattern.
|
* our pattern.
|
||||||
*/
|
*/
|
||||||
|
@ -69,7 +69,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
|
||||||
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> ternaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
|
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> ternaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
|
||||||
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> suffixAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
|
public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> suffixAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
|
||||||
public List<LeftRecursiveRuleAltInfo> prefixAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
|
public List<LeftRecursiveRuleAltInfo> prefixAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
|
||||||
public List<LeftRecursiveRuleAltInfo> otherAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
|
|
||||||
|
|
||||||
/** Pointer to ID node of ^(= ID element) */
|
/** Pointer to ID node of ^(= ID element) */
|
||||||
public List<Pair<GrammarAST,String>> leftRecursiveRuleRefLabels =
|
public List<Pair<GrammarAST,String>> leftRecursiveRuleRefLabels =
|
||||||
|
@ -222,7 +221,9 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
|
||||||
String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null;
|
String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null;
|
||||||
LeftRecursiveRuleAltInfo a =
|
LeftRecursiveRuleAltInfo a =
|
||||||
new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
|
new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
|
||||||
otherAlts.add(a);
|
// 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);
|
||||||
// System.out.println("otherAlt " + alt + ": " + altText);
|
// System.out.println("otherAlt " + alt + ": " + altText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,7 +256,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleST.add("primaryAlts", prefixAlts);
|
ruleST.add("primaryAlts", prefixAlts);
|
||||||
ruleST.add("primaryAlts", otherAlts);
|
|
||||||
|
|
||||||
tool.log("left-recursion", ruleST.render());
|
tool.log("left-recursion", ruleST.render());
|
||||||
|
|
||||||
|
@ -440,7 +440,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
|
||||||
", ternaryAlts=" + ternaryAlts +
|
", ternaryAlts=" + ternaryAlts +
|
||||||
", suffixAlts=" + suffixAlts +
|
", suffixAlts=" + suffixAlts +
|
||||||
", prefixAlts=" + prefixAlts +
|
", prefixAlts=" + prefixAlts +
|
||||||
", otherAlts=" + otherAlts +
|
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,7 +169,6 @@ public class LeftRecursiveRuleTransformer {
|
||||||
// track recursive alt info for codegen
|
// track recursive alt info for codegen
|
||||||
r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
|
r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
|
||||||
r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts);
|
r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts);
|
||||||
r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.otherAlts);
|
|
||||||
if (r.recPrimaryAlts.isEmpty()) {
|
if (r.recPrimaryAlts.isEmpty()) {
|
||||||
tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name);
|
tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue