/
parser.spec.js
434 lines (378 loc) · 16.9 KB
/
parser.spec.js
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var _ = require('Underscore');
var multimethod = require('multimethod');
var p = require('../src/parser').Parser;
var utils = require("../src/utils").Utils;
var checkAst = multimethod()
.dispatch(function(actual, _) {
return utils.type(actual);
})
.when("Array", function(actual, expected) {
expect(actual.length).toEqual(expected.length);
_.map(_.zip(actual, expected), function(pair) {
checkAst(pair[0], pair[1]);
})
})
.when("Object", function(actual, expected) {
expect(actual.tag).toEqual(_.keys(expected)[0]);
checkAst(actual.c, _.values(expected)[0]);
})
.default(function(actual, expected) {
expect(actual).toEqual(expected);
});
describe('parser', function() {
describe('assignment to scalar', function(){
it('should assign an identifier', function() {
checkAst(p.parse("isla is age"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
{is: ["is"]},
{value: [{variable:
[{scalar: [{identifier: ["age"]}]}]}]}]}]}]}]});
});
it('should assign a string in single quotes', function() {
checkAst(p.parse("isla is 'cool'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ["cool"]}]}]}]}]}]}]});
});
it('should assign a string in double quotes', function() {
checkAst(p.parse("isla is \"cool\""),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ["cool"]}]}]}]}]}]}]});
});
it('should not parse assignment of int', function() {
expect(function(){
p.parse("age is 1");
}).toThrow();
});
});
describe('assignment to object attribute', function(){
it('should allow assignment', function() {
checkAst(p.parse("isla age is '1'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{object:
[{identifier: ["isla"]},
{identifier: ["age"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ['1']}]}]}]}]}]}]});
});
});
describe('type assignment', function(){
it('should allow assignment to scalar', function() {
checkAst(p.parse("mary is a girl"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{scalar: [{identifier: ["mary"]}]}]},
{is_a: ["is a"]},
{identifier: ["girl"]}]}]}]}]});
});
it('should allow assignment to an object attribute', function() {
checkAst(p.parse("mary friend is a girl"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{object:
[{identifier: ["mary"]},
{identifier: ["friend"]}]}]},
{is_a: ["is a"]},
{identifier: ["girl"]}]}]}]}]});
});
});
describe('blocks', function(){
it('should allow a two expression block', function() {
checkAst(p.parse("isla is '1'\nmary is '2'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ['1']}]}]}]}]},
{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["mary"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ['2']}]}]}]}]}]}]});
});
it('should allow a three expression block', function() {
checkAst(p.parse("name is 'Isla'\nwrite 'la'\nwrite name"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["name"]}]}]},
{is: ["is"]},
{value: [{literal: [{string: ["Isla"]}]}]}]}]},
{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{literal: [{string: ["la"]}]}]}]}]},
{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{variable:
[{scalar: [{identifier: ["name"]}]}]}]}]}]}
]}]});
});
});
describe('whitespace', function() {
it('should allow whitespace at end of expressions', function() {
expect(p.parse("x is y ").tag).toEqual("root");
expect(p.parse("write x ").tag).toEqual("root");
expect(p.parse("add x to y ").tag).toEqual("root");
expect(p.parse("x is a y ").tag).toEqual("root");
});
it('should allow whitespace at beginning of expressions', function() {
expect(p.parse(" x is y").tag).toEqual("root");
expect(p.parse(" write x").tag).toEqual("root");
expect(p.parse(" add x to y").tag).toEqual("root");
expect(p.parse(" x is a y").tag).toEqual("root");
});
it('should allow whitespace at in middle of expressions', function() {
expect(p.parse("x is y").tag).toEqual("root");
expect(p.parse("write x").tag).toEqual("root");
expect(p.parse("add x to y").tag).toEqual("root");
expect(p.parse("x is a y").tag).toEqual("root");
});
});
describe('index of node in source', function() {
var expr = function(ast) {
return p.extract(ast, "root", 0, "block", 0, "expression", 0);
};
it('should set index of 0 for beginning of expression', function() {
expect(expr(p.parse("x is y")).index).toEqual(0);
});
it('should set index of 0 for assignee at beginning of expression', function() {
var e = expr(p.parse("x is y"));
expect(p.extract(e, "value_assignment", 0).index).toEqual(0);
});
it('should set index of 2 for is in expression', function() {
var e = expr(p.parse("x is y"));
expect(p.extract(e, "value_assignment", 1).index).toEqual(2);
});
it('should set correct col when whitespace in code', function() {
var e = expr(p.parse("x is y"));
expect(p.extract(e, "value_assignment", 1).index).toEqual(12);
});
});
describe('invocation', function() {
it('should not parse invocation with int param', function() {
expect(function(){
p.parse("write 1");
}).toThrow();
});
it('should allow invocation with scalar variable', function() {
checkAst(p.parse("write isla"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{variable:
[{scalar: [{identifier: ["isla"]}]}]}]}]}]}]}]});
});
it('should allow invocation with scalar literal', function() {
checkAst(p.parse("write 'isla'"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{literal: [{string: ["isla"]}]}]}]}]}]}]});
});
it('should allow invocation with object attribute', function() {
checkAst(p.parse("write isla age"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{variable:
[{object: [{identifier: ["isla"]},
{identifier: ["age"]}]}]}]}]}]}]}]});
});
it('should not show string regression', function() {
checkAst(p.parse("write 'My name Isla'"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
{value: [{literal:
[{string: ["My name Isla"]}]}]}]}]}]}]});
});
});
describe('lists', function() {
it('should allow list instantiation', function() {
checkAst(p.parse("items is a list"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{scalar: [{identifier: ["items"]}]}]},
{is_a: ["is a"]},
{identifier: ["list"]}]}]}]}]});
});
it('should not allow addition of int to list', function() {
expect(function(){
p.parse("add 1 to list");
}).toThrow();
});
it('should allow addition of item to list', function() {
checkAst(p.parse("add sword to items"),
{root: [{block: [{expression:
[{list_assignment:
[{list_operation: [{add: ["add"]}]},
{value: [{variable:
[{scalar:
[{identifier: ["sword"]}]}]}]},
{to_from: ["to from"]},
{assignee: [{scalar: [{identifier: ["items"]}]}]}]}]}]}]})
});
});
describe('syntax annotation', function() {
var astExpression = function(code) {
return p.extract(p.parse(code),
"root", 0, "block", 0, "expression", 0).c;
};
describe('keywords', function() {
describe('assignments', function() {
it('should annotate is in scalar assignment', function() {
expect(astExpression("x is 'y'")[1].syntax).toEqual("keyword");
});
it('should annotate is in object assignment', function() {
expect(astExpression("x y is 'z'")[1].syntax).toEqual("keyword");
});
});
describe('type instantiations', function() {
it('should annotate is a in scalar type instantiation', function() {
expect(astExpression("x is a y")[1].syntax).toEqual("keyword");
});
it('should annotate is a in an attribute type instantiation', function() {
expect(astExpression("x y is a z")[1].syntax).toEqual("keyword");
});
});
describe('list operations', function() {
it('should annotate add in a list operation', function() {
expect(astExpression("add x to y")[0].c[0].syntax).toEqual("keyword");
});
it('should annotate to in a list operation', function() {
expect(astExpression("add x to y")[2].syntax).toEqual("keyword");
});
it('should annotate take in a list operation', function() {
expect(astExpression("take x from y")[0].c[0].syntax).toEqual("keyword");
});
it('should annotate from in a list operation', function() {
expect(astExpression("take x from y")[2].syntax).toEqual("keyword");
});
});
});
describe('types', function() {
describe('type instantiations', function() {
it('should annotate in scalar type instantiation', function() {
expect(astExpression("x is a y")[2].syntax).toEqual("type");
});
it('should annotate in an attribute type instantiation', function() {
expect(astExpression("x y is a z")[2].syntax).toEqual("type");
});
});
});
describe('variables', function() {
describe('assignments', function() {
it('should annotate in scalar assignment', function() {
expect(p.extract(astExpression("x is 'y'"),
0, "assignee", 0).syntax).toEqual("variable");
});
it('should annotate in object assignment', function() {
expect(p.extract(astExpression("x y is 'z'"),
0, "assignee", 0, "object", 0).syntax)
.toEqual("variable");
});
});
describe('type instantiations', function() {
it('should annotate in scalar type instantiation', function() {
expect(p.extract(astExpression("x is a y"),
0, "assignee", 0).syntax).toEqual("variable");
});
it('should annotate in an attribute type instantiation', function() {
expect(p.extract(astExpression("x y is a z"),
0, "assignee", 0, "object", 0).syntax)
.toEqual("variable");
});
});
describe('list operations', function() {
it('should annotate item in a list operation', function() {
expect(p.extract(astExpression("add x to y"),
1, "value", 0, "variable", 0).syntax)
.toEqual("variable");
});
it('should annotate list in a list operation', function() {
expect(p.extract(astExpression("add x to y"),
3, "assignee", 0).syntax).toEqual("variable");
});
});
describe('invocations', function() {
it('should annotate scalar param', function() {
expect(p.extract(astExpression("write x"),
1, "value", 0, "variable", 0).syntax)
.toEqual("variable");
});
it('should annotate in object param', function() {
expect(p.extract(astExpression("write x y"),
1, "value", 0, "variable", 0, "object", 0).syntax)
.toEqual("variable");
});
});
});
describe('function identifiers', function() {
describe('invocations', function() {
it('should annotate in invocation', function() {
expect(p.extract(astExpression("write x"),
0).syntax).toEqual("function");
});
});
});
describe('literals', function() {
describe('assignments', function() {
it('should annotate in assignment', function() {
expect(p.extract(astExpression("x is 'y'"),
2, "value", 0).syntax).toEqual("literal");
});
});
describe('list operations', function() {
it('should annotate item in a list operation', function() {
expect(p.extract(astExpression("add 'x' to y"),
1, "value", 0).syntax).toEqual("literal");
});
});
describe('invocations', function() {
it('should annotate scalar param', function() {
expect(p.extract(astExpression("write 'x'"),
1, "value", 0).syntax).toEqual("literal");
});
});
});
describe('attributes', function() {
describe('assignments', function() {
it('should annotate in object assignment', function() {
expect(p.extract(astExpression("x y is 'z'"),
0, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
});
describe('type instantiations', function() {
it('should annotate in an attribute type instantiation', function() {
expect(p.extract(astExpression("x y is a z"),
0, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
});
describe('list operations', function() {
it('should annotate is a list op on a obj attribute', function() {
expect(p.extract(astExpression("add x to y z"),
3, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
});
describe('invocations', function() {
it('should annotate in object param', function() {
expect(p.extract(astExpression("write x y"),
1, "value", 0, "variable", 0, "object", 1).syntax)
.toEqual("attribute");
});
});
});
});
});