📄 testrewriteast.java
字号:
/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr 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.test;import org.antlr.tool.ErrorManager;import org.antlr.tool.GrammarSemanticsMessage;import org.antlr.tool.Message;import org.antlr.tool.Grammar;import org.antlr.Tool;import org.antlr.codegen.CodeGenerator;public class TestRewriteAST extends BaseTest { protected boolean debug = false; public void testDelete() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID INT -> ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc 34", debug); assertEquals("", found); } public void testSingleToken() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID -> ID;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("abc\n", found); } public void testSingleCharLiteral() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : 'c' -> 'c';\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "c", debug); assertEquals("c\n", found); } public void testSingleStringLiteral() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : 'ick' -> 'ick';\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "ick", debug); assertEquals("ick\n", found); } public void testSingleRule() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : b -> b;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("abc\n", found); } public void testReorderTokens() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID INT -> INT ID;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc 34", debug); assertEquals("34 abc\n", found); } public void testReorderTokenAndRule() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : b INT -> INT b;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc 34", debug); assertEquals("34 abc\n", found); } public void testTokenTree() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID INT -> ^(INT ID);\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc 34", debug); assertEquals("(34 abc)\n", found); } public void testTokenTreeAfterOtherStuff() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : 'void' ID INT -> 'void' ^(INT ID);\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "void abc 34", debug); assertEquals("void (34 abc)\n", found); } public void testNestedTokenTreeWithOuterLoop() throws Exception { // verify that ID and INT both iterate over outer index variable String grammar = "grammar T;\n" + "options {output=AST;}\n" + "tokens {DUH;}\n" + "a : ID INT ID INT -> ^( DUH ID ^( DUH INT) )+ ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a 1 b 2", debug); assertEquals("(DUH a (DUH 1)) (DUH b (DUH 2))\n", found); } public void testOptionalSingleToken() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID -> ID? ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("abc\n", found); } public void testClosureSingleToken() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID ID -> ID* ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a b", debug); assertEquals("a b\n", found); } public void testPositiveClosureSingleToken() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID ID -> ID+ ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a b", debug); assertEquals("a b\n", found); } public void testOptionalSingleRule() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : b -> b?;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("abc\n", found); } public void testClosureSingleRule() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : b b -> b*;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a b", debug); assertEquals("a b\n", found); } public void testClosureOfLabel() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : x+=b x+=b -> $x*;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a b", debug); assertEquals("a b\n", found); } public void testOptionalLabelNoListLabel() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : (x=ID)? -> $x?;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a", debug); assertEquals("a\n", found); } public void testPositiveClosureSingleRule() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : b b -> b+;\n" + "b : ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a b", debug); assertEquals("a b\n", found); } public void testSinglePredicateT() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID -> {true}? ID -> ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("abc\n", found); } public void testSinglePredicateF() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID -> {false}? ID -> ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "abc", debug); assertEquals("", found); } public void testMultiplePredicate() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID INT -> {false}? ID\n" + " -> {true}? INT\n" + " -> \n" + " ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a 2", debug); assertEquals("2\n", found); } public void testMultiplePredicateTrees() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ID INT -> {false}? ^(ID INT)\n" + " -> {true}? ^(INT ID)\n" + " -> ID\n" + " ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a 2", debug); assertEquals("(2 a)\n", found); } public void testSimpleTree() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : op INT -> ^(op INT);\n" + "op : '+'|'-' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "-34", debug); assertEquals("(- 34)\n", found); } public void testSimpleTree2() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : op INT -> ^(INT op);\n" + "op : '+'|'-' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "+ 34", debug); assertEquals("(34 +)\n", found); } public void testQueueingOfTokens() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : 'int' ID (',' ID)* ';' -> ^('int' ID+) ;\n" + "op : '+'|'-' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "int a,b,c;", debug); assertEquals("(int a b c)\n", found); } public void testNestedTrees() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : 'var' (ID ':' type ';')+ -> ^('var' ^(':' ID type)+) ;\n" + "type : 'int' | 'float' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "var a:int; b:float;", debug); assertEquals("(var (: a int) (: b float))\n", found); } public void testImaginaryTokenCopy() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "tokens {VAR;}\n" + "a : ID (',' ID)*-> ^(VAR ID)+ ;\n" + "type : 'int' | 'float' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a,b,c", debug); assertEquals("(VAR a) (VAR b) (VAR c)\n", found); } public void testImaginaryTokenCopySetText() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "tokens {VAR;}\n" + "a : ID (',' ID)*-> ^(VAR[\"var\"] ID)+ ;\n" + "type : 'int' | 'float' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "a,b,c", debug); assertEquals("(var a) (var b) (var c)\n", found); } public void testImaginaryTokenNoCopyFromToken() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "tokens {BLOCK;}\n" + "a : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;\n" + "type : 'int' | 'float' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {channel=99;} ;\n"; String found = execParser("t.g", grammar, "TParser", "TLexer", "a", "{a b c}", debug); assertEquals("({ a b c)\n", found); } public void testImaginaryTokenNoCopyFromTokenSetText() throws Exception { String grammar =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -