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

public Class RuleContext

extends Object
implements RuleNode
Class Inheritance
All Implemented Interfaces
org.antlr.v4.runtime.tree.RuleNode, org.antlr.v4.runtime.tree.ParseTree, org.antlr.v4.runtime.tree.SyntaxTree, org.antlr.v4.runtime.tree.Tree
Known Direct Subclasses
org.antlr.v4.runtime.ParserRuleContext
Imports
org.antlr.v4.runtime.atn.ATN, org.antlr.v4.runtime.misc.Interval, org.antlr.v4.runtime.tree.ParseTree, .ParseTreeVisitor, .RuleNode, .Trees, java.util.Arrays, .List

A rule context is a record of a single rule invocation. We form a stack of these context objects using the parent pointer. A parent pointer of null indicates that the current context is the bottom of the stack. The ParserRuleContext subclass as a children list so that we can turn this data structure into a tree. The root node always has a null pointer and invokingState of -1. Upon entry to parsing, the first invoked rule function creates a context object (a subclass specialized for that rule such as SContext) and makes it the root of a parse tree, recorded by field Parser._ctx. public final SContext s() throws RecognitionException { SContext _localctx = new SContext(_ctx, getState()); <-- create new node enterRule(_localctx, 0, RULE_s); <-- push it ... exitRule(); <-- pop back to _localctx return _localctx; } A subsequent rule invocation of r from the start rule s pushes a new context object for r whose parent points at s and use invoking state is the state with r emanating as edge label. The invokingState fields from a context object to the root together form a stack of rule indication states where the root (bottom of the stack) has a -1 sentinel value. If we invoke start symbol s then call r1, which calls r2, the would look like this: SContext[-1] <- root node (bottom of the stack) R1Context[p] <- p in rule s called r1 R2Context[q] <- q in rule r1 called r2 So the top of the stack, _ctx, represents a call to the current rule and it holds the return address from another rule that invoke to this rule. To invoke a rule, we must always have a current context. The parent contexts are useful for computing lookahead sets and getting error information. These objects are used during parsing and prediction. For the special case of parsers, we use the subclass ParserRuleContext.
See Also
ParserRuleContext

Field Summary

Modifier and TypeField and Description
public int
invokingState

What state invoked the rule associated with this context?

public RuleContext
parent

What context invoked this rule?

Constructor Summary

AccessConstructor and Description
public
public
RuleContext(RuleContext parent, int invokingState)

Method Summary

Modifier and TypeMethod and Description
public <T> T
accept(ParseTreeVisitor<? extends T> visitor)

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

The ParseTreeVisitor needs a double dispatch method.
public int
public int
getAltNumber()

For rule associated with this parse tree internal node, return the outer alternative number used to match the input.

public ParseTree
getChild(int i)

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

If there are children, get the ith value indexed from 0.
public int
getChildCount()

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

How many children are there?
public RuleContext
getParent()

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

The parent of this node.
public RuleContext
getPayload()

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

This method returns whatever object represents the data at this node.
public RuleContext
public int
public Interval
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 String
getText()

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

Return the combined text of all child nodes.
public boolean
isEmpty()

A context is empty if there is no invoking state; meaning nobody called current context.

public void
setAltNumber(int altNumber)

Set the outer alternative number for this context node.

public void
setParent(RuleContext parent)

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

Set the parent for this node.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public final String
toString(Recognizer<?, ?> recog)

public final String
toString(List<String> ruleNames)

public String
toString(Recognizer<?, ?> recog, RuleContext stop)

public String
toString(List<String> ruleNames, RuleContext stop)

public String
toStringTree(Parser recog)

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

Print out a whole tree, not just a node, in LISP format (root child1 ..
public String
toStringTree(List<String> ruleNames)

Print out a whole tree, not just a node, in LISP format (root child1 ..

public String
toStringTree()

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

Print out a whole tree, not just a node, in LISP format (root child1 ..
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

invokingStateback to summary
public int invokingState

What state invoked the rule associated with this context? The "return address" is the followState of invokingState If parent is null, this should be -1 this context object represents the start rule.

parentback to summary
public RuleContext parent

What context invoked this rule?

Constructor Detail

RuleContextback to summary
public RuleContext()
RuleContextback to summary
public RuleContext(RuleContext parent, int invokingState)

Method Detail

acceptback to summary
public <T> T accept(ParseTreeVisitor<? extends T> visitor)

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

Doc from org.antlr.v4.runtime.tree.ParseTree.accept.

The ParseTreeVisitor needs a double dispatch method.

Annotations
@Override
depthback to summary
public int depth()
getAltNumberback to summary
public int getAltNumber()

For rule associated with this parse tree internal node, return the outer alternative number used to match the input. Default implementation does not compute nor store this alt num. Create a subclass of ParserRuleContext with backing field and set option contextSuperClass. to set it.

Since
4.5.3
getChildback to summary
public ParseTree getChild(int i)

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
getChildCountback to summary
public int 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 RuleContext getParent()

Implements org.antlr.v4.runtime.tree.ParseTree.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
getPayloadback to summary
public RuleContext getPayload()

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

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

This method returns whatever object represents the data at this node. For example, for parse trees, the payload can be a Token representing a leaf node or a RuleContext object representing a rule invocation. For abstract syntax trees (ASTs), this is a Token object.

Annotations
@Override
getRuleContextback to summary
public RuleContext getRuleContext()

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

Annotations
@Override
getRuleIndexback to summary
public int getRuleIndex()
getSourceIntervalback to summary
public Interval 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
getTextback to summary
public String getText()

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

Return the combined text of all child nodes. This method only considers tokens which have been added to the parse tree.

Since tokens on hidden channels (e.g. whitespace or comments) are not added to the parse trees, they will not appear in the output of this method.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

A context is empty if there is no invoking state; meaning nobody called current context.

setAltNumberback to summary
public void setAltNumber(int altNumber)

Set the outer alternative number for this context node. Default implementation does nothing to avoid backing field overhead for trees that don't need it. Create a subclass of ParserRuleContext with backing field and set option contextSuperClass.

Since
4.5.3
setParentback to summary
public void setParent(RuleContext parent)

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

Doc from org.antlr.v4.runtime.tree.ParseTree.setParent.

Set the parent for this node. This is not backward compatible as it changes the interface but no one was able to create custom nodes anyway so I'm adding as it improves internal code quality. One could argue for a restructuring of the class/interface hierarchy so that setParent, addChild are moved up to Tree but that's a major change. So I'll do the minimal change, which is to add this method.

Annotations
@Override
Since
4.7. ParseTree#setParent comment
See Also
ParseTree#setParent
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
toStringback to summary
public final String toString(Recognizer<?, ?> recog)
toStringback to summary
public final String toString(List<String> ruleNames)
toStringback to summary
public String toString(Recognizer<?, ?> recog, RuleContext stop)
toStringback to summary
public String toString(List<String> ruleNames, RuleContext stop)
toStringTreeback to summary
public String toStringTree(Parser recog)

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

Print out a whole tree, not just a node, in LISP format (root child1 .. childN). Print just a node if this is a leaf. We have to know the recognizer so we can get rule names.

Annotations
@Override
toStringTreeback to summary
public String toStringTree(List<String> ruleNames)

Print out a whole tree, not just a node, in LISP format (root child1 .. childN). Print just a node if this is a leaf.

toStringTreeback to summary
public String toStringTree()

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

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

Print out a whole tree, not just a node, in LISP format (root child1 .. childN). Print just a node if this is a leaf.

Annotations
@Override