Search in sources :

Example 31 with ParserRuleContext

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

the class ParserRuleContext method addErrorNode.

/** Add a child to this node based upon badToken.  It
	 *  creates a ErrorNodeImpl rather than using
	 *  {@link Parser#createErrorNode(ParserRuleContext, Token)}. I'm leaving this
	 *  in for compatibility but the parser doesn't use this anymore.
	 */
@Deprecated
public ErrorNode addErrorNode(Token badToken) {
    ErrorNodeImpl t = new ErrorNodeImpl(badToken);
    addAnyChild(t);
    t.setParent(this);
    return t;
}
Also used : ErrorNodeImpl(org.antlr.v4.runtime.tree.ErrorNodeImpl)

Example 32 with ParserRuleContext

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

the class ParserRuleContext method copyFrom.

/** COPY a ctx (I'm deliberately not using copy constructor) to avoid
	 *  confusion with creating node with parent. Does not copy children
	 *  (except error leaves).
	 *
	 *  This is used in the generated parser code to flip a generic XContext
	 *  node for rule X to a YContext for alt label Y. In that sense, it is
	 *  not really a generic copy function.
	 *
	 *  If we do an error sync() at start of a rule, we might add error nodes
	 *  to the generic XContext so this function must copy those nodes to
	 *  the YContext as well else they are lost!
	 */
public void copyFrom(ParserRuleContext ctx) {
    this.parent = ctx.parent;
    this.invokingState = ctx.invokingState;
    this.start = ctx.start;
    this.stop = ctx.stop;
    // copy any error nodes to alt label node
    if (ctx.children != null) {
        this.children = new ArrayList<>();
        // reset parent pointer for any error nodes
        for (ParseTree child : ctx.children) {
            if (child instanceof ErrorNode) {
                addChild((ErrorNode) child);
            }
        }
    }
}
Also used : ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 33 with ParserRuleContext

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

the class Trees method stripChildrenOutOfRange.

/** Replace any subtree siblings of root that are completely to left
	 *  or right of lookahead range with a CommonToken(Token.INVALID_TYPE,"...")
	 *  node. The source interval for t is not altered to suit smaller range!
	 *
	 *  WARNING: destructive to t.
	 *
	 *  @since 4.5.1
	 */
public static void stripChildrenOutOfRange(ParserRuleContext t, ParserRuleContext root, int startIndex, int stopIndex) {
    if (t == null)
        return;
    for (int i = 0; i < t.getChildCount(); i++) {
        ParseTree child = t.getChild(i);
        Interval range = child.getSourceInterval();
        if (child instanceof ParserRuleContext && (range.b < startIndex || range.a > stopIndex)) {
            if (isAncestorOf(child, root)) {
                // replace only if subtree doesn't have displayed root
                CommonToken abbrev = new CommonToken(Token.INVALID_TYPE, "...");
                t.children.set(i, new TerminalNodeImpl(abbrev));
            }
        }
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Interval(org.antlr.v4.runtime.misc.Interval)

Example 34 with ParserRuleContext

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

the class ParseTreeWalker method enterRule.

/**
	 * The discovery of a rule node, involves sending two events: the generic
	 * {@link ParseTreeListener#enterEveryRule} and a
	 * {@link RuleContext}-specific event. First we trigger the generic and then
	 * the rule specific. We to them in reverse order upon finishing the node.
	 */
protected void enterRule(ParseTreeListener listener, RuleNode r) {
    ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
    listener.enterEveryRule(ctx);
    ctx.enterRule(listener);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext)

Example 35 with ParserRuleContext

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

the class TestExpectedTokens method testFollowIncludedInLeftRecursiveRule.

// Test for https://github.com/antlr/antlr4/issues/1480
// can't reproduce
@Test
public void testFollowIncludedInLeftRecursiveRule() throws Exception {
    String gtext = "grammar T;\n" + "s : expr EOF ;\n" + "expr : L expr R\n" + "     | expr PLUS expr\n" + "     | ID\n" + "     ;\n";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_expr_2->BlockStart_13\n" + "BlockStart_13->s7\n" + "BlockStart_13->s12\n" + "s7-action_1:-1->s8\n" + "s12-ID->BlockEnd_14\n" + "s8-L->s9\n" + "BlockEnd_14->StarLoopEntry_20\n" + "s9-expr->RuleStart_expr_2\n" + "StarLoopEntry_20->StarBlockStart_18\n" + "StarLoopEntry_20->s21\n" + "s10-R->s11\n" + "StarBlockStart_18->s15\n" + "s21->RuleStop_expr_3\n" + "s11->BlockEnd_14\n" + "s15-2 >= _p->s16\n" + "RuleStop_expr_3->s5\n" + "RuleStop_expr_3->s10\n" + "RuleStop_expr_3->BlockEnd_19\n" + "s16-PLUS->s17\n" + "s17-expr->RuleStart_expr_2\n" + "BlockEnd_19->StarLoopBack_22\n" + "StarLoopBack_22->StarLoopEntry_20\n";
    checkRuleATN(g, "expr", atnText);
    ATN atn = g.getATN();
    //		DOTGenerator gen = new DOTGenerator(g);
    //		String dot = gen.getDOT(atn.states.get(2), g.getRuleNames(), false);
    //		System.out.println(dot);
    // Simulate call stack after input '(x' from rule s
    ParserRuleContext callStackFrom_s = new ParserRuleContext(null, 4);
    ParserRuleContext callStackFrom_expr = new ParserRuleContext(callStackFrom_s, 9);
    int afterID = 14;
    IntervalSet tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
    // Simulate call stack after input '(x' from within rule expr
    callStackFrom_expr = new ParserRuleContext(null, 9);
    tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)24 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 SmaliParser (com.googlecode.d2j.smali.antlr4.SmaliParser)3 BitSet (java.util.BitSet)3 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 Token (org.antlr.v4.runtime.Token)3 ATN (org.antlr.v4.runtime.atn.ATN)3 DFAState (org.antlr.v4.runtime.dfa.DFAState)3 Interval (org.antlr.v4.runtime.misc.Interval)3 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)3 ArrayList (java.util.ArrayList)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2