Top Description Fields Constructors Methods
org.antlr.v4.runtime

public Class ParserRuleContext

extends RuleContext
Class Inheritance
Known Direct Subclasses
org.antlr.v4.runtime.InterpreterRuleContext, org.antlr.v4.runtime.RuleContextWithAltNum
Imports
org.antlr.v4.runtime.misc.Interval, .Nullable, org.antlr.v4.runtime.tree.ErrorNode, .ErrorNodeImpl, .ParseTree, .ParseTreeListener, .TerminalNode, .TerminalNodeImpl, java.util.ArrayList, .Collections, .List

A rule invocation record for parsing. Contains all of the information about the current rule not stored in the RuleContext. It handles parse tree children list, Any ATN state tracing, and the default values available for rule invocations: start, stop, rule index, current alt number. Subclasses made for each rule and grammar track the parameters, return values, locals, and labels specific to that rule. These are the objects that are returned from rules. Note text is not an actual field of a rule return value; it is computed from start and stop using the input stream's toString() method. I could add a ctor to this so that we can pass in and store the input stream, but I'm not sure we want to do that. It would seem to be undefined to get the .text property anyway if the rule matches tokens from multiple input streams. I do not use getters for fields of objects that are used simply to group values such as this aggregate. The getters/setters are there to satisfy the superclass interface.

Field Summary

Modifier and TypeField and Description
public List<ParseTree>
children

If we are debugging or building a parse tree for a visitor, we need to track all of the tokens and rule invocations associated with this rule's context.

private static final ParserRuleContext
public RecognitionException
exception

The exception that forced this rule to return.

public Token
start

For debugging/tracing purposes, we want to track all of the nodes in the ATN traversed by the parser for a particular rule.

public Token
stop

For debugging/tracing purposes, we want to track all of the nodes in the ATN traversed by the parser for a particular rule.

Inherited from org.antlr.v4.runtime.RuleContext:
invokingStateparent

Constructor Summary

AccessConstructor and Description
public
public
ParserRuleContext(ParserRuleContext parent, int invokingStateNumber)

Method Summary

Modifier and TypeMethod and Description
public <T extends ParseTree> T
addAnyChild(T t)

Add a parse tree node to this as a child.

public void
addChild(RuleContext ruleInvocation)

public void
addChild(TerminalNode t)

Add a token leaf node child.

public TerminalNode
addChild(Token matchedToken)

Deprecated
Add a child to this node based upon matchedToken.
public ErrorNode
addErrorNode(ErrorNode errorNode)

Add an error node child.

public ErrorNode
addErrorNode(Token badToken)

Deprecated
Add a child to this node based upon badToken.
public void
copyFrom(ParserRuleContext ctx)

COPY a ctx (I'm deliberately not using copy constructor) to avoid confusion with creating node with parent.

public static ParserRuleContext
public void
public void
public ParseTree
getChild(int i)

Overrides org.antlr.v4.runtime.RuleContext.getChild.

Implements org.antlr.v4.runtime.tree.ParseTree.getChild.

If there are children, get the ith value indexed from 0.
public <T extends ParseTree> T
getChild(Class<? extends T> ctxType, int i)

public int
public ParserRuleContext
public <T extends ParserRuleContext> T
getRuleContext(Class<? extends T> ctxType, int i)

public <T extends ParserRuleContext> List<? extends T>
getRuleContexts(Class<? extends T> ctxType)

public Interval
getSourceInterval()

Overrides org.antlr.v4.runtime.RuleContext.getSourceInterval.

Implements org.antlr.v4.runtime.tree.SyntaxTree.getSourceInterval.

Return an Interval indicating the index in the TokenStream of the first and last token associated with this subtree.
public Token
getStart()

Get the initial token in this context.

public Token
getStop()

Get the final token in this context.

public TerminalNode
getToken(int ttype, int i)

public List<? extends TerminalNode>
getTokens(int ttype)

public void
removeLastChild()

Used by enterOuterAlt to toss out a RuleContext previously added as we entered a rule.

public String
toInfoString(Parser recognizer)

Used for rule context info debugging during parse-time, not so much for ATN debugging

Inherited from org.antlr.v4.runtime.RuleContext:
acceptdepthgetAltNumbergetChildContextgetPayloadgetRuleContextgetRuleIndexgetTextisEmptysetAltNumbersetParenttoStringtoStringtoStringtoStringtoStringtoStringTreetoStringTreetoStringTree

Field Detail

childrenback to summary
public List<ParseTree> children

If we are debugging or building a parse tree for a visitor, we need to track all of the tokens and rule invocations associated with this rule's context. This is empty for parsing w/o tree constr. operation because we don't the need to track the details about how we parse this rule.

EMPTYback to summary
private static final ParserRuleContext EMPTY
exceptionback to summary
public RecognitionException exception

The exception that forced this rule to return. If the rule successfully completed, this is null.

startback to summary
public Token start

For debugging/tracing purposes, we want to track all of the nodes in the ATN traversed by the parser for a particular rule. This list indicates the sequence of ATN nodes used to match the elements of the children list. This list does not include ATN nodes and other rules used to match rule invocations. It traces the rule invocation node itself but nothing inside that other rule's ATN submachine. There is NOT a one-to-one correspondence between the children and states list. There are typically many nodes in the ATN traversed for each element in the children list. For example, for a rule invocation there is the invoking state and the following state. The parser setState() method updates field s and adds it to this list if we are debugging/tracing. This does not trace states visited during prediction.

stopback to summary
public Token stop

For debugging/tracing purposes, we want to track all of the nodes in the ATN traversed by the parser for a particular rule. This list indicates the sequence of ATN nodes used to match the elements of the children list. This list does not include ATN nodes and other rules used to match rule invocations. It traces the rule invocation node itself but nothing inside that other rule's ATN submachine. There is NOT a one-to-one correspondence between the children and states list. There are typically many nodes in the ATN traversed for each element in the children list. For example, for a rule invocation there is the invoking state and the following state. The parser setState() method updates field s and adds it to this list if we are debugging/tracing. This does not trace states visited during prediction.

Constructor Detail

ParserRuleContextback to summary
public ParserRuleContext()
ParserRuleContextback to summary
public ParserRuleContext(ParserRuleContext parent, int invokingStateNumber)

Method Detail

addAnyChildback to summary
public <T extends ParseTree> T addAnyChild(T t)

Add a parse tree node to this as a child. Works for internal and leaf nodes. Does not set parent link; other add methods must do that. Other addChild methods call this. We cannot set the parent pointer of the incoming node because the existing interfaces do not have a setParent() method and I don't want to break backward compatibility for this.

Since
4.7
addChildback to summary
public void addChild(RuleContext ruleInvocation)
addChildback to summary
public void addChild(TerminalNode t)

Add a token leaf node child.

addChildback to summary
public TerminalNode addChild(Token matchedToken)

Deprecated

Add a child to this node based upon matchedToken. It creates a TerminalNodeImpl rather than using Parser#createTerminalNode(ParserRuleContext, Token). I'm leaving this in for compatibility but the parser doesn't use this anymore.

Annotations
@Deprecated
addErrorNodeback to summary
public ErrorNode addErrorNode(ErrorNode errorNode)

Add an error node child.

Since
4.7
addErrorNodeback to summary
public ErrorNode addErrorNode(Token badToken)

Deprecated

Add a child to this node based upon badToken. It creates a ErrorNodeImpl rather than using Parser#createErrorNode(ParserRuleContext, Token). I'm leaving this in for compatibility but the parser doesn't use this anymore.

Annotations
@Deprecated
copyFromback to summary
public void copyFrom(ParserRuleContext ctx)

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!

emptyContextback to summary
public static ParserRuleContext emptyContext()
enterRuleback to summary
public void enterRule(ParseTreeListener listener)
exitRuleback to summary
public void exitRule(ParseTreeListener listener)
getChildback to summary
public ParseTree getChild(int i)

Overrides org.antlr.v4.runtime.RuleContext.getChild.

Implements org.antlr.v4.runtime.tree.ParseTree.getChild.

Doc from org.antlr.v4.runtime.tree.Tree.getChild.

If there are children, get the ith value indexed from 0.

Annotations
@Override
getChildback to summary
public <T extends ParseTree> T getChild(Class<? extends T> ctxType, int i)
getChildCountback to summary
public int getChildCount()

Overrides org.antlr.v4.runtime.RuleContext.getChildCount.

Implements org.antlr.v4.runtime.tree.Tree.getChildCount.

Doc from org.antlr.v4.runtime.tree.Tree.getChildCount.

How many children are there? If there is none, then this node represents a leaf node.

Annotations
@Override
getParentback to summary
public ParserRuleContext getParent()

Overrides org.antlr.v4.runtime.RuleContext.getParent.

Implements org.antlr.v4.runtime.tree.RuleNode.getParent.

Doc from org.antlr.v4.runtime.tree.Tree.getParent.

The parent of this node. If the return value is null, then this node is the root of the tree.

Annotations
@Override
getRuleContextback to summary
public <T extends ParserRuleContext> T getRuleContext(Class<? extends T> ctxType, int i)
getRuleContextsback to summary
public <T extends ParserRuleContext> List<? extends T> getRuleContexts(Class<? extends T> ctxType)
getSourceIntervalback to summary
public Interval getSourceInterval()

Overrides org.antlr.v4.runtime.RuleContext.getSourceInterval.

Implements org.antlr.v4.runtime.tree.SyntaxTree.getSourceInterval.

Doc from org.antlr.v4.runtime.tree.SyntaxTree.getSourceInterval.

Return an Interval indicating the index in the TokenStream of the first and last token associated with this subtree. If this node is a leaf, then the interval represents a single token and has interval i..i for token index i.

An interval of i..i-1 indicates an empty interval at position i in the input stream, where 0 <= i <= the size of the input token stream. Currently, the code base can only have i=0..n-1 but in concept one could have an empty interval after EOF.

If source interval is unknown, this returns Interval#INVALID.

As a weird special case, the source interval for rules matched after EOF is unspecified.

Annotations
@Override
getStartback to summary
public Token getStart()

Get the initial token in this context. Note that the range from start to stop is inclusive, so for rules that do not consume anything (for example, zero length or error productions) this token may exceed stop.

getStopback to summary
public Token getStop()

Get the final token in this context. Note that the range from start to stop is inclusive, so for rules that do not consume anything (for example, zero length or error productions) this token may precede start.

getTokenback to summary
public TerminalNode getToken(int ttype, int i)
getTokensback to summary
public List<? extends TerminalNode> getTokens(int ttype)
removeLastChildback to summary
public void removeLastChild()

Used by enterOuterAlt to toss out a RuleContext previously added as we entered a rule. If we have # label, we will need to remove generic ruleContext object.

toInfoStringback to summary
public String toInfoString(Parser recognizer)

Used for rule context info debugging during parse-time, not so much for ATN debugging