TestListeners for Java is working

This commit is contained in:
Sam Harwell 2015-01-25 21:42:24 -06:00
parent 113b72da53
commit 9ce5efc570
13 changed files with 159 additions and 267 deletions

View File

@ -1,6 +1,7 @@
TestFolders ::= [
"CompositeLexers": [],
"LexerExec": [],
"Listeners": [],
"ParserErrors": [],
"ParseTrees": [],
"SemPredEvalLexer": [],

View File

@ -1,3 +1,26 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "1 2"
Rule() ::= "s"
Output() ::= <<
(a 1 2)
1
2<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportListener(grammarName)>
@ -20,4 +43,5 @@ MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;
WS : [ \t\n]+ -> skip ;
>>

View File

@ -0,0 +1,12 @@
//TestFolders ::= [
//]
TestTemplates ::= [
"Basic": [],
"TokenGetters_1": [],
"TokenGetters_2": [],
"RuleGetters_1": [],
"RuleGetters_2": [],
"LR": [],
"LRWithLabels": []
]

View File

@ -1,3 +1,29 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "1+2*3"
Rule() ::= "s"
Output() ::= <<
(e (e 1) + (e (e 2) * (e 3)))
1
2
3
2 3 2
1 2 1<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportListener(grammarName)>
@ -22,3 +48,4 @@ ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;
>>

View File

@ -1,3 +1,28 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "1(2,3)"
Rule() ::= "s"
Output() ::= <<
(e (e 1) ( (eList (e 2) , (e 3)) ))
1
2
3
1 [13 6]<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportListener(grammarName)>
@ -22,3 +47,4 @@ ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;
>>

View File

@ -1,3 +1,16 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportListener(grammarName)>
@ -22,3 +35,4 @@ ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;
>>

View File

@ -0,0 +1,10 @@
import "RuleGetters.stg"
Input() ::= "1 2"
Output() ::= <<
(a (b 1) (b 2))
1 2 1<\n>
>>
Errors() ::= ""

View File

@ -0,0 +1,10 @@
import "RuleGetters.stg"
Input() ::= "abc"
Output() ::= <<
(a (b abc))
abc<\n>
>>
Errors() ::= ""

View File

@ -1,3 +1,16 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportListener(grammarName)>
@ -21,3 +34,4 @@ ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;
>>

View File

@ -0,0 +1,10 @@
import "TokenGetters.stg"
Input() ::= "1 2"
Output() ::= <<
(a 1 2)
1 2 [1, 2]<\n>
>>
Errors() ::= ""

View File

@ -0,0 +1,10 @@
import "TokenGetters.stg"
Input() ::= "abc"
Output() ::= <<
(a abc)
[@0,0:2='abc',\<4>,1:0]<\n>
>>
Errors() ::= ""

View File

@ -154,48 +154,10 @@ public class Generator {
list.add(buildFullContextParsing());
list.add(buildLeftRecursion());
list.add(buildLexerErrors());
list.add(buildListeners());
list.add(buildParserExec());
return list;
}
private JUnitTestFile buildListeners() throws Exception {
JUnitTestFile file = new JUnitTestFile("Listeners");
file.addParserTest(input, "Basic", "T", "s",
"1 2",
"(a 1 2)\n" + "1\n" + "2\n",
null);
file.addParserTests(input, "TokenGetters", "T", "s",
"1 2",
"(a 1 2)\n" +
"1 2 [1, 2]\n",
"abc",
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n");
file.addParserTests(input, "RuleGetters", "T", "s",
"1 2",
"(a (b 1) (b 2))\n" +
"1 2 1\n",
"abc",
"(a (b abc))\n" +
"abc\n");
file.addParserTest(input, "LR", "T", "s",
"1+2*3",
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1\n" +
"2\n" +
"3\n" +
"2 3 2\n" +
"1 2 1\n",
null);
file.addParserTest(input, "LRWithLabels", "T", "s",
"1(2,3)",
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1\n" + "2\n" + "3\n" + "1 [13 6]\n",
null);
return file;
}
private JUnitTestFile buildLexerErrors() throws Exception {
JUnitTestFile file = new JUnitTestFile("LexerErrors");
file.addLexerTest(input, "InvalidCharAtStart", "L",

View File

@ -1,228 +0,0 @@
package org.antlr.v4.test.rt.java;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestListeners extends BaseTest {
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testBasic() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void visitTerminal(TerminalNode node) {\n" +
" System.out.println(node.getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : INT INT\n" +
" | ID\n" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false);
assertEquals("(a 1 2)\n1\n2\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
String testTokenGetters(String input) throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) \n" +
" System.out.printf(\"%s %s %s\",ctx.INT(0).getSymbol().getText(),\n" +
" ctx.INT(1).getSymbol().getText(),ctx.INT());\n" +
" else\n" +
" System.out.println(ctx.ID().getSymbol());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : INT INT\n" +
" | ID\n" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testTokenGetters_1() throws Exception {
String found = testTokenGetters("1 2");
assertEquals("(a 1 2)\n1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testTokenGetters_2() throws Exception {
String found = testTokenGetters("abc");
assertEquals("(a abc)\n[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
String testRuleGetters(String input) throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) {\n" +
" System.out.printf(\"%s %s %s\",ctx.b(0).start.getText(),\n" +
" ctx.b(1).start.getText(),ctx.b().get(0).start.getText());\n" +
" } else \n" +
" System.out.println(ctx.b(0).start.getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : b b // forces list\n" +
" | b // a list still\n" +
" ;\n" +
"b : ID | INT;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testRuleGetters_1() throws Exception {
String found = testRuleGetters("1 2");
assertEquals("(a (b 1) (b 2))\n1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testRuleGetters_2() throws Exception {
String found = testRuleGetters("abc");
assertEquals("(a (b abc))\nabc\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testLR() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitE(TParser.EContext ctx) {\n" +
" if (ctx.getChildCount()==3) {\n" +
" System.out.printf(\"%s %s %s\\n\",ctx.e(0).start.getText(),\n" +
" ctx.e(1).start.getText(), ctx.e().get(0).start.getText());\n" +
" } else \n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=e ;\n" +
"e : e op='*' e\n" +
" | e op='+' e\n" +
" | INT\n" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1+2*3", false);
assertEquals("(e (e 1) + (e (e 2) * (e 3)))\n1\n2\n3\n2 3 2\n1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testLRWithLabels() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitCall(TParser.CallContext ctx) {\n" +
" System.out.printf(\"%s %s\",ctx.e().start.getText(),ctx.eList());\n" +
" }\n" +
" public void exitInt(TParser.IntContext ctx) {\n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=e ;\n" +
"e : e '(' eList ')' # Call\n" +
" | INT # Int\n" +
" ;\n" +
"eList : e (',' e)* ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1(2,3)", false);
assertEquals("(e (e 1) ( (eList (e 2) , (e 3)) ))\n1\n2\n3\n1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
}