⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testautoast.java

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*[The "BSD licence"]Copyright (c) 2005-2006 Terence ParrAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.3. The name of the author may not be used to endorse or promote productsderived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF 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, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.antlr.test;public class TestAutoAST extends BaseTest {	protected boolean debug = false;	public void testTokenList() throws Exception {		String grammar =			"grammar foo;\n" +			"options {output=AST;}\n" +			"a : ID INT ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",								  "a", "abc 34", debug);		assertEquals("abc 34\n", found);	}	public void testTokenListInSingleAltBlock() throws Exception {		String grammar =			"grammar foo;\n" +			"options {output=AST;}\n" +			"a : (ID INT) ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",								  "a", "abc 34", debug);		assertEquals("abc 34\n", found);	}	public void testSimpleRootAtOuterLevel() throws Exception {		String grammar =			"grammar foo;\n" +			"options {output=AST;}\n" +			"a : ID^ INT ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",								  "a", "abc 34", debug);		assertEquals("(abc 34)\n", found);	}	public void testSimpleRootAtOuterLevelReverse() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : INT ID^ ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "34 abc", debug);		assertEquals("(abc 34)\n", found);	}	public void testBang() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : ID INT! ID! INT ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "abc 34 dag 4532", debug);		assertEquals("abc 4532\n", found);	}	public void testLoopRoot() 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=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 1 b 2 c 3", debug);		assertEquals("(a 1) (b 2) (c 3)\n", found);	}	public void testLoopRootReverse() 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=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 1 b 2 c 3", debug);		assertEquals("(1 a) (2 b) (3 c)\n", found);	}	public void testPlusLoopRoot() 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=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 1 b 2 c 3", debug);		assertEquals("(a 1) (b 2) (c 3)\n", found);	}	public void testPlusLoopRootReverse() 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=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 1 b 2 c 3", debug);		assertEquals("(a 1) (b 2) (c 3)\n", found);	}	public void testOptionalThenRoot() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : ( ID INT )? ID^ ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 1 b", debug);		assertEquals("(b a 1)\n", found);	}	public void testLabeledStringRoot() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : v='void'^ ID ';' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "void foo;", debug);		assertEquals("(void foo ;)\n", found);	}	public void testWildcard() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : v='void'^ . ';' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "void foo;", debug);		assertEquals("(void foo ;)\n", found);	}	public void testWildcardRoot() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : v='void' .^ ';' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "void foo;", debug);		assertEquals("(foo void ;)\n", found);	}	public void testRootRoot() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : ID^ INT^ ID ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 34 c", debug);		assertEquals("(34 a c)\n", found);	}	public void testRootRoot2() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : ID INT^ ID^ ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a 34 c", debug);		assertEquals("(c (34 a))\n", found);	}	public void testNestedSubrule() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : 'void' (({;}ID|INT) ID | 'null' ) ';' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "void a b;", debug);		assertEquals("void a b ;\n", found);	}	public void testInvokeRule() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a  : type ID ;\n" +			"type : {;}'int' | 'float' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "int a", debug);		assertEquals("int a\n", found);	}	public void testInvokeRuleAsRoot() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a  : type^ ID ;\n" +			"type : {;}'int' | 'float' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "int a", debug);		assertEquals("(int a)\n", found);	}	public void testRuleRootInLoop() 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=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a+b+c+d", debug);		assertEquals("(+ (+ (+ a b) c) d)\n", found);	}	public void testRuleInvocationRuleRootInLoop() throws Exception {		String grammar =			"grammar T;\n" +			"options {output=AST;}\n" +			"a : ID (op^^ ID)* ;\n" +			"op : {;}'+' | '-' ;\n" +			"ID : 'a'..'z'+ ;\n" +			"INT : '0'..'9'+;\n" +			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";		String found = execParser("T.g", grammar, "TParser", "TLexer",								  "a", "a+b+c-d", debug);		assertEquals("(- (+ (+ a b) c) d)\n", found);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -