getToken/s returns TerminalNode now per Sam's suggestion. altered tests. seems correct.

This commit is contained in:
Terence Parr 2012-02-27 21:39:30 -08:00
parent 9ae92a5cef
commit dd69a7532d
3 changed files with 23 additions and 18 deletions

View File

@ -213,7 +213,8 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
return null;
}
public Token getToken(int ttype, int i) {
@SuppressWarnings("checked")
public TerminalNode<Symbol> getToken(int ttype, int i) {
if ( children==null || i < 0 || i >= children.size() ) {
return null;
}
@ -221,12 +222,12 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
int j = -1; // what token with ttype have we found?
for (ParseTree o : children) {
if ( o instanceof TerminalNode<?> ) {
TerminalNode<?> tnode = (TerminalNode<?>)o;
TerminalNode<Symbol> tnode = (TerminalNode<Symbol>)o;
Token symbol = tnode.getSymbol();
if ( symbol.getType()==ttype ) {
j++;
if ( j == i ) {
return symbol;
return tnode;
}
}
}
@ -235,20 +236,23 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
return null;
}
public List<? extends Token> getTokens(int ttype) {
@SuppressWarnings("checked")
public List<TerminalNode<Symbol>> getTokens(int ttype) {
if ( children==null ) {
return Collections.emptyList();
}
List<Token> tokens = null;
List<TerminalNode<Symbol>> tokens = null;
for (ParseTree o : children) {
if ( o instanceof TerminalNode<?> ) {
TerminalNode<?> tnode = (TerminalNode<?>)o;
TerminalNode<Symbol> tnode = (TerminalNode<Symbol>)o;
Token symbol = tnode.getSymbol();
if ( symbol.getType()==ttype ) {
if ( tokens==null ) {
tokens = new ArrayList<Token>();
tokens = new ArrayList<TerminalNode<Symbol>>();
}
tokens.add(tnode);
}
tokens.add(symbol);
}
}

View File

@ -555,11 +555,11 @@ RuleContextDecl(r) ::= "public <r.ctxName> <r.name>;"
RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>();"
ContextTokenGetterDecl(t) ::=
"public Token <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
"public TerminalNode\<<TokenLabelType()>> <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
ContextTokenListGetterDecl(t) ::=
"public List\<? extends Token> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
"public List\<TerminalNode\<<TokenLabelType()>>> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
ContextTokenListIndexedGetterDecl(t) ::= <<
public Token <t.name>(int i) {
public TerminalNode\<<TokenLabelType()>\> <t.name>(int i) {
return getToken(<parser.name>.<t.name>, i);
}
>>

View File

@ -41,8 +41,8 @@ public class TestListeners extends BaseTest {
"@members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) System.out.printf(\"%s %s %s\",ctx.INT(0).getText(),ctx.INT(1).getText(),ctx.INT());\n" +
" else System.out.println(ctx.ID());\n" +
" if (ctx.getChildCount()==2) System.out.printf(\"%s %s %s\",ctx.INT(0).getSymbol().getText(),ctx.INT(1).getSymbol().getText(),ctx.INT());\n" +
" else System.out.println(ctx.ID().getSymbol());\n" +
" }\n" +
" }}\n" +
"s\n" +
@ -62,8 +62,9 @@ public class TestListeners extends BaseTest {
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "1 2", false);
String expecting = "(a 1 2)\n" +
"1 2 [[@0,0:0='1',<5>,1:0], [@1,2:2='2',<5>,1:2]]\n";
String expecting =
"(a 1 2)\n" +
"1 2 [1, 2]\n";
assertEquals(expecting, result);
result = execParser("T.g", grammar, "TParser", "TLexer", "s", "abc", false);
@ -124,7 +125,7 @@ public class TestListeners extends BaseTest {
" ctx.e(1).start.getText()," +
" ctx.e().get(0).start.getText());\n" +
" }\n" +
" else System.out.println(ctx.INT().getText());\n" +
" else System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
" }" +
"}\n" +
@ -165,7 +166,7 @@ public class TestListeners extends BaseTest {
" ctx.eList());\n" +
" }\n" +
" public void exitInt(TParser.IntContext ctx) {\n" +
" System.out.println(ctx.INT().getText());\n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
" }\n" +
"}\n" +