-
Notifications
You must be signed in to change notification settings - Fork 108
/
parser.spec.ts
459 lines (367 loc) · 18.1 KB
/
parser.spec.ts
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import {Config} from '../../src'
import {CellError, ErrorType} from '../../src/Cell'
import {SheetMapping} from '../../src/DependencyGraph'
import {
AstNodeType,
CellAddress,
CellRangeAst,
CellReferenceAst,
ErrorAst,
MinusOpAst,
MinusUnaryOpAst,
NumberAst,
ParserWithCaching,
ParsingErrorType,
PlusOpAst,
PowerOpAst,
ProcedureAst,
StringAst,
} from '../../src/parser'
import {enGB, plPL} from "../../src/i18n";
import {ParenthesisAst} from "../../src/parser/Ast";
describe('ParserWithCaching', () => {
it('integer literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=42', CellAddress.absolute(0, 0, 0)).ast as NumberAst
expect(ast.type).toBe(AstNodeType.NUMBER)
expect(ast.value).toBe(42)
})
it('negative integer literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=-42', CellAddress.absolute(0, 0, 0)).ast as MinusUnaryOpAst
expect(ast.type).toBe(AstNodeType.MINUS_UNARY_OP)
const value = ast.value as NumberAst
expect(value.type).toBe(AstNodeType.NUMBER)
expect(value.value).toBe(42)
})
it('string literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('="foobar"', CellAddress.absolute(0, 0, 0)).ast as StringAst
expect(ast.type).toBe(AstNodeType.STRING)
expect(ast.value).toBe('foobar')
})
it('plus operator on different nodes', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=1+A5', CellAddress.absolute(0, 0, 0)).ast as PlusOpAst
expect(ast.type).toBe(AstNodeType.PLUS_OP)
expect(ast.left.type).toBe(AstNodeType.NUMBER)
expect(ast.right.type).toBe(AstNodeType.CELL_REFERENCE)
})
it('minus operator', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=1-3', CellAddress.absolute(0, 0, 0)).ast as MinusOpAst
expect(ast.type).toBe(AstNodeType.MINUS_OP)
expect(ast.left.type).toBe(AstNodeType.NUMBER)
expect(ast.right.type).toBe(AstNodeType.NUMBER)
})
it('power operator', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=2^3', CellAddress.absolute(0, 0, 0)).ast as PowerOpAst
expect(ast.type).toBe(AstNodeType.POWER_OP)
expect(ast.left.type).toBe(AstNodeType.NUMBER)
expect(ast.right.type).toBe(AstNodeType.NUMBER)
})
it('power operator order', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=2*2^3', CellAddress.absolute(0, 0, 0)).ast as PowerOpAst
expect(ast.type).toBe(AstNodeType.TIMES_OP)
expect(ast.left.type).toBe(AstNodeType.NUMBER)
expect(ast.right.type).toBe(AstNodeType.POWER_OP)
})
it('SUM function without args', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=SUM()', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('SUM')
expect(ast.args.length).toBe(0)
})
it('function without polish characters', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=żółćąęźśńŻÓŁĆĄĘŹŚŃ()', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('ŻÓŁĆĄĘŹŚŃŻÓŁĆĄĘŹŚŃ')
expect(ast.args.length).toBe(0)
})
it('function with dot separator', () => {
const parser = new ParserWithCaching(new Config({ language: plPL }), new SheetMapping(plPL).get)
const ast = parser.parse('=NR.SER.OST.DN.MIEŚ()', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('EOMONTH')
expect(ast.args.length).toBe(0)
})
it('function name should be translated during parsing', () => {
const parser = new ParserWithCaching(new Config({ language: plPL }), new SheetMapping(plPL).get)
const ast = parser.parse('=SUMA()', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('SUM')
expect(ast.args.length).toBe(0)
})
it('should leave original name if procedure translation not known', () => {
const parser = new ParserWithCaching(new Config({ language: plPL }), new SheetMapping(plPL).get)
const ast = parser.parse('=FOOBAR()', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('FOOBAR')
expect(ast.args.length).toBe(0)
})
it('SUM function with args', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=SUM(1, A1)', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('SUM')
expect(ast.args[0].type).toBe(AstNodeType.NUMBER)
expect(ast.args[1].type).toBe(AstNodeType.CELL_REFERENCE)
})
it('SUM function with expression arg', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=SUM(1 / 2 + SUM(1,2))', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.args.length).toBe(1)
expect(ast.args[0].type).toBe(AstNodeType.PLUS_OP)
const arg = ast.args[0] as PlusOpAst
expect(arg.left.type).toBe(AstNodeType.DIV_OP)
expect(arg.right.type).toBe(AstNodeType.FUNCTION_CALL)
})
it('joining nodes without braces', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=1 + 2 + 3', CellAddress.absolute(0, 0, 0)).ast as PlusOpAst
expect(ast.type).toBe(AstNodeType.PLUS_OP)
expect(ast.left.type).toBe(AstNodeType.PLUS_OP)
expect(ast.right.type).toBe(AstNodeType.NUMBER)
})
it('joining nodes with braces', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=1 + (2 + 3)', CellAddress.absolute(0, 0, 0)).ast as PlusOpAst
expect(ast.type).toBe(AstNodeType.PLUS_OP)
expect(ast.left.type).toBe(AstNodeType.NUMBER)
const right = ast.right as ParenthesisAst
expect(right.type).toBe(AstNodeType.PARENTHESIS)
expect(right.expression.type).toBe(AstNodeType.PLUS_OP)
})
it('float literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=3.14', CellAddress.absolute(0, 0, 0)).ast as NumberAst
expect(ast.type).toBe(AstNodeType.NUMBER)
expect(ast.value).toBe(3.14)
})
it('leading zeros of number literals', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const int = parser.parse('=01234', CellAddress.absolute(0, 0, 0)).ast as NumberAst
const float = parser.parse('=03.14', CellAddress.absolute(0, 0, 0)).ast as NumberAst
expect(int.type).toBe(AstNodeType.NUMBER)
expect(int.value).toBe(1234)
expect(float.type).toBe(AstNodeType.NUMBER)
expect(float.value).toBe(3.14)
})
it('parsing error - unexpected token', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=A', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.args[0].type).toBe(ParsingErrorType.ParserError)
expect(ast.args[0].message).toMatch(/Expecting token/)
})
it('parsing error - unexpected token', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=SUM(A)', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.args[0].type).toBe(ParsingErrorType.ParserError)
})
it('parsing error - not all input parsed', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=A1B1', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.args[0].type).toBe(ParsingErrorType.ParserError)
})
it('errors - lexing errors', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const input = ["='foo'", "=foo'bar", "=''''''", '=@']
input.forEach((formula) => {
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.args[0].type).toBe(ParsingErrorType.LexingError)
})
})
it('functions should not be case sensitive', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=sum(1)', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast.procedureName).toBe('SUM')
})
it('allow to accept different lexer configs', () => {
const parser1 = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const parser2 = new ParserWithCaching(new Config({ functionArgSeparator: ';' }), new SheetMapping(enGB).get)
const ast1 = parser1.parse('=SUM(1, 2)', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
const ast2 = parser2.parse('=SUM(1; 2)', CellAddress.absolute(0, 0, 0)).ast as ProcedureAst
expect(ast1.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast2.type).toBe(AstNodeType.FUNCTION_CALL)
expect(ast1).toEqual(ast2)
})
it('error literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=#REF!', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.error).toEqual(new CellError(ErrorType.REF))
})
it('error literals are case insensitive', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=#rEf!', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.error).toEqual(new CellError(ErrorType.REF))
})
it('unknown error literal', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=#FOO!', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.error).toBeUndefined()
})
})
describe('cell references and ranges', () => {
it('absolute cell reference', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=$B$3', CellAddress.absolute(0, 1, 1)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference).toEqual(CellAddress.absolute(0, 1, 2))
})
it('relative cell reference', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=B3', CellAddress.absolute(0, 1, 1)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference).toEqual(CellAddress.relative(0, 0, 1))
})
it('absolute column cell reference', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=$B3', CellAddress.absolute(0, 1, 1)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference).toEqual(CellAddress.absoluteCol(0, 1, 1))
})
it('absolute row cell reference', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=B$3', CellAddress.absolute(0, 1, 1)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference).toEqual(CellAddress.absoluteRow(0, 0, 2))
})
it('cell references should not be case sensitive', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=d1', CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.col).toBe(3)
expect(ast.reference.row).toBe(0)
})
it('cell reference by default has sheet from the sheet it is written', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=D1', CellAddress.absolute(1, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
expect(ast.reference.col).toBe(3)
expect(ast.reference.row).toBe(0)
})
it('cell reference with sheet name', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet2!D1', CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
expect(ast.reference.col).toBe(3)
expect(ast.reference.row).toBe(0)
})
// incompatibility with product 1
it('using unknown sheet gives REF', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=Sheet2!A1', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.error).toEqual(new CellError(ErrorType.REF))
})
it('sheet name with other characters', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet_zażółć_gęślą_jaźń')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet_zażółć_gęślą_jaźń!A1', CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
})
xit('sheet name is case insensitive', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=shEEt2!A1', CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
})
it('sheet name with spaces', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet with spaces')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse("='Sheet with spaces'!A1", CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
})
it('sheet name inside quotes', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse("='Sheet2'!A1", CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
})
xit('escaping support', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet("Some'sheet")
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse("='Some''sheet'!A1", CellAddress.absolute(0, 0, 0)).ast as CellReferenceAst
expect(ast.type).toBe(AstNodeType.CELL_REFERENCE)
expect(ast.reference.sheet).toBe(1)
})
it('simple cell range', () => {
const parser = new ParserWithCaching(new Config(), new SheetMapping(enGB).get)
const ast = parser.parse('=A1:B2', CellAddress.absolute(0, 0, 0)).ast as CellRangeAst
expect(ast.type).toBe(AstNodeType.CELL_RANGE)
})
it('cell range with both start and end sheets specified', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet2!A1:Sheet2!B2', CellAddress.absolute(0, 0, 0)).ast as CellRangeAst
expect(ast.type).toBe(AstNodeType.CELL_RANGE)
expect(ast.start.sheet).toEqual(1)
expect(ast.end.sheet).toEqual(1)
})
it('cell range may have only sheet specified in start address', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet2!A1:B2', CellAddress.absolute(0, 0, 0)).ast as CellRangeAst
expect(ast.type).toBe(AstNodeType.CELL_RANGE)
expect(ast.start.sheet).toEqual(1)
expect(ast.end.sheet).toEqual(1)
})
it('cell range with different start and end sheets', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
sheetMapping.addSheet('Sheet3')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet2!A1:Sheet3!B2', CellAddress.absolute(0, 0, 0)).ast as CellRangeAst
expect(ast.type).toBe(AstNodeType.CELL_RANGE)
expect(ast.start.sheet).toEqual(1)
expect(ast.end.sheet).toEqual(2)
})
// weird error
it('cell range with unexisting end sheet', () => {
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
const parser = new ParserWithCaching(new Config(), sheetMapping.get)
const ast = parser.parse('=Sheet2!A1:Sheet3!B2', CellAddress.absolute(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.args[0].type).toBe(ParsingErrorType.RangeOffsetNotAllowed)
})
})