diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index 3e23ecbea..675edbf8b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -213,7 +213,8 @@ public class ParserRuleContext extends RuleContext { return null; } - public Token getToken(int ttype, int i) { + @SuppressWarnings("checked") + public TerminalNode getToken(int ttype, int i) { if ( children==null || i < 0 || i >= children.size() ) { return null; } @@ -221,12 +222,12 @@ public class ParserRuleContext 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 tnode = (TerminalNode)o; Token symbol = tnode.getSymbol(); if ( symbol.getType()==ttype ) { j++; if ( j == i ) { - return symbol; + return tnode; } } } @@ -235,20 +236,23 @@ public class ParserRuleContext extends RuleContext { return null; } - public List getTokens(int ttype) { + @SuppressWarnings("checked") + public List> getTokens(int ttype) { if ( children==null ) { return Collections.emptyList(); } - List tokens = null; + List> tokens = null; for (ParseTree o : children) { if ( o instanceof TerminalNode ) { - TerminalNode tnode = (TerminalNode)o; + TerminalNode tnode = (TerminalNode)o; Token symbol = tnode.getSymbol(); - if ( tokens==null ) { - tokens = new ArrayList(); + if ( symbol.getType()==ttype ) { + if ( tokens==null ) { + tokens = new ArrayList>(); + } + tokens.add(tnode); } - tokens.add(symbol); } } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index e8af36bd8..7992fbb87 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -555,11 +555,11 @@ RuleContextDecl(r) ::= "public ;" RuleContextListDecl(rdecl) ::= "public List\<> = new ArrayList\<>();" ContextTokenGetterDecl(t) ::= - "public Token () { return getToken(., 0); }" + "public TerminalNode\<> () { return getToken(., 0); }" ContextTokenListGetterDecl(t) ::= - "public List\ () { return getTokens(.); }" + "public List\>> () { return getTokens(.); }" ContextTokenListIndexedGetterDecl(t) ::= << -public Token (int i) { +public TerminalNode\<\> (int i) { return getToken(., i); } >> diff --git a/tool/test/org/antlr/v4/test/TestListeners.java b/tool/test/org/antlr/v4/test/TestListeners.java index 2b6117135..46691b54d 100644 --- a/tool/test/org/antlr/v4/test/TestListeners.java +++ b/tool/test/org/antlr/v4/test/TestListeners.java @@ -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" +