@scinorandex/sparse 0.0.2 → 0.0.3

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 (62) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +2 -2
  3. package/dist/cli.js.map +1 -1
  4. package/dist/generator.d.ts +4 -1
  5. package/dist/generator.js +47 -32
  6. package/dist/generator.js.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.js +5 -5
  9. package/dist/index.js.map +1 -1
  10. package/dist/meta/common.d.ts +57 -0
  11. package/dist/meta/common.js +62 -0
  12. package/dist/meta/common.js.map +1 -0
  13. package/dist/meta/handwritten.d.ts +12 -0
  14. package/dist/{grammarParser.js → meta/handwritten.js} +24 -24
  15. package/dist/meta/handwritten.js.map +1 -0
  16. package/dist/meta/selfhosted.d.ts +23 -0
  17. package/dist/meta/selfhosted.js +206 -0
  18. package/dist/meta/selfhosted.js.map +1 -0
  19. package/dist/meta/states.d.ts +1580 -0
  20. package/dist/meta/states.js +1493 -0
  21. package/dist/meta/states.js.map +1 -0
  22. package/dist/parser.d.ts +16 -5
  23. package/dist/parser.js +25 -7
  24. package/dist/parser.js.map +1 -1
  25. package/dist/{tableParser.d.ts → table/handwritten.d.ts} +1 -1
  26. package/dist/{tableParser.js → table/handwritten.js} +2 -2
  27. package/dist/table/handwritten.js.map +1 -0
  28. package/dist/table/selfhosted.d.ts +2 -0
  29. package/dist/table/selfhosted.js +65 -0
  30. package/dist/table/selfhosted.js.map +1 -0
  31. package/dist/table/states.d.ts +592 -0
  32. package/dist/table/states.js +593 -0
  33. package/dist/table/states.js.map +1 -0
  34. package/dist/utils/Result.d.ts +1 -1
  35. package/dist/utils/errorWindowBuilder.d.ts +1 -1
  36. package/example/LoLang/example.ts +14 -7
  37. package/example/complicated/grammar.txt +37 -0
  38. package/example/math/example.ts +1 -1
  39. package/example/selfhosted/01-meta-grammar.txt +10 -0
  40. package/example/selfhosted/02-table-grammar.txt +6 -0
  41. package/example/selfhosted/codegen-meta.ts +1489 -0
  42. package/example/selfhosted/codegen-table.ts +589 -0
  43. package/example/selfhosted/example.ts +39 -0
  44. package/package.json +3 -3
  45. package/src/cli.ts +1 -1
  46. package/src/generator.ts +52 -34
  47. package/src/index.ts +3 -2
  48. package/src/meta/common.ts +74 -0
  49. package/src/{grammarParser.ts → meta/handwritten.ts} +17 -21
  50. package/src/meta/selfhosted.ts +272 -0
  51. package/src/meta/states.ts +1489 -0
  52. package/src/parser.ts +53 -19
  53. package/src/{tableParser.ts → table/handwritten.ts} +5 -5
  54. package/src/table/selfhosted.ts +67 -0
  55. package/src/table/states.ts +589 -0
  56. package/src/utils/Result.ts +1 -1
  57. package/src/utils/errorWindowBuilder.ts +1 -1
  58. package/tsconfig.build.json +30 -0
  59. package/tsconfig.json +6 -6
  60. package/dist/grammarParser.d.ts +0 -27
  61. package/dist/grammarParser.js.map +0 -1
  62. package/dist/tableParser.js.map +0 -1
@@ -0,0 +1,589 @@
1
+ export const states = [
2
+ {
3
+ "[L_BRACKET]": {
4
+ "type": "shift",
5
+ "value": 5
6
+ },
7
+ "[L_ANGLE]": {
8
+ "type": "shift",
9
+ "value": 6
10
+ },
11
+ "<PROGRAM>": {
12
+ "type": "goto",
13
+ "value": 1
14
+ },
15
+ "<STATES>": {
16
+ "type": "goto",
17
+ "value": 2
18
+ },
19
+ "<ITEMS>": {
20
+ "type": "goto",
21
+ "value": 3
22
+ },
23
+ "<ITEM>": {
24
+ "type": "goto",
25
+ "value": 4
26
+ }
27
+ },
28
+ {
29
+ "[EOF]": {
30
+ "type": "reduce",
31
+ "value": 0
32
+ }
33
+ },
34
+ {
35
+ "[EOF]": {
36
+ "type": "shift",
37
+ "value": 7
38
+ }
39
+ },
40
+ {
41
+ "[EOF]": {
42
+ "type": "reduce",
43
+ "value": 2
44
+ },
45
+ "[L_BRACKET]": {
46
+ "type": "shift",
47
+ "value": 5
48
+ },
49
+ "[L_ANGLE]": {
50
+ "type": "shift",
51
+ "value": 6
52
+ },
53
+ "<STATES>": {
54
+ "type": "goto",
55
+ "value": 8
56
+ },
57
+ "<ITEMS>": {
58
+ "type": "goto",
59
+ "value": 3
60
+ },
61
+ "<ITEM>": {
62
+ "type": "goto",
63
+ "value": 4
64
+ }
65
+ },
66
+ {
67
+ "[EOF]": {
68
+ "type": "reduce",
69
+ "value": 4
70
+ },
71
+ "[L_BRACKET]": {
72
+ "type": "reduce",
73
+ "value": 4
74
+ },
75
+ "[L_ANGLE]": {
76
+ "type": "reduce",
77
+ "value": 4
78
+ },
79
+ "[COMMA]": {
80
+ "type": "shift",
81
+ "value": 9
82
+ }
83
+ },
84
+ {
85
+ "[IDENTIFIER]": {
86
+ "type": "shift",
87
+ "value": 10
88
+ }
89
+ },
90
+ {
91
+ "[IDENTIFIER]": {
92
+ "type": "shift",
93
+ "value": 11
94
+ }
95
+ },
96
+ {
97
+ "[EOF]": {
98
+ "type": "reduce",
99
+ "value": 1
100
+ }
101
+ },
102
+ {
103
+ "[EOF]": {
104
+ "type": "reduce",
105
+ "value": 3
106
+ }
107
+ },
108
+ {
109
+ "[L_BRACKET]": {
110
+ "type": "shift",
111
+ "value": 5
112
+ },
113
+ "[L_ANGLE]": {
114
+ "type": "shift",
115
+ "value": 6
116
+ },
117
+ "<ITEMS>": {
118
+ "type": "goto",
119
+ "value": 12
120
+ },
121
+ "<ITEM>": {
122
+ "type": "goto",
123
+ "value": 13
124
+ }
125
+ },
126
+ {
127
+ "[R_BRACKET]": {
128
+ "type": "shift",
129
+ "value": 14
130
+ }
131
+ },
132
+ {
133
+ "[R_ANGLE]": {
134
+ "type": "shift",
135
+ "value": 15
136
+ }
137
+ },
138
+ {
139
+ "[EOF]": {
140
+ "type": "reduce",
141
+ "value": 5
142
+ },
143
+ "[L_BRACKET]": {
144
+ "type": "reduce",
145
+ "value": 5
146
+ },
147
+ "[L_ANGLE]": {
148
+ "type": "reduce",
149
+ "value": 5
150
+ }
151
+ },
152
+ {
153
+ "[EOF]": {
154
+ "type": "reduce",
155
+ "value": 4
156
+ },
157
+ "[L_BRACKET]": {
158
+ "type": "reduce",
159
+ "value": 4
160
+ },
161
+ "[L_ANGLE]": {
162
+ "type": "reduce",
163
+ "value": 4
164
+ },
165
+ "[COMMA]": {
166
+ "type": "shift",
167
+ "value": 16
168
+ }
169
+ },
170
+ {
171
+ "[EQUALS]": {
172
+ "type": "shift",
173
+ "value": 17
174
+ }
175
+ },
176
+ {
177
+ "[EQUALS]": {
178
+ "type": "shift",
179
+ "value": 18
180
+ }
181
+ },
182
+ {
183
+ "[L_BRACKET]": {
184
+ "type": "shift",
185
+ "value": 5
186
+ },
187
+ "[L_ANGLE]": {
188
+ "type": "shift",
189
+ "value": 6
190
+ },
191
+ "<ITEMS>": {
192
+ "type": "goto",
193
+ "value": 19
194
+ },
195
+ "<ITEM>": {
196
+ "type": "goto",
197
+ "value": 13
198
+ }
199
+ },
200
+ {
201
+ "[IDENTIFIER]": {
202
+ "type": "shift",
203
+ "value": 20
204
+ }
205
+ },
206
+ {
207
+ "[NUMBER]": {
208
+ "type": "shift",
209
+ "value": 21
210
+ }
211
+ },
212
+ {
213
+ "[EOF]": {
214
+ "type": "reduce",
215
+ "value": 5
216
+ },
217
+ "[L_BRACKET]": {
218
+ "type": "reduce",
219
+ "value": 5
220
+ },
221
+ "[L_ANGLE]": {
222
+ "type": "reduce",
223
+ "value": 5
224
+ }
225
+ },
226
+ {
227
+ "[EOF]": {
228
+ "type": "reduce",
229
+ "value": 6
230
+ },
231
+ "[L_BRACKET]": {
232
+ "type": "reduce",
233
+ "value": 6
234
+ },
235
+ "[L_ANGLE]": {
236
+ "type": "reduce",
237
+ "value": 6
238
+ },
239
+ "[COMMA]": {
240
+ "type": "reduce",
241
+ "value": 6
242
+ }
243
+ },
244
+ {
245
+ "[EOF]": {
246
+ "type": "reduce",
247
+ "value": 7
248
+ },
249
+ "[L_BRACKET]": {
250
+ "type": "reduce",
251
+ "value": 7
252
+ },
253
+ "[L_ANGLE]": {
254
+ "type": "reduce",
255
+ "value": 7
256
+ },
257
+ "[COMMA]": {
258
+ "type": "reduce",
259
+ "value": 7
260
+ }
261
+ }
262
+ ];
263
+ export const productions = [
264
+ {
265
+ "lhs": {
266
+ "column": 1,
267
+ "lexeme": "S",
268
+ "line": 1,
269
+ "type": 0
270
+ },
271
+ "identifier": "<S>",
272
+ "name": null,
273
+ "originalProductionIndex": 0,
274
+ "rhs": [
275
+ {
276
+ "type": "variable",
277
+ "token": {
278
+ "column": 6,
279
+ "lexeme": "PROGRAM",
280
+ "line": 1,
281
+ "type": 0
282
+ },
283
+ "identifier": "<PROGRAM>",
284
+ "name": null
285
+ }
286
+ ]
287
+ },
288
+ {
289
+ "lhs": {
290
+ "column": 1,
291
+ "lexeme": "PROGRAM",
292
+ "line": 2,
293
+ "type": 0
294
+ },
295
+ "identifier": "<PROGRAM>",
296
+ "name": "program",
297
+ "originalProductionIndex": 1,
298
+ "rhs": [
299
+ {
300
+ "type": "variable",
301
+ "token": {
302
+ "column": 21,
303
+ "lexeme": "STATES",
304
+ "line": 2,
305
+ "type": 0
306
+ },
307
+ "identifier": "<STATES>",
308
+ "name": "states"
309
+ },
310
+ {
311
+ "type": "terminal",
312
+ "token": {
313
+ "column": 38,
314
+ "lexeme": "EOF",
315
+ "line": 2,
316
+ "type": 0
317
+ },
318
+ "identifier": "[EOF]",
319
+ "name": null
320
+ }
321
+ ]
322
+ },
323
+ {
324
+ "lhs": {
325
+ "column": 1,
326
+ "lexeme": "STATES",
327
+ "line": 3,
328
+ "type": 0
329
+ },
330
+ "identifier": "<STATES>",
331
+ "name": "states",
332
+ "originalProductionIndex": 2,
333
+ "rhs": [
334
+ {
335
+ "type": "variable",
336
+ "token": {
337
+ "column": 19,
338
+ "lexeme": "ITEMS",
339
+ "line": 3,
340
+ "type": 0
341
+ },
342
+ "identifier": "<ITEMS>",
343
+ "name": "state"
344
+ }
345
+ ]
346
+ },
347
+ {
348
+ "lhs": {
349
+ "column": 1,
350
+ "lexeme": "STATES",
351
+ "line": 3,
352
+ "type": 0
353
+ },
354
+ "identifier": "<STATES>",
355
+ "name": "states",
356
+ "originalProductionIndex": 2,
357
+ "rhs": [
358
+ {
359
+ "type": "variable",
360
+ "token": {
361
+ "column": 19,
362
+ "lexeme": "ITEMS",
363
+ "line": 3,
364
+ "type": 0
365
+ },
366
+ "identifier": "<ITEMS>",
367
+ "name": "state"
368
+ },
369
+ {
370
+ "type": "variable",
371
+ "token": {
372
+ "column": 35,
373
+ "lexeme": "STATES",
374
+ "line": 3,
375
+ "type": 0
376
+ },
377
+ "identifier": "<STATES>",
378
+ "name": "rest"
379
+ }
380
+ ]
381
+ },
382
+ {
383
+ "lhs": {
384
+ "column": 1,
385
+ "lexeme": "ITEMS",
386
+ "line": 4,
387
+ "type": 0
388
+ },
389
+ "identifier": "<ITEMS>",
390
+ "name": "items",
391
+ "originalProductionIndex": 3,
392
+ "rhs": [
393
+ {
394
+ "type": "variable",
395
+ "token": {
396
+ "column": 17,
397
+ "lexeme": "ITEM",
398
+ "line": 4,
399
+ "type": 0
400
+ },
401
+ "identifier": "<ITEM>",
402
+ "name": "item"
403
+ }
404
+ ]
405
+ },
406
+ {
407
+ "lhs": {
408
+ "column": 1,
409
+ "lexeme": "ITEMS",
410
+ "line": 4,
411
+ "type": 0
412
+ },
413
+ "identifier": "<ITEMS>",
414
+ "name": "items",
415
+ "originalProductionIndex": 3,
416
+ "rhs": [
417
+ {
418
+ "type": "variable",
419
+ "token": {
420
+ "column": 17,
421
+ "lexeme": "ITEM",
422
+ "line": 4,
423
+ "type": 0
424
+ },
425
+ "identifier": "<ITEM>",
426
+ "name": "item"
427
+ },
428
+ {
429
+ "type": "terminal",
430
+ "token": {
431
+ "column": 31,
432
+ "lexeme": "COMMA",
433
+ "line": 4,
434
+ "type": 0
435
+ },
436
+ "identifier": "[COMMA]",
437
+ "name": null
438
+ },
439
+ {
440
+ "type": "variable",
441
+ "token": {
442
+ "column": 39,
443
+ "lexeme": "ITEMS",
444
+ "line": 4,
445
+ "type": 0
446
+ },
447
+ "identifier": "<ITEMS>",
448
+ "name": "rest"
449
+ }
450
+ ]
451
+ },
452
+ {
453
+ "lhs": {
454
+ "column": 1,
455
+ "lexeme": "ITEM",
456
+ "line": 5,
457
+ "type": 0
458
+ },
459
+ "identifier": "<ITEM>",
460
+ "name": "terminal",
461
+ "originalProductionIndex": 4,
462
+ "rhs": [
463
+ {
464
+ "type": "terminal",
465
+ "token": {
466
+ "column": 19,
467
+ "lexeme": "L_BRACKET",
468
+ "line": 5,
469
+ "type": 0
470
+ },
471
+ "identifier": "[L_BRACKET]",
472
+ "name": null
473
+ },
474
+ {
475
+ "type": "terminal",
476
+ "token": {
477
+ "column": 31,
478
+ "lexeme": "IDENTIFIER",
479
+ "line": 5,
480
+ "type": 0
481
+ },
482
+ "identifier": "[IDENTIFIER]",
483
+ "name": "name"
484
+ },
485
+ {
486
+ "type": "terminal",
487
+ "token": {
488
+ "column": 50,
489
+ "lexeme": "R_BRACKET",
490
+ "line": 5,
491
+ "type": 0
492
+ },
493
+ "identifier": "[R_BRACKET]",
494
+ "name": null
495
+ },
496
+ {
497
+ "type": "terminal",
498
+ "token": {
499
+ "column": 62,
500
+ "lexeme": "EQUALS",
501
+ "line": 5,
502
+ "type": 0
503
+ },
504
+ "identifier": "[EQUALS]",
505
+ "name": null
506
+ },
507
+ {
508
+ "type": "terminal",
509
+ "token": {
510
+ "column": 71,
511
+ "lexeme": "IDENTIFIER",
512
+ "line": 5,
513
+ "type": 0
514
+ },
515
+ "identifier": "[IDENTIFIER]",
516
+ "name": "action"
517
+ }
518
+ ]
519
+ },
520
+ {
521
+ "lhs": {
522
+ "column": 1,
523
+ "lexeme": "ITEM",
524
+ "line": 6,
525
+ "type": 0
526
+ },
527
+ "identifier": "<ITEM>",
528
+ "name": "variable",
529
+ "originalProductionIndex": 5,
530
+ "rhs": [
531
+ {
532
+ "type": "terminal",
533
+ "token": {
534
+ "column": 19,
535
+ "lexeme": "L_ANGLE",
536
+ "line": 6,
537
+ "type": 0
538
+ },
539
+ "identifier": "[L_ANGLE]",
540
+ "name": null
541
+ },
542
+ {
543
+ "type": "terminal",
544
+ "token": {
545
+ "column": 29,
546
+ "lexeme": "IDENTIFIER",
547
+ "line": 6,
548
+ "type": 0
549
+ },
550
+ "identifier": "[IDENTIFIER]",
551
+ "name": "name"
552
+ },
553
+ {
554
+ "type": "terminal",
555
+ "token": {
556
+ "column": 48,
557
+ "lexeme": "R_ANGLE",
558
+ "line": 6,
559
+ "type": 0
560
+ },
561
+ "identifier": "[R_ANGLE]",
562
+ "name": null
563
+ },
564
+ {
565
+ "type": "terminal",
566
+ "token": {
567
+ "column": 58,
568
+ "lexeme": "EQUALS",
569
+ "line": 6,
570
+ "type": 0
571
+ },
572
+ "identifier": "[EQUALS]",
573
+ "name": null
574
+ },
575
+ {
576
+ "type": "terminal",
577
+ "token": {
578
+ "column": 67,
579
+ "lexeme": "NUMBER",
580
+ "line": 6,
581
+ "type": 0
582
+ },
583
+ "identifier": "[NUMBER]",
584
+ "name": "state"
585
+ }
586
+ ]
587
+ }
588
+ ];
589
+ export const selfhosted = { states, productions };
@@ -0,0 +1,39 @@
1
+ import fs from "fs/promises";
2
+ import { generateStates } from "../../src";
3
+ import { buildProductions, grammarLexerGenerator } from "../../src/meta/selfhosted";
4
+ import { dehydateProduction } from "../../src/meta/common";
5
+
6
+ async function codegenMetaGrammar() {
7
+ const grammar = await fs.readFile("./example/selfhosted/01-meta-grammar.txt", "utf8");
8
+
9
+ const lexer = grammarLexerGenerator.generate(grammar, () => ({}));
10
+ const productions = buildProductions(lexer);
11
+ const statesResult = generateStates(productions);
12
+ if (statesResult.success === false) throw new Error(statesResult.reason);
13
+
14
+ await fs.writeFile(
15
+ "./example/selfhosted/codegen-meta.ts",
16
+ `export const states = ${JSON.stringify(statesResult.value.toJSObject(), null, 2)};
17
+ export const productions = ${JSON.stringify(productions.map(dehydateProduction), null, 2)};
18
+ export const selfhosted = { states, productions };`,
19
+ );
20
+ }
21
+
22
+ async function codegenTableGrammar() {
23
+ const grammar = await fs.readFile("./example/selfhosted/02-table-grammar.txt", "utf8");
24
+
25
+ const lexer = grammarLexerGenerator.generate(grammar, () => ({}));
26
+ const productions = buildProductions(lexer);
27
+ const statesResult = generateStates(productions);
28
+ if (statesResult.success === false) throw new Error(statesResult.reason);
29
+
30
+ await fs.writeFile(
31
+ "./example/selfhosted/codegen-table.ts",
32
+ `export const states = ${JSON.stringify(statesResult.value.toJSObject(), null, 2)};
33
+ export const productions = ${JSON.stringify(productions.map(dehydateProduction), null, 2)};
34
+ export const selfhosted = { states, productions };`,
35
+ );
36
+ }
37
+
38
+ codegenMetaGrammar();
39
+ codegenTableGrammar();
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@scinorandex/sparse",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Yet another parser generator",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
- "build": "tsc",
7
+ "build": "tsc -p tsconfig.build.json",
8
8
  "watch": "tsc -w"
9
9
  },
10
10
  "bin": {
@@ -26,7 +26,7 @@
26
26
  "license": "MIT",
27
27
  "devDependencies": {
28
28
  "@types/minimist": "^1.2.5",
29
- "@types/node": "^22.15.19"
29
+ "@types/node": "^26.0.0"
30
30
  },
31
31
  "resolutions": {},
32
32
  "dependencies": {
package/src/cli.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  #! /usr/bin/env node
2
2
  import fs from "fs/promises";
3
- import { GrammarToken, tryBuildProductions } from "./grammarParser";
3
+ import { tryBuildProductions } from "./index";
4
4
  import { generateStates } from "./generator";
5
5
  import minimist from "minimist";
6
6
  import path from "path";