Search in sources :

Example 1 with RuleStartState

use of org.antlr.v4.runtime.atn.RuleStartState in project antlr4 by tunnelvisionlabs.

the class TestAmbigParseTrees method testInterpAtSpecificAlt.

void testInterpAtSpecificAlt(LexerGrammar lg, Grammar g, String startRule, int startAlt, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    ParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    RuleStartState ruleStartState = g.atn.ruleToStartState[g.getRule(startRule).index];
    Transition tr = ruleStartState.transition(0);
    ATNState t2 = tr.target;
    if (!(t2 instanceof BasicBlockStartState)) {
        throw new IllegalArgumentException("rule has no decision: " + startRule);
    }
    parser.addDecisionOverride(((DecisionState) t2).decision, 0, startAlt);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    assertEquals(expectedParseTree, Trees.toStringTree(t, nodeTextProvider));
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Transition(org.antlr.v4.runtime.atn.Transition) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 2 with RuleStartState

use of org.antlr.v4.runtime.atn.RuleStartState in project antlr4 by tunnelvisionlabs.

the class LexerATNFactory method createATN.

@Override
public ATN createATN() {
    // BUILD ALL START STATES (ONE PER MODE)
    Set<String> modes = ((LexerGrammar) g).modes.keySet();
    for (String modeName : modes) {
        // create s0, start state; implied Tokens rule node
        TokensStartState startState = newState(TokensStartState.class, null);
        atn.defineMode(modeName, startState);
    }
    // INIT ACTION, RULE->TOKEN_TYPE MAP
    atn.ruleToTokenType = new int[g.rules.size()];
    for (Rule r : g.rules.values()) {
        atn.ruleToTokenType[r.index] = g.getTokenType(r.name);
    }
    // CREATE ATN FOR EACH RULE
    _createATN(g.rules.values());
    atn.lexerActions = new LexerAction[indexToActionMap.size()];
    for (Map.Entry<Integer, LexerAction> entry : indexToActionMap.entrySet()) {
        atn.lexerActions[entry.getKey()] = entry.getValue();
    }
    // LINK MODE START STATE TO EACH TOKEN RULE
    for (String modeName : modes) {
        List<Rule> rules = ((LexerGrammar) g).modes.get(modeName);
        TokensStartState startState = atn.modeNameToStartState.get(modeName);
        for (Rule r : rules) {
            if (!r.isFragment()) {
                RuleStartState s = atn.ruleToStartState[r.index];
                epsilon(startState, s);
            }
        }
    }
    ATNOptimizer.optimize(g, atn);
    return atn;
}
Also used : TokensStartState(org.antlr.v4.runtime.atn.TokensStartState) LexerAction(org.antlr.v4.runtime.atn.LexerAction) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RuleStartState

use of org.antlr.v4.runtime.atn.RuleStartState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method rule.

/* start->ruleblock->end */
@NotNull
@Override
public Handle rule(@NotNull GrammarAST ruleAST, @NotNull String name, @NotNull Handle blk) {
    Rule r = g.getRule(name);
    RuleStartState start = atn.ruleToStartState[r.index];
    epsilon(start, blk.left);
    RuleStopState stop = atn.ruleToStopState[r.index];
    epsilon(blk.right, stop);
    Handle h = new Handle(start, stop);
    // ATNPrinter ser = new ATNPrinter(g, h.left);
    // System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
    ruleAST.atnState = start;
    return h;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 4 with RuleStartState

use of org.antlr.v4.runtime.atn.RuleStartState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method _ruleRef.

@NotNull
public Handle _ruleRef(@NotNull GrammarAST node) {
    Rule r = g.getRule(node.getText());
    if (r == null) {
        g.tool.errMgr.grammarError(ErrorType.INTERNAL_ERROR, g.fileName, node.getToken(), "Rule " + node.getText() + " undefined");
        return null;
    }
    RuleStartState start = atn.ruleToStartState[r.index];
    ATNState left = newState(node);
    ATNState right = newState(node);
    int precedence = 0;
    if (((GrammarASTWithOptions) node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
        precedence = Integer.parseInt(((GrammarASTWithOptions) node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
    }
    RuleTransition call = new RuleTransition(start, r.index, precedence, right);
    left.addTransition(call);
    node.atnState = left;
    return new Handle(left, right);
}
Also used : RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) GrammarASTWithOptions(org.antlr.v4.tool.ast.GrammarASTWithOptions) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 5 with RuleStartState

use of org.antlr.v4.runtime.atn.RuleStartState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method createRuleStartAndStopATNStates.

/**
 * Define all the rule begin/end ATNStates to solve forward reference
 *  issues.
 */
void createRuleStartAndStopATNStates() {
    atn.ruleToStartState = new RuleStartState[g.rules.size()];
    atn.ruleToStopState = new RuleStopState[g.rules.size()];
    for (Rule r : g.rules.values()) {
        RuleStartState start = newState(RuleStartState.class, r.ast);
        RuleStopState stop = newState(RuleStopState.class, r.ast);
        start.stopState = stop;
        start.isPrecedenceRule = r instanceof LeftRecursiveRule;
        start.setRuleIndex(r.index);
        stop.setRuleIndex(r.index);
        atn.ruleToStartState[r.index] = start;
        atn.ruleToStopState[r.index] = stop;
    }
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Aggregations

RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)23 ATNState (org.antlr.v4.runtime.atn.ATNState)15 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)11 Rule (org.antlr.v4.tool.Rule)10 Transition (org.antlr.v4.runtime.atn.Transition)9 RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)8 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)5 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)5 DecisionState (org.antlr.v4.runtime.atn.DecisionState)5 StarLoopEntryState (org.antlr.v4.runtime.atn.StarLoopEntryState)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)5 LexerAction (org.antlr.v4.runtime.atn.LexerAction)3 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)3 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)3 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)3 RangeTransition (org.antlr.v4.runtime.atn.RangeTransition)3 SetTransition (org.antlr.v4.runtime.atn.SetTransition)3