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.analysis.State;
031: import org.antlr.tool.FASerializer;
032: import org.antlr.tool.Grammar;
033:
034: public class TestNFAConstruction extends BaseTest {
035:
036: /** Public default constructor used by TestRig */
037: public TestNFAConstruction() {
038: }
039:
040: public void testA() throws Exception {
041: Grammar g = new Grammar("parser grammar P;\n" + "a : A;");
042: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2-A->.s3\n"
043: + ".s3->:s4\n" + ":s4-EOF->.s5\n";
044: checkRule(g, "a", expecting);
045: }
046:
047: public void testAB() throws Exception {
048: Grammar g = new Grammar("parser grammar P;\n" + "a : A B ;");
049: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2-A->.s3\n"
050: + ".s3-B->.s4\n" + ".s4->:s5\n" + ":s5-EOF->.s6\n";
051: checkRule(g, "a", expecting);
052: }
053:
054: public void testAorB() throws Exception {
055: Grammar g = new Grammar("parser grammar P;\n"
056: + "a : A | B {;} ;");
057: /* expecting (0)--Ep-->(1)--Ep-->(2)--A-->(3)--Ep-->(4)--Ep-->(5,end)
058: | ^
059: (6)--Ep-->(7)--B-->(8)--------|
060: */
061: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s7\n"
062: + ".s2-A->.s3\n" + ".s3->.s4\n" + ".s4->:s5\n"
063: + ".s7->.s8\n" + ".s8-B->.s9\n" + ".s9->.s4\n"
064: + ":s5-EOF->.s6\n";
065: checkRule(g, "a", expecting);
066: }
067:
068: public void testRangeOrRange() throws Exception {
069: Grammar g = new Grammar("lexer grammar P;\n"
070: + "A : ('a'..'c' 'h' | 'q' 'j'..'l') ;");
071: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
072: + ".s10-'q'->.s11\n" + ".s11-'j'..'l'->.s12\n"
073: + ".s12->.s6\n" + ".s2->.s3\n" + ".s2->.s9\n"
074: + ".s3-'a'..'c'->.s4\n" + ".s4-'h'->.s5\n"
075: + ".s5->.s6\n" + ".s6->:s7\n" + ".s9->.s10\n"
076: + ":s7-<EOT>->.s8\n";
077: checkRule(g, "A", expecting);
078: }
079:
080: public void testRange() throws Exception {
081: Grammar g = new Grammar("lexer grammar P;\n" + "A : 'a'..'c' ;");
082: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
083: + ".s2-'a'..'c'->.s3\n" + ".s3->:s4\n"
084: + ":s4-<EOT>->.s5\n";
085: checkRule(g, "A", expecting);
086: }
087:
088: public void testCharSetInParser() throws Exception {
089: Grammar g = new Grammar("grammar P;\n" + "a : A|'b' ;");
090: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
091: + ".s2-A..'b'->.s3\n" + ".s3->:s4\n" + ":s4-EOF->.s5\n";
092: checkRule(g, "a", expecting);
093: }
094:
095: public void testABorCD() throws Exception {
096: Grammar g = new Grammar("parser grammar P;\n"
097: + "a : A B | C D;");
098: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s8\n"
099: + ".s10-D->.s11\n" + ".s11->.s5\n" + ".s2-A->.s3\n"
100: + ".s3-B->.s4\n" + ".s4->.s5\n" + ".s5->:s6\n"
101: + ".s8->.s9\n" + ".s9-C->.s10\n" + ":s6-EOF->.s7\n";
102: checkRule(g, "a", expecting);
103: }
104:
105: public void testbA() throws Exception {
106: Grammar g = new Grammar("parser grammar P;\n" + "a : b A ;\n"
107: + "b : B ;");
108: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
109: + ".s3->.s4\n" + ".s4->.s5\n" + ".s5-B->.s6\n"
110: + ".s6->:s7\n" + ".s8-A->.s9\n" + ".s9->:s10\n"
111: + ":s10-EOF->.s11\n" + ":s7->.s8\n";
112: checkRule(g, "a", expecting);
113: }
114:
115: public void testbA_bC() throws Exception {
116: Grammar g = new Grammar("parser grammar P;\n" + "a : b A ;\n"
117: + "b : B ;\n" + "c : b C;");
118: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s12->.s13\n"
119: + ".s13-C->.s14\n" + ".s14->:s15\n" + ".s2->.s3\n"
120: + ".s3->.s4\n" + ".s4->.s5\n" + ".s5-B->.s6\n"
121: + ".s6->:s7\n" + ".s8-A->.s9\n" + ".s9->:s10\n"
122: + ":s10-EOF->.s11\n" + ":s15-EOF->.s16\n"
123: + ":s7->.s12\n" + ":s7->.s8\n";
124: checkRule(g, "a", expecting);
125: }
126:
127: public void testAorEpsilon() throws Exception {
128: Grammar g = new Grammar("parser grammar P;\n" + "a : A | ;");
129: /* expecting (0)--Ep-->(1)--Ep-->(2)--A-->(3)--Ep-->(4)--Ep-->(5,end)
130: | ^
131: (6)--Ep-->(7)--Ep-->(8)-------|
132: */
133: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s7\n"
134: + ".s2-A->.s3\n" + ".s3->.s4\n" + ".s4->:s5\n"
135: + ".s7->.s8\n" + ".s8->.s9\n" + ".s9->.s4\n"
136: + ":s5-EOF->.s6\n";
137: checkRule(g, "a", expecting);
138: }
139:
140: public void testAOptional() throws Exception {
141: Grammar g = new Grammar("parser grammar P;\n" + "a : (A)?;");
142: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
143: + ".s2->.s8\n" + ".s3-A->.s4\n" + ".s4->.s5\n"
144: + ".s5->:s6\n" + ".s8->.s5\n" + ":s6-EOF->.s7\n";
145: checkRule(g, "a", expecting);
146: }
147:
148: public void testNakedAoptional() throws Exception {
149: Grammar g = new Grammar("parser grammar P;\n" + "a : A?;");
150: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
151: + ".s2->.s8\n" + ".s3-A->.s4\n" + ".s4->.s5\n"
152: + ".s5->:s6\n" + ".s8->.s5\n" + ":s6-EOF->.s7\n";
153: checkRule(g, "a", expecting);
154: }
155:
156: public void testAorBthenC() throws Exception {
157: Grammar g = new Grammar("parser grammar P;\n"
158: + "a : (A | B) C;");
159: /* expecting
160:
161: (0)--Ep-->(1)--Ep-->(2)--A-->(3)--Ep-->(4)--Ep-->(5)--C-->(6)--Ep-->(7,end)
162: | ^
163: (8)--Ep-->(9)--B-->(10)-------|
164: */
165: }
166:
167: public void testAplus() throws Exception {
168: Grammar g = new Grammar("parser grammar P;\n" + "a : (A)+;");
169: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
170: + ".s3->.s4\n" + ".s4-A->.s5\n" + ".s5->.s3\n"
171: + ".s5->.s6\n" + ".s6->:s7\n" + ":s7-EOF->.s8\n";
172: checkRule(g, "a", expecting);
173: }
174:
175: public void testNakedAplus() throws Exception {
176: Grammar g = new Grammar("parser grammar P;\n" + "a : A+;");
177: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
178: + ".s3->.s4\n" + ".s4-A->.s5\n" + ".s5->.s3\n"
179: + ".s5->.s6\n" + ".s6->:s7\n" + ":s7-EOF->.s8\n";
180: checkRule(g, "a", expecting);
181: }
182:
183: public void testAplusNonGreedy() throws Exception {
184: Grammar g = new Grammar("lexer grammar t;\n"
185: + "A : (options {greedy=false;}:'0'..'9')+ ;\n");
186: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
187: + ".s3->.s4\n" + ".s4-'0'..'9'->.s5\n" + ".s5->.s3\n"
188: + ".s5->.s6\n" + ".s6->:s7\n" + ":s7-<EOT>->.s8\n";
189: checkRule(g, "A", expecting);
190: }
191:
192: public void testAorBplus() throws Exception {
193: Grammar g = new Grammar("parser grammar P;\n"
194: + "a : (A | B{action})+ ;");
195: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->.s11\n"
196: + ".s11-B->.s12\n" + ".s12->.s6\n" + ".s2->.s3\n"
197: + ".s3->.s10\n" + ".s3->.s4\n" + ".s4-A->.s5\n"
198: + ".s5->.s6\n" + ".s6->.s3\n" + ".s6->.s7\n"
199: + ".s7->:s8\n" + ":s8-EOF->.s9\n";
200: checkRule(g, "a", expecting);
201: }
202:
203: public void testAorBorEmptyPlus() throws Exception {
204: Grammar g = new Grammar("parser grammar P;\n"
205: + "a : (A | B | )+ ;");
206: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->.s11\n"
207: + ".s10->.s13\n" + ".s11-B->.s12\n" + ".s12->.s6\n"
208: + ".s13->.s14\n" + ".s14->.s15\n" + ".s15->.s6\n"
209: + ".s2->.s3\n" + ".s3->.s10\n" + ".s3->.s4\n"
210: + ".s4-A->.s5\n" + ".s5->.s6\n" + ".s6->.s3\n"
211: + ".s6->.s7\n" + ".s7->:s8\n" + ":s8-EOF->.s9\n";
212: checkRule(g, "a", expecting);
213: }
214:
215: public void testAStar() throws Exception {
216: Grammar g = new Grammar("parser grammar P;\n" + "a : (A)*;");
217: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
218: + ".s2->.s9\n" + ".s3->.s4\n" + ".s4-A->.s5\n"
219: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
220: + ".s9->.s6\n" + ":s7-EOF->.s8\n";
221: checkRule(g, "a", expecting);
222: }
223:
224: public void testNestedAstar() throws Exception {
225: Grammar g = new Grammar("parser grammar P;\n" + "a : (A*)*;");
226: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->:s11\n"
227: + ".s13->.s8\n" + ".s14->.s10\n" + ".s2->.s14\n"
228: + ".s2->.s3\n" + ".s3->.s4\n" + ".s4->.s13\n"
229: + ".s4->.s5\n" + ".s5->.s6\n" + ".s6-A->.s7\n"
230: + ".s7->.s5\n" + ".s7->.s8\n" + ".s8->.s9\n"
231: + ".s9->.s10\n" + ".s9->.s3\n" + ":s11-EOF->.s12\n";
232: checkRule(g, "a", expecting);
233: }
234:
235: public void testPlusNestedInStar() throws Exception {
236: Grammar g = new Grammar("parser grammar P;\n" + "a : (A+)*;");
237: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->:s11\n"
238: + ".s13->.s10\n" + ".s2->.s13\n" + ".s2->.s3\n"
239: + ".s3->.s4\n" + ".s4->.s5\n" + ".s5->.s6\n"
240: + ".s6-A->.s7\n" + ".s7->.s5\n" + ".s7->.s8\n"
241: + ".s8->.s9\n" + ".s9->.s10\n" + ".s9->.s3\n"
242: + ":s11-EOF->.s12\n";
243: checkRule(g, "a", expecting);
244: }
245:
246: public void testStarNestedInPlus() throws Exception {
247: Grammar g = new Grammar("parser grammar P;\n" + "a : (A*)+;");
248: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->:s11\n"
249: + ".s13->.s8\n" + ".s2->.s3\n" + ".s3->.s4\n"
250: + ".s4->.s13\n" + ".s4->.s5\n" + ".s5->.s6\n"
251: + ".s6-A->.s7\n" + ".s7->.s5\n" + ".s7->.s8\n"
252: + ".s8->.s9\n" + ".s9->.s10\n" + ".s9->.s3\n"
253: + ":s11-EOF->.s12\n";
254: checkRule(g, "a", expecting);
255: }
256:
257: public void testNakedAstar() throws Exception {
258: Grammar g = new Grammar("parser grammar P;\n" + "a : A*;");
259: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
260: + ".s2->.s9\n" + ".s3->.s4\n" + ".s4-A->.s5\n"
261: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
262: + ".s9->.s6\n" + ":s7-EOF->.s8\n";
263: checkRule(g, "a", expecting);
264: }
265:
266: public void testAorBstar() throws Exception {
267: Grammar g = new Grammar("parser grammar P;\n"
268: + "a : (A | B{action})* ;");
269: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->.s11\n"
270: + ".s11-B->.s12\n" + ".s12->.s6\n" + ".s13->.s7\n"
271: + ".s2->.s13\n" + ".s2->.s3\n" + ".s3->.s10\n"
272: + ".s3->.s4\n" + ".s4-A->.s5\n" + ".s5->.s6\n"
273: + ".s6->.s3\n" + ".s6->.s7\n" + ".s7->:s8\n"
274: + ":s8-EOF->.s9\n";
275: checkRule(g, "a", expecting);
276: }
277:
278: public void testAorBOptionalSubrule() throws Exception {
279: Grammar g = new Grammar("parser grammar P;\n"
280: + "a : ( A | B )? ;");
281: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
282: + ".s2->.s8\n" + ".s3-A..B->.s4\n" + ".s4->.s5\n"
283: + ".s5->:s6\n" + ".s8->.s5\n" + ":s6-EOF->.s7\n";
284: checkRule(g, "a", expecting);
285: }
286:
287: public void testPredicatedAorB() throws Exception {
288: Grammar g = new Grammar("parser grammar P;\n"
289: + "a : {p1}? A | {p2}? B ;");
290: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s8\n"
291: + ".s10-B->.s11\n" + ".s11->.s5\n" + ".s2-{p1}?->.s3\n"
292: + ".s3-A->.s4\n" + ".s4->.s5\n" + ".s5->:s6\n"
293: + ".s8->.s9\n" + ".s9-{p2}?->.s10\n" + ":s6-EOF->.s7\n";
294: checkRule(g, "a", expecting);
295: }
296:
297: public void testMultiplePredicates() throws Exception {
298: Grammar g = new Grammar("parser grammar P;\n"
299: + "a : {p1}? {p1a}? A | {p2}? B | {p3} b;\n"
300: + "b : {p4}? B ;");
301: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s9\n"
302: + ".s10-{p2}?->.s11\n" + ".s11-B->.s12\n"
303: + ".s12->.s6\n" + ".s13->.s14\n" + ".s14->.s15\n"
304: + ".s15->.s16\n" + ".s16->.s17\n"
305: + ".s17-{p4}?->.s18\n" + ".s18-B->.s19\n"
306: + ".s19->:s20\n" + ".s2-{p1}?->.s3\n" + ".s21->.s6\n"
307: + ".s3-{p1a}?->.s4\n" + ".s4-A->.s5\n" + ".s5->.s6\n"
308: + ".s6->:s7\n" + ".s9->.s10\n" + ".s9->.s13\n"
309: + ":s20->.s21\n" + ":s7-EOF->.s8\n";
310: checkRule(g, "a", expecting);
311: }
312:
313: public void testSets() throws Exception {
314: Grammar g = new Grammar("parser grammar P;\n"
315: + "a : ( A | B )+ ;\n" + "b : ( A | B{;} )+ ;\n"
316: + "c : (A|B) (A|B) ;\n" + "d : ( A | B )* ;\n"
317: + "e : ( A | B )? ;");
318: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
319: + ".s3->.s4\n" + ".s4-A..B->.s5\n" + ".s5->.s3\n"
320: + ".s5->.s6\n" + ".s6->:s7\n" + ":s7-EOF->.s8\n";
321: checkRule(g, "a", expecting);
322: expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->.s11\n"
323: + ".s11-B->.s12\n" + ".s12->.s6\n" + ".s2->.s3\n"
324: + ".s3->.s10\n" + ".s3->.s4\n" + ".s4-A->.s5\n"
325: + ".s5->.s6\n" + ".s6->.s3\n" + ".s6->.s7\n"
326: + ".s7->:s8\n" + ":s8-EOF->.s9\n";
327: checkRule(g, "b", expecting);
328: expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2-A..B->.s3\n"
329: + ".s3-A..B->.s4\n" + ".s4->:s5\n" + ":s5-EOF->.s6\n";
330: checkRule(g, "c", expecting);
331: expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
332: + ".s2->.s9\n" + ".s3->.s4\n" + ".s4-A..B->.s5\n"
333: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
334: + ".s9->.s6\n" + ":s7-EOF->.s8\n";
335: checkRule(g, "d", expecting);
336: expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
337: + ".s2->.s8\n" + ".s3-A..B->.s4\n" + ".s4->.s5\n"
338: + ".s5->:s6\n" + ".s8->.s5\n" + ":s6-EOF->.s7\n";
339: checkRule(g, "e", expecting);
340: }
341:
342: public void testNotSet() throws Exception {
343: Grammar g = new Grammar("parser grammar P;\n"
344: + "tokens { A; B; C; }\n" + "a : ~A ;\n");
345: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
346: + ".s2-B..C->.s3\n" + ".s3->:s4\n" + ":s4-EOF->.s5\n";
347: checkRule(g, "a", expecting);
348:
349: String expectingGrammarStr = "1:8: parser grammar P;\n"
350: + "a : ~ A ;";
351: assertEquals(expectingGrammarStr, g.toString());
352: }
353:
354: public void testNotSingletonBlockSet() throws Exception {
355: Grammar g = new Grammar("parser grammar P;\n"
356: + "tokens { A; B; C; }\n" + "a : ~(A) ;\n");
357: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
358: + ".s2-B..C->.s3\n" + ".s3->:s4\n" + ":s4-EOF->.s5\n";
359: checkRule(g, "a", expecting);
360:
361: String expectingGrammarStr = "1:8: parser grammar P;\n"
362: + "a : ~ ( A ) ;";
363: assertEquals(expectingGrammarStr, g.toString());
364: }
365:
366: public void testNotCharSet() throws Exception {
367: Grammar g = new Grammar("lexer grammar P;\n" + "A : ~'3' ;\n");
368: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
369: + ".s2-{'\\u0000'..'2', '4'..'\\uFFFE'}->.s3\n"
370: + ".s3->:s4\n" + ":s4-<EOT>->.s5\n";
371: checkRule(g, "A", expecting);
372:
373: String expectingGrammarStr = "1:7: lexer grammar P;\n"
374: + "A : ~ '3' ;\n" + "Tokens : A ;";
375: assertEquals(expectingGrammarStr, g.toString());
376: }
377:
378: public void testNotBlockSet() throws Exception {
379: Grammar g = new Grammar("lexer grammar P;\n"
380: + "A : ~('3'|'b') ;\n");
381: String expecting = ".s0->.s1\n"
382: + ".s1->.s2\n"
383: + ".s2-{'\\u0000'..'2', '4'..'a', 'c'..'\\uFFFE'}->.s3\n"
384: + ".s3->:s4\n" + ":s4-<EOT>->.s5\n";
385: checkRule(g, "A", expecting);
386:
387: String expectingGrammarStr = "1:7: lexer grammar P;\n"
388: + "A : ~ ( '3' | 'b' ) ;\n" + "Tokens : A ;";
389: assertEquals(expectingGrammarStr, g.toString());
390: }
391:
392: public void testNotSetLoop() throws Exception {
393: Grammar g = new Grammar("lexer grammar P;\n"
394: + "A : ~('3')* ;\n");
395: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
396: + ".s2->.s9\n" + ".s3->.s4\n"
397: + ".s4-{'\\u0000'..'2', '4'..'\\uFFFE'}->.s5\n"
398: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
399: + ".s9->.s6\n" + ":s7-<EOT>->.s8\n";
400: checkRule(g, "A", expecting);
401:
402: String expectingGrammarStr = "1:7: lexer grammar P;\n"
403: + "A : (~ ( '3' ) )* ;\n" + "Tokens : A ;";
404: assertEquals(expectingGrammarStr, g.toString());
405: }
406:
407: public void testNotBlockSetLoop() throws Exception {
408: Grammar g = new Grammar("lexer grammar P;\n"
409: + "A : ~('3'|'b')* ;\n");
410: String expecting = ".s0->.s1\n"
411: + ".s1->.s2\n"
412: + ".s2->.s3\n"
413: + ".s2->.s9\n"
414: + ".s3->.s4\n"
415: + ".s4-{'\\u0000'..'2', '4'..'a', 'c'..'\\uFFFE'}->.s5\n"
416: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
417: + ".s9->.s6\n" + ":s7-<EOT>->.s8\n";
418: checkRule(g, "A", expecting);
419:
420: String expectingGrammarStr = "1:7: lexer grammar P;\n"
421: + "A : (~ ( '3' | 'b' ) )* ;\n" + "Tokens : A ;";
422: assertEquals(expectingGrammarStr, g.toString());
423: }
424:
425: public void testSetsInCombinedGrammarSentToLexer() throws Exception {
426: // not sure this belongs in this test suite, but whatever.
427: Grammar g = new Grammar("grammar t;\n"
428: + "A : '{' ~('}')* '}';\n");
429: String result = g.getLexerGrammar();
430: String expecting = "lexer grammar t;\n" + "\n"
431: + "// $ANTLR src \"<string>\" 2\n"
432: + "A : '{' ~('}')* '}';\n";
433: assertEquals(result, expecting);
434: }
435:
436: public void testLabeledNotSet() throws Exception {
437: Grammar g = new Grammar("parser grammar P;\n"
438: + "tokens { A; B; C; }\n" + "a : t=~A ;\n");
439: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
440: + ".s2-B..C->.s3\n" + ".s3->:s4\n" + ":s4-EOF->.s5\n";
441: checkRule(g, "a", expecting);
442:
443: String expectingGrammarStr = "1:8: parser grammar P;\n"
444: + "a : t=~ A ;";
445: assertEquals(expectingGrammarStr, g.toString());
446: }
447:
448: public void testLabeledNotCharSet() throws Exception {
449: Grammar g = new Grammar("lexer grammar P;\n" + "A : t=~'3' ;\n");
450: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
451: + ".s2-{'\\u0000'..'2', '4'..'\\uFFFE'}->.s3\n"
452: + ".s3->:s4\n" + ":s4-<EOT>->.s5\n";
453: checkRule(g, "A", expecting);
454:
455: String expectingGrammarStr = "1:7: lexer grammar P;\n"
456: + "A : t=~ '3' ;\n" + "Tokens : A ;";
457: assertEquals(expectingGrammarStr, g.toString());
458: }
459:
460: public void testLabeledNotBlockSet() throws Exception {
461: Grammar g = new Grammar("lexer grammar P;\n"
462: + "A : t=~('3'|'b') ;\n");
463: String expecting = ".s0->.s1\n"
464: + ".s1->.s2\n"
465: + ".s2-{'\\u0000'..'2', '4'..'a', 'c'..'\\uFFFE'}->.s3\n"
466: + ".s3->:s4\n" + ":s4-<EOT>->.s5\n";
467: checkRule(g, "A", expecting);
468:
469: String expectingGrammarStr = "1:7: lexer grammar P;\n"
470: + "A : t=~ ( '3' | 'b' ) ;\n" + "Tokens : A ;";
471: assertEquals(expectingGrammarStr, g.toString());
472: }
473:
474: public void testEscapedCharLiteral() throws Exception {
475: Grammar g = new Grammar("grammar P;\n" + "a : '\\n';");
476: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
477: + ".s2-'\\n'->.s3\n" + ".s3->:s4\n" + ":s4-EOF->.s5\n";
478: checkRule(g, "a", expecting);
479: }
480:
481: public void testEscapedStringLiteral() throws Exception {
482: Grammar g = new Grammar("grammar P;\n"
483: + "a : 'a\\nb\\u0030c\\'';");
484: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
485: + ".s2-'a\\nb\\u0030c\\''->.s3\n" + ".s3->:s4\n"
486: + ":s4-EOF->.s5\n";
487: checkRule(g, "a", expecting);
488: }
489:
490: // AUTO BACKTRACKING STUFF
491:
492: public void testAutoBacktracking_RuleBlock() throws Exception {
493: Grammar g = new Grammar("grammar t;\n"
494: + "options {backtrack=true;}\n" + "a : 'a'{;}|'b';");
495: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s8\n"
496: + ".s10->.s5\n" + ".s2-{synpred1}?->.s3\n"
497: + ".s3-'a'->.s4\n" + ".s4->.s5\n" + ".s5->:s6\n"
498: + ".s8->.s9\n" + ".s9-'b'->.s10\n" + ":s6-EOF->.s7\n";
499: checkRule(g, "a", expecting);
500: }
501:
502: public void testAutoBacktracking_RuleSetBlock() throws Exception {
503: Grammar g = new Grammar("grammar t;\n"
504: + "options {backtrack=true;}\n" + "a : 'a'|'b';");
505: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
506: + ".s2-'a'..'b'->.s3\n" + ".s3->:s4\n"
507: + ":s4-EOF->.s5\n";
508: checkRule(g, "a", expecting);
509: }
510:
511: public void testAutoBacktracking_SimpleBlock() throws Exception {
512: Grammar g = new Grammar("grammar t;\n"
513: + "options {backtrack=true;}\n" + "a : ('a'{;}|'b') ;");
514: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
515: + ".s10-'b'->.s11\n" + ".s11->.s6\n" + ".s2->.s3\n"
516: + ".s2->.s9\n" + ".s3-{synpred1}?->.s4\n"
517: + ".s4-'a'->.s5\n" + ".s5->.s6\n" + ".s6->:s7\n"
518: + ".s9->.s10\n" + ":s7-EOF->.s8\n";
519: checkRule(g, "a", expecting);
520: }
521:
522: public void testAutoBacktracking_SetBlock() throws Exception {
523: Grammar g = new Grammar("grammar t;\n"
524: + "options {backtrack=true;}\n" + "a : ('a'|'b') ;");
525: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
526: + ".s2-'a'..'b'->.s3\n" + ".s3->:s4\n"
527: + ":s4-EOF->.s5\n";
528: checkRule(g, "a", expecting);
529: }
530:
531: public void testAutoBacktracking_StarBlock() throws Exception {
532: Grammar g = new Grammar("grammar t;\n"
533: + "options {backtrack=true;}\n" + "a : ('a'{;}|'b')* ;");
534: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s11->.s12\n"
535: + ".s12-{synpred2}?->.s13\n" + ".s13-'b'->.s14\n"
536: + ".s14->.s7\n" + ".s15->.s8\n" + ".s2->.s15\n"
537: + ".s2->.s3\n" + ".s3->.s11\n" + ".s3->.s4\n"
538: + ".s4-{synpred1}?->.s5\n" + ".s5-'a'->.s6\n"
539: + ".s6->.s7\n" + ".s7->.s3\n" + ".s7->.s8\n"
540: + ".s8->:s9\n" + ":s9-EOF->.s10\n";
541: checkRule(g, "a", expecting);
542: }
543:
544: public void testAutoBacktracking_StarSetBlock_IgnoresPreds()
545: throws Exception {
546: Grammar g = new Grammar("grammar t;\n"
547: + "options {backtrack=true;}\n" + "a : ('a'|'b')* ;");
548: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
549: + ".s2->.s9\n" + ".s3->.s4\n" + ".s4-'a'..'b'->.s5\n"
550: + ".s5->.s3\n" + ".s5->.s6\n" + ".s6->:s7\n"
551: + ".s9->.s6\n" + ":s7-EOF->.s8\n";
552: checkRule(g, "a", expecting);
553: }
554:
555: public void testAutoBacktracking_StarSetBlock() throws Exception {
556: Grammar g = new Grammar("grammar t;\n"
557: + "options {backtrack=true;}\n" + "a : ('a'|'b'{;})* ;");
558: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s11->.s12\n"
559: + ".s12-{synpred2}?->.s13\n" + ".s13-'b'->.s14\n"
560: + ".s14->.s7\n" + ".s15->.s8\n" + ".s2->.s15\n"
561: + ".s2->.s3\n" + ".s3->.s11\n" + ".s3->.s4\n"
562: + ".s4-{synpred1}?->.s5\n" + ".s5-'a'->.s6\n"
563: + ".s6->.s7\n" + ".s7->.s3\n" + ".s7->.s8\n"
564: + ".s8->:s9\n" + ":s9-EOF->.s10\n";
565: checkRule(g, "a", expecting);
566: }
567:
568: public void testAutoBacktracking_StarBlock1Alt() throws Exception {
569: Grammar g = new Grammar("grammar t;\n"
570: + "options {backtrack=true;}\n" + "a : ('a')* ;");
571: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s10->.s7\n"
572: + ".s2->.s10\n" + ".s2->.s3\n" + ".s3->.s4\n"
573: + ".s4-{synpred1}?->.s5\n" + ".s5-'a'->.s6\n"
574: + ".s6->.s3\n" + ".s6->.s7\n" + ".s7->:s8\n"
575: + ":s8-EOF->.s9\n";
576: checkRule(g, "a", expecting);
577: }
578:
579: public void testAutoBacktracking_PlusBlock() throws Exception {
580: Grammar g = new Grammar("grammar t;\n"
581: + "options {backtrack=true;}\n" + "a : ('a'{;}|'b')+ ;");
582: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s11->.s12\n"
583: + ".s12-{synpred2}?->.s13\n" + ".s13-'b'->.s14\n"
584: + ".s14->.s7\n" + ".s2->.s3\n" + ".s3->.s11\n"
585: + ".s3->.s4\n" + ".s4-{synpred1}?->.s5\n"
586: + ".s5-'a'->.s6\n" + ".s6->.s7\n" + ".s7->.s3\n"
587: + ".s7->.s8\n" + ".s8->:s9\n" + ":s9-EOF->.s10\n";
588: checkRule(g, "a", expecting);
589: }
590:
591: public void testAutoBacktracking_PlusSetBlock() throws Exception {
592: Grammar g = new Grammar("grammar t;\n"
593: + "options {backtrack=true;}\n" + "a : ('a'|'b'{;})+ ;");
594: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s11->.s12\n"
595: + ".s12-{synpred2}?->.s13\n" + ".s13-'b'->.s14\n"
596: + ".s14->.s7\n" + ".s2->.s3\n" + ".s3->.s11\n"
597: + ".s3->.s4\n" + ".s4-{synpred1}?->.s5\n"
598: + ".s5-'a'->.s6\n" + ".s6->.s7\n" + ".s7->.s3\n"
599: + ".s7->.s8\n" + ".s8->:s9\n" + ":s9-EOF->.s10\n";
600: checkRule(g, "a", expecting);
601: }
602:
603: public void testAutoBacktracking_PlusBlock1Alt() throws Exception {
604: Grammar g = new Grammar("grammar t;\n"
605: + "options {backtrack=true;}\n" + "a : ('a')+ ;");
606: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
607: + ".s3->.s4\n" + ".s4-{synpred1}?->.s5\n"
608: + ".s5-'a'->.s6\n" + ".s6->.s3\n" + ".s6->.s7\n"
609: + ".s7->:s8\n" + ":s8-EOF->.s9\n";
610: checkRule(g, "a", expecting);
611: }
612:
613: public void testAutoBacktracking_OptionalBlock2Alts()
614: throws Exception {
615: Grammar g = new Grammar("grammar t;\n"
616: + "options {backtrack=true;}\n" + "a : ('a'{;}|'b')?;");
617: String expecting = ".s0->.s1\n" + ".s1->.s2\n"
618: + ".s10-{synpred2}?->.s11\n" + ".s11-'b'->.s12\n"
619: + ".s12->.s6\n" + ".s13->.s6\n" + ".s2->.s3\n"
620: + ".s2->.s9\n" + ".s3-{synpred1}?->.s4\n"
621: + ".s4-'a'->.s5\n" + ".s5->.s6\n" + ".s6->:s7\n"
622: + ".s9->.s10\n" + ".s9->.s13\n" + ":s7-EOF->.s8\n";
623: checkRule(g, "a", expecting);
624: }
625:
626: public void testAutoBacktracking_OptionalBlock1Alt()
627: throws Exception {
628: Grammar g = new Grammar("grammar t;\n"
629: + "options {backtrack=true;}\n" + "a : ('a')?;");
630: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s2->.s3\n"
631: + ".s2->.s9\n" + ".s3-{synpred1}?->.s4\n"
632: + ".s4-'a'->.s5\n" + ".s5->.s6\n" + ".s6->:s7\n"
633: + ".s9->.s6\n" + ":s7-EOF->.s8\n";
634: checkRule(g, "a", expecting);
635: }
636:
637: public void testAutoBacktracking_ExistingPred() throws Exception {
638: Grammar g = new Grammar("grammar t;\n"
639: + "options {backtrack=true;}\n"
640: + "a : ('a')=> 'a' | 'b';");
641: String expecting = ".s0->.s1\n" + ".s1->.s2\n" + ".s1->.s8\n"
642: + ".s10->.s5\n" + ".s2-{synpred1}?->.s3\n"
643: + ".s3-'a'->.s4\n" + ".s4->.s5\n" + ".s5->:s6\n"
644: + ".s8->.s9\n" + ".s9-'b'->.s10\n" + ":s6-EOF->.s7\n";
645: checkRule(g, "a", expecting);
646: }
647:
648: private void checkRule(Grammar g, String rule, String expecting) {
649: g.createNFAs();
650: State startState = g.getRuleStartState(rule);
651: FASerializer serializer = new FASerializer(g);
652: String result = serializer.serialize(startState);
653:
654: //System.out.print(result);
655: assertEquals(expecting, result);
656: }
657:
658: }
|