001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.test;
029:
030: public class TestTreeParsing extends BaseTest {
031: public void testFlatList() throws Exception {
032: String grammar = "grammar T;\n" + "options {output=AST;}\n"
033: + "a : ID INT;\n" + "ID : 'a'..'z'+ ;\n"
034: + "INT : '0'..'9'+;\n"
035: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
036:
037: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
038: + "a : ID INT\n"
039: + " {System.out.println($ID+\", \"+$INT);}\n"
040: + " ;\n";
041:
042: String found = execTreeParser("T.g", grammar, "TParser",
043: "TP.g", treeGrammar, "TP", "TLexer", "a", "a", "abc 34");
044: assertEquals("abc, 34\n", found);
045: }
046:
047: public void testSimpleTree() throws Exception {
048: String grammar = "grammar T;\n" + "options {output=AST;}\n"
049: + "a : ID INT -> ^(ID INT);\n" + "ID : 'a'..'z'+ ;\n"
050: + "INT : '0'..'9'+;\n"
051: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
052:
053: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
054: + "a : ^(ID INT)\n"
055: + " {System.out.println($ID+\", \"+$INT);}\n"
056: + " ;\n";
057:
058: String found = execTreeParser("T.g", grammar, "TParser",
059: "TP.g", treeGrammar, "TP", "TLexer", "a", "a", "abc 34");
060: assertEquals("abc, 34\n", found);
061: }
062:
063: public void testFlatVsTreeDecision() throws Exception {
064: String grammar = "grammar T;\n" + "options {output=AST;}\n"
065: + "a : b c ;\n" + "b : ID INT -> ^(ID INT);\n"
066: + "c : ID INT;\n" + "ID : 'a'..'z'+ ;\n"
067: + "INT : '0'..'9'+;\n"
068: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
069:
070: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
071: + "a : b b ;\n"
072: + "b : ID INT {System.out.print($ID+\" \"+$INT);}\n"
073: + " | ^(ID INT) {System.out.print(\"^(\"+$ID+\" \"+$INT+')');}\n"
074: + " ;\n";
075:
076: String found = execTreeParser("T.g", grammar, "TParser",
077: "TP.g", treeGrammar, "TP", "TLexer", "a", "a",
078: "a 1 b 2");
079: assertEquals("^(a 1)b 2\n", found);
080: }
081:
082: public void testFlatVsTreeDecision2() throws Exception {
083: String grammar = "grammar T;\n" + "options {output=AST;}\n"
084: + "a : b c ;\n" + "b : ID INT+ -> ^(ID INT+);\n"
085: + "c : ID INT+;\n" + "ID : 'a'..'z'+ ;\n"
086: + "INT : '0'..'9'+;\n"
087: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
088:
089: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
090: + "a : b b ;\n"
091: + "b : ID INT+ {System.out.print($ID+\" \"+$INT);}\n"
092: + " | ^(x=ID (y=INT)+) {System.out.print(\"^(\"+$x+' '+$y+')');}\n"
093: + " ;\n";
094:
095: String found = execTreeParser("T.g", grammar, "TParser",
096: "TP.g", treeGrammar, "TP", "TLexer", "a", "a",
097: "a 1 2 3 b 4 5");
098: assertEquals("^(a 3)b 5\n", found);
099: }
100:
101: public void testCyclicDFALookahead() throws Exception {
102: String grammar = "grammar T;\n" + "options {output=AST;}\n"
103: + "a : ID INT+ PERIOD;\n" + "ID : 'a'..'z'+ ;\n"
104: + "INT : '0'..'9'+;\n" + "SEMI : ';' ;\n"
105: + "PERIOD : '.' ;\n"
106: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
107:
108: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
109: + "a : ID INT+ PERIOD {System.out.print(\"alt 1\");}"
110: + " | ID INT+ SEMI {System.out.print(\"alt 2\");}\n"
111: + " ;\n";
112:
113: String found = execTreeParser("T.g", grammar, "TParser",
114: "TP.g", treeGrammar, "TP", "TLexer", "a", "a",
115: "a 1 2 3.");
116: assertEquals("alt 1\n", found);
117: }
118:
119: public void testTemplateOutput() throws Exception {
120: String grammar = "grammar T;\n" + "options {output=AST;}\n"
121: + "a : ID INT;\n" + "ID : 'a'..'z'+ ;\n"
122: + "INT : '0'..'9'+;\n"
123: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
124:
125: String treeGrammar = "tree grammar TP;\n"
126: + "options {output=template; ASTLabelType=CommonTree;}\n"
127: + "s : a {System.out.println($a.st);};\n"
128: + "a : ID INT -> {new StringTemplate($INT.text)}\n"
129: + " ;\n";
130:
131: String found = execTreeParser("T.g", grammar, "TParser",
132: "TP.g", treeGrammar, "TP", "TLexer", "a", "s", "abc 34");
133: assertEquals("34\n", found);
134: }
135:
136: public void testNullableChildList() throws Exception {
137: String grammar = "grammar T;\n" + "options {output=AST;}\n"
138: + "a : ID INT? -> ^(ID INT?);\n" + "ID : 'a'..'z'+ ;\n"
139: + "INT : '0'..'9'+;\n"
140: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
141:
142: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
143: + "a : ^(ID INT?)\n"
144: + " {System.out.println($ID);}\n" + " ;\n";
145:
146: String found = execTreeParser("T.g", grammar, "TParser",
147: "TP.g", treeGrammar, "TP", "TLexer", "a", "a", "abc");
148: assertEquals("abc\n", found);
149: }
150:
151: public void testNullableChildList2() throws Exception {
152: String grammar = "grammar T;\n" + "options {output=AST;}\n"
153: + "a : ID INT? SEMI -> ^(ID INT?) SEMI ;\n"
154: + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n"
155: + "SEMI : ';' ;\n"
156: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
157:
158: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
159: + "a : ^(ID INT?) SEMI\n"
160: + " {System.out.println($ID);}\n" + " ;\n";
161:
162: String found = execTreeParser("T.g", grammar, "TParser",
163: "TP.g", treeGrammar, "TP", "TLexer", "a", "a", "abc;");
164: assertEquals("abc\n", found);
165: }
166:
167: public void testNullableChildList3() throws Exception {
168: String grammar = "grammar T;\n"
169: + "options {output=AST;}\n"
170: + "a : x=ID INT? (y=ID)? SEMI -> ^($x INT? $y?) SEMI ;\n"
171: + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n"
172: + "SEMI : ';' ;\n"
173: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
174:
175: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
176: + "a : ^(ID INT? b) SEMI\n"
177: + " {System.out.println($ID+\", \"+$b.text);}\n"
178: + " ;\n" + "b : ID? ;\n";
179:
180: String found = execTreeParser("T.g", grammar, "TParser",
181: "TP.g", treeGrammar, "TP", "TLexer", "a", "a",
182: "abc def;");
183: assertEquals("abc, def\n", found);
184: }
185:
186: public void testActionsAfterRoot() throws Exception {
187: String grammar = "grammar T;\n" + "options {output=AST;}\n"
188: + "a : x=ID INT? SEMI -> ^($x INT?) ;\n"
189: + "ID : 'a'..'z'+ ;\n" + "INT : '0'..'9'+;\n"
190: + "SEMI : ';' ;\n"
191: + "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
192:
193: String treeGrammar = "tree grammar TP; options {ASTLabelType=CommonTree;}\n"
194: + "a @init {int x=0;} : ^(ID {x=1;} {x=2;} INT?)\n"
195: + " {System.out.println($ID+\", \"+x);}\n" + " ;\n";
196:
197: String found = execTreeParser("T.g", grammar, "TParser",
198: "TP.g", treeGrammar, "TP", "TLexer", "a", "a", "abc;");
199: assertEquals("abc, 2\n", found);
200: }
201:
202: }
|