• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *  3. The name of the author may not be used to endorse or promote products
15  *      derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.test;
29 
30 import org.antlr.tool.Grammar;
31 import org.junit.Test;
32 
33 import static org.junit.Assert.*;
34 
35 public class TestASTConstruction extends BaseTest {
36 
37     /** Public default constructor used by TestRig */
TestASTConstruction()38     public TestASTConstruction() {
39     }
40 
testA()41 	@Test public void testA() throws Exception {
42 		Grammar g = new Grammar(
43 				"parser grammar P;\n"+
44 				"a : A;");
45 		String expecting =
46 			"(rule a ARG RET scope (BLOCK (ALT A <end-of-alt>) <end-of-block>) <end-of-rule>)";
47 		String found = g.getRule("a").tree.toStringTree();
48 		assertEquals(expecting, found);
49 	}
50 
testEmptyAlt()51 	@Test public void testEmptyAlt() throws Exception {
52 		Grammar g = new Grammar(
53 				"parser grammar P;\n"+
54 				"a : ;");
55 		String expecting =
56 			"(rule a ARG RET scope (BLOCK (ALT epsilon <end-of-alt>) <end-of-block>) <end-of-rule>)";
57 		String found = g.getRule("a").tree.toStringTree();
58 		assertEquals(expecting, found);
59 	}
60 
testNakeRulePlusInLexer()61 	@Test public void testNakeRulePlusInLexer() throws Exception {
62 		Grammar g = new Grammar(
63 				"lexer grammar P;\n"+
64 				"A : B+;\n" +
65 				"B : 'a';");
66 		String expecting =
67 			"(rule A ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
68 		String found = g.getRule("A").tree.toStringTree();
69 		assertEquals(expecting, found);
70 	}
71 
testRulePlus()72 	@Test public void testRulePlus() throws Exception {
73 		Grammar g = new Grammar(
74 				"parser grammar P;\n"+
75 				"a : (b)+;\n" +
76 				"b : B;");
77 		String expecting =
78 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
79 		String found = g.getRule("a").tree.toStringTree();
80 		assertEquals(expecting, found);
81 	}
82 
testNakedRulePlus()83 	@Test public void testNakedRulePlus() throws Exception {
84 		Grammar g = new Grammar(
85 				"parser grammar P;\n"+
86 				"a : b+;\n" +
87 				"b : B;");
88 		String expecting =
89 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
90 		String found = g.getRule("a").tree.toStringTree();
91 		assertEquals(expecting, found);
92 	}
93 
testRuleOptional()94 	@Test public void testRuleOptional() throws Exception {
95 		Grammar g = new Grammar(
96 				"parser grammar P;\n"+
97 				"a : (b)?;\n" +
98 				"b : B;");
99 		String expecting =
100 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
101 		String found = g.getRule("a").tree.toStringTree();
102 		assertEquals(expecting, found);
103 	}
104 
testNakedRuleOptional()105 	@Test public void testNakedRuleOptional() throws Exception {
106 		Grammar g = new Grammar(
107 				"parser grammar P;\n"+
108 				"a : b?;\n" +
109 				"b : B;");
110 		String expecting =
111 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
112 		String found = g.getRule("a").tree.toStringTree();
113 		assertEquals(expecting, found);
114 	}
115 
testRuleStar()116 	@Test public void testRuleStar() throws Exception {
117 		Grammar g = new Grammar(
118 				"parser grammar P;\n"+
119 				"a : (b)*;\n" +
120 				"b : B;");
121 		String expecting =
122 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
123 		String found = g.getRule("a").tree.toStringTree();
124 		assertEquals(expecting, found);
125 	}
126 
testNakedRuleStar()127 	@Test public void testNakedRuleStar() throws Exception {
128 		Grammar g = new Grammar(
129 				"parser grammar P;\n"+
130 				"a : b*;\n" +
131 				"b : B;");
132 		String expecting =
133 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
134 		String found = g.getRule("a").tree.toStringTree();
135 		assertEquals(expecting, found);
136 	}
137 
testCharStar()138 	@Test public void testCharStar() throws Exception {
139 		Grammar g = new Grammar(
140 				"grammar P;\n"+
141 				"a : 'a'*;");
142 		String expecting =
143 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
144 		String found = g.getRule("a").tree.toStringTree();
145 		assertEquals(expecting, found);
146 	}
147 
testCharStarInLexer()148 	@Test public void testCharStarInLexer() throws Exception {
149 		Grammar g = new Grammar(
150 				"lexer grammar P;\n"+
151 				"B : 'b'*;");
152 		String expecting =
153 			"(rule B ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
154 		String found = g.getRule("B").tree.toStringTree();
155 		assertEquals(expecting, found);
156 	}
157 
testStringStar()158 	@Test public void testStringStar() throws Exception {
159 		Grammar g = new Grammar(
160 				"grammar P;\n"+
161 				"a : 'while'*;");
162 		String expecting =
163 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'while' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
164 		String found = g.getRule("a").tree.toStringTree();
165 		assertEquals(expecting, found);
166 	}
167 
testStringStarInLexer()168 	@Test public void testStringStarInLexer() throws Exception {
169 		Grammar g = new Grammar(
170 				"lexer grammar P;\n"+
171 				"B : 'while'*;");
172 		String expecting =
173 			"(rule B ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'while' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
174 		String found = g.getRule("B").tree.toStringTree();
175 		assertEquals(expecting, found);
176 	}
177 
testCharPlus()178 	@Test public void testCharPlus() throws Exception {
179 		Grammar g = new Grammar(
180 				"grammar P;\n"+
181 				"a : 'a'+;");
182 		String expecting =
183 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
184 		String found = g.getRule("a").tree.toStringTree();
185 		assertEquals(expecting, found);
186 	}
187 
testCharPlusInLexer()188 	@Test public void testCharPlusInLexer() throws Exception {
189 		Grammar g = new Grammar(
190 				"lexer grammar P;\n"+
191 				"B : 'b'+;");
192 		String expecting =
193 			"(rule B ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
194 		String found = g.getRule("B").tree.toStringTree();
195 		assertEquals(expecting, found);
196 	}
197 
testCharOptional()198 	@Test public void testCharOptional() throws Exception {
199 		Grammar g = new Grammar(
200 				"grammar P;\n"+
201 				"a : 'a'?;");
202 		String expecting =
203 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
204 		String found = g.getRule("a").tree.toStringTree();
205 		assertEquals(expecting, found);
206 	}
207 
testCharOptionalInLexer()208 	@Test public void testCharOptionalInLexer() throws Exception {
209 		Grammar g = new Grammar(
210 				"lexer grammar P;\n"+
211 				"B : 'b'?;");
212 		String expecting =
213 			"(rule B ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
214 		String found = g.getRule("B").tree.toStringTree();
215 		assertEquals(expecting, found);
216 	}
217 
testCharRangePlus()218 	@Test public void testCharRangePlus() throws Exception {
219 		Grammar g = new Grammar(
220 				"lexer grammar P;\n"+
221 				"ID : 'a'..'z'+;");
222 		String expecting =
223 			"(rule ID ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (.. 'a' 'z') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
224 		String found = g.getRule("ID").tree.toStringTree();
225 		assertEquals(expecting, found);
226 	}
227 
testLabel()228 	@Test public void testLabel() throws Exception {
229 		Grammar g = new Grammar(
230 				"grammar P;\n"+
231 				"a : x=ID;");
232 		String expecting =
233 			"(rule a ARG RET scope (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>) <end-of-rule>)";
234 		String found = g.getRule("a").tree.toStringTree();
235 		assertEquals(expecting, found);
236 	}
237 
testLabelOfOptional()238 	@Test public void testLabelOfOptional() throws Exception {
239 		Grammar g = new Grammar(
240 				"grammar P;\n"+
241 				"a : x=ID?;");
242 		String expecting =
243 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
244 		String found = g.getRule("a").tree.toStringTree();
245 		assertEquals(expecting, found);
246 	}
247 
testLabelOfClosure()248 	@Test public void testLabelOfClosure() throws Exception {
249 		Grammar g = new Grammar(
250 				"grammar P;\n"+
251 				"a : x=ID*;");
252 		String expecting =
253 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
254 		String found = g.getRule("a").tree.toStringTree();
255 		assertEquals(expecting, found);
256 	}
257 
testRuleLabel()258 	@Test public void testRuleLabel() throws Exception {
259 		Grammar g = new Grammar(
260 				"grammar P;\n"+
261 				"a : x=b;\n" +
262 				"b : ID;\n");
263 		String expecting =
264 			"(rule a ARG RET scope (BLOCK (ALT (= x b) <end-of-alt>) <end-of-block>) <end-of-rule>)";
265 		String found = g.getRule("a").tree.toStringTree();
266 		assertEquals(expecting, found);
267 	}
268 
testSetLabel()269 	@Test public void testSetLabel() throws Exception {
270 		Grammar g = new Grammar(
271 				"grammar P;\n"+
272 				"a : x=(A|B);\n");
273 		String expecting =
274 			"(rule a ARG RET scope (BLOCK (ALT (= x (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
275 		String found = g.getRule("a").tree.toStringTree();
276 		assertEquals(expecting, found);
277 	}
278 
testNotSetLabel()279 	@Test public void testNotSetLabel() throws Exception {
280 		Grammar g = new Grammar(
281 				"grammar P;\n"+
282 				"a : x=~(A|B);\n");
283 		String expecting =
284 			"(rule a ARG RET scope (BLOCK (ALT (= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>) <end-of-rule>)";
285 		String found = g.getRule("a").tree.toStringTree();
286 		assertEquals(expecting, found);
287 	}
288 
testNotSetListLabel()289 	@Test public void testNotSetListLabel() throws Exception {
290 		Grammar g = new Grammar(
291 				"grammar P;\n"+
292 				"a : x+=~(A|B);\n");
293 		String expecting =
294 			"(rule a ARG RET scope (BLOCK (ALT (+= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>) <end-of-rule>)";
295 		String found = g.getRule("a").tree.toStringTree();
296 		assertEquals(expecting, found);
297 	}
298 
testNotSetListLabelInLoop()299 	@Test public void testNotSetListLabelInLoop() throws Exception {
300 		Grammar g = new Grammar(
301 				"grammar P;\n"+
302 				"a : x+=~(A|B)+;\n");
303 		String expecting =
304 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (+= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
305 		String found = g.getRule("a").tree.toStringTree();
306 		assertEquals(expecting, found);
307 	}
308 
testRuleLabelOfPositiveClosure()309 	@Test public void testRuleLabelOfPositiveClosure() throws Exception {
310 		Grammar g = new Grammar(
311 				"grammar P;\n"+
312 				"a : x=b+;\n" +
313 				"b : ID;\n");
314 		String expecting =
315 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (= x b) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
316 		String found = g.getRule("a").tree.toStringTree();
317 		assertEquals(expecting, found);
318 	}
319 
testListLabelOfClosure()320 	@Test public void testListLabelOfClosure() throws Exception {
321 		Grammar g = new Grammar(
322 				"grammar P;\n"+
323 				"a : x+=ID*;");
324 		String expecting =
325 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (+= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
326 		String found = g.getRule("a").tree.toStringTree();
327 		assertEquals(expecting, found);
328 	}
329 
testListLabelOfClosure2()330 	@Test public void testListLabelOfClosure2() throws Exception {
331 		Grammar g = new Grammar(
332 				"grammar P;\n"+
333 				"a : x+='int'*;");
334 		String expecting =
335 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (+= x 'int') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
336 		String found = g.getRule("a").tree.toStringTree();
337 		assertEquals(expecting, found);
338 	}
339 
testRuleListLabelOfPositiveClosure()340 	@Test public void testRuleListLabelOfPositiveClosure() throws Exception {
341 		Grammar g = new Grammar(
342 				"grammar P;\n" +
343 				"options {output=AST;}\n"+
344 				"a : x+=b+;\n" +
345 				"b : ID;\n");
346 		String expecting =
347 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (+= x b) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
348 		String found = g.getRule("a").tree.toStringTree();
349 		assertEquals(expecting, found);
350 	}
351 
testRootTokenInStarLoop()352 	@Test public void testRootTokenInStarLoop() throws Exception {
353 		Grammar g = new Grammar(
354 				"grammar Expr;\n" +
355 				"options { output=AST; backtrack=true; }\n" +
356 				"a : ('*'^)* ;\n");  // bug: the synpred had nothing in it
357 		String expecting =
358 			"(rule synpred1_Expr ARG RET scope (BLOCK (ALT '*' <end-of-alt>) <end-of-block>) <end-of-rule>)";
359 		String found = g.getRule("synpred1_Expr").tree.toStringTree();
360 		assertEquals(expecting, found);
361 	}
362 
testActionInStarLoop()363 	@Test public void testActionInStarLoop() throws Exception {
364 		Grammar g = new Grammar(
365 				"grammar Expr;\n" +
366 				"options { backtrack=true; }\n" +
367 				"a : ({blort} 'x')* ;\n");  // bug: the synpred had nothing in it
368 		String expecting =
369 			"(rule synpred1_Expr ARG RET scope (BLOCK (ALT 'x' <end-of-alt>) <end-of-block>) <end-of-rule>)";
370 		String found = g.getRule("synpred1_Expr").tree.toStringTree();
371 		assertEquals(expecting, found);
372 	}
373 
374 }
375