skir 0.0.1

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 (141) hide show
  1. package/README.md +447 -0
  2. package/dist/casing.d.ts +8 -0
  3. package/dist/casing.d.ts.map +1 -0
  4. package/dist/casing.js +49 -0
  5. package/dist/casing.js.map +1 -0
  6. package/dist/casing.test.d.ts +2 -0
  7. package/dist/casing.test.d.ts.map +1 -0
  8. package/dist/casing.test.js +134 -0
  9. package/dist/casing.test.js.map +1 -0
  10. package/dist/command_line_parser.d.ts +33 -0
  11. package/dist/command_line_parser.d.ts.map +1 -0
  12. package/dist/command_line_parser.js +171 -0
  13. package/dist/command_line_parser.js.map +1 -0
  14. package/dist/command_line_parser.test.d.ts +2 -0
  15. package/dist/command_line_parser.test.d.ts.map +1 -0
  16. package/dist/command_line_parser.test.js +302 -0
  17. package/dist/command_line_parser.test.js.map +1 -0
  18. package/dist/compatibility_checker.d.ts +68 -0
  19. package/dist/compatibility_checker.d.ts.map +1 -0
  20. package/dist/compatibility_checker.js +328 -0
  21. package/dist/compatibility_checker.js.map +1 -0
  22. package/dist/compatibility_checker.test.d.ts +2 -0
  23. package/dist/compatibility_checker.test.d.ts.map +1 -0
  24. package/dist/compatibility_checker.test.js +528 -0
  25. package/dist/compatibility_checker.test.js.map +1 -0
  26. package/dist/compiler.d.ts +3 -0
  27. package/dist/compiler.d.ts.map +1 -0
  28. package/dist/compiler.js +358 -0
  29. package/dist/compiler.js.map +1 -0
  30. package/dist/config.d.ts +47 -0
  31. package/dist/config.d.ts.map +1 -0
  32. package/dist/config.js +23 -0
  33. package/dist/config.js.map +1 -0
  34. package/dist/definition_finder.d.ts +12 -0
  35. package/dist/definition_finder.d.ts.map +1 -0
  36. package/dist/definition_finder.js +180 -0
  37. package/dist/definition_finder.js.map +1 -0
  38. package/dist/definition_finder.test.d.ts +2 -0
  39. package/dist/definition_finder.test.d.ts.map +1 -0
  40. package/dist/definition_finder.test.js +164 -0
  41. package/dist/definition_finder.test.js.map +1 -0
  42. package/dist/encoding.d.ts +2 -0
  43. package/dist/encoding.d.ts.map +1 -0
  44. package/dist/encoding.js +38 -0
  45. package/dist/encoding.js.map +1 -0
  46. package/dist/encoding.test.d.ts +2 -0
  47. package/dist/encoding.test.d.ts.map +1 -0
  48. package/dist/encoding.test.js +23 -0
  49. package/dist/encoding.test.js.map +1 -0
  50. package/dist/error_renderer.d.ts +10 -0
  51. package/dist/error_renderer.d.ts.map +1 -0
  52. package/dist/error_renderer.js +247 -0
  53. package/dist/error_renderer.js.map +1 -0
  54. package/dist/formatter.d.ts +3 -0
  55. package/dist/formatter.d.ts.map +1 -0
  56. package/dist/formatter.js +263 -0
  57. package/dist/formatter.js.map +1 -0
  58. package/dist/formatter.test.d.ts +2 -0
  59. package/dist/formatter.test.d.ts.map +1 -0
  60. package/dist/formatter.test.js +156 -0
  61. package/dist/formatter.test.js.map +1 -0
  62. package/dist/index.d.ts +6 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +5 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/index.test.d.ts +2 -0
  67. package/dist/index.test.d.ts.map +1 -0
  68. package/dist/index.test.js +14 -0
  69. package/dist/index.test.js.map +1 -0
  70. package/dist/io.d.ts +13 -0
  71. package/dist/io.d.ts.map +1 -0
  72. package/dist/io.js +22 -0
  73. package/dist/io.js.map +1 -0
  74. package/dist/language_server.d.ts +15 -0
  75. package/dist/language_server.d.ts.map +1 -0
  76. package/dist/language_server.js +248 -0
  77. package/dist/language_server.js.map +1 -0
  78. package/dist/literals.d.ts +13 -0
  79. package/dist/literals.d.ts.map +1 -0
  80. package/dist/literals.js +100 -0
  81. package/dist/literals.js.map +1 -0
  82. package/dist/literals.test.d.ts +2 -0
  83. package/dist/literals.test.d.ts.map +1 -0
  84. package/dist/literals.test.js +149 -0
  85. package/dist/literals.test.js.map +1 -0
  86. package/dist/module_collector.d.ts +3 -0
  87. package/dist/module_collector.d.ts.map +1 -0
  88. package/dist/module_collector.js +22 -0
  89. package/dist/module_collector.js.map +1 -0
  90. package/dist/module_set.d.ts +44 -0
  91. package/dist/module_set.d.ts.map +1 -0
  92. package/dist/module_set.js +1025 -0
  93. package/dist/module_set.js.map +1 -0
  94. package/dist/module_set.test.d.ts +2 -0
  95. package/dist/module_set.test.d.ts.map +1 -0
  96. package/dist/module_set.test.js +1330 -0
  97. package/dist/module_set.test.js.map +1 -0
  98. package/dist/parser.d.ts +6 -0
  99. package/dist/parser.d.ts.map +1 -0
  100. package/dist/parser.js +971 -0
  101. package/dist/parser.js.map +1 -0
  102. package/dist/parser.test.d.ts +2 -0
  103. package/dist/parser.test.d.ts.map +1 -0
  104. package/dist/parser.test.js +1366 -0
  105. package/dist/parser.test.js.map +1 -0
  106. package/dist/snapshotter.d.ts +6 -0
  107. package/dist/snapshotter.d.ts.map +1 -0
  108. package/dist/snapshotter.js +107 -0
  109. package/dist/snapshotter.js.map +1 -0
  110. package/dist/tokenizer.d.ts +4 -0
  111. package/dist/tokenizer.d.ts.map +1 -0
  112. package/dist/tokenizer.js +192 -0
  113. package/dist/tokenizer.js.map +1 -0
  114. package/dist/tokenizer.test.d.ts +2 -0
  115. package/dist/tokenizer.test.d.ts.map +1 -0
  116. package/dist/tokenizer.test.js +425 -0
  117. package/dist/tokenizer.test.js.map +1 -0
  118. package/dist/types.d.ts +375 -0
  119. package/dist/types.d.ts.map +1 -0
  120. package/dist/types.js +2 -0
  121. package/dist/types.js.map +1 -0
  122. package/package.json +63 -0
  123. package/src/casing.ts +64 -0
  124. package/src/command_line_parser.ts +249 -0
  125. package/src/compatibility_checker.ts +470 -0
  126. package/src/compiler.ts +435 -0
  127. package/src/config.ts +28 -0
  128. package/src/definition_finder.ts +221 -0
  129. package/src/encoding.ts +32 -0
  130. package/src/error_renderer.ts +278 -0
  131. package/src/formatter.ts +274 -0
  132. package/src/index.ts +6 -0
  133. package/src/io.ts +33 -0
  134. package/src/language_server.ts +301 -0
  135. package/src/literals.ts +120 -0
  136. package/src/module_collector.ts +22 -0
  137. package/src/module_set.ts +1175 -0
  138. package/src/parser.ts +1122 -0
  139. package/src/snapshotter.ts +136 -0
  140. package/src/tokenizer.ts +216 -0
  141. package/src/types.ts +518 -0
@@ -0,0 +1,1366 @@
1
+ import { expect } from "buckwheat";
2
+ import { describe, it } from "mocha";
3
+ import { parseModule } from "./parser.js";
4
+ import { tokenizeModule } from "./tokenizer.js";
5
+ function parse(content) {
6
+ const pathToModule = "path/to/module";
7
+ const tokenizerResult = tokenizeModule(content, pathToModule);
8
+ return parseModule(tokenizerResult.result, pathToModule, content);
9
+ }
10
+ describe("module parser", () => {
11
+ it("simple struct", () => {
12
+ const actualModule = parse(`
13
+ struct Point {
14
+ x: float32;
15
+ y: float32;
16
+ removed;
17
+ }`);
18
+ expect(actualModule).toMatch({
19
+ result: {
20
+ kind: "module",
21
+ path: "path/to/module",
22
+ nameToDeclaration: {
23
+ Point: {
24
+ kind: "record",
25
+ name: {
26
+ text: "Point",
27
+ },
28
+ recordType: "struct",
29
+ nameToDeclaration: {
30
+ x: {
31
+ kind: "field",
32
+ name: {
33
+ text: "x",
34
+ },
35
+ number: 0,
36
+ unresolvedType: {
37
+ kind: "primitive",
38
+ primitive: "float32",
39
+ },
40
+ isRecursive: false,
41
+ },
42
+ y: {
43
+ kind: "field",
44
+ name: {
45
+ text: "y",
46
+ },
47
+ number: 1,
48
+ unresolvedType: {
49
+ kind: "primitive",
50
+ primitive: "float32",
51
+ },
52
+ isRecursive: false,
53
+ },
54
+ },
55
+ fields: [
56
+ {
57
+ name: {
58
+ text: "x",
59
+ },
60
+ },
61
+ {
62
+ name: {
63
+ text: "y",
64
+ },
65
+ },
66
+ ],
67
+ nestedRecords: [],
68
+ numbering: "implicit",
69
+ removedNumbers: [2],
70
+ numSlots: 2,
71
+ numSlotsInclRemovedNumbers: 3,
72
+ },
73
+ },
74
+ },
75
+ errors: [],
76
+ });
77
+ });
78
+ it("struct with explicit numbering", () => {
79
+ const actualModule = parse(`
80
+ struct Point {
81
+ x: float32 = 1;
82
+ y: float32 = 0;
83
+ }`);
84
+ expect(actualModule).toMatch({
85
+ result: {
86
+ nameToDeclaration: {
87
+ Point: {
88
+ nameToDeclaration: {
89
+ x: {
90
+ number: 1,
91
+ },
92
+ y: {
93
+ number: 0,
94
+ },
95
+ },
96
+ numbering: "explicit",
97
+ },
98
+ },
99
+ },
100
+ errors: [],
101
+ });
102
+ });
103
+ it("simple enum", () => {
104
+ const actualModule = parse(`
105
+ enum Enum {
106
+ CONSTANT;
107
+ removed;
108
+ value_field: bool;
109
+ }`);
110
+ expect(actualModule).toMatch({
111
+ result: {
112
+ nameToDeclaration: {
113
+ Enum: {
114
+ kind: "record",
115
+ name: {
116
+ text: "Enum",
117
+ },
118
+ recordType: "enum",
119
+ nameToDeclaration: {
120
+ CONSTANT: {
121
+ kind: "field",
122
+ name: {
123
+ text: "CONSTANT",
124
+ },
125
+ number: 1,
126
+ },
127
+ value_field: {
128
+ kind: "field",
129
+ name: {
130
+ text: "value_field",
131
+ },
132
+ number: 3,
133
+ unresolvedType: {
134
+ kind: "primitive",
135
+ },
136
+ isRecursive: false,
137
+ },
138
+ },
139
+ fields: [
140
+ {
141
+ kind: "field",
142
+ name: {
143
+ text: "CONSTANT",
144
+ },
145
+ },
146
+ {
147
+ kind: "field",
148
+ name: {
149
+ text: "value_field",
150
+ },
151
+ },
152
+ ],
153
+ numSlots: 0,
154
+ numSlotsInclRemovedNumbers: 0,
155
+ },
156
+ },
157
+ },
158
+ errors: [],
159
+ });
160
+ });
161
+ it("record with stable id", () => {
162
+ const actualModule = parse(`
163
+ struct Foo(4294967295) {}
164
+ enum Zoo(0) {}`);
165
+ expect(actualModule).toMatch({
166
+ result: {
167
+ kind: "module",
168
+ path: "path/to/module",
169
+ nameToDeclaration: {
170
+ Foo: {
171
+ kind: "record",
172
+ recordNumber: 4294967295,
173
+ },
174
+ Zoo: {
175
+ kind: "record",
176
+ recordNumber: 0,
177
+ },
178
+ },
179
+ },
180
+ errors: [],
181
+ });
182
+ });
183
+ it("nested struct", () => {
184
+ const actualModule = parse(`
185
+ struct Foo {
186
+ struct Bar {}
187
+ }`);
188
+ expect(actualModule).toMatch({
189
+ result: {
190
+ kind: "module",
191
+ path: "path/to/module",
192
+ nameToDeclaration: {
193
+ Foo: {
194
+ kind: "record",
195
+ name: {
196
+ text: "Foo",
197
+ },
198
+ recordType: "struct",
199
+ nameToDeclaration: {
200
+ Bar: {
201
+ kind: "record",
202
+ name: {
203
+ text: "Bar",
204
+ },
205
+ recordType: "struct",
206
+ },
207
+ },
208
+ nestedRecords: [
209
+ {
210
+ kind: "record",
211
+ name: {
212
+ text: "Bar",
213
+ },
214
+ recordType: "struct",
215
+ },
216
+ ],
217
+ },
218
+ },
219
+ },
220
+ errors: [],
221
+ });
222
+ });
223
+ it("struct with all possible types", () => {
224
+ const actualModule = parse(`
225
+ struct AllTypes {
226
+ bool: bool;
227
+ int32: int32;
228
+ int64: int64;
229
+ uint64: uint64;
230
+ float32: float32;
231
+ float64: float64;
232
+ timestamp: timestamp;
233
+ string: string;
234
+ bytes: bytes;
235
+ strings: [string?];
236
+ foos: [Foo|x.y];
237
+ }`);
238
+ expect(actualModule).toMatch({
239
+ result: {
240
+ nameToDeclaration: {
241
+ AllTypes: {
242
+ nameToDeclaration: {
243
+ bool: {
244
+ unresolvedType: {
245
+ kind: "primitive",
246
+ primitive: "bool",
247
+ },
248
+ },
249
+ int32: {
250
+ unresolvedType: {
251
+ kind: "primitive",
252
+ primitive: "int32",
253
+ },
254
+ },
255
+ int64: {
256
+ unresolvedType: {
257
+ kind: "primitive",
258
+ primitive: "int64",
259
+ },
260
+ },
261
+ uint64: {
262
+ unresolvedType: {
263
+ kind: "primitive",
264
+ primitive: "uint64",
265
+ },
266
+ },
267
+ float32: {
268
+ unresolvedType: {
269
+ kind: "primitive",
270
+ primitive: "float32",
271
+ },
272
+ },
273
+ float64: {
274
+ unresolvedType: {
275
+ kind: "primitive",
276
+ primitive: "float64",
277
+ },
278
+ },
279
+ timestamp: {
280
+ unresolvedType: {
281
+ kind: "primitive",
282
+ primitive: "timestamp",
283
+ },
284
+ },
285
+ string: {
286
+ unresolvedType: {
287
+ kind: "primitive",
288
+ primitive: "string",
289
+ },
290
+ },
291
+ bytes: {
292
+ unresolvedType: {
293
+ kind: "primitive",
294
+ primitive: "bytes",
295
+ },
296
+ },
297
+ strings: {
298
+ unresolvedType: {
299
+ kind: "array",
300
+ item: {
301
+ kind: "optional",
302
+ other: {
303
+ kind: "primitive",
304
+ primitive: "string",
305
+ },
306
+ },
307
+ },
308
+ },
309
+ foos: {
310
+ unresolvedType: {
311
+ kind: "array",
312
+ item: {
313
+ kind: "record",
314
+ nameParts: [
315
+ {
316
+ text: "Foo",
317
+ position: 284,
318
+ line: {
319
+ lineNumber: 12,
320
+ line: " foos: [Foo|x.y];",
321
+ position: 269,
322
+ modulePath: "path/to/module",
323
+ },
324
+ colNumber: 15,
325
+ },
326
+ ],
327
+ absolute: false,
328
+ },
329
+ key: {
330
+ pipeToken: {
331
+ text: "|",
332
+ position: 287,
333
+ line: {
334
+ lineNumber: 12,
335
+ line: " foos: [Foo|x.y];",
336
+ position: 269,
337
+ modulePath: "path/to/module",
338
+ },
339
+ colNumber: 18,
340
+ },
341
+ path: [
342
+ {
343
+ name: {
344
+ text: "x",
345
+ position: 288,
346
+ line: {
347
+ lineNumber: 12,
348
+ line: " foos: [Foo|x.y];",
349
+ position: 269,
350
+ modulePath: "path/to/module",
351
+ },
352
+ colNumber: 19,
353
+ },
354
+ },
355
+ {
356
+ name: {
357
+ text: "y",
358
+ position: 290,
359
+ line: {
360
+ lineNumber: 12,
361
+ line: " foos: [Foo|x.y];",
362
+ position: 269,
363
+ modulePath: "path/to/module",
364
+ },
365
+ colNumber: 21,
366
+ },
367
+ declaration: undefined,
368
+ },
369
+ ],
370
+ keyType: {
371
+ kind: "primitive",
372
+ primitive: "bool",
373
+ },
374
+ },
375
+ },
376
+ },
377
+ },
378
+ },
379
+ },
380
+ },
381
+ errors: [],
382
+ });
383
+ });
384
+ it("module with imports", () => {
385
+ const actualModule = parse(`
386
+ import foo from './path/😊/foo';
387
+ import * as bar from "path/to/bar";`);
388
+ expect(actualModule).toMatch({
389
+ result: {
390
+ nameToDeclaration: {
391
+ foo: {
392
+ kind: "import",
393
+ importedNames: [
394
+ {
395
+ text: "foo",
396
+ },
397
+ ],
398
+ modulePath: {
399
+ text: "'./path/😊/foo'",
400
+ },
401
+ },
402
+ bar: {
403
+ kind: "import-alias",
404
+ name: {
405
+ text: "bar",
406
+ },
407
+ modulePath: {
408
+ text: '"path/to/bar"',
409
+ },
410
+ },
411
+ },
412
+ },
413
+ errors: [],
414
+ });
415
+ });
416
+ it("module with duplicate identifier", () => {
417
+ const actualModule = parse(`
418
+ struct A {}
419
+ struct A {}`);
420
+ expect(actualModule).toMatch({
421
+ errors: [
422
+ {
423
+ token: {
424
+ text: "A",
425
+ line: {
426
+ lineNumber: 2,
427
+ },
428
+ },
429
+ message: 'Duplicate identifier "A"',
430
+ },
431
+ ],
432
+ });
433
+ });
434
+ it("enum with sparse numbers", () => {
435
+ const actualModule = parse(`
436
+ enum Enum {
437
+ CONSTANT = 2;
438
+ removed 10, 11;
439
+ removed 12..14;
440
+ removed 30..32, 50, 60..62;
441
+ value_field: bool = 4;
442
+ }`);
443
+ expect(actualModule).toMatch({
444
+ result: {
445
+ nameToDeclaration: {
446
+ Enum: {
447
+ kind: "record",
448
+ name: {
449
+ text: "Enum",
450
+ },
451
+ recordType: "enum",
452
+ nameToDeclaration: {
453
+ CONSTANT: {
454
+ kind: "field",
455
+ name: {
456
+ text: "CONSTANT",
457
+ },
458
+ number: 2,
459
+ },
460
+ value_field: {
461
+ kind: "field",
462
+ name: {
463
+ text: "value_field",
464
+ },
465
+ number: 4,
466
+ },
467
+ },
468
+ removedNumbers: [10, 11, 12, 13, 14, 30, 31, 32, 50, 60, 61, 62],
469
+ },
470
+ },
471
+ },
472
+ errors: [],
473
+ });
474
+ });
475
+ it("struct with duplicate identifier", () => {
476
+ const actualModule = parse(`
477
+ struct A {
478
+ a: bool;
479
+ a: bool;
480
+ }`);
481
+ expect(actualModule).toMatch({
482
+ errors: [
483
+ {
484
+ token: {
485
+ text: "a",
486
+ line: {
487
+ lineNumber: 3,
488
+ },
489
+ },
490
+ message: 'Duplicate identifier "a"',
491
+ },
492
+ ],
493
+ });
494
+ });
495
+ it("struct with invalid casing", () => {
496
+ const actualModule = parse(`
497
+ struct a {
498
+ A: bool;
499
+ }`);
500
+ expect(actualModule).toMatch({
501
+ errors: [
502
+ {
503
+ token: {
504
+ text: "a",
505
+ },
506
+ expected: "UpperCamel",
507
+ },
508
+ {
509
+ token: {
510
+ text: "A",
511
+ },
512
+ expected: "lower_underscore",
513
+ },
514
+ ],
515
+ });
516
+ });
517
+ it("struct with mixed numbering", () => {
518
+ const actualModule = parse(`
519
+ struct A {
520
+ a: bool;
521
+ b: bool = 1;
522
+ }`);
523
+ expect(actualModule).toMatch({
524
+ errors: [
525
+ {
526
+ token: {
527
+ text: "b",
528
+ },
529
+ message: "Cannot mix implicit and explicit numbering",
530
+ },
531
+ ],
532
+ });
533
+ });
534
+ it("struct with removed fields and mixed numbering", () => {
535
+ const actualModule = parse(`
536
+ struct A {
537
+ removed;
538
+ b: bool = 1;
539
+ }`);
540
+ expect(actualModule).toMatch({
541
+ errors: [
542
+ {
543
+ token: {
544
+ text: "b",
545
+ },
546
+ message: "Cannot mix implicit and explicit numbering",
547
+ },
548
+ ],
549
+ });
550
+ });
551
+ it("struct with duplicate number", () => {
552
+ const actualModule = parse(`
553
+ struct A {
554
+ a: bool = 0;
555
+ b: bool = 0;
556
+ }`);
557
+ expect(actualModule).toMatch({
558
+ errors: [
559
+ {
560
+ token: {
561
+ text: "b",
562
+ },
563
+ message: "Duplicate field number 0",
564
+ },
565
+ ],
566
+ });
567
+ });
568
+ it("struct with duplicate number in removed declaration", () => {
569
+ const actualModule = parse(`
570
+ struct A {
571
+ removed 0, 1, 2, 5, 2, 3, 4;
572
+ }`);
573
+ expect(actualModule).toMatch({
574
+ errors: [
575
+ {
576
+ token: {
577
+ text: "removed",
578
+ },
579
+ message: "Duplicate field number 2",
580
+ },
581
+ ],
582
+ });
583
+ });
584
+ it("struct with duplicate number in removed range declaration", () => {
585
+ const actualModule = parse(`
586
+ struct A {
587
+ removed 0, 1, 2, 5, 2..4;
588
+ }`);
589
+ expect(actualModule).toMatch({
590
+ errors: [
591
+ {
592
+ token: {
593
+ text: "removed",
594
+ },
595
+ message: "Duplicate field number 2",
596
+ },
597
+ ],
598
+ });
599
+ });
600
+ it("struct with invalid range in removed declaration", () => {
601
+ const actualModule = parse(`
602
+ struct A {
603
+ removed 0..0;
604
+ }`);
605
+ expect(actualModule).toMatch({
606
+ errors: [
607
+ {
608
+ token: {
609
+ text: "removed",
610
+ },
611
+ message: "Upper bound must be greater than lower bound",
612
+ },
613
+ ],
614
+ });
615
+ });
616
+ it("struct with removed fields and duplicate number", () => {
617
+ const actualModule = parse(`
618
+ struct A {
619
+ a: bool = 0;
620
+ removed 0;
621
+ }`);
622
+ expect(actualModule).toMatch({
623
+ errors: [
624
+ {
625
+ token: {
626
+ text: "removed",
627
+ },
628
+ message: "Duplicate field number 0",
629
+ },
630
+ ],
631
+ });
632
+ });
633
+ it("struct with missing number", () => {
634
+ const actualModule = parse(`
635
+ struct A {
636
+ a: bool = 2;
637
+ b: bool = 0;
638
+ }`);
639
+ expect(actualModule).toMatch({
640
+ errors: [
641
+ {
642
+ token: {
643
+ text: "A",
644
+ },
645
+ message: "Missing field number 1",
646
+ },
647
+ ],
648
+ });
649
+ });
650
+ it("struct with stable id out of bounds", () => {
651
+ const actualModule = parse(`
652
+ struct A(4294967296) {}`);
653
+ expect(actualModule).toMatch({
654
+ errors: [
655
+ {
656
+ token: {
657
+ text: "4294967296",
658
+ },
659
+ message: "Value out of uint32 range",
660
+ },
661
+ ],
662
+ });
663
+ });
664
+ it("enum with invalid casing", () => {
665
+ const actualModule = parse(`
666
+ enum a {
667
+ foo = 1;
668
+ BAR: string = 100;
669
+ }`);
670
+ expect(actualModule).toMatch({
671
+ errors: [
672
+ {
673
+ token: {
674
+ text: "a",
675
+ },
676
+ expected: "UpperCamel",
677
+ },
678
+ {
679
+ token: {
680
+ text: "foo",
681
+ },
682
+ expected: "UPPER_UNDERSCORE",
683
+ },
684
+ {
685
+ token: {
686
+ text: "BAR",
687
+ },
688
+ expected: "lower_underscore",
689
+ },
690
+ ],
691
+ });
692
+ });
693
+ it("struct with removed fields", () => {
694
+ const actualModule = parse(`
695
+ struct Point {
696
+ removed;
697
+ a: bool;
698
+ removed;
699
+ }`);
700
+ expect(actualModule).toMatch({
701
+ result: {
702
+ kind: "module",
703
+ path: "path/to/module",
704
+ nameToDeclaration: {
705
+ Point: {
706
+ removedNumbers: [0, 2],
707
+ },
708
+ },
709
+ pathToImportedNames: {},
710
+ records: [{}],
711
+ },
712
+ errors: [],
713
+ });
714
+ });
715
+ it("struct with removed fields and explicit numbering", () => {
716
+ const actualModule = parse(`
717
+ struct Point {
718
+ removed 3, 4, 5, 6;
719
+ removed 0, 1, 2;
720
+ }`);
721
+ expect(actualModule).toMatch({
722
+ result: {
723
+ kind: "module",
724
+ path: "path/to/module",
725
+ nameToDeclaration: {
726
+ Point: {
727
+ removedNumbers: [0, 1, 2, 3, 4, 5, 6],
728
+ },
729
+ },
730
+ pathToImportedNames: {},
731
+ records: [{}],
732
+ },
733
+ errors: [],
734
+ });
735
+ });
736
+ it("empty struct", () => {
737
+ const actualModule = parse(`
738
+ struct Point {}`);
739
+ expect(actualModule).toMatch({
740
+ result: {
741
+ kind: "module",
742
+ path: "path/to/module",
743
+ nameToDeclaration: {
744
+ Point: {
745
+ kind: "record",
746
+ name: {
747
+ text: "Point",
748
+ },
749
+ recordType: "struct",
750
+ numbering: "",
751
+ },
752
+ },
753
+ },
754
+ errors: [],
755
+ });
756
+ });
757
+ it("empty enum", () => {
758
+ const actualModule = parse(`
759
+ enum Enum {
760
+ removed;
761
+ }`);
762
+ expect(actualModule).toMatch({
763
+ result: {
764
+ kind: "module",
765
+ path: "path/to/module",
766
+ nameToDeclaration: {
767
+ Enum: {
768
+ kind: "record",
769
+ recordType: "enum",
770
+ name: {
771
+ text: "Enum",
772
+ },
773
+ },
774
+ },
775
+ },
776
+ errors: [],
777
+ });
778
+ });
779
+ it("enum with field named UNKNOWN", () => {
780
+ const actualModule = parse(`
781
+ enum Enum {
782
+ UNKNOWN = 1;
783
+ }`);
784
+ expect(actualModule).toMatch({
785
+ errors: [
786
+ {
787
+ token: {
788
+ text: "UNKNOWN",
789
+ },
790
+ message: "Cannot name field of enum: UNKNOWN",
791
+ },
792
+ ],
793
+ });
794
+ });
795
+ it("enum with field number set to zero", () => {
796
+ const actualModule = parse(`
797
+ enum Enum {
798
+ A = 0;
799
+ }`);
800
+ expect(actualModule).toMatch({
801
+ errors: [
802
+ {
803
+ token: {
804
+ text: "A",
805
+ },
806
+ message: "Number 0 is reserved for UNKNOWN field",
807
+ },
808
+ ],
809
+ });
810
+ });
811
+ it("enum with zero field number removed", () => {
812
+ const actualModule = parse(`
813
+ enum Enum {
814
+ removed 0;
815
+ }`);
816
+ expect(actualModule).toMatch({
817
+ errors: [
818
+ {
819
+ token: {
820
+ text: "removed",
821
+ },
822
+ message: "Number 0 is reserved for UNKNOWN field",
823
+ },
824
+ ],
825
+ });
826
+ });
827
+ it("method", () => {
828
+ const actualModule = parse(`
829
+ method Search(req):resp;`);
830
+ expect(actualModule).toMatch({
831
+ result: {
832
+ kind: "module",
833
+ path: "path/to/module",
834
+ nameToDeclaration: {
835
+ Search: {
836
+ kind: "method",
837
+ name: {
838
+ text: "Search",
839
+ },
840
+ unresolvedRequestType: {
841
+ kind: "record",
842
+ nameParts: [
843
+ {
844
+ text: "req",
845
+ },
846
+ ],
847
+ },
848
+ unresolvedResponseType: {
849
+ kind: "record",
850
+ nameParts: [
851
+ {
852
+ text: "resp",
853
+ },
854
+ ],
855
+ },
856
+ number: 2446226092,
857
+ hasExplicitNumber: false,
858
+ },
859
+ },
860
+ methods: [
861
+ {
862
+ kind: "method",
863
+ name: {
864
+ text: "Search",
865
+ },
866
+ number: 2446226092,
867
+ hasExplicitNumber: false,
868
+ },
869
+ ],
870
+ },
871
+ errors: [],
872
+ });
873
+ });
874
+ it("method with explicit number", () => {
875
+ const actualModule = parse(`
876
+ method Search(req):resp = 200;`);
877
+ expect(actualModule).toMatch({
878
+ result: {
879
+ kind: "module",
880
+ path: "path/to/module",
881
+ nameToDeclaration: {
882
+ Search: {
883
+ kind: "method",
884
+ name: {
885
+ text: "Search",
886
+ },
887
+ unresolvedRequestType: {
888
+ kind: "record",
889
+ nameParts: [
890
+ {
891
+ text: "req",
892
+ },
893
+ ],
894
+ },
895
+ unresolvedResponseType: {
896
+ kind: "record",
897
+ nameParts: [
898
+ {
899
+ text: "resp",
900
+ },
901
+ ],
902
+ },
903
+ number: 200,
904
+ },
905
+ },
906
+ methods: [
907
+ {
908
+ kind: "method",
909
+ name: {
910
+ text: "Search",
911
+ },
912
+ number: 200,
913
+ hasExplicitNumber: true,
914
+ },
915
+ ],
916
+ },
917
+ errors: [],
918
+ });
919
+ });
920
+ it("string constant", () => {
921
+ const actualModule = parse(`
922
+ const FOO: string = "Foo";`);
923
+ expect(actualModule).toMatch({
924
+ result: {
925
+ kind: "module",
926
+ path: "path/to/module",
927
+ nameToDeclaration: {
928
+ FOO: {
929
+ kind: "constant",
930
+ name: {
931
+ text: "FOO",
932
+ },
933
+ unresolvedType: {
934
+ kind: "primitive",
935
+ primitive: "string",
936
+ },
937
+ value: {
938
+ kind: "literal",
939
+ token: {
940
+ text: '"Foo"',
941
+ },
942
+ },
943
+ },
944
+ },
945
+ constants: [
946
+ {
947
+ kind: "constant",
948
+ name: {
949
+ text: "FOO",
950
+ },
951
+ },
952
+ ],
953
+ },
954
+ errors: [],
955
+ });
956
+ });
957
+ it("constant with invalid casing", () => {
958
+ const actualModule = parse(`
959
+ const foo: string = "Foo";`);
960
+ expect(actualModule).toMatch({
961
+ errors: [
962
+ {
963
+ token: {
964
+ text: "foo",
965
+ },
966
+ expected: "UPPER_UNDERSCORE",
967
+ },
968
+ ],
969
+ });
970
+ });
971
+ it("complex constant", () => {
972
+ const actualModule = parse(`
973
+ const FOO: [Foo] = {
974
+ foo: true,
975
+ x: [
976
+ {foo: true},
977
+ {bar: false,},
978
+ "hey",
979
+ 3.14,
980
+ -3.14,
981
+ ],
982
+ empty_array: [],
983
+ empty_object: {},
984
+ };`);
985
+ expect(actualModule).toMatch({
986
+ result: {
987
+ kind: "module",
988
+ path: "path/to/module",
989
+ nameToDeclaration: {
990
+ FOO: {
991
+ kind: "constant",
992
+ name: {
993
+ text: "FOO",
994
+ },
995
+ unresolvedType: {
996
+ kind: "array",
997
+ item: {
998
+ kind: "record",
999
+ },
1000
+ },
1001
+ value: {
1002
+ kind: "object",
1003
+ token: {
1004
+ text: "{",
1005
+ },
1006
+ entries: {
1007
+ foo: {
1008
+ value: {
1009
+ kind: "literal",
1010
+ token: {
1011
+ text: "true",
1012
+ },
1013
+ },
1014
+ },
1015
+ x: {
1016
+ value: {
1017
+ kind: "array",
1018
+ items: [
1019
+ {
1020
+ kind: "object",
1021
+ entries: {
1022
+ foo: {
1023
+ value: {
1024
+ kind: "literal",
1025
+ token: {
1026
+ text: "true",
1027
+ },
1028
+ },
1029
+ },
1030
+ },
1031
+ },
1032
+ {
1033
+ kind: "object",
1034
+ entries: {
1035
+ bar: {
1036
+ value: {
1037
+ kind: "literal",
1038
+ token: {
1039
+ text: "false",
1040
+ },
1041
+ },
1042
+ },
1043
+ },
1044
+ },
1045
+ {
1046
+ kind: "literal",
1047
+ token: {
1048
+ text: '"hey"',
1049
+ },
1050
+ },
1051
+ {
1052
+ kind: "literal",
1053
+ token: {
1054
+ text: "3.14",
1055
+ },
1056
+ },
1057
+ {
1058
+ kind: "literal",
1059
+ token: {
1060
+ text: "-3.14",
1061
+ },
1062
+ },
1063
+ ],
1064
+ },
1065
+ },
1066
+ empty_array: {
1067
+ value: {
1068
+ kind: "array",
1069
+ items: [],
1070
+ },
1071
+ },
1072
+ empty_object: {
1073
+ value: {
1074
+ kind: "object",
1075
+ },
1076
+ },
1077
+ },
1078
+ partial: false,
1079
+ },
1080
+ },
1081
+ },
1082
+ constants: [
1083
+ {
1084
+ kind: "constant",
1085
+ name: {
1086
+ text: "FOO",
1087
+ },
1088
+ },
1089
+ ],
1090
+ },
1091
+ errors: [],
1092
+ });
1093
+ });
1094
+ it("partial constant", () => {
1095
+ const actualModule = parse(`
1096
+ const FOO: [Foo] = {|
1097
+ foo: true,
1098
+ |};`);
1099
+ expect(actualModule).toMatch({
1100
+ result: {
1101
+ kind: "module",
1102
+ path: "path/to/module",
1103
+ nameToDeclaration: {
1104
+ FOO: {
1105
+ kind: "constant",
1106
+ name: {
1107
+ text: "FOO",
1108
+ },
1109
+ unresolvedType: {
1110
+ kind: "array",
1111
+ item: {
1112
+ kind: "record",
1113
+ },
1114
+ },
1115
+ value: {
1116
+ kind: "object",
1117
+ token: {
1118
+ text: "{|",
1119
+ },
1120
+ entries: {
1121
+ foo: {
1122
+ value: {
1123
+ kind: "literal",
1124
+ token: {
1125
+ text: "true",
1126
+ },
1127
+ },
1128
+ },
1129
+ },
1130
+ partial: true,
1131
+ },
1132
+ },
1133
+ },
1134
+ constants: [
1135
+ {
1136
+ kind: "constant",
1137
+ name: {
1138
+ text: "FOO",
1139
+ },
1140
+ },
1141
+ ],
1142
+ },
1143
+ errors: [],
1144
+ });
1145
+ });
1146
+ it("partial empty constant", () => {
1147
+ const actualModule = parse(`
1148
+ const FOO: [Foo] = {||};`);
1149
+ expect(actualModule).toMatch({
1150
+ result: {
1151
+ kind: "module",
1152
+ path: "path/to/module",
1153
+ nameToDeclaration: {
1154
+ FOO: {
1155
+ kind: "constant",
1156
+ name: {
1157
+ text: "FOO",
1158
+ },
1159
+ unresolvedType: {
1160
+ kind: "array",
1161
+ item: {
1162
+ kind: "record",
1163
+ },
1164
+ },
1165
+ value: {
1166
+ kind: "object",
1167
+ token: {
1168
+ text: "{|",
1169
+ },
1170
+ entries: {},
1171
+ partial: true,
1172
+ },
1173
+ },
1174
+ },
1175
+ constants: [
1176
+ {
1177
+ kind: "constant",
1178
+ name: {
1179
+ text: "FOO",
1180
+ },
1181
+ },
1182
+ ],
1183
+ },
1184
+ errors: [],
1185
+ });
1186
+ });
1187
+ describe("handle and recover from bad statements", () => {
1188
+ it("#0", () => {
1189
+ const actualModule = parse(`
1190
+ const FOO: string = "";
1191
+ a b c;
1192
+ struct Foo {}`);
1193
+ expect(actualModule).toMatch({
1194
+ result: {
1195
+ nameToDeclaration: {
1196
+ FOO: {},
1197
+ Foo: {},
1198
+ },
1199
+ },
1200
+ errors: [
1201
+ {
1202
+ token: {
1203
+ text: "a",
1204
+ },
1205
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1206
+ },
1207
+ ],
1208
+ });
1209
+ });
1210
+ it("#1", () => {
1211
+ const actualModule = parse(`
1212
+ const FOO: string = "";
1213
+ a;
1214
+ struct Foo {}`);
1215
+ expect(actualModule).toMatch({
1216
+ result: {
1217
+ nameToDeclaration: {
1218
+ FOO: {},
1219
+ Foo: {},
1220
+ },
1221
+ },
1222
+ errors: [
1223
+ {
1224
+ token: {
1225
+ text: "a",
1226
+ },
1227
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1228
+ },
1229
+ ],
1230
+ });
1231
+ });
1232
+ it("#2", () => {
1233
+ const actualModule = parse(`
1234
+ const FOO: string = "";
1235
+ ;;;
1236
+ struct Foo {}`);
1237
+ expect(actualModule).toMatch({
1238
+ result: {
1239
+ nameToDeclaration: {
1240
+ FOO: {},
1241
+ Foo: {},
1242
+ },
1243
+ },
1244
+ errors: [
1245
+ {
1246
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1247
+ },
1248
+ {
1249
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1250
+ },
1251
+ {
1252
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1253
+ },
1254
+ ],
1255
+ });
1256
+ });
1257
+ it("#3", () => {
1258
+ const actualModule = parse(`
1259
+ const FOO: string = "";;`);
1260
+ expect(actualModule).toMatch({
1261
+ result: {
1262
+ nameToDeclaration: {
1263
+ FOO: {},
1264
+ },
1265
+ },
1266
+ errors: [
1267
+ {
1268
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1269
+ },
1270
+ ],
1271
+ });
1272
+ });
1273
+ it("#4", () => {
1274
+ const actualModule = parse(`
1275
+ struct Foo { struct Bar {}`);
1276
+ expect(actualModule).toMatch({
1277
+ result: {
1278
+ nameToDeclaration: {
1279
+ Foo: {
1280
+ nameToDeclaration: {
1281
+ Bar: {},
1282
+ },
1283
+ },
1284
+ },
1285
+ },
1286
+ errors: [
1287
+ {
1288
+ expected: '"}"',
1289
+ },
1290
+ ],
1291
+ });
1292
+ });
1293
+ it("#5", () => {
1294
+ const actualModule = parse(`
1295
+ struct Foo { a { a; {} } b: string; }`);
1296
+ expect(actualModule).toMatch({
1297
+ result: {
1298
+ nameToDeclaration: {
1299
+ Foo: {
1300
+ nameToDeclaration: {
1301
+ b: {},
1302
+ },
1303
+ },
1304
+ },
1305
+ },
1306
+ errors: [
1307
+ {
1308
+ expected: '":"',
1309
+ },
1310
+ ],
1311
+ });
1312
+ });
1313
+ it("#6", () => {
1314
+ const actualModule = parse(`
1315
+ struct Foo { }} struct Bar { }`);
1316
+ expect(actualModule).toMatch({
1317
+ result: {
1318
+ nameToDeclaration: {
1319
+ Foo: {},
1320
+ Bar: {},
1321
+ },
1322
+ },
1323
+ errors: [
1324
+ {
1325
+ expected: 'one of: "struct", "enum", "import", "method", "const"',
1326
+ },
1327
+ ],
1328
+ });
1329
+ });
1330
+ });
1331
+ describe("grammar errors", () => {
1332
+ it("expected: record name", () => {
1333
+ const actualModule = parse(`struct {}`);
1334
+ expect(actualModule).toMatch({
1335
+ errors: [
1336
+ {
1337
+ token: {
1338
+ text: "{",
1339
+ line: {
1340
+ lineNumber: 0,
1341
+ },
1342
+ },
1343
+ expected: "identifier",
1344
+ },
1345
+ ],
1346
+ });
1347
+ });
1348
+ it("expected: positive integer", () => {
1349
+ const actualModule = parse(`struct A { n: int32 = bool; }`);
1350
+ expect(actualModule).toMatch({
1351
+ errors: [
1352
+ {
1353
+ token: {
1354
+ text: "bool",
1355
+ line: {
1356
+ lineNumber: 0,
1357
+ },
1358
+ },
1359
+ expected: "positive integer",
1360
+ },
1361
+ ],
1362
+ });
1363
+ });
1364
+ });
1365
+ });
1366
+ //# sourceMappingURL=parser.test.js.map