1/*
2 * [The "BSD license"]
3 * Copyright (c) 2011 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
29package org.antlr.test;
30
31import org.junit.Test;
32
33/** test runtime parse errors */
34public class TestSyntaxErrors extends BaseTest {
35	@Test public void testLL2() throws Exception {
36		String grammar =
37			"grammar T;\n" +
38			"a : 'a' 'b'" +
39			"  | 'a' 'c'" +
40			";\n" +
41			"q : 'e' ;\n";
42		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ae", false);
43		String expecting = "line 1:1 no viable alternative at input 'e'\n";
44		String result = stderrDuringParse;
45		assertEquals(expecting, result);
46	}
47
48	@Test public void testLL3() throws Exception {
49		String grammar =
50			"grammar T;\n" +
51			"a : 'a' 'b'* 'c'" +
52			"  | 'a' 'b' 'd'" +
53			"  ;\n" +
54			"q : 'e' ;\n";
55		System.out.println(grammar);
56		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abe", false);
57		String expecting = "line 1:2 no viable alternative at input 'e'\n";
58		String result = stderrDuringParse;
59		assertEquals(expecting, result);
60	}
61
62	@Test public void testLLStar() throws Exception {
63		String grammar =
64			"grammar T;\n" +
65			"a : 'a'+ 'b'" +
66			"  | 'a'+ 'c'" +
67			";\n" +
68			"q : 'e' ;\n";
69		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aaae", false);
70		String expecting = "line 1:3 no viable alternative at input 'e'\n";
71		String result = stderrDuringParse;
72		assertEquals(expecting, result);
73	}
74
75	@Test public void testSynPred() throws Exception {
76		String grammar =
77			"grammar T;\n" +
78			"a : (e '.')=> e '.'" +
79			"  | (e ';')=> e ';'" +
80			"  | 'z'" +
81			"  ;\n" +
82			"e : '(' e ')'" +
83			"  | 'i'" +
84			"  ;\n";
85		System.out.println(grammar);
86		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "((i))z", false);
87		String expecting = "line 1:0 no viable alternative at input '('\n";
88		String result = stderrDuringParse;
89		assertEquals(expecting, result);
90	}
91
92	@Test public void testLL1ErrorInfo() throws Exception {
93		String grammar =
94			"grammar T;\n" +
95			"start : animal (AND acClass)? service EOF;\n" +
96			"animal : (DOG | CAT );\n" +
97			"service : (HARDWARE | SOFTWARE) ;\n" +
98			"AND : 'and';\n" +
99			"DOG : 'dog';\n" +
100			"CAT : 'cat';\n" +
101			"HARDWARE: 'hardware';\n" +
102			"SOFTWARE: 'software';\n" +
103			"WS : ' ' {skip();} ;" +
104			"acClass\n" +
105			"@init\n" +
106			"{ System.out.println(computeContextSensitiveRuleFOLLOW().toString(tokenNames)); }\n" +
107			"  : ;\n";
108		String result = execParser("T.g", grammar, "TParser", "TLexer", "start", "dog and software", false);
109		String expecting = "{HARDWARE,SOFTWARE}\n";
110		assertEquals(expecting, result);
111	}
112}
113