Search in sources :

Example 6 with Alternative

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;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) List(java.util.List) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap) Pair(org.antlr.v4.runtime.misc.Pair)

Example 7 with Alternative

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)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 8 with Alternative

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();
            }
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) Rule(org.antlr.v4.tool.Rule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 9 with Alternative

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);
}
Also used : Alternative(org.antlr.v4.tool.Alternative)

Example 10 with Alternative

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();
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Aggregations

ArrayList (java.util.ArrayList)4 Alternative (org.antlr.v4.tool.Alternative)4 Rule (org.antlr.v4.tool.Rule)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 BitSet (java.util.BitSet)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 ATNState (org.antlr.v4.runtime.atn.ATNState)3 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 DFAState (org.antlr.v4.runtime.dfa.DFAState)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 ActionAST (org.antlr.v4.tool.ast.ActionAST)2 Map (java.util.Map)1 CommonToken (org.antlr.runtime.CommonToken)1 Token (org.antlr.runtime.Token)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1