-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTest.java
More file actions
317 lines (272 loc) · 10.9 KB
/
ParserTest.java
File metadata and controls
317 lines (272 loc) · 10.9 KB
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
/* *
* Developed for the class project in COP5556 Programming Language Principles
* at the University of Florida, Fall 2019.
*
* This software is solely for the educational benefit of students
* enrolled in the course during the Fall 2019 semester.
*
* This software, and any software derived from it, may not be shared with others or posted to public web sites,
* either during the course or afterwards.
*
* @Beverly A. Sanders, 2019
*/
package cop5556fa19;
import cop5556fa19.AST.*;
import cop5556fa19.Parser.SyntaxException;
import org.junit.jupiter.api.Test;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import static cop5556fa19.Token.Kind.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ParserTest {
// 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 a scanner, parser, and parses the input.
Exp parseExpAndShow(String input) throws Exception {
show("parser input:\n" + input); // Display the input
Reader r = new StringReader(input);
Scanner scanner = new Scanner(r); // Create a Scanner and initialize it
Parser parser = new Parser(scanner); // Create a parser
Exp e = parser.exp(); // Parse and expression
show("e=" + e); //Show the resulting AST
return e;
}
@Test
void testIdent0() throws Exception {
String input = "function () end";
Exp e = parseExpAndShow(input);
}
@Test
void testIdent1() throws Exception {
String input = "(x)";
Exp e = parseExpAndShow(input);
assertEquals(ExpName.class, e.getClass());
assertEquals("x", ((ExpName) e).name);
}
@Test
void testString() throws Exception {
String input = "\"string\"";
Exp e = parseExpAndShow(input);
assertEquals(ExpString.class, e.getClass());
assertEquals("string", ((ExpString) e).v);
}
@Test
void testBoolean0() throws Exception {
String input = "true";
Exp e = parseExpAndShow(input);
assertEquals(ExpTrue.class, e.getClass());
}
@Test
void testBoolean1() throws Exception {
String input = "false";
Exp e = parseExpAndShow(input);
assertEquals(ExpFalse.class, e.getClass());
}
@Test
void testBinary0() throws Exception {
String input = "1 + 2";
Exp e = parseExpAndShow(input);
Exp expected = Expressions.makeBinary(1, OP_PLUS, 2);
show("expected=" + expected);
assertEquals(expected, e);
}
@Test
void testUnary0() throws Exception {
String input = "-2";
Exp e = parseExpAndShow(input);
Exp expected = Expressions.makeExpUnary(OP_MINUS, 2);
show("expected=" + expected);
assertEquals(expected, e);
}
@Test
void testUnary1() throws Exception {
String input = "-*2\n";
assertThrows(SyntaxException.class, () -> {
Exp e = parseExpAndShow(input);
});
}
@Test
void testRightAssoc() throws Exception {
String input = "\"concat\" .. \"is\"..\"right associative\"";
Exp e = parseExpAndShow(input);
Exp expected = Expressions.makeBinary(
Expressions.makeExpString("concat")
, DOTDOT
, Expressions.makeBinary("is", DOTDOT, "right associative"));
show("expected=" + expected);
assertEquals(expected, e);
}
@Test
void testLeftAssoc() throws Exception {
String input = "\"minus\" - \"is\" - \"left associative\"";
Exp e = parseExpAndShow(input);
Exp expected = Expressions.makeBinary(
Expressions.makeBinary(
Expressions.makeExpString("minus")
, OP_MINUS
, Expressions.makeExpString("is")), OP_MINUS,
Expressions.makeExpString("left associative"));
show("expected=" + expected);
assertEquals(expected, e);
}
@Test
void testMultiUnaryOperator() throws Exception {
String input = "not #~1";
Exp e = parseExpAndShow(input);
}
// creates a scanner, parser, and parses the input by calling block()
Block parseBlockAndShow(String input) throws Exception {
show("parser input:\n" + input); // Display the input
Reader r = new StringReader(input);
Scanner scanner = new Scanner(r); // Create a Scanner and initialize it
Parser parser = new Parser(scanner);
Method method = Parser.class.getDeclaredMethod("block");
method.setAccessible(true);
Block b = (Block) method.invoke(parser);
show("b=" + b);
return b;
}
//creates a scanner, parser, and parses the input by calling parse()
//this corresponds to the actual use case of the parser
Chunk parseAndShow(String input) throws Exception {
show("parser input:\n" + input); // Display the input
Reader r = new StringReader(input);
Scanner scanner = new Scanner(r); // Create a Scanner and initialize it
Parser parser = new Parser(scanner);
Chunk c = parser.parse();
show("c="+c);
return c;
}
@Test
void testEmpty1() throws Exception {
String input = "";
Block b = parseBlockAndShow(input);
Block expected = Expressions.makeBlock();
assertEquals(expected, b);
}
@Test
void testEmpty2() throws Exception {
String input = "";
ASTNode n = parseAndShow(input);
Block b = Expressions.makeBlock();
Chunk expected = new Chunk(b.firstToken,b);
assertEquals(expected,n);
}
@Test
void testAssign1() throws Exception {
String input = "a=b";
Block b = parseBlockAndShow(input);
List<Exp> lhs = Expressions.makeExpList(Expressions.makeExpName("a"));
List<Exp> rhs = Expressions.makeExpList(Expressions.makeExpName("b"));
StatAssign s = Expressions.makeStatAssign(lhs,rhs);
Block expected = Expressions.makeBlock(s);
assertEquals(expected,b);
}
@Test
void testAssignChunk1() throws Exception {
String input = "a=b";
ASTNode c = parseAndShow(input);
List<Exp> lhs = Expressions.makeExpList(Expressions.makeExpName("a"));
List<Exp> rhs = Expressions.makeExpList(Expressions.makeExpName("b"));
StatAssign s = Expressions.makeStatAssign(lhs,rhs);
Block b = Expressions.makeBlock(s);
Chunk expected = new Chunk(b.firstToken,b);
assertEquals(expected,c);
}
@Test
void testMultiAssign1() throws Exception {
String input = "a,c=8,9";
Block b = parseBlockAndShow(input);
List<Exp> lhs = Expressions.makeExpList(
Expressions.makeExpName("a")
,Expressions.makeExpName("c"));
Exp e1 = Expressions.makeExpInt(8);
Exp e2 = Expressions.makeExpInt(9);
List<Exp> rhs = Expressions.makeExpList(e1,e2);
StatAssign s = Expressions.makeStatAssign(lhs,rhs);
Block expected = Expressions.makeBlock(s);
assertEquals(expected,b);
}
@Test
void testMultiAssign3() throws Exception {
String input = "a,c=8,f(x)";
Block b = parseBlockAndShow(input);
List<Exp> lhs = Expressions.makeExpList(
Expressions.makeExpName("a")
,Expressions.makeExpName("c"));
Exp e1 = Expressions.makeExpInt(8);
List<Exp> args = new ArrayList<>();
args.add(Expressions.makeExpName("x"));
Exp e2 = Expressions.makeExpFunCall(Expressions.makeExpName("f"),args, null);
List<Exp> rhs = Expressions.makeExpList(e1,e2);
StatAssign s = Expressions.makeStatAssign(lhs,rhs);
Block expected = Expressions.makeBlock(s);
assertEquals(expected,b);
}
@Test
void testAssignToTable() throws Exception {
String input = "g.a.b = 3";
Block bl = parseBlockAndShow(input);
ExpName g = Expressions.makeExpName("g");
ExpString a = Expressions.makeExpString("a");
Exp gtable = Expressions.makeExpTableLookup(g,a);
ExpString b = Expressions.makeExpString("b");
Exp v = Expressions.makeExpTableLookup(gtable, b);
Exp three = Expressions.makeExpInt(3);
Stat s = Expressions.makeStatAssign(Expressions.makeExpList(v), Expressions.makeExpList(three));;
Block expected = Expressions.makeBlock(s);
assertEquals(expected,bl);
}
@Test
void testAssignTableToVar() throws Exception {
String input = "x = g.a.b";
Block bl = parseBlockAndShow(input);
ExpName g = Expressions.makeExpName("g");
ExpString a = Expressions.makeExpString("a");
Exp gtable = Expressions.makeExpTableLookup(g,a);
ExpString b = Expressions.makeExpString("b");
Exp e = Expressions.makeExpTableLookup(gtable, b);
Exp v = Expressions.makeExpName("x");
Stat s = Expressions.makeStatAssign(Expressions.makeExpList(v), Expressions.makeExpList(e));;
Block expected = Expressions.makeBlock(s);
assertEquals(expected,bl);
}
@Test
void testmultistatements6() throws Exception {
String input = "x = g.a.b ; ::mylabel:: do y = 2 goto mylabel f=a(0,200) end break"; //same as testmultistatements0 except ;
ASTNode c = parseAndShow(input);
ExpName g = Expressions.makeExpName("g");
ExpString a = Expressions.makeExpString("a");
Exp gtable = Expressions.makeExpTableLookup(g,a);
ExpString b = Expressions.makeExpString("b");
Exp e = Expressions.makeExpTableLookup(gtable, b);
Exp v = Expressions.makeExpName("x");
Stat s0 = Expressions.makeStatAssign(v,e);
StatLabel s1 = Expressions.makeStatLabel("mylabel");
Exp y = Expressions.makeExpName("y");
Exp two = Expressions.makeExpInt(2);
Stat s2 = Expressions.makeStatAssign(y,two);
Stat s3 = Expressions.makeStatGoto("mylabel");
Exp f = Expressions.makeExpName("f");
Exp ae = Expressions.makeExpName("a");
Exp zero = Expressions.makeExpInt(0);
Exp twohundred = Expressions.makeExpInt(200);
List<Exp> args = Expressions.makeExpList(zero, twohundred);
ExpFunctionCall fc = Expressions.makeExpFunCall(ae, args, null);
StatAssign s4 = Expressions.makeStatAssign(f,fc);
StatDo statdo = Expressions.makeStatDo(s2,s3,s4);
StatBreak statBreak = Expressions.makeStatBreak();
Block expectedBlock = Expressions.makeBlock(s0,s1,statdo,statBreak);
Chunk expectedChunk = new Chunk(expectedBlock.firstToken, expectedBlock);
assertEquals(expectedChunk,c);
}
}