/
language.test.js
369 lines (369 loc) · 15.9 KB
/
language.test.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
"use strict";
const assert = require("chai").assert;
const Language = require("../lib/language.js");
const {syntax, literal: lit, numlit} = Language;
const langCalc = require("../sample/calc.js");
const debug = require("debug")("Language");
describe("Language", () => {
describe("parse", () => {
it("should throw, if syntax does not exits", ()=>{
assert.throw(()=>{
const lang = new Language([
syntax("calc", [["expression"]]),
]);
lang.parse("1+2");
});
});
it("should be error the expression is incomplete", ()=>{
const expr = `1 + `;
const tokens = langCalc.tokenize(expr);
const result = langCalc.parse(tokens);
assert.instanceOf(result.error, Error);
});
it("should accept a token list", ()=>{
const expr = `1 + 2`;
const tokens = langCalc.tokenize(expr);
const result = langCalc.parse(tokens);
debug(`result: ${JSON.stringify(result, null, 2)}`);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 3);
});
it("should throw error, if rule contains invalid element", ()=>{
assert.throw(()=>{
const lang = new Language([
syntax("floating-constant",
[
[numlit, 1, numlit],
]),
]);
lang.parse("123");
});
});
describe("Issue of the right recursion (Issue #14)", ()=>{
describe("evaluation expression '1 - 2 - 3'", ()=>{
it("should be (-1) in right-recursive definition", ()=>{
const lang = new Language([
syntax("additive-expression",
[
["integer-constant", lit("-"), "additive-expression"],
["integer-constant"],
],
(term) => {
const terms = term.contents();
const [a, ope, b] = terms;
return !ope ? a : ope == "+" ? a + b: a - b;
}),
syntax("integer-constant", [[numlit]], (term) => parseInt(term.str())),
]);
const expr = `1 - 2 - 3`;
const result = lang.parse(expr);
const value = lang.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 2);
});
it("should be (-4) with repeating definition", ()=>{
const lang = new Language([
syntax("additive-expression",
[["integer-constant", "additive-expression-rest*"]],
(term) => {
debug(`additive-expression.term: ${JSON.stringify(term, null, 2)}`);
const terms = [].concat(...term.contents());
debug(`additive-expression.terms: ${JSON.stringify(terms, null, 2)}`);
let acc = terms[0];
for(let i = 1; i < terms.length; i += 2) {
const ope = terms[i];
const value = terms[i + 1];
acc -= value;
}
return acc;
}),
syntax("additive-expression-rest",
[
[lit("-"), "integer-constant"],
],
term => term.contents()),
syntax("integer-constant", [[numlit]], (term) => parseInt(term.str())),
]);
const expr = `1 - 2 - 3`;
const result = lang.parse(expr);
const value = lang.evaluate(result);
assert.isNull(result.error);
assert.equal(value, -4);
});
});
describe("Countermeasure by repeating specifier", ()=>{
describe("Repeating rule", ()=>{
const lang = new Language([
syntax("repeat-term",
[[lit("A"), "repeat*"]]),
syntax("repeat",
[[lit("."), lit("A") ]]),
]);
it("should not throw", ()=>{
assert.doesNotThrow(()=>{
lang.parse("A.A.A");
});
});
it("should not be error for no repeating", ()=>{
const result = lang.parse("A");
assert.isNull(result.error);
});
it("should not be error for repeating one time", ()=>{
const result = lang.parse("A.A");
assert.isNull(result.error);
});
it("should not be error for more repeating", ()=>{
const result = lang.parse("A.A.A");
assert.isNull(result.error);
});
});
describe("Not repeating rule", ()=>{
it("should be error", ()=>{
const lang = new Language([
syntax("repeat-term",
[[lit("A"), "nRepeat"]]),
syntax("nRepeat",
[[lit("."), lit("A")]]),
]);
const result = lang.parse("A");
assert.isNotNull(result.error);
});
it("should be error", ()=>{
const lang = new Language([
syntax("repeat-term",
[[lit("A"), "nRepeat"]]),
syntax("nRepeat",
[[lit("."), lit("A")]]),
]);
const result = lang.parse("A.A.A");
assert.isNotNull(result.error);
});
it("should not be error", ()=>{
const lang = new Language([
syntax("repeat-term",
[[lit("A"), "nRepeat"]]),
syntax("nRepeat",
[[lit("."), lit("A")]]),
]);
const result = lang.parse("A.A");
assert.isNull(result.error);
});
});
});
});
});
describe("evaluate", () => {
it("should throw error, if evaluater does not exist", ()=>{
const lang = new Language([
syntax("floating-constant",
[
[numlit, lit("."), numlit],
]),
]);
const result = lang.parse("123");
assert.throw(()=>{
lang.evaluate(result);
});
});
it("should throw error that evaluator throws", ()=>{
const lang = new Language([
syntax("floating-constant",
[["floating-real-part"]],
(term) => parseFloat(term.str())),
syntax("floating-real-part",
[[numlit, lit("."), numlit]],
(term) => { throw new Error("TEST"); }),
]);
const result = lang.parse("123.45");
assert.throw(()=>{
lang.evaluate(result);
});
});
});
describe("calc.js sample implementation", () => {
describe("correct expression", () => {
it("`1` should be 1", () => {
const expr = `1`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 1);
});
it("`(1)` should be 1", ()=>{
const expr = `(1)`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 1);
});
it("`1 + 2` should be 3", ()=>{
const expr = `1 + 2`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 3);
});
it("`3 * 4` should be 12", ()=>{
const expr = `3 * 4`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 12);
});
it("`5 - 6` should be -1", ()=>{
const expr = `5 - 6`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, -1);
});
it("`7 / 8` should be 0.875", ()=>{
const expr = `7 / 8`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 0.875);
});
it("`1 - 2 - 3` should be -4", ()=>{
const expr = `1 - 2 - 3`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, -4);
});
it("`1 * 2 + 3 * 4` should be 14", ()=>{
const expr = `1 * 2 + 3 * 4`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 14);
});
it("`1 / (2 + 3) + 4` should be 4.2", ()=>{
const expr = `1 / (2 + 3) + 4`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 4.2);
});
it("`(1 + 2) * (3 + 4)` should be 21", ()=>{
const expr = `(1 + 2) * (3 + 4)`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 21);
});
it("`(1 + 2) * ((3 + 4) / 2)` should be 10.5 (only parsing)", ()=>{
const expr = `(1 + 2) * ((3 + 4) / 2)`;
const result = langCalc.parse(expr);
assert.isNull(result.error);
}).timeout(5000);
it("`(1 + 2) * ((3 + 4) / 2)` should be 10.5", ()=>{
const expr = `(1 + 2) * ((3 + 4) / 2)`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 10.5);
}).timeout(5000);
it("`1.5 * 2` should be 3", ()=>{
const expr = `1.5 * 2`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 3);
});
it("`1.5e+2 * -2` should be -300", ()=>{
const expr = `1.5e+2 * -2`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, -300);
});
it("`1.5e2 * 2` should be 300", ()=>{
const expr = `1.5e2 * 2`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 300);
});
describe("Multi lines", ()=>{
it("should not be error even if the expression contains LF", ()=>{
const eol = "\n";
const expr = `1${eol}+ 2${eol}+3${eol} +4`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 10);
});
it("should not be error even if the expression contains CR-LF", ()=>{
const eol = "\r\n";
const expr = `1${eol}+ 2${eol}+3${eol} +4`;
const result = langCalc.parse(expr);
const value = langCalc.evaluate(result);
assert.isNull(result.error);
assert.equal(value, 10);
});
});
describe("Term#toString", ()=>{
describe("with no syntax error", ()=>{
it("should not throw", ()=>{
assert.doesNotThrow(()=>{
const result = langCalc.parse("1");
result.toString();
});
});
it("should not throw", ()=>{
assert.doesNotThrow(()=>{
const result = langCalc.parse("1 + 2) * (3 + 4)");
result.toString();
});
});
it("should returns string", ()=>{
const result = langCalc.parse("1");
const s = result.toString();
assert.isString(s);
});
});
describe("with syntax error", ()=>{
it("should not throw", ()=>{
assert.doesNotThrow(()=>{
const result = langCalc.parse("(1 + 2) * xyz(3 + 4) / 2)");
result.toString();
});
});
it("should returns string", ()=>{
const result = langCalc.parse("(1 + 2) * xyz(3 + 4) / 2)");
const s = result.toString();
assert.isString(s);
});
});
})
});
describe("parsing error", () => {
it("`+` should be parser error", ()=>{
const expr = `+`;
const result = langCalc.parse(expr);
assert.instanceOf(result.error, Error);
});
it("`1 + 2 * 3 + 4)` should be parser error", () => {
const expr = `1 + 2 * 3 + 4)`;
const result = langCalc.parse(expr);
assert.instanceOf(result.error, Error);
});
it("`1 + 2 * (3 + 4` should be parser error", () => {
const expr = `1 + 2 * (3 + 4`;
const result = langCalc.parse(expr);
assert.instanceOf(result.error, Error);
});
});
describe("evaluation error", () => {
it("`1 .5 * 2` should throw ", () => {
const expr = `1 .5 * 2`;
const result = langCalc.parse(expr);
assert.throw(()=>{
langCalc.evaluate(result);
});
});
});
});
});