TestSets.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
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 */
28package org.antlr.test;
29
30import org.junit.Test;
31
32/** Test the set stuff in lexer and parser */
33public class TestSets extends BaseTest {
34	protected boolean debug = false;
35
36	/** Public default constructor used by TestRig */
37	public TestSets() {
38	}
39
40	@Test public void testSeqDoesNotBecomeSet() throws Exception {
41		// this must return A not I to the parser; calling a nonfragment rule
42		// from a nonfragment rule does not set the overall token.
43		String grammar =
44			"grammar P;\n" +
45			"a : C {System.out.println(input);} ;\n" +
46			"fragment A : '1' | '2';\n" +
47			"fragment B : '3' '4';\n" +
48			"C : A | B;\n";
49		String found = execParser("P.g", grammar, "PParser", "PLexer",
50								  "a", "34", debug);
51		assertEquals("34\n", found);
52	}
53
54	@Test public void testParserSet() throws Exception {
55		String grammar =
56			"grammar T;\n" +
57			"a : t=('x'|'y') {System.out.println($t.text);} ;\n";
58		String found = execParser("T.g", grammar, "TParser", "TLexer",
59								  "a", "x", debug);
60		assertEquals("x\n", found);
61	}
62
63	@Test public void testParserNotSet() throws Exception {
64		String grammar =
65			"grammar T;\n" +
66			"a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;\n";
67		String found = execParser("T.g", grammar, "TParser", "TLexer",
68								  "a", "zz", debug);
69		assertEquals("z\n", found);
70	}
71
72	@Test public void testParserNotToken() throws Exception {
73		String grammar =
74			"grammar T;\n" +
75			"a : ~'x' 'z' {System.out.println(input);} ;\n";
76		String found = execParser("T.g", grammar, "TParser", "TLexer",
77								  "a", "zz", debug);
78		assertEquals("zz\n", found);
79	}
80
81	@Test public void testParserNotTokenWithLabel() throws Exception {
82		String grammar =
83			"grammar T;\n" +
84			"a : t=~'x' 'z' {System.out.println($t.text);} ;\n";
85		String found = execParser("T.g", grammar, "TParser", "TLexer",
86								  "a", "zz", debug);
87		assertEquals("z\n", found);
88	}
89
90	@Test public void testRuleAsSet() throws Exception {
91		String grammar =
92			"grammar T;\n" +
93			"a @after {System.out.println(input);} : 'a' | 'b' |'c' ;\n";
94		String found = execParser("T.g", grammar, "TParser", "TLexer",
95								  "a", "b", debug);
96		assertEquals("b\n", found);
97	}
98
99	@Test public void testRuleAsSetAST() throws Exception {
100		String grammar =
101			"grammar T;\n" +
102			"options {output=AST;}\n" +
103			"a : 'a' | 'b' |'c' ;\n";
104		String found = execParser("T.g", grammar, "TParser", "TLexer",
105								  "a", "b", debug);
106		assertEquals("b\n", found);
107	}
108
109	@Test public void testNotChar() throws Exception {
110		String grammar =
111			"grammar T;\n" +
112			"a : A {System.out.println($A.text);} ;\n" +
113			"A : ~'b' ;\n";
114		String found = execParser("T.g", grammar, "TParser", "TLexer",
115								  "a", "x", debug);
116		assertEquals("x\n", found);
117	}
118
119	@Test public void testOptionalSingleElement() throws Exception {
120		String grammar =
121			"grammar T;\n" +
122			"a : A? 'c' {System.out.println(input);} ;\n" +
123			"A : 'b' ;\n";
124		String found = execParser("T.g", grammar, "TParser", "TLexer",
125								  "a", "bc", debug);
126		assertEquals("bc\n", found);
127	}
128
129	@Test public void testOptionalLexerSingleElement() throws Exception {
130		String grammar =
131			"grammar T;\n" +
132			"a : A {System.out.println(input);} ;\n" +
133			"A : 'b'? 'c' ;\n";
134		String found = execParser("T.g", grammar, "TParser", "TLexer",
135								  "a", "bc", debug);
136		assertEquals("bc\n", found);
137	}
138
139	@Test public void testStarLexerSingleElement() throws Exception {
140		String grammar =
141			"grammar T;\n" +
142			"a : A {System.out.println(input);} ;\n" +
143			"A : 'b'* 'c' ;\n";
144		String found = execParser("T.g", grammar, "TParser", "TLexer",
145								  "a", "bbbbc", debug);
146		assertEquals("bbbbc\n", found);
147		found = execParser("T.g", grammar, "TParser", "TLexer",
148								  "a", "c", debug);
149		assertEquals("c\n", found);
150	}
151
152	@Test public void testPlusLexerSingleElement() throws Exception {
153		String grammar =
154			"grammar T;\n" +
155			"a : A {System.out.println(input);} ;\n" +
156			"A : 'b'+ 'c' ;\n";
157		String found = execParser("T.g", grammar, "TParser", "TLexer",
158								  "a", "bbbbc", debug);
159		assertEquals("bbbbc\n", found);
160	}
161
162	@Test public void testOptionalSet() throws Exception {
163		String grammar =
164			"grammar T;\n" +
165			"a : ('a'|'b')? 'c' {System.out.println(input);} ;\n";
166		String found = execParser("T.g", grammar, "TParser", "TLexer",
167								  "a", "ac", debug);
168		assertEquals("ac\n", found);
169	}
170
171	@Test public void testStarSet() throws Exception {
172		String grammar =
173			"grammar T;\n" +
174			"a : ('a'|'b')* 'c' {System.out.println(input);} ;\n";
175		String found = execParser("T.g", grammar, "TParser", "TLexer",
176								  "a", "abaac", debug);
177		assertEquals("abaac\n", found);
178	}
179
180	@Test public void testPlusSet() throws Exception {
181		String grammar =
182			"grammar T;\n" +
183			"a : ('a'|'b')+ 'c' {System.out.println(input);} ;\n";
184		String found = execParser("T.g", grammar, "TParser", "TLexer",
185								  "a", "abaac", debug);
186		assertEquals("abaac\n", found);
187	}
188
189	@Test public void testLexerOptionalSet() throws Exception {
190		String grammar =
191			"grammar T;\n" +
192			"a : A {System.out.println(input);} ;\n" +
193			"A : ('a'|'b')? 'c' ;\n";
194		String found = execParser("T.g", grammar, "TParser", "TLexer",
195								  "a", "ac", debug);
196		assertEquals("ac\n", found);
197	}
198
199	@Test public void testLexerStarSet() throws Exception {
200		String grammar =
201			"grammar T;\n" +
202			"a : A {System.out.println(input);} ;\n" +
203			"A : ('a'|'b')* 'c' ;\n";
204		String found = execParser("T.g", grammar, "TParser", "TLexer",
205								  "a", "abaac", debug);
206		assertEquals("abaac\n", found);
207	}
208
209	@Test public void testLexerPlusSet() throws Exception {
210		String grammar =
211			"grammar T;\n" +
212			"a : A {System.out.println(input);} ;\n" +
213			"A : ('a'|'b')+ 'c' ;\n";
214		String found = execParser("T.g", grammar, "TParser", "TLexer",
215								  "a", "abaac", debug);
216		assertEquals("abaac\n", found);
217	}
218
219	@Test public void testNotCharSet() throws Exception {
220		String grammar =
221			"grammar T;\n" +
222			"a : A {System.out.println($A.text);} ;\n" +
223			"A : ~('b'|'c') ;\n";
224		String found = execParser("T.g", grammar, "TParser", "TLexer",
225								  "a", "x", debug);
226		assertEquals("x\n", found);
227	}
228
229	@Test public void testNotCharSetWithLabel() throws Exception {
230		// This doesn't work in lexer yet.
231		// Generates: h=input.LA(1); but h is defined as a Token
232		String grammar =
233			"grammar T;\n" +
234			"a : A {System.out.println($A.text);} ;\n" +
235			"A : h=~('b'|'c') ;\n";
236		String found = execParser("T.g", grammar, "TParser", "TLexer",
237								  "a", "x", debug);
238		assertEquals("x\n", found);
239	}
240
241	@Test public void testNotCharSetWithRuleRef() throws Exception {
242		String grammar =
243			"grammar T;\n" +
244			"a : A {System.out.println($A.text);} ;\n" +
245			"A : ~('a'|B) ;\n" +
246			"B : 'b' ;\n";
247		String found = execParser("T.g", grammar, "TParser", "TLexer",
248								  "a", "x", debug);
249		assertEquals("x\n", found);
250	}
251
252	@Test public void testNotCharSetWithRuleRef2() throws Exception {
253		String grammar =
254			"grammar T;\n" +
255			"a : A {System.out.println($A.text);} ;\n" +
256			"A : ~('a'|B) ;\n" +
257			"B : 'b'|'c' ;\n";
258		String found = execParser("T.g", grammar, "TParser", "TLexer",
259								  "a", "x", debug);
260		assertEquals("x\n", found);
261	}
262
263	@Test public void testNotCharSetWithRuleRef3() throws Exception {
264		String grammar =
265			"grammar T;\n" +
266			"a : A {System.out.println($A.text);} ;\n" +
267			"A : ('a'|B) ;\n" +
268			"fragment\n" +
269			"B : ~('a'|'c') ;\n";
270		String found = execParser("T.g", grammar, "TParser", "TLexer",
271								  "a", "x", debug);
272		assertEquals("x\n", found);
273	}
274
275	@Test public void testNotCharSetWithRuleRef4() throws Exception {
276		String grammar =
277			"grammar T;\n" +
278			"a : A {System.out.println($A.text);} ;\n" +
279			"A : ('a'|B) ;\n" +
280			"fragment\n" +
281			"B : ~('a'|C) ;\n" +
282			"fragment\n" +
283			"C : 'c'|'d' ;\n ";
284		String found = execParser("T.g", grammar, "TParser", "TLexer",
285								  "a", "x", debug);
286		assertEquals("x\n", found);
287	}
288
289}
290