-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserTest.java
331 lines (305 loc) · 10.5 KB
/
ParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package cop5556sp18;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556sp18.Scanner.Kind;
import cop5556sp18.Parser;
import cop5556sp18.Scanner;
import cop5556sp18.AST.ASTNode;
import cop5556sp18.AST.Block;
import cop5556sp18.AST.Declaration;
import cop5556sp18.AST.Expression;
import cop5556sp18.AST.ExpressionBinary;
import cop5556sp18.AST.ExpressionIdent;
import cop5556sp18.AST.ExpressionIntegerLiteral;
import cop5556sp18.AST.StatementInput;
import cop5556sp18.AST.StatementShow;
import cop5556sp18.AST.LHS;
import cop5556sp18.AST.LHSIdent;
import cop5556sp18.AST.LHSPixel;
import cop5556sp18.AST.LHSSample;
import cop5556sp18.AST.PixelSelector;
import cop5556sp18.AST.Program;
import cop5556sp18.AST.Statement;
import cop5556sp18.AST.StatementAssign;
import cop5556sp18.AST.StatementWrite;
import cop5556sp18.Parser.SyntaxException;
import cop5556sp18.Scanner.LexicalException;
import cop5556sp18.Scanner.Token;
import static cop5556sp18.Scanner.Kind.*;
public class ParserTest {
//set Junit to be able to catch exceptions
@Rule
public ExpectedException thrown = ExpectedException.none();
//To make it easy to print objects and turn this output on and off
static final boolean doPrint = true;
private void show(Object input) {
if (doPrint) {
System.out.println(input.toString());
}
}
//creates and returns a parser for the given input.
private Parser makeParser(String input) throws LexicalException {
show(input); //Display the input
Scanner scanner = new Scanner(input).scan(); //Create a Scanner and initialize it
show(scanner); //Display the Scanner
Parser parser = new Parser(scanner);
return parser;
}
/**
* Simple test case with an empty program. This throws an exception
* because it lacks an identifier and a block
*
* @throws LexicalException
* @throws SyntaxException
*/
/**
* Smallest legal program.
*
* @throws LexicalException
* @throws SyntaxException
*/
/**
* Checks that an element in a block is a declaration with the given type and name.
* The element to check is indicated by the value of index.
*
* @param block
* @param index
* @param type
* @param name
* @return
*/
Declaration checkDec(Block block, int index, Kind type,
String name) {
ASTNode node = block.decOrStatement(index);
assertEquals(Declaration.class, node.getClass());
Declaration dec = (Declaration) node;
assertEquals(type, dec.type);
assertEquals(name, dec.name);
return dec;
}
@Test
public void testDec0() throws LexicalException, SyntaxException {
String input = "b{int c; image j;}";
Parser parser = makeParser(input);
Program p = parser.parse();
show(p);
checkDec(p.block, 0, Kind.KW_int, "c");
checkDec(p.block, 1, Kind.KW_image, "j");
}
/** This test illustrates how you can test specific grammar elements by themselves by
* calling the corresponding parser method directly, instead of calling parse.
* This requires that the methods are visible (not private).
*
* @throws LexicalException
* @throws SyntaxException
*/
@Test
public void testExpression() throws LexicalException, SyntaxException {
String input = "x + 2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
assertEquals(ExpressionBinary.class, e.getClass());
ExpressionBinary b = (ExpressionBinary)e;
assertEquals(ExpressionIdent.class, b.leftExpression.getClass());
ExpressionIdent left = (ExpressionIdent)b.leftExpression;
assertEquals("x", left.name);
assertEquals(ExpressionIntegerLiteral.class, b.rightExpression.getClass());
ExpressionIntegerLiteral right = (ExpressionIntegerLiteral)b.rightExpression;
assertEquals(2, right.value);
assertEquals(OP_PLUS, b.op);
}
@Test
public void testExpression1() throws LexicalException, SyntaxException {
String input = "+2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test2() throws LexicalException, SyntaxException {
String input = "x**+2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test3() throws LexicalException, SyntaxException {
String input = "+x";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test4() throws LexicalException, SyntaxException {
String input = "+0.0";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test5() throws LexicalException, SyntaxException {
String input = "+Z";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test6() throws LexicalException, SyntaxException {
String input = "-+2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test7() throws LexicalException, SyntaxException {
String input = "++2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test8() throws LexicalException, SyntaxException {
String input = "a**b**c*d/e%f+g*d-d*e<a+b>c+d<=e+f>=g+h==c!=d&a&b|c|d?a:b";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test9() throws LexicalException, SyntaxException {
String input = "f==a-b*5";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test10() throws LexicalException, SyntaxException {
String input = "a**b**c*d/e%f+g*d-d*e<a+b>c+d<=e+f>=g+h==c!=d&a&b|c|d";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test11() throws LexicalException, SyntaxException {
String input = "while x {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test12() throws LexicalException, SyntaxException {
String input = "while 1 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test13() throws LexicalException, SyntaxException {
String input = "while 2.0 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test14() throws LexicalException, SyntaxException {
String input = "while Z {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test15() throws LexicalException, SyntaxException {
String input = "while 1+2 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test16() throws LexicalException, SyntaxException {
String input = "while 5&4 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test17() throws LexicalException, SyntaxException {
String input = "**b**c*d/e%f+g*d-d*e<a+b>c+d<=e+f>=g+h==c!=d";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test18() throws LexicalException, SyntaxException {
String input = "if x {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test19() throws LexicalException, SyntaxException {
String input = "if 1 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test20() throws LexicalException, SyntaxException {
String input = "if 1.0 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test21() throws LexicalException, SyntaxException {
String input = "if Z {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test22() throws LexicalException, SyntaxException {
String input = "if 1+2 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test23() throws LexicalException, SyntaxException {
String input = "if 8&2 {}";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test24() throws LexicalException, SyntaxException {
String input = "x==(1+2)*5";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test25() throws LexicalException, SyntaxException {
String input = "a**b**c*d/e%f+g*d-d*e<a+b>c+d<=e+f>=g+h==c!=d&a&b";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test26() throws LexicalException, SyntaxException {
String input = "a==25";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
@Test
public void test27() throws LexicalException, SyntaxException {
String input = "x==1+2";
Parser parser = makeParser(input);
Expression e = parser.expression(); //call expression here instead of parse
show(e);
}
}