TestParseTrees for Java is working

This commit is contained in:
Sam Harwell 2015-01-21 19:35:33 -06:00
parent 039bd22b67
commit 4ec94e0fae
21 changed files with 277 additions and 319 deletions

View File

@ -1,5 +1,6 @@
TestFolders ::= [
"CompositeLexers": []
"CompositeLexers": [],
"ParseTrees": []
]
//TestTemplates ::= [

View File

@ -0,0 +1,29 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "xyyxyxz"
Rule() ::= "s"
Output() ::= <<
(a x y y x y x z)<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : ('x' | 'y')* 'z'
;
>>

View File

@ -0,0 +1,29 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "y"
Rule() ::= "s"
Output() ::= <<
(a y)<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' | 'y'
;
>>

View File

@ -0,0 +1,34 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "xzy"
Rule() ::= "s"
Output() ::= <<
(a x z y)<\n>
>>
Errors() ::= <<
line 1:1 extraneous input 'z' expecting 'y'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'
;
Z : 'z'
;
>>

View File

@ -0,0 +1,13 @@
//TestFolders ::= [
//]
TestTemplates ::= [
"TokenAndRuleContextString": [],
"Token2": [],
"2Alts": [],
"2AltLoop": [],
"RuleRef": [],
"ExtraToken": [],
"NoViableAlt": [],
"Sync": []
]

View File

@ -0,0 +1,33 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "z"
Rule() ::= "s"
Output() ::= <<
(a z)<\n>
>>
Errors() ::= <<
line 1:0 mismatched input 'z' expecting {'x', 'y'}<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' | 'y'
;
Z : 'z'
;
>>

View File

@ -0,0 +1,31 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "yx"
Rule() ::= "s"
Output() ::= <<
(a (b y) x)<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : b 'x'
;
b : 'y'
;
>>

View File

@ -0,0 +1,33 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "xzyy!"
Rule() ::= "s"
Output() ::= <<
(a x z y y !)<\n>
>>
Errors() ::= <<
line 1:1 extraneous input 'z' expecting {'y', '!'}<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'* '!'
;
Z : 'z'
;
>>

View File

@ -0,0 +1,29 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "xy"
Rule() ::= "s"
Output() ::= <<
(a x y)<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'
;
>>

View File

@ -0,0 +1,31 @@
TestType() ::= "Parser"
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "x"
Rule() ::= "s"
Output() ::= <<
[a, s]
(a x)<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' {
<RuleInvocationStack():writeln()>
} ;
>>

View File

@ -158,7 +158,6 @@ public class Generator {
list.add(buildListeners());
list.add(buildParserErrors());
list.add(buildParserExec());
list.add(buildParseTrees());
list.add(buildSemPredEvalLexer());
list.add(buildSemPredEvalParser());
return list;
@ -437,43 +436,6 @@ public class Generator {
return file;
}
private JUnitTestFile buildParseTrees() throws Exception {
JUnitTestFile file = new JUnitTestFile("ParseTrees");
file.addParserTest(input, "TokenAndRuleContextString", "T", "s",
"x",
"[a, s]\n(a x)\n",
null);
file.addParserTest(input, "Token2", "T", "s",
"xy",
"(a x y)\n",
null);
file.addParserTest(input, "2Alts", "T", "s",
"y",
"(a y)\n",
null);
file.addParserTest(input, "2AltLoop", "T", "s",
"xyyxyxz",
"(a x y y x y x z)\n",
null);
file.addParserTest(input, "RuleRef", "T", "s",
"yx",
"(a (b y) x)\n",
null);
file.addParserTest(input, "ExtraToken", "T", "s",
"xzy",
"(a x z y)\n", // ERRORs not shown. z is colored red in tree view
"line 1:1 extraneous input 'z' expecting 'y'\n");
file.addParserTest(input, "NoViableAlt", "T", "s",
"z",
"(a z)\n",
"line 1:0 mismatched input 'z' expecting {'x', 'y'}\n");
file.addParserTest(input, "Sync", "T", "s",
"xzyy!",
"(a x z y y !)\n",
"line 1:1 extraneous input 'z' expecting {'y', '!'}\n");
return file;
}
private JUnitTestFile buildParserErrors() throws Exception {
JUnitTestFile file = new JUnitTestFile("ParserErrors");
file.addParserTest(input, "TokenMismatch", "T", "a",

View File

@ -1,11 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : ('x' | 'y')* 'z'
;

View File

@ -1,11 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' | 'y'
;

View File

@ -1,14 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'
;
Z : 'z'
;

View File

@ -1,14 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' | 'y'
;
Z : 'z'
;

View File

@ -1,13 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : b 'x'
;
b : 'y'
;

View File

@ -1,13 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'* '!'
;
Z : 'z'
;

View File

@ -1,11 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' 'y'
;

View File

@ -1,12 +0,0 @@
grammar <grammarName>;
s
@init {
<BuildParseTrees()>
}
@after {
<ToStringTree("$r.ctx"):writeln()>
}
: r=a ;
a : 'x' {
<RuleInvocationStack():writeln()>
} ;

View File

@ -1,171 +0,0 @@
package org.antlr.v4.test.rt.java;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestParseTrees extends BaseTest {
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testTokenAndRuleContextString() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' { \n" +
"System.out.println(getRuleInvocationStack());\n" +
"} ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false);
assertEquals("[a, s]\n(a x)\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testToken2() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' 'y'\n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xy", false);
assertEquals("(a x y)\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void test2Alts() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' | 'y'\n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y", false);
assertEquals("(a y)\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void test2AltLoop() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : ('x' | 'y')* 'z'\n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xyyxyxz", false);
assertEquals("(a x y y x y x z)\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testRuleRef() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : b 'x'\n" +
" ;\n" +
"b : 'y' \n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "yx", false);
assertEquals("(a (b y) x)\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testExtraToken() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' 'y'\n" +
" ;\n" +
"Z : 'z' \n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzy", false);
assertEquals("(a x z y)\n", found);
assertEquals("line 1:1 extraneous input 'z' expecting 'y'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testNoViableAlt() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' | 'y'\n" +
" ;\n" +
"Z : 'z' \n" +
" ;\n" +
" ";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "z", false);
assertEquals("(a z)\n", found);
assertEquals("line 1:0 mismatched input 'z' expecting {'x', 'y'}\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testSync() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' 'y'* '!'\n" +
" ;\n" +
"Z : 'z' \n" +
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzyy!", false);
assertEquals("(a x z y y !)\n", found);
assertEquals("line 1:1 extraneous input 'z' expecting {'y', '!'}\n", this.stderrDuringParse);
}
}

View File

@ -74,6 +74,8 @@ writeLines(textLines) ::= <%
ParserTestMethod(test) ::= <<
@Test
public void test<test.name>() throws Exception {
mkdir(tmpdir);
<!<test.slaveGrammars:{ grammar |
String slave_<grammar.grammarName> = <grammar.lines:{ line | "<line>};separator="\\n\" +\n", wrap, anchor>";
<if(test.slaveIsLexer)>
@ -84,19 +86,20 @@ public void test<test.name>() throws Exception {
<endif>
};separator="\n", wrap, anchor>!>
mkdir(tmpdir);
<test.Grammars: {grammarFileName | writeFile("<grammarFileName>", <string(test.Grammars.(grammarFileName))>);}; separator="\n", anchor>
writeFile(tmpdir, "<test.Grammars.keys>"
String grammar = <test.grammar.lines:{ line | "<line>};separator="\\n\" +\n", wrap, anchor>";
<test.afterGrammar>
String found = execParser("<test.grammar.grammarName>.g4", grammar, "<test.grammar.grammarName><if(!test.slaveIsLexer)>Parser<endif>", "<if(test.slaveIsLexer)><first(test.slaveGrammars).grammarName><else><test.grammar.grammarName>Lexer<endif>", "<test.startRule>", "<test.input>", <test.debug>);
assertEquals("<test.expectedOutput>", found);
<if(isEmpty.(test.Errors))>
assertEquals("<test.expectedErrors>", this.stderrDuringParse);
<test.Grammar:{grammar |
String grammar = <writeStringLiteral(test.Grammar.(grammar))>;
<if(test.AfterGrammar)>
<test.AfterGrammar>
<endif>
String input =<writeStringLiteral(test.Input)>;
String found = execParser("<grammar>.g4", grammar, "<grammar>Parser", "<grammar>Lexer", "<test.Rule>", input, <writeBoolean(test.Debug)>);
assertEquals(<writeStringLiteral(test.Output)>, found);
<if(!isEmpty.(test.Errors))>
assertEquals(<writeStringLiteral(test.Errors)>, this.stderrDuringParse);
<else>
assertNull(this.stderrDuringParse);
<endif>
}>
}
>>