use of org.antlr.v4.tool.Alternative 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.Alternative in project antlr4 by antlr.
the class TestGrammarParserInterpreter method testAltsWithLabels.
@Test
public void testAltsWithLabels() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : ID # foo\n" + " | INT # bar\n" + " ;\n", lg);
// it won't show the labels here because my simple node text provider above just shows the alternative
testInterp(lg, g, "s", "a", "(s:1 a)");
testInterp(lg, g, "s", "3", "(s:2 3)");
}
use of org.antlr.v4.tool.Alternative in project antlr4 by antlr.
the class UseDefAnalyzer method trackTokenRuleRefsInActions.
// side-effect: updates Alternative with refs in actions
public static void trackTokenRuleRefsInActions(Grammar g) {
for (Rule r : g.rules.values()) {
for (int i = 1; i <= r.numberOfAlts; i++) {
Alternative alt = r.alt[i];
for (ActionAST a : alt.actions) {
ActionSniffer sniffer = new ActionSniffer(g, r, alt, a, a.token);
sniffer.examineAction();
}
}
}
}
use of org.antlr.v4.tool.Alternative in project antlr4 by antlr.
the class ParserFactory method needsImplicitLabel.
@Override
public boolean needsImplicitLabel(GrammarAST ID, LabeledOp op) {
Alternative currentOuterMostAlt = getCurrentOuterMostAlt();
boolean actionRefsAsToken = currentOuterMostAlt.tokenRefsInActions.containsKey(ID.getText());
boolean actionRefsAsRule = currentOuterMostAlt.ruleRefsInActions.containsKey(ID.getText());
return op.getLabels().isEmpty() && (actionRefsAsToken || actionRefsAsRule);
}
use of org.antlr.v4.tool.Alternative in project antlr4 by antlr.
the class AttributeChecks method checkAllAttributeExpressions.
public static void checkAllAttributeExpressions(Grammar g) {
for (ActionAST act : g.namedActions.values()) {
AttributeChecks checker = new AttributeChecks(g, null, null, act, act.token);
checker.examineAction();
}
for (Rule r : g.rules.values()) {
for (ActionAST a : r.namedActions.values()) {
AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
checker.examineAction();
}
for (int i = 1; i <= r.numberOfAlts; i++) {
Alternative alt = r.alt[i];
for (ActionAST a : alt.actions) {
AttributeChecks checker = new AttributeChecks(g, r, alt, a, a.token);
checker.examineAction();
}
}
for (GrammarAST e : r.exceptions) {
ActionAST a = (ActionAST) e.getChild(1);
AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
checker.examineAction();
}
if (r.finallyAction != null) {
AttributeChecks checker = new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.token);
checker.examineAction();
}
}
}
Aggregations