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;
}
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);
}
}
}
}
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));
}
}
}
}
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);
}
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()));
}
Aggregations