• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 package org.antlr.test;
30 
31 import org.antlr.tool.ErrorManager;
32 import org.junit.Test;
33 
34 import static org.junit.Assert.*;
35 
36 import java.io.File;
37 
38 /** test runtime parse errors */
39 public class TestSyntaxErrors extends BaseTest {
testLL2()40 	@Test public void testLL2() throws Exception {
41 		String grammar =
42 			"grammar T;\n" +
43 			"a : 'a' 'b'" +
44 			"  | 'a' 'c'" +
45 			";\n" +
46 			"q : 'e' ;\n";
47 		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "ae", false);
48 		String expecting = "line 1:1 no viable alternative at input 'e'\n";
49 		String result = stderrDuringParse;
50 		assertEquals(expecting, result);
51 	}
52 
testLL3()53 	@Test public void testLL3() throws Exception {
54 		String grammar =
55 			"grammar T;\n" +
56 			"a : 'a' 'b'* 'c'" +
57 			"  | 'a' 'b' 'd'" +
58 			"  ;\n" +
59 			"q : 'e' ;\n";
60 		System.out.println(grammar);
61 		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "abe", false);
62 		String expecting = "line 1:2 no viable alternative at input 'e'\n";
63 		String result = stderrDuringParse;
64 		assertEquals(expecting, result);
65 	}
66 
testLLStar()67 	@Test public void testLLStar() throws Exception {
68 		String grammar =
69 			"grammar T;\n" +
70 			"a : 'a'+ 'b'" +
71 			"  | 'a'+ 'c'" +
72 			";\n" +
73 			"q : 'e' ;\n";
74 		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "aaae", false);
75 		String expecting = "line 1:3 no viable alternative at input 'e'\n";
76 		String result = stderrDuringParse;
77 		assertEquals(expecting, result);
78 	}
79 
testSynPred()80 	@Test public void testSynPred() throws Exception {
81 		String grammar =
82 			"grammar T;\n" +
83 			"a : (e '.')=> e '.'" +
84 			"  | (e ';')=> e ';'" +
85 			"  | 'z'" +
86 			"  ;\n" +
87 			"e : '(' e ')'" +
88 			"  | 'i'" +
89 			"  ;\n";
90 		System.out.println(grammar);
91 		String found = execParser("T.g", grammar, "TParser", "TLexer", "a", "((i))z", false);
92 		String expecting = "line 1:1 no viable alternative at input '('\n";
93 		String result = stderrDuringParse;
94 		assertEquals(expecting, result);
95 	}
96 
testLL1ErrorInfo()97 	@Test public void testLL1ErrorInfo() throws Exception {
98 		String grammar =
99 			"grammar T;\n" +
100 			"start : animal (AND acClass)? service EOF;\n" +
101 			"animal : (DOG | CAT );\n" +
102 			"service : (HARDWARE | SOFTWARE) ;\n" +
103 			"AND : 'and';\n" +
104 			"DOG : 'dog';\n" +
105 			"CAT : 'cat';\n" +
106 			"HARDWARE: 'hardware';\n" +
107 			"SOFTWARE: 'software';\n" +
108 			"WS : ' ' {skip();} ;" +
109 			"acClass\n" +
110 			"@init\n" +
111 			"{ System.out.println(computeContextSensitiveRuleFOLLOW().toString(tokenNames)); }\n" +
112 			"  : ;\n";
113 		String result = execParser("T.g", grammar, "TParser", "TLexer", "start", "dog and software", false);
114 		String expecting = "{HARDWARE,SOFTWARE}\n";
115 		assertEquals(expecting, result);
116 	}
117 
testStrayBracketRecovery()118 	@Test public void testStrayBracketRecovery() {
119 		String grammar =
120 			"grammar T;\n" +
121 			"options {output = AST;}\n" +
122 			"tokens{NODE;}\n" +
123 			"s : a=ID INT -> ^(NODE[$a]] INT);\n" +
124 			"ID: 'a'..'z'+;\n" +
125 			"INT: '0'..'9'+;\n";
126 
127 		ErrorQueue errorQueue = new ErrorQueue();
128 		ErrorManager.setErrorListener(errorQueue);
129 
130 		boolean found =
131 			rawGenerateAndBuildRecognizer(
132 				"T.g", grammar, "TParser", "TLexer", false);
133 
134 		assertFalse(found);
135 		assertEquals(
136 			"[error(100): :4:27: syntax error: antlr: dangling ']'? make sure to escape with \\]]",
137 			errorQueue.errors.toString());
138 	}
139 
140 	/**
141 	 * This is a regression test for antlr/antlr3#61.
142 	 * https://github.com/antlr/antlr3/issues/61
143 	 */
testMissingAttributeAccessPreventsCodeGeneration()144 	@Test public void testMissingAttributeAccessPreventsCodeGeneration() throws Exception {
145 		String grammar =
146 			"grammar T;\n" +
147 			"options {\n" +
148 			"    backtrack = true; \n" +
149 			"}\n" +
150 			"// if b is rule ref, gens bad void x=null code\n" +
151 			"a : x=b {Object o = $x; System.out.println(\"alt1\");}\n" +
152 			"  | y=b\n" +
153 			"  ;\n" +
154 			"\n" +
155 			"b : 'a' ;\n" ;
156 
157 		ErrorQueue errorQueue = new ErrorQueue();
158 		ErrorManager.setErrorListener(errorQueue);
159 		boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
160 		assertFalse(success);
161 		assertEquals(
162 			"[error(117): "+tmpdir.toString()+File.separatorChar+"T.g:6:9: missing attribute access on rule scope: x]",
163 			errorQueue.errors.toString());
164 	}
165 }
166