diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..53b65e9f3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +root = true + +[*.{java,stg}] +charset = utf-8 +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..5edc9289f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# This rule applies to all files which don't match another line below +* text=auto diff --git a/contributors.txt b/contributors.txt index 8ae2b2a08..1f4fd15ac 100644 --- a/contributors.txt +++ b/contributors.txt @@ -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 diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/csharp/CSharp.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/csharp/CSharp.test.stg index 1a8642a35..a63b980c5 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/csharp/CSharp.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/csharp/CSharp.test.stg @@ -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 ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/java/Java.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/java/Java.test.stg index c63e306c8..aead6e363 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/java/Java.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/java/Java.test.stg @@ -428,6 +428,13 @@ Declare_pred() ::= <)>> 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 ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/javascript/node/Node.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/javascript/node/Node.test.stg index 8a11ff670..a616be111 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/javascript/node/Node.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/javascript/node/Node.test.stg @@ -227,6 +227,8 @@ TokenStartColumnEquals(i) ::= <%this._tokenStartColumn===%> ImportListener(X) ::= <Listener = require('./Listener').Listener;>> +ImportVisitor(X) ::= <Visitor = require('./Visitor').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(), ); >> +BasicVisitor(X) ::= << +this.LeafVisitor = function() { + this.visitTerminal = function(node) { + return node.symbol.text; + }; + return this; +}; +this.LeafVisitor.prototype = Object.create(Visitor.prototype); +this.LeafVisitor.prototype.constructor = this.LeafVisitor; + +>> + +WalkVisitor(s) ::= << +var visitor = new this.LeafVisitor(); +console.log(.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(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(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(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(Visitor.prototype); +this.LeafVisitor.prototype.constructor = this.LeafVisitor; + +>> + DeclareContextListGettersFunction() ::= << function foo() { var s = new SContext(); diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/python2/Python2.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/python2/Python2.test.stg index b9664b1fe..d5fd8c08a 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/python2/Python2.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/python2/Python2.test.stg @@ -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 ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/python3/Python3.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/python3/Python3.test.stg index 5cd74b50c..e3b8279a6 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/python3/Python3.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/python3/Python3.test.stg @@ -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 ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Index.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Index.stg index 549026694..711d31ef4 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Index.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Index.stg @@ -12,5 +12,6 @@ TestFolders ::= [ "Performance": [], "SemPredEvalLexer": [], "SemPredEvalParser": [], - "Sets": [] + "Sets": [], + "Visitors": [] ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Basic.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Basic.stg new file mode 100644 index 000000000..2f8daec40 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Basic.stg @@ -0,0 +1,46 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "1 2" + +Rule() ::= "s" + +Output() ::= << +(a 1 2) +[ '1', '2' ]<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@parser::header { + +} + +@parser::members { + +} + +s +@after { + + +} + : r=a ; +a : INT INT + | ID + ; +MULT: '*' ; +ADD : '+' ; +INT : [0-9]+ ; +ID : [a-z]+ ; +WS : [ \t\n]+ -> skip ; +>> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Index.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Index.stg new file mode 100644 index 000000000..6d405b3ea --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/Index.stg @@ -0,0 +1,9 @@ +TestTemplates ::= [ + "Basic": [], + "TokenGetters_1": [], + "TokenGetters_2": [], + "RuleGetters_1": [], + "RuleGetters_2": [], + "LR": [], + "LRWithLabels": [] +] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LR.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LR.stg new file mode 100644 index 000000000..46daf55c7 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LR.stg @@ -0,0 +1,47 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +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 ; +@parser::header { + +} + +@parser::members { + +} + +s +@after { + + +} + : r=e ; +e : e op='*' e + | e op='+' e + | INT + ; +MULT: '*' ; +ADD : '+' ; +INT : [0-9]+ ; +ID : [a-z]+ ; +WS : [ \t\n]+ -> skip ; +>> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LRWithLabels.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LRWithLabels.stg new file mode 100644 index 000000000..5d38fe670 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/LRWithLabels.stg @@ -0,0 +1,47 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +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 ; +@parser::header { + +} + +@parser::members { + +} + +s +@after { + + +} + : r=e ; +e : e '(' eList ')' # Call + | INT # Int + ; +eList : e (',' e)* ; +MULT: '*' ; +ADD : '+' ; +INT : [0-9]+ ; +ID : [a-z]+ ; +WS : [ \t\n]+ -> skip ; +>> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters.stg new file mode 100644 index 000000000..61056bb20 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters.stg @@ -0,0 +1,38 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "s" + +grammar(grammarName) ::= << +grammar ; +@parser::header { + +} + +@parser::members { + +} + +s +@after { + + +} + : 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 ; +>> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_1.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_1.stg new file mode 100644 index 000000000..0446a8ece --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_1.stg @@ -0,0 +1,10 @@ +import "RuleGetters.stg" + +Input() ::= "1 2" + +Output() ::= << +(a (b 1) (b 2)) +,1 2 1<\n> +>> + +Errors() ::= "" diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_2.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_2.stg new file mode 100644 index 000000000..c1ce57406 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/RuleGetters_2.stg @@ -0,0 +1,10 @@ +import "RuleGetters.stg" + +Input() ::= "abc" + +Output() ::= << +(a (b abc)) +abc<\n> +>> + +Errors() ::= "" diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters.stg new file mode 100644 index 000000000..d28f0521c --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters.stg @@ -0,0 +1,37 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "s" + +grammar(grammarName) ::= << +grammar ; +@parser::header { + +} + +@parser::members { + +} + +s +@after { + + +} + : r=a ; +a : INT INT + | ID + ; +MULT: '*' ; +ADD : '+' ; +INT : [0-9]+ ; +ID : [a-z]+ ; +WS : [ \t\n]+ -> skip ; +>> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_1.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_1.stg new file mode 100644 index 000000000..a5c8c5839 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_1.stg @@ -0,0 +1,10 @@ +import "TokenGetters.stg" + +Input() ::= "1 2" + +Output() ::= << +(a 1 2) +,1 2 [1, 2]<\n> +>> + +Errors() ::= "" diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_2.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_2.stg new file mode 100644 index 000000000..e01d01e5f --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Visitors/TokenGetters_2.stg @@ -0,0 +1,10 @@ +import "TokenGetters.stg" + +Input() ::= "abc" + +Output() ::= << +(a abc) +[@0,0:2='abc',\<4>,1:0]<\n> +>> + +Errors() ::= "" diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/TestVisitors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/TestVisitors.java new file mode 100644 index 000000000..7431d748a --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/TestVisitors.java @@ -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); + + } + +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java index f31f9f54f..bea5701e7 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java @@ -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 paths = new ArrayList(); // 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) { diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestVisitors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestVisitors.java new file mode 100644 index 000000000..0db79a147 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestVisitors.java @@ -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); + + } + + +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/TestVisitors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/TestVisitors.java new file mode 100644 index 000000000..44f7411e4 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/TestVisitors.java @@ -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); + + } + +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/TestVisitors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/TestVisitors.java new file mode 100644 index 000000000..79c001ec5 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/TestVisitors.java @@ -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); + + } + + +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/TestVisitors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/TestVisitors.java new file mode 100644 index 000000000..d8cb927c9 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/TestVisitors.java @@ -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); + + } + + +} diff --git a/runtime/JavaScript/src/antlr4/atn/ATNConfig.js b/runtime/JavaScript/src/antlr4/atn/ATNConfig.js index 988729dc7..dd265fc2d 100644 --- a/runtime/JavaScript/src/antlr4/atn/ATNConfig.js +++ b/runtime/JavaScript/src/antlr4/atn/ATNConfig.js @@ -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; diff --git a/runtime/JavaScript/src/antlr4/atn/ATNConfigSet.js b/runtime/JavaScript/src/antlr4/atn/ATNConfigSet.js index 723eeceb6..295ab2b2c 100644 --- a/runtime/JavaScript/src/antlr4/atn/ATNConfigSet.js +++ b/runtime/JavaScript/src/antlr4/atn/ATNConfigSet.js @@ -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) { diff --git a/runtime/JavaScript/src/antlr4/tree/Tree.js b/runtime/JavaScript/src/antlr4/tree/Tree.js index 03581c740..55192d76b 100644 --- a/runtime/JavaScript/src/antlr4/tree/Tree.js +++ b/runtime/JavaScript/src/antlr4/tree/Tree.js @@ -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; \ No newline at end of file +exports.INVALID_INTERVAL = INVALID_INTERVAL; diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg index 9d01d0458..d7ffdf334 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg @@ -276,7 +276,7 @@ func (l *) Action(localctx antlr.RuleContext, ruleIndex, actionIndex switch ruleIndex { : - + l._Action(localctx, actionIndex) var t *Context = nil @@ -304,7 +304,7 @@ func (l *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex switch ruleIndex { : - + return l._Sempred(localctx, predIndex) var t *Context = nil @@ -330,7 +330,7 @@ func (l *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex * overriding implementation impossible to maintain. */ RuleActionFunction(r, actions) ::= << -func (l *) _Action(localctx antlr.RuleContext*, actionIndex int) { +func (l *) _Action(localctx antlr.RuleContext*, actionIndex int) { switch actionIndex { : @@ -348,7 +348,7 @@ func (l *) _Action(localctx ) _Sempred(localctx antlr.RuleContext, predIndex int) bool { +func (p *) _Sempred(localctx antlr.RuleContext, predIndex int) bool { switch predIndex { : @@ -372,7 +372,7 @@ RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedAction -func (p *) ( }; separator=", ">) (localctx I) { +func (p *) ( }; separator=", ">) (localctx I) { localctx = New(p, p.GetParserRuleContext(), p.GetState()}>) p.EnterRule(localctx, , RULE_) @@ -437,11 +437,11 @@ LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs -func (p *) ( }; separator=", ">) (localctx I) { +func (p *) ( }; separator=", ">) (localctx I) { return p.(0}>) } -func (p *) (_p int }>) (localctx I) { +func (p *) (_p int }>) (localctx I) { var _parentctx antlr.ParserRuleContext = p.GetParserRuleContext() _parentState := p.GetState() localctx = New(p, p.GetParserRuleContext(), _parentState}>) @@ -885,7 +885,7 @@ TokenDecl(t) ::= " " TokenTypeDecl(t) ::= " int" TokenListDecl(t) ::= " []antlr.Token" -RuleContextDecl(r) ::= " I" +RuleContextDecl(r) ::= " I " RuleContextListDecl(rdecl) ::= " []I" AttributeDecl(d) ::= " // TODO = " @@ -962,7 +962,7 @@ ListLabelName(label) ::= "