added ctx stack test

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9110]
This commit is contained in:
parrt 2011-10-05 15:15:58 -08:00
parent c99ce6d26e
commit 1efa316de9
2 changed files with 5 additions and 9 deletions

View File

@ -30,9 +30,7 @@ package org.antlr.v4.runtime;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.atn.ATNConfig;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.ParserATNSimulator;
import org.antlr.v4.runtime.atn.RuleTransition;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.misc.OrderedHashSet;
@ -242,11 +240,9 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
String[] ruleNames = getRuleNames();
List<String> stack = new ArrayList<String>();
RuleContext p = _ctx;
while ( p!=null && p.invokingState>=0 ) {
while ( p!=null ) {
// compute what follows who invoked us
ATNState invokingState = _interp.atn.states.get(p.invokingState);
RuleTransition rt = (RuleTransition)invokingState.transition(0);
stack.add(ruleNames[rt.ruleIndex]);
stack.add(ruleNames[p.getRuleIndex()]);
p = p.parent;
}
return stack;

View File

@ -3,16 +3,16 @@ package org.antlr.v4.test;
import org.junit.Test;
public class TestParseTrees extends BaseTest {
@Test public void testToken() throws Exception {
@Test public void testTokenAndRuleContextString() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTrees(true);}\n" +
"@after {System.out.println($r.toStringTree(this));}\n" +
" :r=a ;\n" +
"a : 'x' ;\n";
"a : 'x' {System.out.println(getRuleInvocationStack());} ;\n";
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false);
String expecting = "(a x)\n";
String expecting = "[a, s]\n(a x)\n";
assertEquals(expecting, result);
}