This commit is contained in:
ericvergnaud 2014-10-15 23:15:13 +08:00
parent 4df40e58e2
commit 50466f61dd
6 changed files with 156 additions and 4 deletions

View File

@ -1,8 +1,5 @@
package org.antlr.v4.testgen;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -14,7 +11,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;
@ -116,10 +112,48 @@ public class Generator {
list.add(buildFullContextParsing());
list.add(buildLeftRecursion());
list.add(buildLexerErrors());
list.add(buildListeners());
return list;
}
private TestFile buildListeners() throws Exception {
TestFile file = new TestFile("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 TestFile buildLexerErrors() throws Exception {
TestFile file = new TestFile("LexerErrors");
file.addLexerTest(input, "InvalidCharAtStart", "L",

View File

@ -0,0 +1,23 @@
grammar <grammarName>;
@parser::header {
<importListener(grammarName)>
}
@parser::members {
<BasicListener(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
}
: r=a ;
a : INT INT
| ID
;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -0,0 +1,24 @@
grammar <grammarName>;
@parser::header {
<importListener(grammarName)>
}
@parser::members {
<LRListener(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
}
: r=e ;
e : e op='*' e
| e op='+' e
| INT
;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -0,0 +1,24 @@
grammar <grammarName>;
@parser::header {
<importListener(grammarName)>
}
@parser::members {
<LRWithLabelsListener(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
}
: r=e ;
e : e '(' eList ')' # Call
| INT # Int
;
eList : e (',' e)* ;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -0,0 +1,24 @@
grammar <grammarName>;
@parser::header {
<importListener(grammarName)>
}
@parser::members {
<RuleGetterListener(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
}
: r=a ;
a : b b // forces list
| b // a list still
;
b : ID | INT;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -0,0 +1,23 @@
grammar <grammarName>;
@parser::header {
<importListener(grammarName)>
}
@parser::members {
<TokenGetterListener(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
}
: r=a ;
a : INT INT
| ID
;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;
ID : [a-z]+ ;
WS : [ \t\n]+ -> skip ;