validated Listeners tests

This commit is contained in:
ericvergnaud 2014-10-25 02:39:39 +08:00
parent 740c30d21d
commit 5ee6990f8c
8 changed files with 99 additions and 392 deletions

View File

@ -10,7 +10,7 @@ grammar <grammarName>;
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
<WalkListener("$ctx.r")>
}
: r=a ;
a : INT INT

View File

@ -10,7 +10,7 @@ grammar <grammarName>;
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
<WalkListener("$ctx.r")>
}
: r=e ;
e : e op='*' e

View File

@ -10,7 +10,7 @@ grammar <grammarName>;
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
<WalkListener("$ctx.r")>
}
: r=e ;
e : e '(' eList ')' # Call

View File

@ -10,7 +10,7 @@ grammar <grammarName>;
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
<WalkListener("$ctx.r")>
}
: r=a ;
a : b b // forces list

View File

@ -10,7 +10,7 @@ grammar <grammarName>;
s
@after {
<ToStringTree("$ctx.r"):writeln()>
<walkListener("$ctx.r")>
<WalkListener("$ctx.r")>
}
: r=a ;
a : INT INT

View File

@ -155,7 +155,7 @@ LANotEquals(i, v) ::= <%this._input.LA(<i>)!=<v>%>
TokenStartColumnEquals(i) ::= <%this._tokenStartCharPositionInLine==<i>%>
ImportListener(X) ::= <<var <X>Listener = require('./<X>Listener').<X>Listener;>>
ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)"
@ -248,93 +248,64 @@ protected static class PositionAdjustingLexerATNSimulator extends LexerATNSimula
>>
BasicListener(X) ::= <<
this.LeafListener = function() {
this.visitTerminal = function(node) {
document.getElementById('output').value += node.symbol.text + '\\n';
};
return this;
};
this.LeafListener.prototype = Object.create(<X>Listener.prototype);
this.LeafListener.prototype.constructor = this.LeafListener;
public static class LeafListener extends TBaseListener {
public void visitTerminal(TerminalNode node) {
System.out.println(node.getSymbol().getText());
}
}
>>
walkListener(s) ::= <<
var walker = new antlr4.tree.ParseTreeWalker();
walker.walk(new this.LeafListener(), <s>);
WalkListener(s) ::= <<
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new LeafListener(), <s>);
>>
TokenGetterListener(X) ::= <<
this.LeafListener = function() {
this.exitA = 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();
}
document.getElementById('output').value += str + '\\n';
};
return this;
};
this.LeafListener.prototype = Object.create(<X>Listener.prototype);\n" +
this.LeafListener.prototype.constructor = this.LeafListener;\n" +
public static class LeafListener extends TBaseListener {
public void exitA(TParser.AContext ctx) {
if (ctx.getChildCount()==2)
System.out.printf("%s %s %s",ctx.INT(0).getSymbol().getText(),
ctx.INT(1).getSymbol().getText(),ctx.INT());
else
System.out.println(ctx.ID().getSymbol());
}
}
>>
RuleGetterListener(X) ::= <<
this.LeafListener = function() {
this.exitA = 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;
}
document.getElementById('output').value += str + '\\n';
};
return this;
};
this.LeafListener.prototype = Object.create(<X>Listener.prototype);\n" +
this.LeafListener.prototype.constructor = this.LeafListener;\n" +
public static class LeafListener extends TBaseListener {
public void exitA(TParser.AContext ctx) {
if (ctx.getChildCount()==2) {
System.out.printf("%s %s %s",ctx.b(0).start.getText(),
ctx.b(1).start.getText(),ctx.b().get(0).start.getText());
} else
System.out.println(ctx.b(0).start.getText());
}
}
>>
LRListener(X) ::= <<
this.LeafListener = function() {
this.exitE = 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;
}
document.getElementById('output').value += str + '\\n';
};
return this;
};
this.LeafListener.prototype = Object.create(<X>Listener.prototype);
this.LeafListener.prototype.constructor = this.LeafListener;
public static class LeafListener extends TBaseListener {
public void exitE(TParser.EContext ctx) {
if (ctx.getChildCount()==3) {
System.out.printf("%s %s %s\n",ctx.e(0).start.getText(),
ctx.e(1).start.getText(), ctx.e().get(0).start.getText());
} else
System.out.println(ctx.INT().getSymbol().getText());
}
}
>>
LRWithLabelsListener(X) ::= <<
this.LeafListener = function() {
this.exitCall = function(ctx) {
var str = ctx.e().start.text + ' ' + ctx.eList();
document.getElementById('output').value += str + '\\n';
};
this.exitInt = function(ctx) {
var str = ctx.INT().symbol.text;
document.getElementById('output').value += str + '\\n';
};
return this;
};
this.LeafListener.prototype = Object.create(<X>Listener.prototype);
this.LeafListener.prototype.constructor = this.LeafListener;
public static class LeafListener extends TBaseListener {
public void exitCall(TParser.CallContext ctx) {
System.out.printf("%s %s",ctx.e().start.getText(),ctx.eList());
}
public void exitInt(TParser.IntContext ctx) {
System.out.println(ctx.INT().getSymbol().getText());
}
}
>>
DeclareContextListGettersFunction() ::= <<

View File

@ -9,27 +9,21 @@ public class TestListeners extends BaseTest {
public void testBasic() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"var TListener = require('./TListener').TListener;\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"this.LeafListener = function() {\n" +
" this.visitTerminal = function(node) {\n" +
" document.getElementById('output').value += node.symbol.text + '\\n';\n" +
" };\n" +
" return this;\n" +
"};\n" +
"this.LeafListener.prototype = Object.create(TListener.prototype);\n" +
"this.LeafListener.prototype.constructor = this.LeafListener;\n" +
"\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void visitTerminal(TerminalNode node) {\n" +
" System.out.println(node.getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"var walker = new antlr4.tree.ParseTreeWalker();\n" +
"walker.walk(new this.LeafListener(), $ctx.r);\n" +
"\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : INT INT\n" +
@ -48,33 +42,25 @@ public class TestListeners extends BaseTest {
String testTokenGetters(String input) throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"var TListener = require('./TListener').TListener;\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"this.LeafListener = function() {\n" +
" this.exitA = function(ctx) {\n" +
" var str;\n" +
" if(ctx.getChildCount()===2) {\n" +
" str = ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + antlr4.Utils.arrayToString(ctx.INT());\n" +
" } else {\n" +
" str = ctx.ID().symbol.toString();\n" +
" }\n" +
" document.getElementById('output').value += str + '\\n';\n" +
" };\n" +
" return this;\n" +
"};\n" +
"this.LeafListener.prototype = Object.create(TListener.prototype);\\n\" +\n" +
"this.LeafListener.prototype.constructor = this.LeafListener;\\n\" +\n" +
"\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) \n" +
" System.out.printf(\"%s %s %s\",ctx.INT(0).getSymbol().getText(),\n" +
" ctx.INT(1).getSymbol().getText(),ctx.INT());\n" +
" else\n" +
" System.out.println(ctx.ID().getSymbol());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"var walker = new antlr4.tree.ParseTreeWalker();\n" +
"walker.walk(new this.LeafListener(), $ctx.r);\n" +
"\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : INT INT\n" +
@ -105,33 +91,25 @@ public class TestListeners extends BaseTest {
String testRuleGetters(String input) throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"var TListener = require('./TListener').TListener;\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"this.LeafListener = function() {\n" +
" this.exitA = function(ctx) {\n" +
" var str;\n" +
" if(ctx.getChildCount()===2) {\n" +
" str = ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text;\n" +
" } else {\n" +
" str = ctx.b(0).start.text;\n" +
" }\n" +
" document.getElementById('output').value += str + '\\n';\n" +
" };\n" +
" return this;\n" +
"};\n" +
"this.LeafListener.prototype = Object.create(TListener.prototype);\\n\" +\n" +
"this.LeafListener.prototype.constructor = this.LeafListener;\\n\" +\n" +
"\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) {\n" +
" System.out.printf(\"%s %s %s\",ctx.b(0).start.getText(),\n" +
" ctx.b(1).start.getText(),ctx.b().get(0).start.getText());\n" +
" } else \n" +
" System.out.println(ctx.b(0).start.getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"var walker = new antlr4.tree.ParseTreeWalker();\n" +
"walker.walk(new this.LeafListener(), $ctx.r);\n" +
"\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=a ;\n" +
"a : b b // forces list\n" +
@ -164,33 +142,25 @@ public class TestListeners extends BaseTest {
public void testLR() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"var TListener = require('./TListener').TListener;\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"this.LeafListener = function() {\n" +
" this.exitE = function(ctx) {\n" +
" var str;\n" +
" if(ctx.getChildCount()===3) {\n" +
" str = ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text;\n" +
" } else {\n" +
" str = ctx.INT().symbol.text;\n" +
" }\n" +
" document.getElementById('output').value += str + '\\n';\n" +
" };\n" +
" return this;\n" +
"};\n" +
"this.LeafListener.prototype = Object.create(TListener.prototype);\n" +
"this.LeafListener.prototype.constructor = this.LeafListener;\n" +
"\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitE(TParser.EContext ctx) {\n" +
" if (ctx.getChildCount()==3) {\n" +
" System.out.printf(\"%s %s %s\\n\",ctx.e(0).start.getText(),\n" +
" ctx.e(1).start.getText(), ctx.e().get(0).start.getText());\n" +
" } else \n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"var walker = new antlr4.tree.ParseTreeWalker();\n" +
"walker.walk(new this.LeafListener(), $ctx.r);\n" +
"\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=e ;\n" +
"e : e op='*' e\n" +
@ -211,32 +181,24 @@ public class TestListeners extends BaseTest {
public void testLRWithLabels() throws Exception {
String grammar = "grammar T;\n" +
"@parser::header {\n" +
"var TListener = require('./TListener').TListener;\n" +
"}\n" +
"\n" +
"@parser::members {\n" +
"this.LeafListener = function() {\n" +
" this.exitCall = function(ctx) {\n" +
" var str = ctx.e().start.text + ' ' + ctx.eList();\n" +
" document.getElementById('output').value += str + '\\n';\n" +
" };\n" +
" this.exitInt = function(ctx) {\n" +
" var str = ctx.INT().symbol.text;\n" +
" document.getElementById('output').value += str + '\\n';\n" +
" };\n" +
" return this;\n" +
"};\n" +
"this.LeafListener.prototype = Object.create(TListener.prototype);\n" +
"this.LeafListener.prototype.constructor = this.LeafListener;\n" +
"\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitCall(TParser.CallContext ctx) {\n" +
" System.out.printf(\"%s %s\",ctx.e().start.getText(),ctx.eList());\n" +
" }\n" +
" public void exitInt(TParser.IntContext ctx) {\n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
"}\n" +
"}\n" +
"\n" +
"s\n" +
"@after {\n" +
"System.out.println($ctx.r.toStringTree(this));\n" +
"var walker = new antlr4.tree.ParseTreeWalker();\n" +
"walker.walk(new this.LeafListener(), $ctx.r);\n" +
"\n" +
"ParseTreeWalker walker = new ParseTreeWalker();\n" +
"walker.walk(new LeafListener(), $ctx.r);\n" +
"}\n" +
" : r=e ;\n" +
"e : e '(' eList ')' # Call\n" +

View File

@ -1,226 +0,0 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (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.test.tool;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestListeners extends BaseTest {
@Test public void testBasic() throws Exception {
String grammar =
"grammar T;\n" +
"@header {import org.antlr.v4.runtime.tree.*;}\n"+
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void visitTerminal(TerminalNode node) {\n" +
" System.out.println(node.getSymbol().getText());\n" +
" }\n" +
" }}\n" +
"s\n" +
"@after {" +
" System.out.println($r.ctx.toStringTree(this));" +
" ParseTreeWalker walker = new ParseTreeWalker();\n" +
" walker.walk(new LeafListener(), $r.ctx);" +
"}\n" +
" : r=a ;\n" +
"a : INT INT" +
" | ID" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false);
String expecting = "(a 1 2)\n" +
"1\n" +
"2\n";
assertEquals(expecting, result);
}
@Test public void testTokenGetters() throws Exception {
String grammar =
"grammar T;\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) System.out.printf(\"%s %s %s\",ctx.INT(0).getSymbol().getText(),ctx.INT(1).getSymbol().getText(),ctx.INT());\n" +
" else System.out.println(ctx.ID().getSymbol());\n" +
" }\n" +
" }}\n" +
"s\n" +
"@after {" +
" System.out.println($r.ctx.toStringTree(this));" +
" ParseTreeWalker walker = new ParseTreeWalker();\n" +
" walker.walk(new LeafListener(), $r.ctx);" +
"}\n" +
" : r=a ;\n" +
"a : INT INT" +
" | ID" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false);
String expecting =
"(a 1 2)\n" +
"1 2 [1, 2]\n";
assertEquals(expecting, result);
result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", false);
expecting = "(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n";
assertEquals(expecting, result);
}
@Test public void testRuleGetters() throws Exception {
String grammar =
"grammar T;\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitA(TParser.AContext ctx) {\n" +
" if (ctx.getChildCount()==2) {\n" +
" System.out.printf(\"%s %s %s\",ctx.b(0).start.getText(),\n" +
" ctx.b(1).start.getText(),ctx.b().get(0).start.getText());\n" +
" }\n" +
" else System.out.println(ctx.b(0).start.getText());\n" +
" }\n" +
" }}\n" +
"s\n" +
"@after {" +
" System.out.println($r.ctx.toStringTree(this));" +
" ParseTreeWalker walker = new ParseTreeWalker();\n" +
" walker.walk(new LeafListener(), $r.ctx);" +
"}\n" +
" : r=a ;\n" +
"a : b b" + // forces list
" | b" + // a list still
" ;\n" +
"b : ID | INT ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false);
String expecting = "(a (b 1) (b 2))\n" +
"1 2 1\n";
assertEquals(expecting, result);
result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", false);
expecting = "(a (b abc))\n" +
"abc\n";
assertEquals(expecting, result);
}
@Test public void testLR() throws Exception {
String grammar =
"grammar T;\n" +
"@parser::members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void exitE(TParser.EContext ctx) {\n" +
" if (ctx.getChildCount()==3) {\n" +
" System.out.printf(\"%s %s %s\\n\",ctx.e(0).start.getText(),\n" +
" ctx.e(1).start.getText()," +
" ctx.e().get(0).start.getText());\n" +
" }\n" +
" else System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
" }" +
"}\n" +
"s\n" +
"@after {" +
" System.out.println($r.ctx.toStringTree(this));" +
" ParseTreeWalker walker = new ParseTreeWalker();\n" +
" walker.walk(new LeafListener(), $r.ctx);" +
"}\n" +
" : r=e ;\n" +
"e : e op='*' e\n" +
" | e op='+' e\n" +
" | INT\n" +
" ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1+2*3", false);
String expecting =
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1\n" +
"2\n" +
"3\n" +
"2 3 2\n" +
"1 2 1\n";
assertEquals(expecting, result);
}
@Test public void testLRWithLabels() throws Exception {
String grammar =
"grammar T;\n" +
"@parser::members {\n" +
" public static class LeafListener extends TBaseListener {\n" +
" public void exitCall(TParser.CallContext ctx) {\n" +
" System.out.printf(\"%s %s\",ctx.e().start.getText(),\n" +
" ctx.eList());\n" +
" }\n" +
" public void exitInt(TParser.IntContext ctx) {\n" +
" System.out.println(ctx.INT().getSymbol().getText());\n" +
" }\n" +
" }\n" +
"}\n" +
"s\n" +
"@after {" +
" System.out.println($r.ctx.toStringTree(this));" +
" ParseTreeWalker walker = new ParseTreeWalker();\n" +
" walker.walk(new LeafListener(), $r.ctx);" +
"}\n" +
" : r=e ;\n" +
"e : e '(' eList ')' # Call\n" +
" | INT # Int\n" +
" ; \n" +
"eList : e (',' e)* ;\n" +
"MULT: '*' ;\n" +
"ADD : '+' ;\n" +
"INT : [0-9]+ ;\n" +
"WS : [ \\t\\n]+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1(2,3)", false);
String expecting =
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1\n" +
"2\n" +
"3\n" +
"1 [13 6]\n";
assertEquals(expecting, result);
}
}