Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/java/org/antlr/v4/runtime/RuleContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* 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.
* <p>
* The root node always has a null pointer and invokingState of -1.
* The root node of an actual parse tree always has a null parent pointer and invokingState of -1.
* <p>
* 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.
Expand Down Expand Up @@ -67,11 +67,9 @@ public class RuleContext implements RuleNode {
public @Nullable RuleContext parent;

/**
* 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.
* What state invoked the rule associated with this context? The "return address" is the followState of invokingState.
* For the root node of an actual parse tree, where parent is null, this should be -1.
*/
// todo на самом деле все не совсем так, есть варианты, когда
// todo parent == null, а стейт отличен от -1
public int invokingState = -1;

public RuleContext(@Nullable RuleContext parent, int invokingState) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/antlr/v4/runtime/tree/TreesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setUp() {
terminal1 = new TerminalNodeImpl(token1);
terminal2 = new TerminalNodeImpl(token2);

root = new TestParserRuleContext(null, 0, 0);
root = new TestParserRuleContext(null, -1, 0);
child1 = new TestParserRuleContext(root, -1, 1);
child2 = new TestParserRuleContext(root, -1, 1);
grandChild = new TestParserRuleContext(child1, -1, 2);
Expand Down Expand Up @@ -167,7 +167,7 @@ void testGetTokensFromParseTree() {

@Test
void testGetTokensFromParseTreeEmpty() {
ParserRuleContext emptyContext = new TestParserRuleContext(null, 0, 0);
ParserRuleContext emptyContext = new TestParserRuleContext(null, -1, 0);
List<Token> tokens = Trees.getTokensFromParseTree(emptyContext);
assertThat(tokens).isEmpty();
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/antlr/v4/test/tool/ExpectedTokensTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ void testFollowIncludedInLeftRecursiveRule() throws Exception {
ATN atn = g.getATN();

// Simulate call stack after input '(x' from rule s
ParserRuleContext callStackFrom_s = new ParserRuleContext(null, 4);
ParserRuleContext root = ParserRuleContext.emptyContext();
ParserRuleContext callStackFrom_s = new ParserRuleContext(root, 4);
ParserRuleContext callStackFrom_expr = new ParserRuleContext(callStackFrom_s, 9);
int afterID = 14;
IntervalSet tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
assertThat(tokens.toString(g.getVocabulary())).isEqualTo("{R, PLUS}");

// Simulate call stack after input '(x' from within rule expr
callStackFrom_expr = new ParserRuleContext(null, 9);
callStackFrom_expr = new ParserRuleContext(ParserRuleContext.emptyContext(), 9);
tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
assertThat(tokens.toString(g.getVocabulary())).isEqualTo("{R, PLUS}");
}
Expand Down