use of org.antlr.v4.tool.Rule in project antlr4 by antlr.
the class LeftRecursiveRule method getPrimaryAlts.
/** Return an array that maps predicted alt from primary decision
* to original alt of rule. For following rule, return [0, 2, 4]
*
e : e '*' e
| INT
| e '+' e
| ID
;
* That maps predicted alt 1 to original alt 2 and predicted 2 to alt 4.
*
* @since 4.5.1
*/
public int[] getPrimaryAlts() {
if (recPrimaryAlts.size() == 0)
return null;
int[] alts = new int[recPrimaryAlts.size() + 1];
for (int i = 0; i < recPrimaryAlts.size(); i++) {
// recPrimaryAlts is a List not Map like recOpAlts
LeftRecursiveRuleAltInfo altInfo = recPrimaryAlts.get(i);
alts[i + 1] = altInfo.altNum;
}
return alts;
}
use of org.antlr.v4.tool.Rule in project antlr4 by antlr.
the class Rule method getAltLabels.
/**
* Get {@code #} labels. The keys of the map are the labels applied to outer
* alternatives of a lexer rule, and the values are collections of pairs
* (alternative number and {@link AltAST}) identifying the alternatives with
* this label. Unlabeled alternatives are not included in the result.
*/
public Map<String, List<Pair<Integer, AltAST>>> getAltLabels() {
Map<String, List<Pair<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Pair<Integer, AltAST>>>();
for (int i = 1; i <= numberOfAlts; i++) {
GrammarAST altLabel = alt[i].ast.altLabel;
if (altLabel != null) {
List<Pair<Integer, AltAST>> list = labels.get(altLabel.getText());
if (list == null) {
list = new ArrayList<Pair<Integer, AltAST>>();
labels.put(altLabel.getText(), list);
}
list.add(new Pair<Integer, AltAST>(i, alt[i].ast));
}
}
if (labels.isEmpty())
return null;
return labels;
}
use of org.antlr.v4.tool.Rule in project antlr4 by antlr.
the class BaseJavaTest method checkRuleDFA.
List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(gtext, equeue);
ATN atn = createATN(g, false);
ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
if (s == null) {
System.err.println("no such rule: " + ruleName);
return null;
}
ATNState t = s.transition(0).target;
if (!(t instanceof DecisionState)) {
System.out.println(ruleName + " has no decision");
return null;
}
DecisionState blk = (DecisionState) t;
checkRuleDFA(g, blk, expecting);
return equeue.all;
}
use of org.antlr.v4.tool.Rule in project antlr4 by antlr.
the class BaseCppTest method checkRuleDFA.
List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(gtext, equeue);
ATN atn = createATN(g, false);
ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
if (s == null) {
System.err.println("no such rule: " + ruleName);
return null;
}
ATNState t = s.transition(0).target;
if (!(t instanceof DecisionState)) {
System.out.println(ruleName + " has no decision");
return null;
}
DecisionState blk = (DecisionState) t;
checkRuleDFA(g, blk, expecting);
return equeue.all;
}
use of org.antlr.v4.tool.Rule in project antlr4 by antlr.
the class TestATNParserPrediction method testAltsForLRRuleComputation.
@Test
public void testAltsForLRRuleComputation() throws Exception {
Grammar g = new Grammar("grammar T;\n" + "e : e '*' e\n" + " | INT\n" + " | e '+' e\n" + " | ID\n" + " ;\n" + "ID : [a-z]+ ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\r\\t\\n]+ ;");
Rule e = g.getRule("e");
assertTrue(e instanceof LeftRecursiveRule);
LeftRecursiveRule lr = (LeftRecursiveRule) e;
assertEquals("[0, 2, 4]", Arrays.toString(lr.getPrimaryAlts()));
assertEquals("[0, 1, 3]", Arrays.toString(lr.getRecursiveOpAlts()));
}
Aggregations