Search in sources :

Example 1 with InterpreterRuleContext

use of org.antlr.v4.runtime.InterpreterRuleContext in project antlr4 by antlr.

the class ParserInterpreter method visitState.

protected void visitState(ATNState p) {
    //		System.out.println("visitState "+p.stateNumber);
    int predictedAlt = 1;
    if (p instanceof DecisionState) {
        predictedAlt = visitDecisionState((DecisionState) p);
    }
    Transition transition = p.transition(predictedAlt - 1);
    switch(transition.getSerializationType()) {
        case Transition.EPSILON:
            if (p.getStateType() == ATNState.STAR_LOOP_ENTRY && ((StarLoopEntryState) p).isPrecedenceDecision && !(transition.target instanceof LoopEndState)) {
                // We are at the start of a left recursive rule's (...)* loop
                // and we're not taking the exit branch of loop.
                InterpreterRuleContext localctx = createInterpreterRuleContext(_parentContextStack.peek().a, _parentContextStack.peek().b, _ctx.getRuleIndex());
                pushNewRecursionContext(localctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.getRuleIndex());
            }
            break;
        case Transition.ATOM:
            match(((AtomTransition) transition).label);
            break;
        case Transition.RANGE:
        case Transition.SET:
        case Transition.NOT_SET:
            if (!transition.matches(_input.LA(1), Token.MIN_USER_TOKEN_TYPE, 65535)) {
                recoverInline();
            }
            matchWildcard();
            break;
        case Transition.WILDCARD:
            matchWildcard();
            break;
        case Transition.RULE:
            RuleStartState ruleStartState = (RuleStartState) transition.target;
            int ruleIndex = ruleStartState.ruleIndex;
            InterpreterRuleContext newctx = createInterpreterRuleContext(_ctx, p.stateNumber, ruleIndex);
            if (ruleStartState.isLeftRecursiveRule) {
                enterRecursionRule(newctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition) transition).precedence);
            } else {
                enterRule(newctx, transition.target.stateNumber, ruleIndex);
            }
            break;
        case Transition.PREDICATE:
            PredicateTransition predicateTransition = (PredicateTransition) transition;
            if (!sempred(_ctx, predicateTransition.ruleIndex, predicateTransition.predIndex)) {
                throw new FailedPredicateException(this);
            }
            break;
        case Transition.ACTION:
            ActionTransition actionTransition = (ActionTransition) transition;
            action(_ctx, actionTransition.ruleIndex, actionTransition.actionIndex);
            break;
        case Transition.PRECEDENCE:
            if (!precpred(_ctx, ((PrecedencePredicateTransition) transition).precedence)) {
                throw new FailedPredicateException(this, String.format("precpred(_ctx, %d)", ((PrecedencePredicateTransition) transition).precedence));
            }
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized ATN transition type.");
    }
    setState(transition.target.stateNumber);
}
Also used : LoopEndState(org.antlr.v4.runtime.atn.LoopEndState) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) StarLoopEntryState(org.antlr.v4.runtime.atn.StarLoopEntryState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) Transition(org.antlr.v4.runtime.atn.Transition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) DecisionState(org.antlr.v4.runtime.atn.DecisionState)

Example 2 with InterpreterRuleContext

use of org.antlr.v4.runtime.InterpreterRuleContext in project antlr4 by antlr.

the class TestGrammarParserInterpreter method testInterp.

InterpreterRuleContext testInterp(LexerGrammar lg, Grammar g, String startRule, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    String treeStr = Trees.toStringTree(t, nodeTextProvider);
    //		System.out.println("parse tree: "+treeStr);
    assertEquals(expectedParseTree, treeStr);
    return (InterpreterRuleContext) t;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InterpreterRuleContext(org.antlr.v4.runtime.InterpreterRuleContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 InterpreterRuleContext (org.antlr.v4.runtime.InterpreterRuleContext)1 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)1 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)1 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)1 DecisionState (org.antlr.v4.runtime.atn.DecisionState)1 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)1 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)1 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)1 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)1 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)1 StarLoopEntryState (org.antlr.v4.runtime.atn.StarLoopEntryState)1 Transition (org.antlr.v4.runtime.atn.Transition)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)1