forked from ShaderFrog/glsl-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.test.ts
More file actions
689 lines (601 loc) · 12.3 KB
/
preprocessor.test.ts
File metadata and controls
689 lines (601 loc) · 12.3 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
import util from 'util';
import {
preprocessComments,
preprocessAst,
PreprocessorProgram,
} from './preprocessor.js';
import generate from './generator.js';
import { PreprocessorOptions } from './preprocessor.js';
import { GlslSyntaxError } from '../error.js';
import { buildPreprocessorParser } from '../parser/test-helpers.js';
let c!: ReturnType<typeof buildPreprocessorParser>;
let parse: (src: string, options?: PreprocessorOptions) => PreprocessorProgram;
beforeAll(() => {
c = buildPreprocessorParser();
parse = c.parse;
});
const debugProgram = (program: string): void => {
debugAst(c.parse(program));
};
const debugAst = (ast: any) => {
console.log(util.inspect(ast, false, null, true));
};
const expectParsedProgram = (
sourceGlsl: string,
options?: PreprocessorOptions
) => {
const ast = parse(sourceGlsl, options);
const glsl = generate(ast);
if (glsl !== sourceGlsl) {
debugAst(ast);
expect(glsl).toBe(sourceGlsl);
}
};
// test('pre test file', () => {
// expectParsedProgram(fileContents('./preprocess-test-grammar.glsl'));
// });
test('#preprocessComments', () => {
// Should strip comments and replace single-line comments with a single space
expect(
preprocessComments(`// ccc
/* cc */aaa/* cc */
/**
* cccc
*/
bbb
`)
).toBe(`
aaa
bbb
`);
});
test('preprocessor error', () => {
let error: GlslSyntaxError | undefined;
try {
parse(`#if defined(#)`);
} catch (e) {
error = e as GlslSyntaxError;
}
expect(error).toBeInstanceOf(GlslSyntaxError);
expect(error!.location.start.line).toBe(1);
expect(error!.location.end.line).toBe(1);
});
test('preprocessor ast', () => {
expectParsedProgram(`
#line 0
#version 100 "hi"
#define GL_es_profile 1
#extension all : disable
#error whoopsie
#define A 1
before if
#if A == 1 || B == 2
inside if
#define A
#elif A == 1 || defined B && C == 2
float a;
#elif A == 1 || defined(B) && C == 2
float a;
#define B
#endif
outside endif
#pragma mypragma: something(else)
final line after program
`);
});
test('directive whitespace', () => {
const program = `# define X Y
X`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`Y`);
});
test('nested expand macro', () => {
const program = `#define X Y
#define Y Z
X`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`Z`);
});
test('binary evaluation', () => {
const program = `
#if 1 + 1 > 0
true
#endif
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
true
`);
});
test('evaluates binary expression with simple macro', () => {
const program = `
#define A 1
#if A + 1 == 2
true
#endif
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
true
`);
});
test('evaluates binary expression with nested macro', () => {
const program = `
#define A 1
#define B A + 1
#if B == 2
true
#endif
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
true
`);
});
test('define inside if/else is properly expanded when the if branch is chosen', () => {
const program = `
#define MACRO
#ifdef MACRO
#define BRANCH a
#else
#define BRANCH b
#endif
BRANCH
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
a
`);
});
test('define inside if/else is properly expanded when the else branch is chosen', () => {
const program = `
#ifdef MACRO
#define BRANCH a
#else
#define BRANCH b
#endif
BRANCH
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
b
`);
});
test('ifdef inside else is properly expanded', () => {
// Regression: Make sure #ifdef MACRO inside #else isn't expanded
const program = `
#define MACRO
#ifdef NOT_DEFINED
false
#else
#ifdef MACRO
____true
#endif
#endif
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
____true
`);
});
test('macro without body becoms empty string', () => {
// There is intentionally whitespace after MACRO to make sure it doesn't apply
// to the expansion-to-nothing
const program = `
#define MACRO
fn(MACRO);
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
fn();
`);
});
test('if expression', () => {
const program = `
#define A
before if
#if !defined(A) && (defined(B) && C == 2)
inside first if
#endif
#if ((defined B && C == 2) || defined(A))
inside second if
#endif
after if
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
before if
inside second if
after if
`);
});
test('evaluate if branch', () => {
const program = `
#define A
before if
#if defined(A)
inside if
#endif
after if
`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
before if
inside if
after if
`);
});
test('evaluate elseif branch', () => {
const program = `
#define A
before if
#if defined(B)
inside if
#elif defined(A)
inside elif
#else
else body
#endif
after if`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
before if
inside elif
after if`);
});
test('empty branch', () => {
const program = `before if
#ifdef GL_ES
precision mediump float;
#endif
after if`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`before if
after if`);
});
test('evaluate else branch', () => {
const program = `
#define A
before if
#if defined(D)
inside if
#elif defined(E)
inside elif
#else
else body
#endif
after if`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
before if
else body
after if`);
});
test('self referential object macro', () => {
const program = `
#define first first second
#define second first
second`;
// If this has an infinte loop, the test will never finish
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
first second`);
});
test('self referential function macro', () => {
const program = `
#define foo() foo()
foo()`;
// If this has an infinte loop, the test will never finish
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
foo()`);
});
test('self referential macro combinations', () => {
const program = `
#define b c
#define first(a,b) a + b
#define second first(1,b)
second`;
// If this has an infinte loop, the test will never finish
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
1 + c`);
});
test("function call macro isn't expanded", () => {
const program = `
#define foo() no expand
foo`;
const ast = parse(program);
// debugAst(ast);
preprocessAst(ast);
expect(generate(ast)).toBe(`
foo`);
});
test(`function macro where source variable is same as macro argument`, () => {
const program = `
#define FN(x, y) x + y
FN(y, x);
FN(y.y, x.x);
FN(yy, xx);
`;
const ast = parse(program);
preprocessAst(ast);
// Ensure that if the argument passed to the fn FN(X) has the
// same name as the macro definition #define FN(X), it doesn't get expanded
// https://github.com/ShaderFrog/glsl-parser/issues/31
expect(generate(ast)).toBe(`
y + x;
y.y + x.x;
yy + xx;
`);
});
test("macro that isn't macro function call call is expanded", () => {
const program = `
#define foo () yes expand
foo`;
const ast = parse(program);
// debugAst(ast);
preprocessAst(ast);
expect(generate(ast)).toBe(`
() yes expand`);
});
test('unterminated macro function call', () => {
const program = `
#define foo() yes expand
foo(
foo()`;
const ast = parse(program);
expect(() => preprocessAst(ast)).toThrow(
'foo( unterminated macro invocation'
);
});
test('macro function calls with no arguments', () => {
const program = `
#define foo() yes expand
foo()
foo
()`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
yes expand
yes expand`);
});
test('macro function calls with bad arguments', () => {
expect(() => {
preprocessAst(
parse(`
#define foo( a, b ) a + b
foo(1,2,3)`)
);
}).toThrow("'foo': Too many arguments for macro");
expect(() => {
preprocessAst(
parse(`
#define foo( a ) a + b
foo(,)`)
);
}).toThrow("'foo': Too many arguments for macro");
expect(() => {
preprocessAst(
parse(`
#define foo( a, b ) a + b
foo(1)`)
);
}).toThrow("'foo': Not enough arguments for macro");
});
test('macro function calls with arguments', () => {
const program = `
#define foo( a, b ) a + b
foo(x + y, (z-t + vec3(0.0, 1.0)))
foo
(q,
r)
foo(,)`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
x + y + (z-t + vec3(0.0, 1.0))
q + r
+ `);
});
test('nested function macro expansion', () => {
const program = `
#define foo(x, y) x + y
foo (foo (a, b), c)
foo(foo (foo (x, y), z) , w)`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
a + b + c
x + y + z + w`);
});
test('nested function macro expansion referencing other macros', () => {
const program = `
#define foo(x, y) bar(x, y) + bar(y, x)
#define bar(x, y) (x * y)
foo(a, b)`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
(a * b) + (b * a)`);
});
test('macros that reference each other', () => {
const program = `
#define foo() bar()
#define bar() foo()
bar()`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
bar()`);
});
// The preprocessor does the wrong thing here so I'm commenting out this test
// for now. The current preprocessor results in bar(bar(1.0)). To achieve the
// correct result here I likely need to redo the expansion system to use "blue
// painting" https://en.wikipedia.org/wiki/Painted_blue
xtest('nested function macros that reference each other', () => {
const program = `
#define foo(x) bar(x)
#define bar(x) foo(x)
bar(foo(1.0))`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
bar(foo(1.0))`);
});
test('multi-pass cross-referencing object macros', () => {
const program = `
#define INNER x
#define OUTER (1.0/INNER)
OUTER
INNER`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
(1.0/x)
x`);
});
test('token pasting', () => {
const program = `
#define COMMAND(NAME) { NAME, NAME ## _command ## x ## y }
COMMAND(x)`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
{ x, x_commandxy }`);
});
test('preservation', () => {
const program = `
#line 0
#version 100 "hi"
#define GL_es_profile 1
#extension all : disable
#error whoopsie
#define A 1
before if
#if A == 1 || B == 2
inside if
#define A
#elif A == 1 || defined(B) && C == 2
float a;
#define B
#endif
outside endif
#pragma mypragma: something(else)
function_call line after program`;
const ast = parse(program);
preprocessAst(ast, {
preserve: {
conditional: () => false,
line: () => true,
error: () => true,
extension: () => true,
pragma: () => true,
version: () => true,
},
});
expect(generate(ast)).toBe(`
#line 0
#version 100 "hi"
#extension all : disable
#error whoopsie
before if
inside if
outside endif
#pragma mypragma: something(else)
function_call line after program`);
});
test('different line breaks character', () => {
const program = '#ifndef x\rfloat a = 1.0;\r\n#endif';
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe('float a = 1.0;\r\n');
});
test('generate #ifdef & #ifndef & #else', () => {
expectParsedProgram(`
#ifdef AA
float a;
#else
float b;
#endif
#ifndef CC
float c;
#endif
#if AA == 2
float d;
#endif
`);
});
test('test macro with "defined" at start of name', () => {
const program = `
#define definedX 1
#if defined(definedX) && defined definedX && definedX
true
#endif
`;
expectParsedProgram(program);
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
true
`);
});
test('inline comments in if statement expression', () => {
const program = `
#define AAA
#define BBB
#if defined/**/AAA && defined/**/ BBB
true
#endif
`;
expectParsedProgram(program, { preserveComments: true });
const ast = parse(program, { preserveComments: true });
preprocessAst(ast);
expect(generate(ast)).toBe(`
true
`);
});
test('multiline macros', () => {
const program = `
#define X a\\
b
#define Y \\
c\\
d\\
#define Z \\
e \\
f
#define W\\
vec3 x() {
X
Y
Z
W
}`;
const ast = parse(program);
preprocessAst(ast);
expect(generate(ast)).toBe(`
vec3 x() {
a b
cd
e f
}`);
});