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: import org.antlr.tool.Grammar;
031:
032: public class TestASTConstruction extends BaseTest {
033:
034: /** Public default constructor used by TestRig */
035: public TestASTConstruction() {
036: }
037:
038: public void testA() throws Exception {
039: Grammar g = new Grammar("parser grammar P;\n" + "a : A;");
040: String expecting = " ( rule a ARG RET scope ( BLOCK ( ALT A <end-of-alt> ) <end-of-block> ) <end-of-rule> )";
041: String found = g.getRule("a").tree.toStringTree();
042: assertEquals(expecting, found);
043: }
044:
045: public void testNakeRulePlusInLexer() throws Exception {
046: Grammar g = new Grammar("lexer grammar P;\n" + "A : B+;\n"
047: + "B : 'a';");
048: String expecting = " ( 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> )";
049: String found = g.getRule("A").tree.toStringTree();
050: assertEquals(expecting, found);
051: }
052:
053: public void testRulePlus() throws Exception {
054: Grammar g = new Grammar("parser grammar P;\n" + "a : (b)+;\n"
055: + "b : B;");
056: String expecting = " ( 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> )";
057: String found = g.getRule("a").tree.toStringTree();
058: assertEquals(expecting, found);
059: }
060:
061: public void testNakedRulePlus() throws Exception {
062: Grammar g = new Grammar("parser grammar P;\n" + "a : b+;\n"
063: + "b : B;");
064: String expecting = " ( 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> )";
065: String found = g.getRule("a").tree.toStringTree();
066: assertEquals(expecting, found);
067: }
068:
069: public void testRuleOptional() throws Exception {
070: Grammar g = new Grammar("parser grammar P;\n" + "a : (b)?;\n"
071: + "b : B;");
072: String expecting = " ( 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> )";
073: String found = g.getRule("a").tree.toStringTree();
074: assertEquals(expecting, found);
075: }
076:
077: public void testNakedRuleOptional() throws Exception {
078: Grammar g = new Grammar("parser grammar P;\n" + "a : b?;\n"
079: + "b : B;");
080: String expecting = " ( 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> )";
081: String found = g.getRule("a").tree.toStringTree();
082: assertEquals(expecting, found);
083: }
084:
085: public void testRuleStar() throws Exception {
086: Grammar g = new Grammar("parser grammar P;\n" + "a : (b)*;\n"
087: + "b : B;");
088: String expecting = " ( 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> )";
089: String found = g.getRule("a").tree.toStringTree();
090: assertEquals(expecting, found);
091: }
092:
093: public void testNakedRuleStar() throws Exception {
094: Grammar g = new Grammar("parser grammar P;\n" + "a : b*;\n"
095: + "b : B;");
096: String expecting = " ( 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> )";
097: String found = g.getRule("a").tree.toStringTree();
098: assertEquals(expecting, found);
099: }
100:
101: public void testCharStar() throws Exception {
102: Grammar g = new Grammar("grammar P;\n" + "a : 'a'*;");
103: String expecting = " ( 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> )";
104: String found = g.getRule("a").tree.toStringTree();
105: assertEquals(expecting, found);
106: }
107:
108: public void testCharStarInLexer() throws Exception {
109: Grammar g = new Grammar("lexer grammar P;\n" + "B : 'b'*;");
110: String expecting = " ( 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> )";
111: String found = g.getRule("B").tree.toStringTree();
112: assertEquals(expecting, found);
113: }
114:
115: public void testStringStar() throws Exception {
116: Grammar g = new Grammar("grammar P;\n" + "a : 'while'*;");
117: String expecting = " ( 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> )";
118: String found = g.getRule("a").tree.toStringTree();
119: assertEquals(expecting, found);
120: }
121:
122: public void testStringStarInLexer() throws Exception {
123: Grammar g = new Grammar("lexer grammar P;\n" + "B : 'while'*;");
124: String expecting = " ( 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> )";
125: String found = g.getRule("B").tree.toStringTree();
126: assertEquals(expecting, found);
127: }
128:
129: public void testCharPlus() throws Exception {
130: Grammar g = new Grammar("grammar P;\n" + "a : 'a'+;");
131: String expecting = " ( 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> )";
132: String found = g.getRule("a").tree.toStringTree();
133: assertEquals(expecting, found);
134: }
135:
136: public void testCharPlusInLexer() throws Exception {
137: Grammar g = new Grammar("lexer grammar P;\n" + "B : 'b'+;");
138: String expecting = " ( 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> )";
139: String found = g.getRule("B").tree.toStringTree();
140: assertEquals(expecting, found);
141: }
142:
143: public void testCharOptional() throws Exception {
144: Grammar g = new Grammar("grammar P;\n" + "a : 'a'?;");
145: String expecting = " ( 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> )";
146: String found = g.getRule("a").tree.toStringTree();
147: assertEquals(expecting, found);
148: }
149:
150: public void testCharOptionalInLexer() throws Exception {
151: Grammar g = new Grammar("lexer grammar P;\n" + "B : 'b'?;");
152: String expecting = " ( 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> )";
153: String found = g.getRule("B").tree.toStringTree();
154: assertEquals(expecting, found);
155: }
156:
157: public void testCharRangePlus() throws Exception {
158: Grammar g = new Grammar("lexer grammar P;\n"
159: + "ID : 'a'..'z'+;");
160: String expecting = " ( 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> )";
161: String found = g.getRule("ID").tree.toStringTree();
162: assertEquals(expecting, found);
163: }
164:
165: public void testLabel() throws Exception {
166: Grammar g = new Grammar("grammar P;\n" + "a : x=ID;");
167: String expecting = " ( rule a ARG RET scope ( BLOCK ( ALT ( = x ID ) <end-of-alt> ) <end-of-block> ) <end-of-rule> )";
168: String found = g.getRule("a").tree.toStringTree();
169: assertEquals(expecting, found);
170: }
171:
172: public void testLabelOfOptional() throws Exception {
173: Grammar g = new Grammar("grammar P;\n" + "a : x=ID?;");
174: String expecting = " ( 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> )";
175: String found = g.getRule("a").tree.toStringTree();
176: assertEquals(expecting, found);
177: }
178:
179: public void testLabelOfClosure() throws Exception {
180: Grammar g = new Grammar("grammar P;\n" + "a : x=ID*;");
181: String expecting = " ( 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> )";
182: String found = g.getRule("a").tree.toStringTree();
183: assertEquals(expecting, found);
184: }
185:
186: public void testRuleLabel() throws Exception {
187: Grammar g = new Grammar("grammar P;\n" + "a : x=b;\n"
188: + "b : ID;\n");
189: String expecting = " ( rule a ARG RET scope ( BLOCK ( ALT ( = x b ) <end-of-alt> ) <end-of-block> ) <end-of-rule> )";
190: String found = g.getRule("a").tree.toStringTree();
191: assertEquals(expecting, found);
192: }
193:
194: public void testSetLabel() throws Exception {
195: Grammar g = new Grammar("grammar P;\n" + "a : x=(A|B);\n");
196: String expecting = " ( 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> )";
197: String found = g.getRule("a").tree.toStringTree();
198: assertEquals(expecting, found);
199: }
200:
201: public void testNotSetLabel() throws Exception {
202: Grammar g = new Grammar("grammar P;\n" + "a : x=~(A|B);\n");
203: String expecting = " ( 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> )";
204: String found = g.getRule("a").tree.toStringTree();
205: assertEquals(expecting, found);
206: }
207:
208: public void testNotSetListLabel() throws Exception {
209: Grammar g = new Grammar("grammar P;\n" + "a : x+=~(A|B);\n");
210: String expecting = " ( 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> )";
211: String found = g.getRule("a").tree.toStringTree();
212: assertEquals(expecting, found);
213: }
214:
215: public void testNotSetListLabelInLoop() throws Exception {
216: Grammar g = new Grammar("grammar P;\n" + "a : x+=~(A|B)+;\n");
217: String expecting = " ( 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> )";
218: String found = g.getRule("a").tree.toStringTree();
219: assertEquals(expecting, found);
220: }
221:
222: public void testRuleLabelOfPositiveClosure() throws Exception {
223: Grammar g = new Grammar("grammar P;\n" + "a : x=b+;\n"
224: + "b : ID;\n");
225: String expecting = " ( 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> )";
226: String found = g.getRule("a").tree.toStringTree();
227: assertEquals(expecting, found);
228: }
229:
230: public void testListLabelOfClosure() throws Exception {
231: Grammar g = new Grammar("grammar P;\n" + "a : x+=ID*;");
232: String expecting = " ( 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> )";
233: String found = g.getRule("a").tree.toStringTree();
234: assertEquals(expecting, found);
235: }
236:
237: public void testListLabelOfClosure2() throws Exception {
238: Grammar g = new Grammar("grammar P;\n" + "a : x+='int'*;");
239: String expecting = " ( 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> )";
240: String found = g.getRule("a").tree.toStringTree();
241: assertEquals(expecting, found);
242: }
243:
244: public void testRuleListLabelOfPositiveClosure() throws Exception {
245: Grammar g = new Grammar("grammar P;\n"
246: + "options {output=AST;}\n" + "a : x+=b+;\n"
247: + "b : ID;\n");
248: String expecting = " ( 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> )";
249: String found = g.getRule("a").tree.toStringTree();
250: assertEquals(expecting, found);
251: }
252:
253: public void testRootTokenInStarLoop() throws Exception {
254: Grammar g = new Grammar("grammar Expr;\n"
255: + "options { backtrack=true; }\n" + "a : ('*'^)* ;\n"); // bug: the synpred had nothing in it
256: String expecting = " ( rule synpred1 ARG RET scope ( BLOCK ( ALT '*' <end-of-alt> ) <end-of-block> ) <end-of-rule> )";
257: String found = g.getRule("synpred1").tree.toStringTree();
258: assertEquals(expecting, found);
259: }
260:
261: public void testActionInStarLoop() throws Exception {
262: Grammar g = new Grammar("grammar Expr;\n"
263: + "options { backtrack=true; }\n"
264: + "a : ({blort} 'x')* ;\n"); // bug: the synpred had nothing in it
265: String expecting = " ( rule synpred1 ARG RET scope ( BLOCK ( ALT blort 'x' <end-of-alt> ) <end-of-block> ) <end-of-rule> )";
266: String found = g.getRule("synpred1").tree.toStringTree();
267: assertEquals(expecting, found);
268: }
269:
270: }
|