Merge pull request #83 from pboyer/reduce-model-changes

Reduce model changes from parrt
This commit is contained in:
Peter Boyer 2016-11-05 15:29:37 -04:00 committed by GitHub
commit 485465ee7a
41 changed files with 2239 additions and 239 deletions

7
.editorconfig Normal file
View File

@ -0,0 +1,7 @@
root = true
[*.{java,stg}]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# This rule applies to all files which don't match another line below
* text=auto

View File

@ -96,4 +96,6 @@ YYYY/MM/DD, github id, Full name, email
2016/08/08, wjkohnen, Wolfgang Johannes Kohnen, wjkohnen-go-antlr@ko-sys.com
2016/08/11, BurtHarris, Ralph "Burt" Harris, Burt_Harris_antlr4@azxs.33mail.com
2016/08/19, andjo403, Andreas Jonson, andjo403@hotmail.com
2016/10/13, cgudrian, Christian Gudrian, christian.gudrian@gmx.de
2016/10/13, nielsbasjes, Niels Basjes, niels@basjes.nl
2016/11/05, runner-mei, meifakun, runner.mei@gmail.com

View File

@ -1,4 +1,11 @@
IgnoredTests ::= [
"Visitors.Basic": true,
"Visitors.LR": true,
"Visitors.LRWithLabels": true,
"Visitors.RuleGetters_1": true,
"Visitors.RuleGetters_2": true,
"Visitors.TokenGetters_1": true,
"Visitors.TokenGetters_2": true,
default: false
]

View File

@ -428,6 +428,13 @@ Declare_pred() ::= <<boolean pred(boolean v) {
Invoke_pred(v) ::= <<this.pred(<v>)>>
IgnoredTests ::= [
"Visitors.Basic": true,
"Visitors.LR": true,
"Visitors.LRWithLabels": true,
"Visitors.RuleGetters_1": true,
"Visitors.RuleGetters_2": true,
"Visitors.TokenGetters_1": true,
"Visitors.TokenGetters_2": true,
default: false
]

View File

@ -227,6 +227,8 @@ TokenStartColumnEquals(i) ::= <%this._tokenStartColumn===<i>%>
ImportListener(X) ::= <<var <X>Listener = require('./<X>Listener').<X>Listener;>>
ImportVisitor(X) ::= <<var <X>Visitor = require('./<X>Visitor').<X>Visitor;>>
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.literalNames)"
RuleInvocationStack() ::= "antlr4.Utils.arrayToString(this.getRuleInvocationStack())"
@ -328,6 +330,24 @@ var walker = new antlr4.tree.ParseTreeWalker();
walker.walk(new this.LeafListener(), <s>);
>>
BasicVisitor(X) ::= <<
this.LeafVisitor = function() {
this.visitTerminal = function(node) {
return node.symbol.text;
};
return this;
};
this.LeafVisitor.prototype = Object.create(<X>Visitor.prototype);
this.LeafVisitor.prototype.constructor = this.LeafVisitor;
>>
WalkVisitor(s) ::= <<
var visitor = new this.LeafVisitor();
console.log(<s>.accept(visitor));
>>
TreeNodeWithAltNumField(X) ::= <<
@parser::header {
@ -364,6 +384,24 @@ this.LeafListener.prototype.constructor = this.LeafListener;
>>
TokenGetterVisitor(X) ::= <<
this.LeafVisitor = function() {
this.visitA = function(ctx) {
var str;
if(ctx.getChildCount()===2) {
str = ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + antlr4.Utils.arrayToString(ctx.INT());
} else {
str = ctx.ID().symbol.toString();
}
return this.visitChildren(ctx) + str;
};
return this;
};
this.LeafVisitor.prototype = Object.create(<X>Visitor.prototype);
this.LeafVisitor.prototype.constructor = this.LeafVisitor;
>>
RuleGetterListener(X) ::= <<
this.LeafListener = function() {
this.exitA = function(ctx) {
@ -382,6 +420,24 @@ this.LeafListener.prototype.constructor = this.LeafListener;
>>
RuleGetterVisitor(X) ::= <<
this.LeafVisitor = function() {
this.visitA = function(ctx) {
var str;
if(ctx.getChildCount()===2) {
str = ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text;
} else {
str = ctx.b(0).start.text;
}
return this.visitChildren(ctx) + str;
};
return this;
};
this.LeafVisitor.prototype = Object.create(<X>Visitor.prototype);
this.LeafVisitor.prototype.constructor = this.LeafVisitor;
>>
LRListener(X) ::= <<
this.LeafListener = function() {
@ -401,6 +457,24 @@ this.LeafListener.prototype.constructor = this.LeafListener;
>>
LRVisitor(X) ::= <<
this.LeafVisitor = function() {
this.visitE = function(ctx) {
var str;
if(ctx.getChildCount()===3) {
str = ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text;
} else {
str = ctx.INT().symbol.text;
}
return this.visitChildren(ctx) + str;
};
return this;
};
this.LeafVisitor.prototype = Object.create(<X>Visitor.prototype);
this.LeafVisitor.prototype.constructor = this.LeafVisitor;
>>
LRWithLabelsListener(X) ::= <<
this.LeafListener = function() {
this.exitCall = function(ctx) {
@ -418,6 +492,23 @@ this.LeafListener.prototype.constructor = this.LeafListener;
>>
LRWithLabelsVisitor(X) ::= <<
this.LeafVisitor = function() {
this.visitCall = function(ctx) {
var str = ctx.e().start.text + ' ' + ctx.eList();
return this.visitChildren(ctx) + str;
};
this.visitInt = function(ctx) {
var str = ctx.INT().symbol.text;
return this.visitChildren(ctx) + str;
};
return this;
};
this.LeafVisitor.prototype = Object.create(<X>Visitor.prototype);
this.LeafVisitor.prototype.constructor = this.LeafVisitor;
>>
DeclareContextListGettersFunction() ::= <<
function foo() {
var s = new SContext();

View File

@ -1,4 +1,11 @@
IgnoredTests ::= [
"Visitors.Basic": true,
"Visitors.LR": true,
"Visitors.LRWithLabels": true,
"Visitors.RuleGetters_1": true,
"Visitors.RuleGetters_2": true,
"Visitors.TokenGetters_1": true,
"Visitors.TokenGetters_2": true,
default: false
]

View File

@ -1,4 +1,11 @@
IgnoredTests ::= [
"Visitors.Basic": true,
"Visitors.LR": true,
"Visitors.LRWithLabels": true,
"Visitors.RuleGetters_1": true,
"Visitors.RuleGetters_2": true,
"Visitors.TokenGetters_1": true,
"Visitors.TokenGetters_2": true,
default: false
]

View File

@ -12,5 +12,6 @@ TestFolders ::= [
"Performance": [],
"SemPredEvalLexer": [],
"SemPredEvalParser": [],
"Sets": []
"Sets": [],
"Visitors": []
]

View File

@ -0,0 +1,46 @@
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 {
<ImportVisitor(grammarName)>
}
@parser::members {
<BasicVisitor(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<WalkVisitor("$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,9 @@
TestTemplates ::= [
"Basic": [],
"TokenGetters_1": [],
"TokenGetters_2": [],
"RuleGetters_1": [],
"RuleGetters_2": [],
"LR": [],
"LRWithLabels": []
]

View File

@ -0,0 +1,47 @@
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,,32 3 21 2 1<\n>
>>
Errors() ::= ""
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportVisitor(grammarName)>
}
@parser::members {
<LRVisitor(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<WalkVisitor("$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,47 @@
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 {
<ImportVisitor(grammarName)>
}
@parser::members {
<LRWithLabelsVisitor(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<WalkVisitor("$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,38 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportVisitor(grammarName)>
}
@parser::members {
<RuleGetterVisitor(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<WalkVisitor("$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,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

@ -0,0 +1,37 @@
TestType() ::= "Parser"
Options ::= [
"Debug": false
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
@parser::header {
<ImportVisitor(grammarName)>
}
@parser::members {
<TokenGetterVisitor(grammarName)>
}
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<WalkVisitor("$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,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

@ -0,0 +1,260 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.csharp;
import org.junit.Ignore;
import org.junit.Test;
@SuppressWarnings("unused")
public class TestVisitors extends BaseTest {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(223);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"[ '1', '2' ]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(238);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1,,2,,32 3 21 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(278);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1,,2,,3,1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(265);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
",1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(265);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(223);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a 1 2)\n" +
",1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(223);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("Console.WriteLine($ctx.r.ToStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -170,7 +170,8 @@ public abstract class BaseTest {
}
private static void cacheGoRuntime(File tmpPackageDir) throws Exception {
ProcessBuilder pb = new ProcessBuilder("go", "install", "-x");
String goExecutable = locateGo();
ProcessBuilder pb = new ProcessBuilder(goExecutable, "install", "-x");
pb.directory(tmpPackageDir);
pb.environment().put("GOPATH", tmpGopath.getPath());
pb.redirectErrorStream(true);
@ -477,7 +478,7 @@ public abstract class BaseTest {
return null;
}
private String locateTool(String tool) {
private static String locateTool(String tool) {
ArrayList<String> paths = new ArrayList<String>(); // default cap is about right
// GOROOT should have priority if set
@ -508,7 +509,7 @@ public abstract class BaseTest {
return null;
}
private String locateGo() {
private static String locateGo() {
String propName = "antlr-go";
String prop = System.getProperty(propName);
if (prop == null || prop.length() == 0) {

View File

@ -0,0 +1,290 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.java;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class TestVisitors extends BaseTest {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(224);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"[ '1', '2' ]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(239);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1,,2,,32 3 21 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(279);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1,,2,,3,1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(266);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
",1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(266);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(224);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a 1 2)\n" +
",1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(224);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("System.out.println($ctx.r.toStringTree(this));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,388 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.javascript.node;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@SuppressWarnings("unused")
public class TestVisitors extends BaseTest {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(603);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitTerminal = function(node) {\n");
grammarBuilder.append(" return node.symbol.text;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(a 1 2)\n" +
"[ '1', '2' ]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(843);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitE = function(ctx) {\n");
grammarBuilder.append(" var str;\n");
grammarBuilder.append(" if(ctx.getChildCount()===3) {\n");
grammarBuilder.append(" str = ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text;\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" str = ctx.INT().symbol.text;\n");
grammarBuilder.append(" }\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1,,2,,32 3 21 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(854);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitCall = function(ctx) {\n");
grammarBuilder.append(" var str = ctx.e().start.text + ' ' + ctx.eList();\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" this.visitInt = function(ctx) {\n");
grammarBuilder.append(" var str = ctx.INT().symbol.text;\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1,,2,,3,1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(868);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitA = function(ctx) {\n");
grammarBuilder.append(" var str;\n");
grammarBuilder.append(" if(ctx.getChildCount()===2) {\n");
grammarBuilder.append(" str = ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text;\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" str = ctx.b(0).start.text;\n");
grammarBuilder.append(" }\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
",1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(868);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitA = function(ctx) {\n");
grammarBuilder.append(" var str;\n");
grammarBuilder.append(" if(ctx.getChildCount()===2) {\n");
grammarBuilder.append(" str = ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text;\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" str = ctx.b(0).start.text;\n");
grammarBuilder.append(" }\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(855);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitA = function(ctx) {\n");
grammarBuilder.append(" var str;\n");
grammarBuilder.append(" if(ctx.getChildCount()===2) {\n");
grammarBuilder.append(" str = ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + antlr4.Utils.arrayToString(ctx.INT());\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" str = ctx.ID().symbol.toString();\n");
grammarBuilder.append(" }\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(a 1 2)\n" +
",1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(855);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("var TVisitor = require('./TVisitor').TVisitor;\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("this.LeafVisitor = function() {\n");
grammarBuilder.append(" this.visitA = function(ctx) {\n");
grammarBuilder.append(" var str;\n");
grammarBuilder.append(" if(ctx.getChildCount()===2) {\n");
grammarBuilder.append(" str = ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + antlr4.Utils.arrayToString(ctx.INT());\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" str = ctx.ID().symbol.toString();\n");
grammarBuilder.append(" }\n");
grammarBuilder.append(" return this.visitChildren(ctx) + str;\n");
grammarBuilder.append(" };\n");
grammarBuilder.append(" return this;\n");
grammarBuilder.append("};\n");
grammarBuilder.append("this.LeafVisitor.prototype = Object.create(TVisitor.prototype);\n");
grammarBuilder.append("this.LeafVisitor.prototype.constructor = this.LeafVisitor;\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("console.log($ctx.r.toStringTree(null, this));\n");
grammarBuilder.append("var visitor = new this.LeafVisitor();\n");
grammarBuilder.append("console.log($ctx.r.accept(visitor));\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"TListener", "TVisitor",
"s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,298 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@SuppressWarnings("unused")
public class TestVisitors extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"[ '1', '2' ]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(231);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1,,2,,32 3 21 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(271);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1,,2,,3,1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(258);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
",1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(258);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
",1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,298 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python3;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@SuppressWarnings("unused")
public class TestVisitors extends BasePython3Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"[ '1', '2' ]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(231);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1,,2,,32 3 21 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(271);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1,,2,,3,1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(258);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
",1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(258);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
",1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
@Ignore("true")
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(216);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -96,10 +96,16 @@ ATNConfig.prototype.checkContext = function(params, config) {
}
};
ATNConfig.prototype.hashString = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" +
(this.context===null ? "" : this.context.hashString()) +
"/" + this.semanticContext.hashString();
};
// An ATN configuration is equal to another if both have
// the same state, they predict the same alternative, and
// syntactic/semantic contexts are the same.
///
ATNConfig.prototype.equals = function(other) {
if (this === other) {
return true;
@ -114,16 +120,23 @@ ATNConfig.prototype.equals = function(other) {
}
};
ATNConfig.prototype.shortHashString = function() {
ATNConfig.prototype.hashStringForConfigSet = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" + this.semanticContext;
};
ATNConfig.prototype.hashString = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" +
(this.context===null ? "" : this.context.hashString()) +
"/" + this.semanticContext.hashString();
ATNConfig.prototype.equalsForConfigSet = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof ATNConfig)) {
return false;
} else {
return this.state.stateNumber===other.state.stateNumber &&
this.alt===other.alt &&
this.semanticContext.equals(other.semanticContext);
}
};
ATNConfig.prototype.toString = function() {
return "(" + this.state + "," + this.alt +
(this.context!==null ? ",[" + this.context.toString() + "]" : "") +
@ -171,6 +184,11 @@ LexerATNConfig.prototype.equals = function(other) {
}
};
LexerATNConfig.prototype.hashStringForConfigSet = LexerATNConfig.prototype.hashString;
LexerATNConfig.prototype.equalsForConfigSet = LexerATNConfig.prototype.eqials;
LexerATNConfig.prototype.checkNonGreedyDecision = function(source, target) {
return source.passedThroughNonGreedyDecision ||
(target instanceof DecisionState) && target.nonGreedy;

View File

@ -41,19 +41,17 @@ var SemanticContext = require('./SemanticContext').SemanticContext;
var merge = require('./../PredictionContext').merge;
function hashATNConfig(c) {
return c.shortHashString();
return c.hashStringForConfigSet();
}
function equalATNConfigs(a, b) {
if ( a===b ) {
return true;
}
if ( a===null || b===null ) {
} else if ( a===null || b===null ) {
return false;
}
return a.state.stateNumber===b.state.stateNumber &&
a.alt===b.alt && a.semanticContext.equals(b.semanticContext);
}
} else
return a.equalsForConfigSet(b);
}
function ATNConfigSet(fullCtx) {

View File

@ -95,6 +95,10 @@ ParseTreeVisitor.prototype.visit = function(ctx) {
}
};
ParseTreeVisitor.prototype.visitChildren = function(ctx) {
return this.visit(ctx.children);
}
ParseTreeVisitor.prototype.visitTerminal = function(node) {
};
@ -103,14 +107,7 @@ ParseTreeVisitor.prototype.visitErrorNode = function(node) {
var visitAtom = function(visitor, ctx) {
if (ctx.parser === undefined) { //is terminal
return;
}
var name = ctx.parser.ruleNames[ctx.ruleIndex];
var funcName = "visit" + Utils.titleCase(name);
return visitor[funcName](ctx);
return ctx.accept(visitor);
};
function ParseTreeListener() {
@ -253,4 +250,4 @@ exports.TerminalNodeImpl = TerminalNodeImpl;
exports.ParseTreeListener = ParseTreeListener;
exports.ParseTreeVisitor = ParseTreeVisitor;
exports.ParseTreeWalker = ParseTreeWalker;
exports.INVALID_INTERVAL = INVALID_INTERVAL;
exports.INVALID_INTERVAL = INVALID_INTERVAL;

View File

@ -276,7 +276,7 @@ func (l *<lexer.name>) Action(localctx antlr.RuleContext, ruleIndex, actionIndex
switch ruleIndex {
<if(recog.actionFuncs.values)>
<recog.actionFuncs.values:{f | case <f.ruleIndex>:
<if(f.isRuleContext)>
<if(f.factory.grammar.lexer)>
l.<f.name>_Action(localctx, actionIndex)
<else>
var t *<f.name; format="cap">Context = nil
@ -304,7 +304,7 @@ func (l *<lexer.name>) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex
switch ruleIndex {
<if(recog.sempredFuncs.values)>
<recog.sempredFuncs.values:{f | case <f.ruleIndex>:
<if(f.isRuleContext)>
<if(f.factory.grammar.lexer)>
return l.<f.name>_Sempred(localctx, predIndex)
<else>
var t *<f.name; format="cap">Context = nil
@ -330,7 +330,7 @@ func (l *<lexer.name>) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex
* overriding implementation impossible to maintain.
*/
RuleActionFunction(r, actions) ::= <<
func (l *<lexer.name>) <r.name; format="cap">_Action(localctx <if(r.isRuleContext)>antlr.RuleContext<else>*<r.ctxType><endif>, actionIndex int) {
func (l *<lexer.name>) <r.name; format="cap">_Action(localctx <if(r.factory.grammar.lexer)>antlr.RuleContext<else>*<r.ctxType><endif>, actionIndex int) {
switch actionIndex {
<if(actions)>
<actions:{index | case <index>:
@ -348,7 +348,7 @@ func (l *<lexer.name>) <r.name; format="cap">_Action(localctx <if(r.isRuleContex
* overriding implementation impossible to maintain.
*/
RuleSempredFunction(r, actions) ::= <<
func (p *<r.recognizerName>) <r.name; format="cap">_Sempred(localctx antlr.RuleContext, predIndex int) bool {
func (p *<r.factory.grammar.recognizerName>) <r.name; format="cap">_Sempred(localctx antlr.RuleContext, predIndex int) bool {
switch predIndex {
<if(actions)>
<actions:{index | case <index>:
@ -372,7 +372,7 @@ RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedAction
<endif>
func (p *<currentRule.recognizerName>) <currentRule.name; format="cap">(<currentRule.args:{a | <a.name> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
func (p *<parser.name>) <currentRule.name; format="cap">(<currentRule.args:{a | <a.name> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
localctx = New<currentRule.ctxType>(p, p.GetParserRuleContext(), p.GetState()<currentRule.args:{a | , <a.name>}>)
p.EnterRule(localctx, <currentRule.startState>, <parser.name>RULE_<currentRule.name>)
<if(namedActions.init)>
@ -437,11 +437,11 @@ LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs
<endif>
func (p *<currentRule.recognizerName>) <currentRule.name; format="cap">(<args:{a | <a.name> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
func (p *<parser.name>) <currentRule.name; format="cap">(<args:{a | <a.name> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
return p.<currentRule.name>(0<args:{a | , <a.name>}>)
}
func (p *<currentRule.recognizerName>) <currentRule.name>(_p int<args:{a | , <a.name> <a.type>}>) (localctx I<currentRule.ctxType>) {
func (p *<parser.name>) <currentRule.name>(_p int<args:{a | , <a.name> <a.type>}>) (localctx I<currentRule.ctxType>) {
var _parentctx antlr.ParserRuleContext = p.GetParserRuleContext()
_parentState := p.GetState()
localctx = New<currentRule.ctxType>(p, p.GetParserRuleContext(), _parentState<args:{a | , <a.name>}>)
@ -885,7 +885,7 @@ TokenDecl(t) ::= "<t.name> <TokenLabelType()>"
TokenTypeDecl(t) ::= "<t.name> int"
TokenListDecl(t) ::= "<t.name> []antlr.Token"
RuleContextDecl(r) ::= "<r.name> I<r.ctxName>"
RuleContextDecl(r) ::= "<r.name> I<r.ctxName> "
RuleContextListDecl(rdecl) ::= "<rdecl.name> []I<rdecl.ctxName>"
AttributeDecl(d) ::= "<d.name> <d.type;format={lower}><if(d.initValue)>// TODO = <d.initValue><endif>"
@ -962,7 +962,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = p.GetTokenStream().LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = p.GetTokenStream().LA(1)"
StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers, tokenDecls, tokenTypeDecls, tokenListDecls, ruleContextDecls, ruleContextListDecls, attributeDecls, superClass = {ParserRuleContext}) ::= <<
StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers) ::= <<
// I<struct.name> is an interface to support dynamic dispatch.
type I<struct.name> interface {
antlr.ParserRuleContext
@ -1478,10 +1478,4 @@ initValue(typeName) ::= <<
<javaTypeInitMap.(typeName)>
>>
RecognizerFileName(name, type) ::= "<name; format={lower}>_<type; format={lower}>"
ListenerFileName(name) ::= "<name; format={lower}>_listener"
VisitorFileName(name) ::= "<name; format={lower}>_visitor"
BaseListenerFileName(name) ::= "<name; format={lower}>_base_listener"
BaseVisitorFileName(name) ::= "<name; format={lower}>_base_visitor"
codeFileExtension() ::= ".go"

View File

@ -91,7 +91,6 @@ exports.<file.grammarName>Listener = <file.grammarName>Listener;
VisitorFile(file, header) ::= <<
<fileHeader(file.grammarFileName, file.ANTLRVersion)>
var antlr4 = require('antlr4/index');
<header>
// This class defines a complete generic visitor for a parse tree produced by <file.parserName>.
@ -106,6 +105,7 @@ function <file.grammarName>Visitor() {
<file.visitorNames:{lname |
// Visit a parse tree produced by <file.parserName>#<lname>.
<file.grammarName>Visitor.prototype.visit<lname; format="cap"> = function(ctx) {
return this.visitChildren(ctx);
\};
}; separator="\n">

View File

@ -2,6 +2,7 @@
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* Copyright (c) 2016 Mike Lischke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,7 +28,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.codegen;
import org.antlr.v4.parse.ANTLRParser;
@ -59,8 +59,8 @@ public class CodeGenPipeline {
for (GrammarAST idNode : idNodes) {
if ( gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode) ) {
g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD,
g.fileName, idNode.getToken(),
idNode.getText());
g.fileName, idNode.getToken(),
idNode.getText());
}
}
@ -70,46 +70,84 @@ public class CodeGenPipeline {
int errorCount = g.tool.errMgr.getNumErrors();
if ( g.isLexer() ) {
ST lexer = gen.generateLexer();
if (gen.getTarget().needsHeader()) {
ST lexer = gen.generateLexer(true); // Header file if needed.
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(lexer, gen, true);
}
}
ST lexer = gen.generateLexer(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(lexer, gen);
writeRecognizer(lexer, gen, false);
}
}
else {
ST parser = gen.generateParser();
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(parser, gen);
}
if ( g.tool.gen_listener ) {
ST listener = gen.generateListener();
if (gen.getTarget().needsHeader()) {
ST parser = gen.generateParser(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeListener(listener);
writeRecognizer(parser, gen, true);
}
}
ST parser = gen.generateParser(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(parser, gen, false);
}
if ( g.tool.gen_listener ) {
if (gen.getTarget().needsHeader()) {
ST listener = gen.generateListener(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeListener(listener, true);
}
}
ST listener = gen.generateListener(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeListener(listener, false);
}
if (gen.getTarget().needsHeader()) {
ST baseListener = gen.generateBaseListener(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseListener(baseListener, true);
}
}
if (gen.getTarget().wantsBaseListener()) {
ST baseListener = gen.generateBaseListener();
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseListener(baseListener);
ST baseListener = gen.generateBaseListener(false);
if ( g.tool.errMgr.getNumErrors()==errorCount ) {
gen.writeBaseListener(baseListener, false);
}
}
}
if ( g.tool.gen_visitor ) {
ST visitor = gen.generateVisitor();
if (gen.getTarget().needsHeader()) {
ST visitor = gen.generateVisitor(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeVisitor(visitor, true);
}
}
ST visitor = gen.generateVisitor(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeVisitor(visitor);
gen.writeVisitor(visitor, false);
}
if (gen.getTarget().needsHeader()) {
ST baseVisitor = gen.generateBaseVisitor(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseVisitor(baseVisitor, true);
}
}
if (gen.getTarget().wantsBaseVisitor()) {
ST baseVisitor = gen.generateBaseVisitor();
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseVisitor(baseVisitor);
ST baseVisitor = gen.generateBaseVisitor(false);
if ( g.tool.errMgr.getNumErrors()==errorCount ) {
gen.writeBaseVisitor(baseVisitor, false);
}
}
}
gen.writeHeaderFile();
}
gen.writeVocabFile();
}
protected void writeRecognizer(ST template, CodeGenerator gen) {
protected void writeRecognizer(ST template, CodeGenerator gen, boolean header) {
if ( g.tool.launch_ST_inspector ) {
STViz viz = template.inspect();
if (g.tool.ST_inspector_wait_for_close) {
@ -122,6 +160,6 @@ public class CodeGenPipeline {
}
}
gen.writeRecognizer(template);
gen.writeRecognizer(template, header);
}
}

View File

@ -2,6 +2,7 @@
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* Copyright (c) 2016 Mike Lischke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,7 +33,6 @@ package org.antlr.v4.codegen;
import org.antlr.v4.Tool;
import org.antlr.v4.codegen.model.OutputModelObject;
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar;
@ -135,17 +135,28 @@ public class CodeGenerator {
return controller;
}
private ST walk(OutputModelObject outputModel) {
private ST walk(OutputModelObject outputModel, boolean header) {
OutputModelWalker walker = new OutputModelWalker(tool, getTemplates());
return walker.walk(outputModel);
return walker.walk(outputModel, header);
}
public ST generateLexer() { return walk(createController().buildLexerOutputModel()); }
public ST generateParser() { return walk(createController().buildParserOutputModel()); }
public ST generateListener() { return walk(createController().buildListenerOutputModel()); }
public ST generateBaseListener() { return walk(createController().buildBaseListenerOutputModel()); }
public ST generateVisitor() { return walk(createController().buildVisitorOutputModel()); }
public ST generateBaseVisitor() { return walk(createController().buildBaseVisitorOutputModel()); }
public ST generateLexer() { return generateLexer(false); }
public ST generateLexer(boolean header) { return walk(createController().buildLexerOutputModel(header), header); }
public ST generateParser() { return generateParser(false); }
public ST generateParser(boolean header) { return walk(createController().buildParserOutputModel(header), header); }
public ST generateListener() { return generateListener(false); }
public ST generateListener(boolean header) { return walk(createController().buildListenerOutputModel(header), header); }
public ST generateBaseListener() { return generateBaseListener(false); }
public ST generateBaseListener(boolean header) { return walk(createController().buildBaseListenerOutputModel(header), header); }
public ST generateVisitor() { return generateVisitor(false); }
public ST generateVisitor(boolean header) { return walk(createController().buildVisitorOutputModel(header), header); }
public ST generateBaseVisitor() { return generateBaseVisitor(false); }
public ST generateBaseVisitor(boolean header) { return walk(createController().buildBaseVisitorOutputModel(header), header); }
/** Generate a token vocab file with all the token names/types. For example:
* ID=7
@ -179,35 +190,24 @@ public class CodeGenerator {
return vocabFileST;
}
public void writeRecognizer(ST outputFileST) {
getTarget().genFile(g, outputFileST, getRecognizerFileName());
public void writeRecognizer(ST outputFileST, boolean header) {
getTarget().genFile(g, outputFileST, getRecognizerFileName(header));
}
public void writeListener(ST outputFileST) {
getTarget().genFile(g, outputFileST, getListenerFileName());
public void writeListener(ST outputFileST, boolean header) {
getTarget().genFile(g, outputFileST, getListenerFileName(header));
}
public void writeBaseListener(ST outputFileST) {
getTarget().genFile(g, outputFileST, getBaseListenerFileName());
public void writeBaseListener(ST outputFileST, boolean header) {
getTarget().genFile(g, outputFileST, getBaseListenerFileName(header));
}
public void writeVisitor(ST outputFileST) {
getTarget().genFile(g, outputFileST, getVisitorFileName());
public void writeVisitor(ST outputFileST, boolean header) {
getTarget().genFile(g, outputFileST, getVisitorFileName(header));
}
public void writeBaseVisitor(ST outputFileST) {
getTarget().genFile(g, outputFileST, getBaseVisitorFileName());
}
public void writeHeaderFile() {
String fileName = getHeaderFileName();
if ( fileName==null ) return;
if ( getTemplates().isDefined("headerFile") ) {
ST extST = getTemplates().getInstanceOf("headerFileExtension");
ST headerFileST = null;
// TODO: don't hide this header file generation here!
getTarget().genRecognizerHeaderFile(g, headerFileST, extST.render(lineWidth));
}
public void writeBaseVisitor(ST outputFileST, boolean header) {
getTarget().genFile(g, outputFileST, getBaseVisitorFileName(header));
}
public void writeVocabFile() {
@ -237,94 +237,17 @@ public class CodeGenerator {
}
}
/** Generate TParser.java and TLexer.java from T.g4 if combined, else
* just use T.java as output regardless of type.
*/
public String getRecognizerFileName() {
ST extST = getTemplates().getInstanceOf("codeFileExtension");
ST fullST = getTemplates().getInstanceOf("RecognizerFileName");
if (fullST != null) {
switch (g.getType()) {
case ANTLRParser.PARSER:
fullST.add("name", g.name.endsWith("Parser") ? g.name.substring(0, g.name.length() - 6) : g.name); // trim off "parser"
fullST.add("type", "parser");
break;
case ANTLRParser.LEXER:
fullST.add("name", g.name.endsWith("Lexer") ?g.name.substring(0, g.name.length() - 5) : g.name); // trim off "lexer"
fullST.add("type", "lexer");
break;
case ANTLRParser.COMBINED:
fullST.add("name", g.name);
fullST.add("type", "parser");
break;
}
return fullST.render() + extST.render();
}
String recognizerName = g.getRecognizerName();
return recognizerName+extST.render();
}
public String getRecognizerFileName() { return getRecognizerFileName(false); }
public String getListenerFileName() { return getListenerFileName(false); }
public String getVisitorFileName() { return getVisitorFileName(false); }
public String getBaseListenerFileName() { return getBaseListenerFileName(false); }
public String getBaseVisitorFileName() { return getBaseVisitorFileName(false); }
/** A given grammar T, return the listener name such as
* TListener.java, if we're using the Java target.
*/
public String getListenerFileName() {
assert g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
ST fullST = getTemplates().getInstanceOf("ListenerFileName");
if (fullST != null) {
fullST.add("name", g.name);
return fullST.render() + extST.render();
}
String listenerName = g.name + "Listener";
return listenerName+extST.render();
}
/** A given grammar T, return the visitor name such as
* TVisitor.java, if we're using the Java target.
*/
public String getVisitorFileName() {
assert g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
ST fullST = getTemplates().getInstanceOf("VisitorFileName");
if (fullST != null) {
fullST.add("name", g.name);
return fullST.render() + extST.render();
}
String listenerName = g.name + "Visitor";
return listenerName+extST.render();
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseListenerFileName() {
assert g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
ST fullST = getTemplates().getInstanceOf("BaseListenerFileName");
if (fullST != null) {
fullST.add("name", g.name);
return fullST.render() + extST.render();
}
String listenerName = g.name + "BaseListener";
return listenerName+extST.render();
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseVisitorFileName() {
assert g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
ST fullST = getTemplates().getInstanceOf("BaseVisitorFileName");
if (fullST != null) {
fullST.add("name", g.name);
return fullST.render() + extST.render();
}
String listenerName = g.name + "BaseVisitor";
return listenerName+extST.render();
}
public String getRecognizerFileName(boolean header) { return getTarget().getRecognizerFileName(header); }
public String getListenerFileName(boolean header) { return getTarget().getListenerFileName(header); }
public String getVisitorFileName(boolean header) { return getTarget().getVisitorFileName(header); }
public String getBaseListenerFileName(boolean header) { return getTarget().getBaseListenerFileName(header); }
public String getBaseVisitorFileName(boolean header) { return getTarget().getBaseVisitorFileName(header); }
/** What is the name of the vocab file generated for this grammar?
* Returns null if no .tokens file should be generated.

View File

@ -2,6 +2,7 @@
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* Copyright (c) 2016 Mike Lischke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -107,24 +108,23 @@ public class OutputModelController {
* controller as factory in SourceGenTriggers so it triggers codegen
* extensions too, not just the factory functions in this factory.
*/
public OutputModelObject buildParserOutputModel() {
Grammar g = delegate.getGrammar();
public OutputModelObject buildParserOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
ParserFile file = parserFile(gen.getRecognizerFileName());
ParserFile file = parserFile(gen.getRecognizerFileName(header));
setRoot(file);
Parser parser = parser(file);
file.parser = parser;
file.parser = parser(file);
Grammar g = delegate.getGrammar();
for (Rule r : g.rules.values()) {
buildRuleFunction(parser, r);
buildRuleFunction(file.parser, r);
}
return file;
}
public OutputModelObject buildLexerOutputModel() {
public OutputModelObject buildLexerOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
LexerFile file = lexerFile(gen.getRecognizerFileName());
LexerFile file = lexerFile(gen.getRecognizerFileName(header));
setRoot(file);
file.lexer = lexer(file);
@ -136,24 +136,24 @@ public class OutputModelController {
return file;
}
public OutputModelObject buildListenerOutputModel() {
public OutputModelObject buildListenerOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
return new ListenerFile(delegate, gen.getListenerFileName());
return new ListenerFile(delegate, gen.getListenerFileName(header));
}
public OutputModelObject buildBaseListenerOutputModel() {
public OutputModelObject buildBaseListenerOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
return new BaseListenerFile(delegate, gen.getBaseListenerFileName());
return new BaseListenerFile(delegate, gen.getBaseListenerFileName(header));
}
public OutputModelObject buildVisitorOutputModel() {
public OutputModelObject buildVisitorOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
return new VisitorFile(delegate, gen.getVisitorFileName());
return new VisitorFile(delegate, gen.getVisitorFileName(header));
}
public OutputModelObject buildBaseVisitorOutputModel() {
public OutputModelObject buildBaseVisitorOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
return new BaseVisitorFile(delegate, gen.getBaseVisitorFileName());
return new BaseVisitorFile(delegate, gen.getBaseVisitorFileName(header));
}
public ParserFile parserFile(String fileName) {

View File

@ -2,6 +2,7 @@
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* Copyright (c) 2016 Mike Lischke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -75,7 +76,7 @@ public class OutputModelWalker {
this.templates = templates;
}
public ST walk(OutputModelObject omo) {
public ST walk(OutputModelObject omo, boolean header) {
// CREATE TEMPLATE FOR THIS OUTPUT OBJECT
Class<? extends OutputModelObject> cl = omo.getClass();
String templateName = cl.getSimpleName();
@ -83,6 +84,9 @@ public class OutputModelWalker {
tool.errMgr.toolError(ErrorType.NO_MODEL_TO_TEMPLATE_MAPPING, cl.getSimpleName());
return new ST("["+templateName+" invalid]");
}
if (header) templateName += "Header";
ST st = templates.getInstanceOf(templateName);
if ( st == null ) {
tool.errMgr.toolError(ErrorType.CODE_GEN_TEMPLATES_INCOMPLETE, templateName);
@ -124,7 +128,7 @@ public class OutputModelWalker {
Object o = fi.get(omo);
if ( o instanceof OutputModelObject ) { // SINGLE MODEL OBJECT?
OutputModelObject nestedOmo = (OutputModelObject)o;
ST nestedST = walk(nestedOmo);
ST nestedST = walk(nestedOmo, header);
// System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
st.add(fieldName, nestedST);
}
@ -136,7 +140,7 @@ public class OutputModelWalker {
Collection<?> nestedOmos = (Collection<?>)o;
for (Object nestedOmo : nestedOmos) {
if ( nestedOmo==null ) continue;
ST nestedST = walk((OutputModelObject)nestedOmo);
ST nestedST = walk((OutputModelObject)nestedOmo, header);
// System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
st.add(fieldName, nestedST);
}
@ -145,7 +149,7 @@ public class OutputModelWalker {
Map<?, ?> nestedOmoMap = (Map<?, ?>)o;
Map<Object, ST> m = new LinkedHashMap<Object, ST>();
for (Map.Entry<?, ?> entry : nestedOmoMap.entrySet()) {
ST nestedST = walk((OutputModelObject)entry.getValue());
ST nestedST = walk((OutputModelObject)entry.getValue(), header);
// System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
m.put(entry.getKey(), nestedST);
}

View File

@ -2,6 +2,7 @@
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* Copyright (c) 2016 Mike Lischke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -121,20 +122,6 @@ public abstract class Target {
getCodeGenerator().write(outputFileST, fileName);
}
protected void genListenerFile(Grammar g,
ST outputFileST)
{
String fileName = getCodeGenerator().getListenerFileName();
getCodeGenerator().write(outputFileST, fileName);
}
protected void genRecognizerHeaderFile(Grammar g,
ST headerFileST,
String extName) // e.g., ".h"
{
// no header file by default
}
/** Get a meaningful name for a token type useful during code generation.
* Literals without associated names are converted to the string equivalent
* of their integer values. Used to generate x==ID and x==34 type comparisons
@ -392,6 +379,55 @@ public abstract class Target {
return getTokenTypeAsTargetLabel(getCodeGenerator().g, ttype);
}
/** Generate TParser.java and TLexer.java from T.g4 if combined, else
* just use T.java as output regardless of type.
*/
public String getRecognizerFileName(boolean header) {
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String recognizerName = gen.g.getRecognizerName();
return recognizerName+extST.render();
}
/** A given grammar T, return the listener name such as
* TListener.java, if we're using the Java target.
*/
public String getListenerFileName(boolean header) {
assert gen.g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = gen.g.name + "Listener";
return listenerName+extST.render();
}
/** A given grammar T, return the visitor name such as
* TVisitor.java, if we're using the Java target.
*/
public String getVisitorFileName(boolean header) {
assert gen.g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = gen.g.name + "Visitor";
return listenerName+extST.render();
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseListenerFileName(boolean header) {
assert gen.g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = gen.g.name + "BaseListener";
return listenerName+extST.render();
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseVisitorFileName(boolean header) {
assert gen.g.name != null;
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String listenerName = gen.g.name + "BaseVisitor";
return listenerName+extST.render();
}
/**
* Gets the maximum number of 16-bit unsigned integers that can be encoded
* in a single segment of the serialized ATN.
@ -524,4 +560,7 @@ public abstract class Target {
public boolean supportsOverloadedMethods() {
return true;
}
/** @since 4.6 */
public boolean needsHeader() { return false; }; // Override in targets that need header files.
}

View File

@ -42,8 +42,6 @@ import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.GrammarAST;
public class LeftRecursiveRuleFunction extends RuleFunction {
public String recognizerName;
public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r) {
super(factory, r);
@ -74,7 +72,5 @@ public class LeftRecursiveRuleFunction extends RuleFunction {
struct.addDecl(d); // stick in overall rule's ctx
}
}
recognizerName = factory.getGrammar().getRecognizerName();
}
}

View File

@ -38,7 +38,6 @@ import java.util.LinkedHashMap;
public class RuleActionFunction extends OutputModelObject {
public String name;
public String ctxType;
public boolean isRuleContext;
public int ruleIndex;
/** Map actionIndex to Action */
@ -49,7 +48,6 @@ public class RuleActionFunction extends OutputModelObject {
super(factory);
name = r.name;
ruleIndex = r.index;
isRuleContext = ctxType.equals("RuleContext");
this.ctxType = ctxType;
}
}

View File

@ -81,7 +81,6 @@ public class RuleFunction extends OutputModelObject {
public Rule rule;
public AltLabelStructDecl[] altToContext;
public boolean hasLookaheadBlock;
public String recognizerName;
@ModelElement public List<SrcOp> code;
@ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx?
@ -139,7 +138,6 @@ public class RuleFunction extends OutputModelObject {
}
startState = factory.getGrammar().atn.ruleToStartState[r.index];
recognizerName = factory.getGrammar().getRecognizerName();
}
public void addContextGetters(OutputModelFactory factory, Rule r) {

View File

@ -34,10 +34,7 @@ import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.Rule;
public class RuleSempredFunction extends RuleActionFunction {
public String recognizerName;
public RuleSempredFunction(OutputModelFactory factory, Rule r, String ctxType) {
super(factory, r, ctxType);
recognizerName = factory.getGrammar().getRecognizerName();
}
}

View File

@ -57,12 +57,15 @@ public class StructDecl extends Decl {
@ModelElement public List<OutputModelObject> interfaces;
@ModelElement public List<OutputModelObject> extensionMembers;
@ModelElement public OrderedHashSet<Decl> tokenDecls = new OrderedHashSet<Decl>();
@ModelElement public OrderedHashSet<Decl> tokenTypeDecls = new OrderedHashSet<Decl>();
@ModelElement public OrderedHashSet<Decl> tokenListDecls = new OrderedHashSet<Decl>();
@ModelElement public OrderedHashSet<Decl> ruleContextDecls = new OrderedHashSet<Decl>();
@ModelElement public OrderedHashSet<Decl> ruleContextListDecls = new OrderedHashSet<Decl>();
@ModelElement public OrderedHashSet<Decl> attributeDecls = new OrderedHashSet<Decl>();
// Track these separately; Go target needs to generate getters/setters
// Do not make them templates; we only need the Decl object not the ST
// built from it. Avoids adding args to StructDecl template
public OrderedHashSet<Decl> tokenDecls = new OrderedHashSet<Decl>();
public OrderedHashSet<Decl> tokenTypeDecls = new OrderedHashSet<Decl>();
public OrderedHashSet<Decl> tokenListDecls = new OrderedHashSet<Decl>();
public OrderedHashSet<Decl> ruleContextDecls = new OrderedHashSet<Decl>();
public OrderedHashSet<Decl> ruleContextListDecls = new OrderedHashSet<Decl>();
public OrderedHashSet<Decl> attributeDecls = new OrderedHashSet<Decl>();
public StructDecl(OutputModelFactory factory, Rule r) {
super(factory, factory.getGenerator().getTarget().getRuleFunctionContextStructName(r));
@ -91,17 +94,23 @@ public class StructDecl extends Decl {
if ( d instanceof ContextGetterDecl ) getters.add(d);
else attrs.add(d);
if (d instanceof TokenTypeDecl ) {
// add to specific "lists"
if ( d instanceof TokenTypeDecl ) {
tokenTypeDecls.add(d);
} else if (d instanceof TokenListDecl ) {
}
else if ( d instanceof TokenListDecl ) {
tokenListDecls.add(d);
} else if (d instanceof TokenDecl){
}
else if ( d instanceof TokenDecl ) {
tokenDecls.add(d);
} else if (d instanceof RuleContextListDecl ) {
}
else if ( d instanceof RuleContextListDecl ) {
ruleContextListDecls.add(d);
} else if (d instanceof RuleContextDecl ) {
}
else if ( d instanceof RuleContextDecl ) {
ruleContextDecls.add(d);
} else if (d instanceof AttributeDecl ){
}
else if ( d instanceof AttributeDecl ) {
attributeDecls.add(d);
}
}

View File

@ -2,6 +2,7 @@ package org.antlr.v4.codegen.target;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.ast.GrammarAST;
import org.stringtemplate.v4.ST;
@ -84,6 +85,7 @@ public class GoTarget extends Target {
// because "gofmt -w" uses ioutil.WriteFile internally, which means it literally writes in-place with O_TRUNC.
// That could result in a race. (Why oh why doesn't it do tmpfile + rename?)
try {
// TODO: need something like: String goExecutable = locateGo();
ProcessBuilder gofmtBuilder = new ProcessBuilder("gofmt", "-w", "-s", fileName.getPath());
gofmtBuilder.redirectErrorStream(true);
Process gofmt = gofmtBuilder.start();
@ -133,6 +135,65 @@ public class GoTarget extends Target {
return result;
}
public String getRecognizerFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
assert g!=null;
String name;
switch ( g.getType()) {
case ANTLRParser.PARSER:
name = g.name.endsWith("Parser") ? g.name.substring(0, g.name.length()-6) : g.name;
return name.toLowerCase()+"_parser.go";
case ANTLRParser.LEXER:
name = g.name.endsWith("Lexer") ? g.name.substring(0, g.name.length()-5) : g.name; // trim off "lexer"
return name.toLowerCase()+"_lexer.go";
case ANTLRParser.COMBINED:
return g.name.toLowerCase()+"_parser.go";
default :
return "INVALID_FILE_NAME";
}
}
/** A given grammar T, return the listener name such as
* TListener.java, if we're using the Java target.
*/
public String getListenerFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
assert g.name != null;
return g.name.toLowerCase()+"_listener.go";
}
/** A given grammar T, return the visitor name such as
* TVisitor.java, if we're using the Java target.
*/
public String getVisitorFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
assert g.name != null;
return g.name.toLowerCase()+"_visitor.go";
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseListenerFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
assert g.name != null;
return g.name.toLowerCase()+"_base_listener.go";
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/
public String getBaseVisitorFileName(boolean header) {
CodeGenerator gen = getCodeGenerator();
Grammar g = gen.g;
assert g.name != null;
return g.name.toLowerCase()+"_base_visitor.go";
}
protected static class JavaStringRenderer extends StringRenderer {
@Override