Package org.antlr.v4.runtime
Class ParserRuleContext
- java.lang.Object
-
- org.antlr.v4.runtime.RuleContext
-
- org.antlr.v4.runtime.ParserRuleContext
-
- All Implemented Interfaces:
ParseTree
,RuleNode
,SyntaxTree
,Tree
- Direct Known Subclasses:
InterpreterRuleContext
,RuleContextWithAltNum
public class ParserRuleContext extends RuleContext
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
Fields Modifier and Type Field Description 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.static ParserRuleContext
EMPTY
RecognitionException
exception
The exception that forced this rule to return.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.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.-
Fields inherited from class org.antlr.v4.runtime.RuleContext
invokingState, parent
-
-
Constructor Summary
Constructors Constructor Description ParserRuleContext()
ParserRuleContext(ParserRuleContext parent, int invokingStateNumber)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T extends ParseTree>
TaddAnyChild(T t)
Add a parse tree node to this as a child.RuleContext
addChild(RuleContext ruleInvocation)
TerminalNode
addChild(Token matchedToken)
Deprecated.TerminalNode
addChild(TerminalNode t)
Add a token leaf node child and force its parent to be this node.ErrorNode
addErrorNode(Token badToken)
Deprecated.ErrorNode
addErrorNode(ErrorNode errorNode)
Add an error node child and force its parent to be this node.void
copyFrom(ParserRuleContext ctx)
COPY a ctx (I'm deliberately not using copy constructor) to avoid confusion with creating node with parent.void
enterRule(ParseTreeListener listener)
void
exitRule(ParseTreeListener listener)
ParseTree
getChild(int i)
If there are children, get thei
th value indexed from 0.<T extends ParseTree>
TgetChild(Class<? extends T> ctxType, int i)
int
getChildCount()
How many children are there? If there is none, then this node represents a leaf node.ParserRuleContext
getParent()
The parent of this node.<T extends ParserRuleContext>
TgetRuleContext(Class<? extends T> ctxType, int i)
<T extends ParserRuleContext>
List<T>getRuleContexts(Class<? extends T> ctxType)
Interval
getSourceInterval()
Return anInterval
indicating the index in theTokenStream
of the first and last token associated with this subtree.Token
getStart()
Get the initial token in this context.Token
getStop()
Get the final token in this context.TerminalNode
getToken(int ttype, int i)
List<TerminalNode>
getTokens(int ttype)
void
removeLastChild()
Used by enterOuterAlt to toss out a RuleContext previously added as we entered a rule.String
toInfoString(Parser recognizer)
Used for rule context info debugging during parse-time, not so much for ATN debugging-
Methods inherited from class org.antlr.v4.runtime.RuleContext
accept, depth, getAltNumber, getPayload, getRuleContext, getRuleIndex, getText, isEmpty, setAltNumber, setParent, toString, toString, toString, toString, toString, toStringTree, toStringTree, toStringTree
-
-
-
-
Field Detail
-
EMPTY
public static final ParserRuleContext EMPTY
-
children
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.
-
start
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.
-
stop
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.
-
exception
public RecognitionException exception
The exception that forced this rule to return. If the rule successfully completed, this isnull
.
-
-
Constructor Detail
-
ParserRuleContext
public ParserRuleContext()
-
ParserRuleContext
public ParserRuleContext(ParserRuleContext parent, int invokingStateNumber)
-
-
Method Detail
-
copyFrom
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!
-
enterRule
public void enterRule(ParseTreeListener listener)
-
exitRule
public void exitRule(ParseTreeListener listener)
-
addAnyChild
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
-
addChild
public RuleContext addChild(RuleContext ruleInvocation)
-
addChild
public TerminalNode addChild(TerminalNode t)
Add a token leaf node child and force its parent to be this node.
-
addErrorNode
public ErrorNode addErrorNode(ErrorNode errorNode)
Add an error node child and force its parent to be this node.- Since:
- 4.7
-
addChild
@Deprecated public TerminalNode addChild(Token matchedToken)
Deprecated.Add a child to this node based upon matchedToken. It creates a TerminalNodeImpl rather than usingParser.createTerminalNode(ParserRuleContext, Token)
. I'm leaving this in for compatibility but the parser doesn't use this anymore.
-
addErrorNode
@Deprecated public ErrorNode addErrorNode(Token badToken)
Deprecated.Add a child to this node based upon badToken. It creates a ErrorNodeImpl rather than usingParser.createErrorNode(ParserRuleContext, Token)
. I'm leaving this in for compatibility but the parser doesn't use this anymore.
-
removeLastChild
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.
-
getParent
public ParserRuleContext getParent()
Description copied from interface:Tree
The parent of this node. If the return value is null, then this node is the root of the tree.
-
getChild
public ParseTree getChild(int i)
Description copied from interface:Tree
If there are children, get thei
th value indexed from 0.
-
getToken
public TerminalNode getToken(int ttype, int i)
-
getTokens
public List<TerminalNode> getTokens(int ttype)
-
getRuleContext
public <T extends ParserRuleContext> T getRuleContext(Class<? extends T> ctxType, int i)
-
getRuleContexts
public <T extends ParserRuleContext> List<T> getRuleContexts(Class<? extends T> ctxType)
-
getChildCount
public int getChildCount()
Description copied from interface:Tree
How many children are there? If there is none, then this node represents a leaf node.- Specified by:
getChildCount
in interfaceTree
- Overrides:
getChildCount
in classRuleContext
-
getSourceInterval
public Interval getSourceInterval()
Description copied from interface:SyntaxTree
Return anInterval
indicating the index in theTokenStream
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.
- Specified by:
getSourceInterval
in interfaceSyntaxTree
- Overrides:
getSourceInterval
in classRuleContext
-
getStart
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.
-
getStop
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.
-
-