📄 testautoast.java
字号:
} public void testTailRecursion() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "s : a ;\n" + "a : atom ('exp'^^ a)? ;\n" + "atom : INT ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; String found = execParser("T.g", grammar, "TParser", "TLexer", "s", "3 exp 4 exp 5", debug); assertEquals("(exp 3 (exp 4 5))\n", found); } public void testSet() 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", "abc", debug); assertEquals("abc\n", found); } public void testSetRoot() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ('+' | '-')^ ID ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "+abc", debug); assertEquals("(+ abc)\n", found); } public void testSetAsRuleRootInLoop() 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", debug); assertEquals("(- (+ a b) c)\n", found); } public void testNotSet() 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", "34+2", debug); assertEquals("34 + 2\n", found); } public void testNotSetRoot() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ~'+'^ INT ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "34 55", debug); assertEquals("(34 55)\n", found); } public void testNotSetRuleRootInLoop() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : INT (~INT^^ INT)* ;\n" + "blort : '+' ;\n" + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n" + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "3+4+5", debug); assertEquals("(+ (+ 3 4) 5)\n", found); } public void testTokenLabelReuse() throws Exception { // check for compilation problem due to multiple defines String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : id=ID id=ID {System.out.print(\"2nd id=\"+$id.text+';');} ;\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", debug); assertEquals("2nd id=b;a b\n", found); } public void testTokenLabelReuse2() throws Exception { // check for compilation problem due to multiple defines String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : id=ID id=ID^ {System.out.print(\"2nd id=\"+$id.text+';');} ;\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", debug); assertEquals("2nd id=b;(b a)\n", found); } public void testTokenListLabelReuse() throws Exception { // check for compilation problem due to multiple defines // make sure ids has both ID tokens String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ids+=ID ids+=ID {System.out.print(\"id list=\"+$ids+';');} ;\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", debug); String expecting = "id list=[[@0,0:0='a',<4>,1:0], [@2,2:2='b',<4>,1:2]];a b\n"; assertEquals(expecting, found); } public void testTokenListLabelReuse2() throws Exception { // check for compilation problem due to multiple defines // make sure ids has both ID tokens String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ids+=ID^ ids+=ID {System.out.print(\"id list=\"+$ids+';');} ;\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", debug); String expecting = "id list=[[@0,0:0='a',<4>,1:0], [@2,2:2='b',<4>,1:2]];(a b)\n"; assertEquals(expecting, found); } public void testTokenListLabelRuleRoot() 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", debug); assertEquals("a\n", found); } public void testTokenListLabelBang() 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", debug); assertEquals("nil\n", found); } public void testRuleListLabel() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : x+=b x+=b {" + "b_return ret=(b_return)$x.get(1);" + "System.out.print(\"2nd x=\"+((CommonTree)ret.tree).toStringTree()+';');} ;\n" + "b : 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", debug); assertEquals("2nd x=b;a b\n", found); } public void testRuleListLabelRoot() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ( x+=b^ )+ {" + "b_return ret=(b_return)$x.get(1);" + "System.out.print(\"x=\"+((CommonTree)ret.tree).toStringTree()+';');} ;\n" + "b : 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", debug); assertEquals("x=b;a b\n", found); } public void testRuleListLabelRuleRoot() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ( x+=b^^ )+ {" + "b_return ret=(b_return)$x.get(1);" + "System.out.print(\"x=\"+((CommonTree)ret.tree).toStringTree()+';');} ;\n" + "b : 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", debug); assertEquals("x=(b a);(b a)\n", found); } public void testRuleListLabelBang() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : x+=b! x+=b {" + "b_return ret=(b_return)$x.get(0);" + "System.out.print(\"1st x=\"+((CommonTree)ret.tree).toStringTree()+';');} ;\n" + "b : 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", debug); assertEquals("1st x=a;b\n", found); } public void testComplicatedMelange() throws Exception { // check for compilation problem String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : A b=B b=B c+=C c+=C D {$D.text;} ;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n" + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n"; String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "a b b c c d", debug); assertEquals("a b b c c d\n", found); } public void testReturnValueWithAST() throws Exception { String grammar = "grammar foo;\n" + "options {output=AST;}\n" + "a : ID b {System.out.println($b.i);} ;\n" + "b returns [int i] : INT {$i=Integer.parseInt($INT.text);} ;\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("34\nabc 34\n", found); } // S U P P O R T public void _test() throws Exception { String grammar = "grammar T;\n" + "options {output=AST;}\n" + "a : ;\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", debug); assertEquals("\n", found); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -