wesl 0.7.26 → 0.7.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.ts +283 -147
  3. package/dist/index.js +1765 -1143
  4. package/package.json +2 -2
  5. package/src/AbstractElems.ts +264 -82
  6. package/src/ClickableError.ts +8 -1
  7. package/src/Linker.ts +63 -67
  8. package/src/LinkerUtil.ts +141 -7
  9. package/src/LowerAndEmit.ts +660 -304
  10. package/src/Mangler.ts +1 -1
  11. package/src/ModuleResolver.ts +15 -4
  12. package/src/ParseWESL.ts +21 -33
  13. package/src/SrcMap.ts +7 -19
  14. package/src/StandardTypes.ts +1 -1
  15. package/src/WeslDevice.ts +1 -0
  16. package/src/debug/ASTtoString.ts +92 -76
  17. package/src/index.ts +1 -1
  18. package/src/parse/AttachComments.ts +289 -0
  19. package/src/parse/ExpressionUtil.ts +3 -3
  20. package/src/parse/Keywords.ts +1 -1
  21. package/src/parse/ParseAttribute.ts +49 -71
  22. package/src/parse/ParseCall.ts +3 -4
  23. package/src/parse/ParseControlFlow.ts +100 -56
  24. package/src/parse/ParseDirective.ts +9 -8
  25. package/src/parse/ParseDoBlock.ts +63 -0
  26. package/src/parse/ParseExpression.ts +11 -10
  27. package/src/parse/ParseFn.ts +11 -33
  28. package/src/parse/ParseGlobalVar.ts +36 -27
  29. package/src/parse/ParseIdent.ts +4 -5
  30. package/src/parse/ParseLocalVar.ts +16 -12
  31. package/src/parse/ParseLoop.ts +76 -39
  32. package/src/parse/ParseModule.ts +65 -19
  33. package/src/parse/ParseSimpleStatement.ts +112 -66
  34. package/src/parse/ParseStatement.ts +40 -67
  35. package/src/parse/ParseStruct.ts +8 -22
  36. package/src/parse/ParseType.ts +10 -13
  37. package/src/parse/ParseUtil.ts +26 -12
  38. package/src/parse/ParseValueDeclaration.ts +18 -31
  39. package/src/parse/ParseWesl.ts +11 -14
  40. package/src/parse/ParsingContext.ts +11 -9
  41. package/src/parse/WeslStream.ts +138 -121
  42. package/src/parse/stream/RegexMatchers.ts +45 -0
  43. package/src/parse/stream/WeslLexer.ts +249 -0
  44. package/src/test/BevyLink.test.ts +18 -11
  45. package/src/test/ConditionalElif.test.ts +4 -2
  46. package/src/test/DeclCommentEmit.test.ts +122 -0
  47. package/src/test/DoBlock.test.ts +164 -0
  48. package/src/test/FilterValidElements.test.ts +4 -6
  49. package/src/test/Linker.test.ts +23 -7
  50. package/src/test/Mangling.test.ts +8 -4
  51. package/src/test/ParseComments.test.ts +234 -6
  52. package/src/test/ParseConditionsV2.test.ts +47 -175
  53. package/src/test/ParseElifV2.test.ts +12 -33
  54. package/src/test/ParseErrorV2.test.ts +0 -0
  55. package/src/test/ParseWeslV2.test.ts +247 -626
  56. package/src/test/StatementEmit.test.ts +143 -0
  57. package/src/test/StripWesl.ts +24 -3
  58. package/src/test/TestLink.ts +19 -3
  59. package/src/test/TestUtil.ts +18 -8
  60. package/src/test/Tokenizer.test.ts +96 -0
  61. package/src/test/__snapshots__/ParseWeslV2.test.ts.snap +10 -42
  62. package/src/RawEmit.ts +0 -103
  63. package/src/Reflection.ts +0 -336
  64. package/src/TransformBindingStructs.ts +0 -320
  65. package/src/parse/ContentsHelpers.ts +0 -70
  66. package/src/parse/stream/CachingStream.ts +0 -48
  67. package/src/parse/stream/MatchersStream.ts +0 -85
@@ -15,8 +15,7 @@ test("parse fn foo() { }", () => {
15
15
  "module
16
16
  fn foo()
17
17
  decl %foo
18
- statement
19
- text '{ }'"
18
+ block"
20
19
  `);
21
20
  });
22
21
 
@@ -27,17 +26,13 @@ test("parse fn with calls", () => {
27
26
  "module
28
27
  fn foo()
29
28
  decl %foo
30
- statement
31
- text '{'
32
- statement
33
- text ' '
34
- ref foo
35
- text '();'
36
- statement
37
- text ' '
38
- ref bar
39
- text '();'
40
- text ' }'"
29
+ block
30
+ call
31
+ call-expression call
32
+ ref foo
33
+ call
34
+ call-expression call
35
+ ref bar"
41
36
  `);
42
37
  });
43
38
 
@@ -67,13 +62,11 @@ test("parse global var", () => {
67
62
  expect(astString).toMatchInlineSnapshot(`
68
63
  "module
69
64
  gvar %x : i32
70
- text 'var '
71
65
  typeDecl %x : i32
72
66
  decl %x
73
- text ': '
74
67
  type i32
75
68
  ref i32
76
- text ' = 1;'"
69
+ literal literal(1)"
77
70
  `);
78
71
  });
79
72
 
@@ -84,12 +77,9 @@ test("parse alias", () => {
84
77
  expect(astString).toMatchInlineSnapshot(`
85
78
  "module
86
79
  alias %Num=i32
87
- text 'alias '
88
80
  decl %Num
89
- text ' = '
90
81
  type i32
91
- ref i32
92
- text ';'"
82
+ ref i32"
93
83
  `);
94
84
  });
95
85
 
@@ -100,10 +90,9 @@ test("parse const", () => {
100
90
  expect(astString).toMatchInlineSnapshot(`
101
91
  "module
102
92
  const %y
103
- text 'const '
104
93
  typeDecl %y
105
94
  decl %y
106
- text ' = 11u;'"
95
+ literal literal(11u)"
107
96
  `);
108
97
  });
109
98
 
@@ -114,13 +103,10 @@ test("parse override ", () => {
114
103
  expect(astString).toMatchInlineSnapshot(`
115
104
  "module
116
105
  override %z : f32
117
- text 'override '
118
106
  typeDecl %z : f32
119
107
  decl %z
120
- text ': '
121
108
  type f32
122
- ref f32
123
- text ';'"
109
+ ref f32"
124
110
  `);
125
111
  });
126
112
 
@@ -131,11 +117,9 @@ test("parse const_assert", () => {
131
117
  expect(astString).toMatchInlineSnapshot(`
132
118
  "module
133
119
  assert
134
- text 'const_assert '
135
- ref x
136
- text ' < '
137
- ref y
138
- text ';'"
120
+ binary-expression binop(<)
121
+ ref x
122
+ ref y"
139
123
  `);
140
124
  });
141
125
 
@@ -146,22 +130,15 @@ test("parse struct", () => {
146
130
  expect(astString).toMatchInlineSnapshot(`
147
131
  "module
148
132
  struct foo
149
- text 'struct '
150
133
  decl %foo
151
- text ' { '
152
134
  member bar: i32
153
135
  name bar
154
- text ': '
155
136
  type i32
156
137
  ref i32
157
- text ', '
158
138
  member zip: u32
159
139
  name zip
160
- text ': '
161
140
  type u32
162
- ref u32
163
- text ', }'
164
- text ' ;'"
141
+ ref u32"
165
142
  `);
166
143
  });
167
144
 
@@ -175,18 +152,10 @@ test("parse global diagnostic", () => {
175
152
  const astString = astToString(ast.moduleElem);
176
153
  expect(astString).toMatchInlineSnapshot(`
177
154
  "module
178
- text '
179
- '
180
155
  directive diagnostic(off, derivative_uniformity)
181
- text '
182
-
183
- '
184
156
  fn main()
185
157
  decl %main
186
- statement
187
- text '{}'
188
- text '
189
- '"
158
+ block"
190
159
  `);
191
160
  });
192
161
 
@@ -199,9 +168,7 @@ test("parse @attribute before fn", () => {
199
168
  fn main() @compute
200
169
  attribute @compute
201
170
  decl %main
202
- statement
203
- text '{}'
204
- text ' '"
171
+ block"
205
172
  `);
206
173
  });
207
174
 
@@ -215,16 +182,11 @@ test("parse @compute @workgroup_size(a, b, 1) before fn", () => {
215
182
  const astString = astToString(ast.moduleElem);
216
183
  expect(astString).toMatchInlineSnapshot(`
217
184
  "module
218
- text '
219
- '
220
185
  fn main() @compute @workgroup_size
221
186
  attribute @compute
222
- attribute @workgroup_size(ref a, ' ' ref b, ' 1')
187
+ attribute @workgroup_size(ref a, ref b, literal literal(1))
223
188
  decl %main
224
- statement
225
- text '{}'
226
- text '
227
- '"
189
+ block"
228
190
  `);
229
191
  });
230
192
 
@@ -238,28 +200,17 @@ test("parse top level var", () => {
238
200
  const astString = astToString(ast.moduleElem);
239
201
  expect(astString).toMatchInlineSnapshot(`
240
202
  "module
241
- text '
242
- '
243
203
  gvar %u : Uniforms @group @binding
244
- attribute @group('0')
245
- text ' '
246
- attribute @binding('0')
247
- text ' var<uniform> '
204
+ attribute @group(literal literal(0))
205
+ attribute @binding(literal literal(0))
206
+ name uniform
248
207
  typeDecl %u : Uniforms
249
208
  decl %u
250
- text ': '
251
209
  type Uniforms
252
210
  ref Uniforms
253
- text ';'
254
- text '
255
-
256
- '
257
211
  fn main()
258
212
  decl %main
259
- statement
260
- text '{}'
261
- text '
262
- '"
213
+ block"
263
214
  `);
264
215
  });
265
216
 
@@ -274,29 +225,17 @@ test("parse top level override and const", () => {
274
225
  const astString = astToString(ast.moduleElem);
275
226
  expect(astString).toMatchInlineSnapshot(`
276
227
  "module
277
- text '
278
- '
279
228
  override %x
280
- text 'override '
281
229
  typeDecl %x
282
230
  decl %x
283
- text ' = 21;'
284
- text '
285
- '
231
+ literal literal(21)
286
232
  const %y
287
- text 'const '
288
233
  typeDecl %y
289
234
  decl %y
290
- text ' = 1;'
291
- text '
292
-
293
- '
235
+ literal literal(1)
294
236
  fn main()
295
237
  decl %main
296
- statement
297
- text '{}'
298
- text '
299
- '"
238
+ block"
300
239
  `);
301
240
  });
302
241
 
@@ -304,10 +243,7 @@ test("parse root level ;;", () => {
304
243
  const src = ";;";
305
244
  const ast = parseTest(src);
306
245
  const astString = astToString(ast.moduleElem);
307
- expect(astString).toMatchInlineSnapshot(`
308
- "module
309
- text ';;'"
310
- `);
246
+ expect(astString).toMatchInlineSnapshot(`"module"`);
311
247
  });
312
248
 
313
249
  test("parse simple alias", () => {
@@ -317,12 +253,9 @@ test("parse simple alias", () => {
317
253
  expect(astString).toMatchInlineSnapshot(`
318
254
  "module
319
255
  alias %NewType=OldType
320
- text 'alias '
321
256
  decl %NewType
322
- text ' = '
323
257
  type OldType
324
- ref OldType
325
- text ';'"
258
+ ref OldType"
326
259
  `);
327
260
  });
328
261
 
@@ -334,20 +267,12 @@ test("parse array alias", () => {
334
267
  const astString = astToString(ast.moduleElem);
335
268
  expect(astString).toMatchInlineSnapshot(`
336
269
  "module
337
- text '
338
- '
339
- alias %Points3=array<ref Point, literal literal(3)>
340
- text 'alias '
270
+ alias %Points3=array<Point, 3>
341
271
  decl %Points3
342
- text ' = '
343
- type array<ref Point, literal literal(3)>
272
+ type array<Point, 3>
344
273
  ref array
345
- text '<'
346
274
  ref Point
347
- text ', 3>'
348
- text ';'
349
- text '
350
- '"
275
+ literal literal(3)"
351
276
  `);
352
277
  });
353
278
 
@@ -361,8 +286,7 @@ test("fnDecl parses fn with return type", () => {
361
286
  decl %foo
362
287
  type MyType
363
288
  ref MyType
364
- statement
365
- text '{ }'"
289
+ block"
366
290
  `);
367
291
  });
368
292
 
@@ -374,20 +298,14 @@ test("fnDecl parses :type specifier in fn args", () => {
374
298
  const astString = astToString(ast.moduleElem);
375
299
  expect(astString).toMatchInlineSnapshot(`
376
300
  "module
377
- text '
378
- '
379
301
  fn foo(a: MyType)
380
302
  decl %foo
381
303
  param
382
304
  typeDecl %a : MyType
383
305
  decl %a
384
- text ': '
385
306
  type MyType
386
307
  ref MyType
387
- statement
388
- text '{ }'
389
- text '
390
- '"
308
+ block"
391
309
  `);
392
310
  });
393
311
 
@@ -401,25 +319,14 @@ test("fnDecl parses :type specifier in fn block", () => {
401
319
  const astString = astToString(ast.moduleElem);
402
320
  expect(astString).toMatchInlineSnapshot(`
403
321
  "module
404
- text '
405
- '
406
322
  fn foo()
407
323
  decl %foo
408
- statement
409
- text '{
410
- '
324
+ block
411
325
  var %b : MyType
412
- text 'var '
413
326
  typeDecl %b : MyType
414
327
  decl %b
415
- text ':'
416
328
  type MyType
417
- ref MyType
418
- text ';'
419
- text '
420
- }'
421
- text '
422
- '"
329
+ ref MyType"
423
330
  `);
424
331
  });
425
332
 
@@ -430,22 +337,15 @@ test("parse type in <template> in fn args", () => {
430
337
  const astString = astToString(ast.moduleElem);
431
338
  expect(astString).toMatchInlineSnapshot(`
432
339
  "module
433
- text '
434
- '
435
- fn foo(a: vec2<ref MyStruct>)
340
+ fn foo(a: vec2<MyStruct>)
436
341
  decl %foo
437
342
  param
438
- typeDecl %a : vec2<ref MyStruct>
343
+ typeDecl %a : vec2<MyStruct>
439
344
  decl %a
440
- text ': '
441
- type vec2<ref MyStruct>
345
+ type vec2<MyStruct>
442
346
  ref vec2
443
- text '<'
444
347
  ref MyStruct
445
- text '>'
446
- statement
447
- text '{ }'
448
- text ';'"
348
+ block"
449
349
  `);
450
350
  });
451
351
 
@@ -456,19 +356,16 @@ test("parse simple templated type", () => {
456
356
  const astString = astToString(ast.moduleElem);
457
357
  expect(astString).toMatchInlineSnapshot(`
458
358
  "module
459
- fn main(a: array<ref MyStruct, literal literal(4)>)
359
+ fn main(a: array<MyStruct, 4>)
460
360
  decl %main
461
361
  param
462
- typeDecl %a : array<ref MyStruct, literal literal(4)>
362
+ typeDecl %a : array<MyStruct, 4>
463
363
  decl %a
464
- text ': '
465
- type array<ref MyStruct, literal literal(4)>
364
+ type array<MyStruct, 4>
466
365
  ref array
467
- text '<'
468
366
  ref MyStruct
469
- text ',4>'
470
- statement
471
- text '{ }'"
367
+ literal literal(4)
368
+ block"
472
369
  `);
473
370
  });
474
371
 
@@ -478,19 +375,16 @@ test("parse with space before template", () => {
478
375
  const astString = astToString(ast.moduleElem);
479
376
  expect(astString).toMatchInlineSnapshot(`
480
377
  "module
481
- fn main(a: array<ref MyStruct, literal literal(4)>)
378
+ fn main(a: array<MyStruct, 4>)
482
379
  decl %main
483
380
  param
484
- typeDecl %a : array<ref MyStruct, literal literal(4)>
381
+ typeDecl %a : array<MyStruct, 4>
485
382
  decl %a
486
- text ': '
487
- type array<ref MyStruct, literal literal(4)>
383
+ type array<MyStruct, 4>
488
384
  ref array
489
- text ' <'
490
385
  ref MyStruct
491
- text ',4>'
492
- statement
493
- text '{ }'"
386
+ literal literal(4)
387
+ block"
494
388
  `);
495
389
  });
496
390
 
@@ -500,21 +394,18 @@ test("parse nested template that ends with >> ", () => {
500
394
  const astString = astToString(ast.moduleElem);
501
395
  expect(astString).toMatchInlineSnapshot(`
502
396
  "module
503
- fn main(a: vec2<array<ref MyStruct, literal literal(4)>>)
397
+ fn main(a: vec2<array<MyStruct, 4>>)
504
398
  decl %main
505
399
  param
506
- typeDecl %a : vec2<array<ref MyStruct, literal literal(4)>>
400
+ typeDecl %a : vec2<array<MyStruct, 4>>
507
401
  decl %a
508
- text ': '
509
- type vec2<array<ref MyStruct, literal literal(4)>>
402
+ type vec2<array<MyStruct, 4>>
510
403
  ref vec2
511
- text '<'
512
- ref array
513
- text ' <'
514
- ref MyStruct
515
- text ',4>>'
516
- statement
517
- text '{ }'"
404
+ type array<MyStruct, 4>
405
+ ref array
406
+ ref MyStruct
407
+ literal literal(4)
408
+ block"
518
409
  `);
519
410
  });
520
411
 
@@ -525,17 +416,14 @@ test("parse type in <template> in global var", () => {
525
416
  const astString = astToString(ast.moduleElem);
526
417
  expect(astString).toMatchInlineSnapshot(`
527
418
  "module
528
- gvar %x : array<ref MyStruct, literal literal(8)>
529
- text 'var<private> '
530
- typeDecl %x : array<ref MyStruct, literal literal(8)>
419
+ gvar %x : array<MyStruct, 8>
420
+ name private
421
+ typeDecl %x : array<MyStruct, 8>
531
422
  decl %x
532
- text ':'
533
- type array<ref MyStruct, literal literal(8)>
423
+ type array<MyStruct, 8>
534
424
  ref array
535
- text '<'
536
425
  ref MyStruct
537
- text ', 8>'
538
- text ';'"
426
+ literal literal(8)"
539
427
  `);
540
428
  });
541
429
 
@@ -549,31 +437,20 @@ test("parse for(;;) {} not as a fn call", () => {
549
437
  const astString = astToString(ast.moduleElem);
550
438
  expect(astString).toMatchInlineSnapshot(`
551
439
  "module
552
- text '
553
- '
554
440
  fn main()
555
441
  decl %main
556
- statement
557
- text '{'
558
- statement
559
- text '
560
- for ('
442
+ block
443
+ for
561
444
  var %a
562
- text 'var '
563
445
  typeDecl %a
564
446
  decl %a
565
- text ' = 1;'
566
- text ' '
567
- ref a
568
- text ' < 10; '
569
- ref a
570
- text '++) '
571
- statement
572
- text '{}'
573
- text '
574
- }'
575
- text '
576
- '"
447
+ literal literal(1)
448
+ binary-expression binop(<)
449
+ ref a
450
+ literal literal(10)
451
+ increment
452
+ ref a
453
+ block"
577
454
  `);
578
455
  });
579
456
 
@@ -585,14 +462,9 @@ test("eolf followed by blank line", () => {
585
462
  const astString = astToString(ast.moduleElem);
586
463
  expect(astString).toMatchInlineSnapshot(`
587
464
  "module
588
- text '
589
- '
590
465
  fn foo()
591
466
  decl %foo
592
- statement
593
- text '{ }'
594
- text '
595
- '"
467
+ block"
596
468
  `);
597
469
  });
598
470
 
@@ -609,35 +481,24 @@ test("parse fn with attributes and suffix comma", () => {
609
481
  const astString = astToString(ast.moduleElem);
610
482
  expect(astString).toMatchInlineSnapshot(`
611
483
  "module
612
- text '
613
- '
614
- fn main(grid: vec3<ref u32>, localIndex: u32) @compute @workgroup_size
484
+ fn main(grid: vec3<u32>, localIndex: u32) @compute @workgroup_size
615
485
  attribute @compute
616
- attribute @workgroup_size(ref workgroupThreads, ' 1', ' 1')
486
+ attribute @workgroup_size(ref workgroupThreads, literal literal(1), literal literal(1))
617
487
  decl %main
618
488
  param
619
489
  attribute @builtin(global_invocation_id)
620
- text ' '
621
- typeDecl %grid : vec3<ref u32>
490
+ typeDecl %grid : vec3<u32>
622
491
  decl %grid
623
- text ': '
624
- type vec3<ref u32>
492
+ type vec3<u32>
625
493
  ref vec3
626
- text '<'
627
494
  ref u32
628
- text '>'
629
495
  param
630
496
  attribute @builtin(local_invocation_index)
631
- text ' '
632
497
  typeDecl %localIndex : u32
633
498
  decl %localIndex
634
- text ': '
635
499
  type u32
636
500
  ref u32
637
- statement
638
- text '{ }'
639
- text '
640
- '"
501
+ block"
641
502
  `);
642
503
  });
643
504
 
@@ -652,22 +513,18 @@ test("parse fn", () => {
652
513
  param
653
514
  typeDecl %x : i32
654
515
  decl %x
655
- text ': '
656
516
  type i32
657
517
  ref i32
658
518
  param
659
519
  typeDecl %y : u32
660
520
  decl %y
661
- text ': '
662
521
  type u32
663
522
  ref u32
664
523
  type f32
665
524
  ref f32
666
- statement
667
- text '{'
668
- statement
669
- text ' return 1.0;'
670
- text ' }'"
525
+ block
526
+ return
527
+ literal literal(1.0)"
671
528
  `);
672
529
  });
673
530
 
@@ -680,9 +537,7 @@ test("parse @attribute before fn", () => {
680
537
  fn main() @compute
681
538
  attribute @compute
682
539
  decl %main
683
- statement
684
- text '{}'
685
- text ' '"
540
+ block"
686
541
  `);
687
542
  });
688
543
 
@@ -704,13 +559,10 @@ test("parse foo::bar(); ", () => {
704
559
  "module
705
560
  fn main()
706
561
  decl %main
707
- statement
708
- text '{'
709
- statement
710
- text ' '
711
- ref foo::bar
712
- text '();'
713
- text ' }'"
562
+ block
563
+ call
564
+ call-expression call
565
+ ref foo::bar"
714
566
  `);
715
567
  });
716
568
 
@@ -722,17 +574,13 @@ test("parse let x: foo::bar; ", () => {
722
574
  "module
723
575
  fn main()
724
576
  decl %main
725
- statement
726
- text '{ '
577
+ block
727
578
  let %x : foo::bar
728
- text 'let '
729
579
  typeDecl %x : foo::bar
730
580
  decl %x
731
- text ': '
732
581
  type foo::bar
733
582
  ref foo::bar
734
- text ' = 1;'
735
- text ' }'"
583
+ literal literal(1)"
736
584
  `);
737
585
  });
738
586
 
@@ -746,24 +594,15 @@ test("parse var x: foo::bar;", () => {
746
594
  const astString = astToString(ast.moduleElem);
747
595
  expect(astString).toMatchInlineSnapshot(`
748
596
  "module
749
- text '
750
- '
751
597
  gvar %x : foo::bar
752
- text 'var<private> '
598
+ name private
753
599
  typeDecl %x : foo::bar
754
600
  decl %x
755
- text ': '
756
601
  type foo::bar
757
602
  ref foo::bar
758
- text ';'
759
- text '
760
- '
761
603
  fn main()
762
604
  decl %main
763
- statement
764
- text '{ }'
765
- text '
766
- '"
605
+ block"
767
606
  `);
768
607
  });
769
608
 
@@ -780,45 +619,24 @@ test("parse switch statement", () => {
780
619
  const astString = astToString(ast.moduleElem);
781
620
  expect(astString).toMatchInlineSnapshot(`
782
621
  "module
783
- text '
784
- '
785
622
  fn main(x: i32)
786
623
  decl %main
787
624
  param
788
625
  typeDecl %x : i32
789
626
  decl %x
790
- text ': '
791
627
  type i32
792
628
  ref i32
793
- statement
794
- text '{'
795
- statement
796
- text '
797
- switch ('
798
- ref x
799
- text ') {'
629
+ block
630
+ switch
631
+ parenthesized-expression parens
632
+ ref x
800
633
  switch-clause
801
- text '
802
- case 1: '
803
- statement
804
- text '{'
805
- statement
806
- text ' break;'
807
- text ' }'
634
+ literal literal(1)
635
+ block
636
+ break
808
637
  switch-clause
809
- text '
810
- default: '
811
- statement
812
- text '{'
813
- statement
814
- text ' break;'
815
- text ' }'
816
- text '
817
- }'
818
- text '
819
- }'
820
- text '
821
- '"
638
+ block
639
+ break"
822
640
  `);
823
641
  });
824
642
 
@@ -836,48 +654,28 @@ test("parse switch statement-2", () => {
836
654
  const astString = astToString(ast.moduleElem);
837
655
  expect(astString).toMatchInlineSnapshot(`
838
656
  "module
839
- text '
840
-
841
- '
842
657
  fn main(x: u32)
843
658
  decl %main
844
659
  param
845
660
  typeDecl %x : u32
846
661
  decl %x
847
- text ': '
848
662
  type u32
849
663
  ref u32
850
- statement
851
- text '{'
852
- statement
853
- text '
854
- switch ( '
855
- ref code
856
- text ' ) {'
664
+ block
665
+ switch
666
+ parenthesized-expression parens
667
+ ref code
857
668
  switch-clause
858
- text '
859
- case 5u: '
860
- statement
861
- text '{'
862
- statement
863
- text ' if 1 > 0 '
864
- statement
865
- text '{ }'
866
- text ' }'
669
+ literal literal(5u)
670
+ block
671
+ if
672
+ binary-expression binop(>)
673
+ literal literal(1)
674
+ literal literal(0)
675
+ block
867
676
  switch-clause
868
- text '
869
- default: '
870
- statement
871
- text '{'
872
- statement
873
- text ' break;'
874
- text ' }'
875
- text '
876
- }'
877
- text '
878
- }'
879
- text '
880
- '"
677
+ block
678
+ break"
881
679
  `);
882
680
  });
883
681
 
@@ -891,24 +689,15 @@ test("parse struct constructor in assignment", () => {
891
689
  const astString = astToString(ast.moduleElem);
892
690
  expect(astString).toMatchInlineSnapshot(`
893
691
  "module
894
- text '
895
- '
896
692
  fn main()
897
693
  decl %main
898
- statement
899
- text '{
900
- '
694
+ block
901
695
  var %x
902
- text 'var '
903
696
  typeDecl %x
904
697
  decl %x
905
- text ' = '
906
- ref AStruct
907
- text '(1u);'
908
- text '
909
- }'
910
- text '
911
- '"
698
+ call-expression call
699
+ ref AStruct
700
+ literal literal(1u)"
912
701
  `);
913
702
  });
914
703
 
@@ -922,24 +711,15 @@ test("parse struct.member (component_or_swizzle)", () => {
922
711
  const astString = astToString(ast.moduleElem);
923
712
  expect(astString).toMatchInlineSnapshot(`
924
713
  "module
925
- text '
926
- '
927
714
  fn main()
928
715
  decl %main
929
- statement
930
- text '{
931
- '
716
+ block
932
717
  let %x
933
- text 'let '
934
718
  typeDecl %x
935
719
  decl %x
936
- text ' = '
937
- ref u
938
- text '.frame;'
939
- text '
940
- }'
941
- text '
942
- '"
720
+ component-member-expression .
721
+ ref u
722
+ name frame"
943
723
  `);
944
724
  });
945
725
 
@@ -948,17 +728,14 @@ test("var<workgroup> work: array<u32, 128>;", ctx => {
948
728
  const astString = astToString(ast.moduleElem);
949
729
  expect(astString).toMatchInlineSnapshot(`
950
730
  "module
951
- gvar %work : array<ref u32, literal literal(128)>
952
- text 'var<workgroup> '
953
- typeDecl %work : array<ref u32, literal literal(128)>
731
+ gvar %work : array<u32, 128>
732
+ name workgroup
733
+ typeDecl %work : array<u32, 128>
954
734
  decl %work
955
- text ': '
956
- type array<ref u32, literal literal(128)>
735
+ type array<u32, 128>
957
736
  ref array
958
- text '<'
959
737
  ref u32
960
- text ', 128>'
961
- text ';'"
738
+ literal literal(128)"
962
739
  `);
963
740
  });
964
741
 
@@ -969,11 +746,9 @@ test("fn f() { _ = 1; }", ctx => {
969
746
  "module
970
747
  fn f()
971
748
  decl %f
972
- statement
973
- text '{'
974
- statement
975
- text ' _ = 1;'
976
- text ' }'"
749
+ block
750
+ assign
751
+ literal literal(1)"
977
752
  `);
978
753
  });
979
754
 
@@ -982,19 +757,17 @@ test("var foo: vec2<f32 >= vec2( 0.5, -0.5);", ctx => {
982
757
  const astString = astToString(ast.moduleElem);
983
758
  expect(astString).toMatchInlineSnapshot(`
984
759
  "module
985
- gvar %foo : vec2<ref f32>
986
- text 'var '
987
- typeDecl %foo : vec2<ref f32>
760
+ gvar %foo : vec2<f32>
761
+ typeDecl %foo : vec2<f32>
988
762
  decl %foo
989
- text ': '
990
- type vec2<ref f32>
763
+ type vec2<f32>
991
764
  ref vec2
992
- text '<'
993
765
  ref f32
994
- text ' >'
995
- text '= '
996
- ref vec2
997
- text '( 0.5, -0.5);'"
766
+ call-expression call
767
+ ref vec2
768
+ literal literal(0.5)
769
+ unary-expression unop(-)
770
+ literal literal(0.5)"
998
771
  `);
999
772
  });
1000
773
 
@@ -1005,22 +778,20 @@ test("fn main() { var tmp: array<i32, 1 << 1>=array(1, 2); }", ctx => {
1005
778
  "module
1006
779
  fn main()
1007
780
  decl %main
1008
- statement
1009
- text '{ '
1010
- var %tmp : array<ref i32, binary-expression binop(<<)>
1011
- text 'var '
1012
- typeDecl %tmp : array<ref i32, binary-expression binop(<<)>
781
+ block
782
+ var %tmp : array<i32, 1 << 1>
783
+ typeDecl %tmp : array<i32, 1 << 1>
1013
784
  decl %tmp
1014
- text ': '
1015
- type array<ref i32, binary-expression binop(<<)>
785
+ type array<i32, 1 << 1>
1016
786
  ref array
1017
- text '<'
1018
787
  ref i32
1019
- text ', 1 << 1>'
1020
- text '='
1021
- ref array
1022
- text '(1, 2);'
1023
- text ' }'"
788
+ binary-expression binop(<<)
789
+ literal literal(1)
790
+ literal literal(1)
791
+ call-expression call
792
+ ref array
793
+ literal literal(1)
794
+ literal literal(2)"
1024
795
  `);
1025
796
  });
1026
797
 
@@ -1088,25 +859,14 @@ test(`parse ptr`, () => {
1088
859
  const astString = astToString(ast.moduleElem);
1089
860
  expect(astString).toMatchInlineSnapshot(`
1090
861
  "module
1091
- text '
1092
- '
1093
- gvar %particles : ptr<ref storage, ref f32, ref read_write>
1094
- text 'var '
1095
- typeDecl %particles : ptr<ref storage, ref f32, ref read_write>
862
+ gvar %particles : ptr<storage, f32, read_write>
863
+ typeDecl %particles : ptr<storage, f32, read_write>
1096
864
  decl %particles
1097
- text ': '
1098
- type ptr<ref storage, ref f32, ref read_write>
865
+ type ptr<storage, f32, read_write>
1099
866
  ref ptr
1100
- text '<'
1101
867
  ref storage
1102
- text ', '
1103
868
  ref f32
1104
- text ', '
1105
- ref read_write
1106
- text '>'
1107
- text ';'
1108
- text '
1109
- '"
869
+ ref read_write"
1110
870
  `);
1111
871
  });
1112
872
 
@@ -1118,27 +878,16 @@ test(`parse ptr with internal array`, () => {
1118
878
  const astString = astToString(ast.moduleElem);
1119
879
  expect(astString).toMatchInlineSnapshot(`
1120
880
  "module
1121
- text '
1122
- '
1123
- gvar %particles : ptr<ref storage, array<ref f32>, ref read_write>
1124
- text 'var '
1125
- typeDecl %particles : ptr<ref storage, array<ref f32>, ref read_write>
881
+ gvar %particles : ptr<storage, array<f32>, read_write>
882
+ typeDecl %particles : ptr<storage, array<f32>, read_write>
1126
883
  decl %particles
1127
- text ': '
1128
- type ptr<ref storage, array<ref f32>, ref read_write>
884
+ type ptr<storage, array<f32>, read_write>
1129
885
  ref ptr
1130
- text '<'
1131
886
  ref storage
1132
- text ', '
1133
- ref array
1134
- text '<'
1135
- ref f32
1136
- text '>, '
1137
- ref read_write
1138
- text '>'
1139
- text ';'
1140
- text '
1141
- '"
887
+ type array<f32>
888
+ ref array
889
+ ref f32
890
+ ref read_write"
1142
891
  `);
1143
892
  });
1144
893
 
@@ -1152,35 +901,19 @@ test(`parse binding struct`, () => {
1152
901
  const astString = astToString(ast.moduleElem);
1153
902
  expect(astString).toMatchInlineSnapshot(`
1154
903
  "module
1155
- text '
1156
- '
1157
904
  struct Bindings
1158
- text 'struct '
1159
905
  decl %Bindings
1160
- text ' {
1161
- '
1162
- member @group @binding particles: ptr<ref storage, array<ref f32>, ref read_write>
1163
- attribute @group('0')
1164
- text ' '
1165
- attribute @binding('0')
1166
- text ' '
906
+ member @group @binding particles: ptr<storage, array<f32>, read_write>
907
+ attribute @group(literal literal(0))
908
+ attribute @binding(literal literal(0))
1167
909
  name particles
1168
- text ': '
1169
- type ptr<ref storage, array<ref f32>, ref read_write>
910
+ type ptr<storage, array<f32>, read_write>
1170
911
  ref ptr
1171
- text '<'
1172
912
  ref storage
1173
- text ', '
1174
- ref array
1175
- text '<'
1176
- ref f32
1177
- text '>, '
1178
- ref read_write
1179
- text '>'
1180
- text ',
1181
- }'
1182
- text '
1183
- '"
913
+ type array<f32>
914
+ ref array
915
+ ref f32
916
+ ref read_write"
1184
917
  `);
1185
918
  });
1186
919
 
@@ -1192,22 +925,17 @@ test(`parse struct reference`, () => {
1192
925
  const astString = astToString(ast.moduleElem);
1193
926
  expect(astString).toMatchInlineSnapshot(`
1194
927
  "module
1195
- text '
1196
- '
1197
928
  fn f()
1198
929
  decl %f
1199
- statement
1200
- text '{ '
930
+ block
1201
931
  let %x
1202
- text 'let '
1203
932
  typeDecl %x
1204
933
  decl %x
1205
- text ' = '
1206
- ref a
1207
- text '.b[0];'
1208
- text ' }'
1209
- text ';
1210
- '"
934
+ component-expression []
935
+ component-member-expression .
936
+ ref a
937
+ name b
938
+ literal literal(0)"
1211
939
  `);
1212
940
  });
1213
941
 
@@ -1221,25 +949,22 @@ test("member reference with extra components", () => {
1221
949
  const astString = astToString(ast.moduleElem);
1222
950
  expect(astString).toMatchInlineSnapshot(`
1223
951
  "module
1224
- text '
1225
- '
1226
952
  fn foo()
1227
953
  decl %foo
1228
- statement
1229
- text '{'
1230
- statement
1231
- text '
1232
- '
1233
- ref output
1234
- text '[ '
1235
- ref out
1236
- text ' + 0u ] = '
1237
- ref c
1238
- text '.p0.t0.x;'
1239
- text '
1240
- }'
1241
- text '
1242
- '"
954
+ block
955
+ assign
956
+ component-expression []
957
+ ref output
958
+ binary-expression binop(+)
959
+ ref out
960
+ literal literal(0u)
961
+ component-member-expression .
962
+ component-member-expression .
963
+ component-member-expression .
964
+ ref c
965
+ name p0
966
+ name t0
967
+ name x"
1243
968
  `);
1244
969
  });
1245
970
 
@@ -1253,26 +978,19 @@ test("parse let declaration", () => {
1253
978
  const astString = astToString(ast.moduleElem);
1254
979
  expect(astString).toMatchInlineSnapshot(`
1255
980
  "module
1256
- text '
1257
- '
1258
981
  fn vertexMain()
1259
982
  decl %vertexMain
1260
- statement
1261
- text '{
1262
- '
983
+ block
1263
984
  let %char
1264
- text 'let '
1265
985
  typeDecl %char
1266
986
  decl %char
1267
- text ' = '
1268
- ref array
1269
- text '<'
1270
- ref u32
1271
- text ', 2>(0, 0);'
1272
- text '
1273
- }'
1274
- text '
1275
- '"
987
+ call-expression call
988
+ type array<u32, 2>
989
+ ref array
990
+ ref u32
991
+ literal literal(2)
992
+ literal literal(0)
993
+ literal literal(0)"
1276
994
  `);
1277
995
  });
1278
996
 
@@ -1286,25 +1004,15 @@ test("parse let declaration with type", () => {
1286
1004
  const astString = astToString(ast.moduleElem);
1287
1005
  expect(astString).toMatchInlineSnapshot(`
1288
1006
  "module
1289
- text '
1290
- '
1291
1007
  fn vertexMain()
1292
1008
  decl %vertexMain
1293
- statement
1294
- text '{
1295
- '
1009
+ block
1296
1010
  let %char : u32
1297
- text 'let '
1298
1011
  typeDecl %char : u32
1299
1012
  decl %char
1300
- text ' : '
1301
1013
  type u32
1302
1014
  ref u32
1303
- text ' = 0;'
1304
- text '
1305
- }'
1306
- text '
1307
- '"
1015
+ literal literal(0)"
1308
1016
  `);
1309
1017
  });
1310
1018
 
@@ -1318,24 +1026,13 @@ test("separator in let assignment", () => {
1318
1026
  const astString = astToString(ast.moduleElem);
1319
1027
  expect(astString).toMatchInlineSnapshot(`
1320
1028
  "module
1321
- text '
1322
- '
1323
1029
  fn vertexMain()
1324
1030
  decl %vertexMain
1325
- statement
1326
- text '{
1327
- '
1031
+ block
1328
1032
  let %a
1329
- text 'let '
1330
1033
  typeDecl %a
1331
1034
  decl %a
1332
- text ' = '
1333
- ref b::c
1334
- text ';'
1335
- text '
1336
- }'
1337
- text '
1338
- '"
1035
+ ref b::c"
1339
1036
  `);
1340
1037
  });
1341
1038
 
@@ -1349,21 +1046,12 @@ test("separator in fn call ", () => {
1349
1046
  const astString = astToString(ast.moduleElem);
1350
1047
  expect(astString).toMatchInlineSnapshot(`
1351
1048
  "module
1352
- text '
1353
- '
1354
1049
  fn vertexMain()
1355
1050
  decl %vertexMain
1356
- statement
1357
- text '{'
1358
- statement
1359
- text '
1360
- '
1361
- ref b::c
1362
- text '();'
1363
- text '
1364
- }'
1365
- text '
1366
- '"
1051
+ block
1052
+ call
1053
+ call-expression call
1054
+ ref b::c"
1367
1055
  `);
1368
1056
  });
1369
1057
 
@@ -1380,76 +1068,40 @@ test("binding struct", () => {
1380
1068
  const astString = astToString(ast.moduleElem);
1381
1069
  expect(astString).toMatchInlineSnapshot(`
1382
1070
  "module
1383
- text '
1384
- '
1385
1071
  struct Bindings
1386
- text 'struct '
1387
1072
  decl %Bindings
1388
- text ' {
1389
- '
1390
- member @group @binding particles: ptr<ref storage, array<ref f32>, ref read_write>
1391
- attribute @group('0')
1392
- text ' '
1393
- attribute @binding('0')
1394
- text ' '
1073
+ member @group @binding particles: ptr<storage, array<f32>, read_write>
1074
+ attribute @group(literal literal(0))
1075
+ attribute @binding(literal literal(0))
1395
1076
  name particles
1396
- text ': '
1397
- type ptr<ref storage, array<ref f32>, ref read_write>
1077
+ type ptr<storage, array<f32>, read_write>
1398
1078
  ref ptr
1399
- text '<'
1400
1079
  ref storage
1401
- text ', '
1402
- ref array
1403
- text '<'
1404
- ref f32
1405
- text '>, '
1080
+ type array<f32>
1081
+ ref array
1082
+ ref f32
1406
1083
  ref read_write
1407
- text '>'
1408
- text ',
1409
- '
1410
- member @group @binding uniforms: ptr<ref uniform, ref Uniforms>
1411
- attribute @group('0')
1412
- text ' '
1413
- attribute @binding('1')
1414
- text ' '
1084
+ member @group @binding uniforms: ptr<uniform, Uniforms>
1085
+ attribute @group(literal literal(0))
1086
+ attribute @binding(literal literal(1))
1415
1087
  name uniforms
1416
- text ': '
1417
- type ptr<ref uniform, ref Uniforms>
1088
+ type ptr<uniform, Uniforms>
1418
1089
  ref ptr
1419
- text '<'
1420
1090
  ref uniform
1421
- text ', '
1422
1091
  ref Uniforms
1423
- text '>'
1424
- text ',
1425
- '
1426
- member @group @binding tex: texture_2d<ref rgba8unorm>
1427
- attribute @group('0')
1428
- text ' '
1429
- attribute @binding('2')
1430
- text ' '
1092
+ member @group @binding tex: texture_2d<rgba8unorm>
1093
+ attribute @group(literal literal(0))
1094
+ attribute @binding(literal literal(2))
1431
1095
  name tex
1432
- text ': '
1433
- type texture_2d<ref rgba8unorm>
1096
+ type texture_2d<rgba8unorm>
1434
1097
  ref texture_2d
1435
- text '<'
1436
1098
  ref rgba8unorm
1437
- text '>'
1438
- text ',
1439
- '
1440
1099
  member @group @binding samp: sampler
1441
- attribute @group('0')
1442
- text ' '
1443
- attribute @binding('3')
1444
- text ' '
1100
+ attribute @group(literal literal(0))
1101
+ attribute @binding(literal literal(3))
1445
1102
  name samp
1446
- text ': '
1447
1103
  type sampler
1448
- ref sampler
1449
- text ',
1450
- }'
1451
- text '
1452
- '"
1104
+ ref sampler"
1453
1105
  `);
1454
1106
  });
1455
1107
 
@@ -1463,23 +1115,20 @@ test("memberRefs with extra components", () => {
1463
1115
  const astString = astToString(ast.moduleElem);
1464
1116
  expect(astString).toMatchInlineSnapshot(`
1465
1117
  "module
1466
- text '
1467
- '
1468
1118
  fn main()
1469
1119
  decl %main
1470
- statement
1471
- text '{'
1472
- statement
1473
- text '
1474
- '
1475
- ref b
1476
- text '.particles[0] = '
1477
- ref b
1478
- text '.uniforms.foo;'
1479
- text '
1480
- }'
1481
- text '
1482
- '"
1120
+ block
1121
+ assign
1122
+ component-expression []
1123
+ component-member-expression .
1124
+ ref b
1125
+ name particles
1126
+ literal literal(0)
1127
+ component-member-expression .
1128
+ component-member-expression .
1129
+ ref b
1130
+ name uniforms
1131
+ name foo"
1483
1132
  `);
1484
1133
  });
1485
1134
 
@@ -1493,23 +1142,16 @@ test("memberRef with ref in array", () => {
1493
1142
  const astString = astToString(ast.moduleElem);
1494
1143
  expect(astString).toMatchInlineSnapshot(`
1495
1144
  "module
1496
- text '
1497
- '
1498
1145
  fn main()
1499
1146
  decl %main
1500
- statement
1501
- text '{'
1502
- statement
1503
- text '
1504
- '
1505
- ref vsOut
1506
- text '.barycenticCoord['
1507
- ref vertNdx
1508
- text '] = 1.0;'
1509
- text '
1510
- }'
1511
- text '
1512
- '"
1147
+ block
1148
+ assign
1149
+ component-expression []
1150
+ component-member-expression .
1151
+ ref vsOut
1152
+ name barycenticCoord
1153
+ ref vertNdx
1154
+ literal literal(1.0)"
1513
1155
  `);
1514
1156
  });
1515
1157
 
@@ -1523,21 +1165,12 @@ test("parse inline package reference", () => {
1523
1165
  const astString = astToString(ast.moduleElem);
1524
1166
  expect(astString).toMatchInlineSnapshot(`
1525
1167
  "module
1526
- text '
1527
- '
1528
1168
  fn main()
1529
1169
  decl %main
1530
- statement
1531
- text '{'
1532
- statement
1533
- text '
1534
- '
1535
- ref package::foo::bar
1536
- text '();'
1537
- text '
1538
- }'
1539
- text '
1540
- '"
1170
+ block
1171
+ call
1172
+ call-expression call
1173
+ ref package::foo::bar"
1541
1174
  `);
1542
1175
  });
1543
1176
 
@@ -1552,31 +1185,19 @@ test("parse @location", () => {
1552
1185
  const astString = astToString(ast.moduleElem);
1553
1186
  expect(astString).toMatchInlineSnapshot(`
1554
1187
  "module
1555
- text '
1556
- '
1557
1188
  fn fragmentMain(pos: vec4f) @fragment -> vec4f
1558
1189
  attribute @fragment
1559
1190
  decl %fragmentMain
1560
1191
  param
1561
1192
  attribute @builtin(position)
1562
- text ' '
1563
1193
  typeDecl %pos : vec4f
1564
1194
  decl %pos
1565
- text ': '
1566
1195
  type vec4f
1567
1196
  ref vec4f
1568
1197
  type vec4f
1569
1198
  ref vec4f
1570
- statement
1571
- text '{'
1572
- statement
1573
- text '
1574
- return '
1575
- ref pos
1576
- text ';'
1577
- text '
1578
- }'
1579
- text '
1580
- '"
1199
+ block
1200
+ return
1201
+ ref pos"
1581
1202
  `);
1582
1203
  });