FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/packages/language/src/codegen/parser.ts at main · modelscript/modelscript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
modelscript
/
modelscript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
12
Code
Issues
1
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modelscript
/
packages
/
language
/
src
/
codegen
/
parser.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
1248 lines (1137 loc) · 48.6 KB
Breadcrumbs
modelscript
/
packages
/
language
/
src
/
codegen
/
parser.ts
Copy path
File metadata and controls
1248 lines (1137 loc) · 48.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import
{
GLRTable
,
LRAutomaton
}
from
"../automata.js"
;
import
{
LanguageOptions
,
SOURCE_PATH_SYMBOL
,
SOURCE_TEXT_SYMBOL
}
from
"../dsl.js"
;
import
{
NormalizedGrammar
}
from
"../grammar.js"
;
import
{
extractLanguageAST
}
from
"./ast-loader.js"
;
import
{
arenaCode
,
arrayCode
,
bdfCode
,
bltCode
,
builtins_mathCode
,
casCode
,
coloringCode
,
correspondenceCode
,
cseCode
,
cursorCode
,
daeCode
,
delayCode
,
engineCode
,
evalCode
,
eventsCode
,
fmi2_wasmCode
,
fmi3_wasmCode
,
foldCode
,
gssCode
,
hashmapCode
,
homotopyCode
,
integratorsCode
,
isolationCode
,
lspCode
,
matrixCode
,
ontologyCode
,
ontology_projectionCode
,
pantelidesCode
,
parserLoopCode
,
polyglot_arenaCode
,
recoveryCode
,
recoveryConfigCode
,
scalarizeCode
,
scope_stackCode
,
sparse_choleskyCode
,
sparse_luCode
,
string_poolCode
,
stubCode
,
tapeCode
,
tearingCode
,
trigramCode
,
vmapCode
,
}
from
"../../build/src-gen/runtime-templates.js"
;
import
{
generateAliasAnalysis
}
from
"./alias.js"
;
import
{
generateCFG
}
from
"./cfg.js"
;
import
{
compileMcpConfig
}
from
"./compile_mcp.js"
;
import
{
compileTGGRules
}
from
"./compile_tgg.js"
;
import
{
generateDataflow
}
from
"./dataflow.js"
;
import
{
generateEGraphEngine
}
from
"./egraph.js"
;
import
{
generateCodeGraphBridge
}
from
"./graph.js"
;
import
{
generateBlockLayoutConstants
}
from
"./ir_layout.js"
;
import
{
generateIsolationDomain
}
from
"./isolation.js"
;
import
{
generateLexer
}
from
"./lexer.js"
;
import
{
generateOctagonDomain
}
from
"./octagon.js"
;
import
{
generatePantelidesDomain
}
from
"./pantelides.js"
;
import
{
generateReasoner
}
from
"./reasoner.js"
;
import
{
generateSAT
}
from
"./sat.js"
;
import
{
generateSimplex
}
from
"./simplex.js"
;
import
{
generateSSA
}
from
"./ssa.js"
;
import
{
transpileClass
,
transpileHelperFunction
}
from
"./transpiler.js"
;
import
{
generateTypes
}
from
"./types.js"
;
import
{
generateTypeSystem
}
from
"./typesys.js"
;
/**
* The consolidated result of a successful grammar analysis and parsing phase.
*/
export
interface
ParserGenerationResult
{
/** The normalized grammar AST containing rules, precedence matrices, and aliases. */
grammar
:
NormalizedGrammar
;
/** The generated LR State Machine handling states, GOTO, and lookaheads. */
automaton
:
LRAutomaton
;
/** The action and goto lookup tables computed for the GLR parser. */
table
:
GLRTable
;
}
/**
* Orchestrates the compilation of a raw DSL definition into a normalized grammar,
* builds the LALR(1) state machine, and generates the GLR lookup tables.
*
*
@param
options The compiler options / DSL definition
*
@returns
The generated grammar, LR automaton, and GLR action/goto tables
*/
export
function
generateParser
<
RuleName
extends
string
>
(
options
:
LanguageOptions
<
RuleName
>
)
:
ParserGenerationResult
{
const
grammar
=
new
NormalizedGrammar
(
options
as
unknown
as
LanguageOptions
<
any
>
)
;
const
automaton
=
new
LRAutomaton
(
grammar
)
;
const
table
=
new
GLRTable
(
grammar
,
automaton
)
;
return
{
grammar
,
automaton
,
table
}
;
}
/**
* A virtual file object representing an AssemblyScript source file.
*/
export
interface
GeneratedFile
{
/** The desired relative filename (e.g. `parser.ts`, `arena.ts`). */
filename
:
string
;
/** The generated source code content. */
content
:
string
;
}
/**
* Orchestrates the conversion of the GLR lookup tables and AST logic
* into executable AssemblyScript source files. Generates static WASM
* arrays and injects token/preprocessor hooks into the runtime templates.
*
*
@param
originalGrammar The original DSL definition block
*
@param
grammar The normalized grammar representation
*
@param
table The precomputed GLR tables
*
@param
syncTokens Tokens marked explicitly for error recovery anchors
*
@param
preprocessorHook The name of the lexer entry function (default: "lex")
*
@returns
Array of AssemblyScript file payloads to be compiled by `asc`
*/
export
function
generateParserTables
(
originalGrammar
:
LanguageOptions
<
any
>
,
grammar
:
NormalizedGrammar
,
table
:
GLRTable
,
syncTokens
:
string
[
]
=
[
]
,
preprocessorHook
=
""
,
)
:
GeneratedFile
[
]
{
(
originalGrammar
as
any
)
.
fieldToInt
=
grammar
.
fieldToInt
;
const
LEX_FN
=
preprocessorHook
?
preprocessorHook
:
"lex"
;
let
code
=
`import { ChunkedUint32Array, ChunkedInt32Array, UnmanagedUint32Array } from "./array";\nimport { allocNode, getInputBuffer, atomicChunkAlloc, getArenaOffset, getNodeType, getNodeFirstChild, getNodeNextSibling } from "./arena";\nimport { DaeBuilder } from "./dae";\nimport { allocDiagnostic } from "./graph";\nimport { CorrespondenceIndex } from "./correspondence";\nimport { PolyglotArena } from "./polyglot_arena";\nexport { getInputBuffer } from "./arena";\n\n@external("parser", "logInt")\nexport declare function logInt(val: i32): void;\n\nexport function decodeHexIntArray(hex: string, numElements: i32): usize {
let raw = atomicChunkAlloc((numElements + 1) * 4);
let ptr = (raw + 3) & ~3;
store<i32>(ptr, numElements);
let dataPtr = ptr + 4;
let arr = changetype<UnmanagedUint32Array>(dataPtr);
for (let i = 0; i < numElements; i++) {
let val: u32 = 0;
for (let j = 0; j < 8; j++) {
let c = hex.charCodeAt(i * 8 + j);
let nibble = c >= 97 ? c - 97 + 10 : (c >= 65 ? c - 65 + 10 : c - 48);
val = (val << 4) | (nibble as u32);
}
arr[i] = val;
}
return dataPtr;
}\n\nexport let expected_tokens: usize = 0;\n\n`
;
// Types & SyntaxType enum
code
+=
generateTypes
(
originalGrammar
,
grammar
)
;
code
+=
`\n// GLR Parser Tables\n`
;
code
+=
`// Generated for
${
grammar
.
productions
.
length
}
productions and
${
table
.
actionTable
.
size
}
states\n\n`
;
const
symToInt
=
grammar
.
symToInt
;
const
startSymName
=
Object
.
keys
(
originalGrammar
.
rules
)
[
0
]
||
"Program"
;
const
startSymId
=
symToInt
.
get
(
startSymName
)
||
1
;
code
+=
`export const SYMBOL_COUNT =
${
symToInt
.
size
}
;\n`
;
code
+=
`export const STATE_COUNT =
${
table
.
actionTable
.
size
}
;\n`
;
code
+=
`export const START_SYMBOL_ID =
${
startSymId
}
;\n\n`
;
const
actionOffsets
:
number
[
]
=
[
]
;
const
actionData
:
number
[
]
=
[
]
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
actionOffsets
.
push
(
actionData
.
length
)
;
const
actions
=
table
.
actionTable
.
get
(
stateId
)
!
;
actionData
.
push
(
actions
.
size
)
;
for
(
const
[
sym
,
acts
]
of
actions
.
entries
(
)
)
{
actionData
.
push
(
symToInt
.
get
(
sym
)
!
)
;
actionData
.
push
(
acts
.
length
)
;
const
sortedActs
=
[
...
acts
]
.
sort
(
(
a
,
b
)
=>
{
if
(
a
.
type
!==
1
||
b
.
type
!==
1
)
return
0
;
// 1 is ActionType.REDUCE
const
prodA
=
grammar
.
productions
.
find
(
(
p
)
=>
p
.
id
===
a
.
target
)
;
const
prodB
=
grammar
.
productions
.
find
(
(
p
)
=>
p
.
id
===
b
.
target
)
;
const
precDiff
=
(
prodB
?.
dynamicPrec
||
0
)
-
(
prodA
?.
dynamicPrec
||
0
)
;
if
(
precDiff
!==
0
)
return
precDiff
;
return
(
b
.
target
||
0
)
-
(
a
.
target
||
0
)
;
}
)
;
for
(
const
act
of
sortedActs
)
{
actionData
.
push
(
act
.
type
)
;
actionData
.
push
(
act
.
target
||
0
)
;
}
}
}
const
generateStaticArray
=
(
arr
:
number
[
]
,
name
:
string
)
=>
{
if
(
arr
.
length
===
0
)
return
`export const
${
name
}
: usize = memory.data<i32>([0, 0]) + 4;\n`
;
let
hex
=
""
;
for
(
const
item
of
arr
)
{
const
val
=
item
===
undefined
?
1
:
item
;
hex
+=
(
val
>>>
0
)
.
toString
(
16
)
.
padStart
(
8
,
"0"
)
;
}
return
`export const
${
name
}
: usize = decodeHexIntArray("
${
hex
}
",
${
arr
.
length
}
);\n`
;
}
;
code
+=
generateStaticArray
(
actionOffsets
,
"action_offsets"
)
;
code
+=
generateStaticArray
(
actionData
,
"action_data"
)
;
const
gotoOffsets
:
number
[
]
=
[
]
;
const
gotoData
:
number
[
]
=
[
]
;
for
(
let
stateId
=
0
;
stateId
<
table
.
gotoTable
.
size
;
stateId
++
)
{
gotoOffsets
.
push
(
gotoData
.
length
)
;
const
gotos
=
table
.
gotoTable
.
get
(
stateId
)
!
;
gotoData
.
push
(
gotos
.
size
)
;
for
(
const
[
sym
,
target
]
of
gotos
.
entries
(
)
)
{
gotoData
.
push
(
symToInt
.
get
(
sym
)
!
)
;
gotoData
.
push
(
target
)
;
}
}
code
+=
generateStaticArray
(
gotoOffsets
,
"goto_offsets"
)
;
code
+=
generateStaticArray
(
gotoData
,
"goto_data"
)
;
const
mrd
=
table
.
automaton
.
computeMRD
(
)
;
code
+=
generateStaticArray
(
mrd
,
"mrd_data"
)
;
const
terminalFreq
=
new
Map
<
string
,
number
>
(
)
;
for
(
const
p
of
grammar
.
productions
)
{
for
(
const
sym
of
p
.
right
)
{
terminalFreq
.
set
(
sym
,
(
terminalFreq
.
get
(
sym
)
||
0
)
+
1
)
;
}
}
const
termList
=
Array
.
from
(
grammar
.
terminals
)
;
const
tokenInsertCosts
:
number
[
]
=
new
Array
(
termList
.
length
+
5
)
.
fill
(
1
)
;
// 1. Analyze grammar productions for structural roles (Tree-sitter style)
const
structuralOpeners
=
new
Set
<
string
>
(
)
;
const
structuralClosers
=
new
Set
<
string
>
(
)
;
const
structuralSeparators
=
new
Set
<
string
>
(
)
;
for
(
const
p
of
grammar
.
productions
)
{
const
rhs
=
p
.
right
;
if
(
rhs
.
length
===
0
)
continue
;
const
firstSym
=
rhs
[
0
]
;
const
lastSym
=
rhs
[
rhs
.
length
-
1
]
;
if
(
grammar
.
terminals
.
has
(
firstSym
)
)
{
structuralOpeners
.
add
(
firstSym
)
;
}
if
(
grammar
.
terminals
.
has
(
lastSym
)
)
{
structuralClosers
.
add
(
lastSym
)
;
}
// Infix separators: terminals flanked by non-terminals in sequences/repetitions
for
(
let
i
=
1
;
i
<
rhs
.
length
-
1
;
i
++
)
{
const
sym
=
rhs
[
i
]
;
if
(
grammar
.
terminals
.
has
(
sym
)
&&
grammar
.
nonTerminals
.
has
(
rhs
[
i
-
1
]
)
&&
grammar
.
nonTerminals
.
has
(
rhs
[
i
+
1
]
)
)
{
if
(
p
.
prec
===
undefined
&&
!
p
.
assoc
)
{
structuralSeparators
.
add
(
sym
)
;
}
}
}
}
const
grammarOperators
=
new
Set
<
string
>
(
)
;
for
(
const
p
of
grammar
.
productions
)
{
const
rhs
=
p
.
right
;
if
(
p
.
prec
!==
undefined
||
p
.
assoc
!==
undefined
)
{
for
(
const
sym
of
rhs
)
{
if
(
grammar
.
terminals
.
has
(
sym
)
)
{
grammarOperators
.
add
(
sym
)
;
}
}
}
else
if
(
rhs
.
length
===
3
)
{
const
[
left
,
op
,
right
]
=
rhs
;
if
(
grammar
.
nonTerminals
.
has
(
left
)
&&
grammar
.
terminals
.
has
(
op
)
&&
grammar
.
nonTerminals
.
has
(
right
)
)
{
grammarOperators
.
add
(
op
)
;
}
}
}
const
customDelims
=
originalGrammar
.
recovery
?.
delimiters
||
[
]
;
const
customOps
=
originalGrammar
.
recovery
?.
operators
||
[
]
;
for
(
const
op
of
customOps
)
{
grammarOperators
.
add
(
op
)
;
}
for
(
let
i
=
0
;
i
<
termList
.
length
;
i
++
)
{
const
sym
=
termList
[
i
]
;
const
symId
=
symToInt
.
get
(
sym
)
??
i
;
const
cleanSym
=
sym
.
replace
(
/
^
"
|
"
$
/
g
,
""
)
;
const
isCustomDelim
=
customDelims
.
includes
(
sym
)
||
customDelims
.
includes
(
cleanSym
)
;
const
isOperator
=
grammarOperators
.
has
(
sym
)
||
grammarOperators
.
has
(
cleanSym
)
;
const
isWord
=
/
^
[
a
-
z
A
-
Z
_
]
/
.
test
(
cleanSym
)
;
const
isStructuralDelimiter
=
isCustomDelim
||
(
(
structuralClosers
.
has
(
sym
)
||
structuralOpeners
.
has
(
sym
)
)
&&
!
isWord
&&
cleanSym
!==
";"
&&
cleanSym
!==
","
&&
cleanSym
!==
":"
)
;
if
(
cleanSym
===
";"
||
cleanSym
===
","
||
cleanSym
===
":"
)
{
tokenInsertCosts
[
symId
]
=
1
;
// Low cost strictly for structural punctuation and list separators ; , :
}
else
{
tokenInsertCosts
[
symId
]
=
50
;
// Restricted insertion cost (50) for operators (=, +, -, etc.), keywords, types, block delimiters, and data terminals
}
}
code
+=
generateStaticArray
(
tokenInsertCosts
,
"token_insert_costs"
)
;
const
tokenIsWord
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
for
(
const
[
sym
,
symId
]
of
symToInt
.
entries
(
)
)
{
if
(
sym
.
startsWith
(
'"'
)
)
{
const
cleanOp
=
sym
.
slice
(
1
,
-
1
)
;
if
(
/
^
[
a
-
z
A
-
Z
_
]
/
.
test
(
cleanOp
)
)
{
tokenIsWord
[
symId
]
=
1
;
}
}
else
if
(
sym
.
startsWith
(
"/"
)
)
{
if
(
sym
.
includes
(
"a-z"
)
||
sym
.
includes
(
"A-Z"
)
||
sym
.
includes
(
"_"
)
)
{
tokenIsWord
[
symId
]
=
1
;
}
}
}
code
+=
generateStaticArray
(
tokenIsWord
,
"token_is_word"
)
;
const
tokenIsOperator
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
for
(
const
[
sym
,
symId
]
of
symToInt
.
entries
(
)
)
{
const
cleanSym
=
sym
.
replace
(
/
^
"
|
"
$
/
g
,
""
)
;
if
(
grammarOperators
.
has
(
sym
)
||
grammarOperators
.
has
(
cleanSym
)
)
{
tokenIsOperator
[
symId
]
=
1
;
}
}
code
+=
generateStaticArray
(
tokenIsOperator
,
"token_is_operator"
)
;
let
maxTerminalId
=
0
;
for
(
const
term
of
grammar
.
terminals
)
{
if
(
term
!==
"EOF"
&&
term
!==
"ERROR"
)
{
maxTerminalId
++
;
}
}
const
sortedSymbols
=
Array
.
from
(
{
length
:
symToInt
.
size
}
,
(
_
,
i
)
=>
i
+
1
)
.
filter
(
(
id
)
=>
id
<=
maxTerminalId
)
;
sortedSymbols
.
sort
(
(
a
,
b
)
=>
tokenInsertCosts
[
a
]
-
tokenInsertCosts
[
b
]
)
;
code
+=
generateStaticArray
(
sortedSymbols
,
"sorted_insertion_symbols"
)
;
const
MAX_REACHABILITY_DEPTH
=
Math
.
min
(
64
,
table
.
actionTable
.
size
)
;
const
reachabilityMatrix
=
new
Uint8Array
(
table
.
actionTable
.
size
*
(
maxTerminalId
+
1
)
)
;
reachabilityMatrix
.
fill
(
254
)
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
const
actions
=
table
.
actionTable
.
get
(
stateId
)
;
if
(
actions
)
{
for
(
const
[
sym
,
acts
]
of
actions
.
entries
(
)
)
{
if
(
acts
.
some
(
(
a
)
=>
a
.
type
===
0
)
)
{
// 0 is SHIFT
const
symId
=
symToInt
.
get
(
sym
)
;
if
(
symId
!==
undefined
&&
symId
<=
maxTerminalId
)
{
reachabilityMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
symId
]
=
0
;
}
}
}
}
}
console
.
log
(
"Building Unbounded Reachability Matrix for"
,
table
.
actionTable
.
size
,
"states and"
,
maxTerminalId
,
"terminals"
,
)
;
// Precompute GOTO targets for each non-terminal
const
gotoTargets
=
new
Map
<
number
,
number
[
]
>
(
)
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
const
gotos
=
table
.
gotoTable
.
get
(
stateId
)
;
if
(
gotos
)
{
for
(
const
[
sym
,
nextState
]
of
gotos
.
entries
(
)
)
{
const
symId
=
symToInt
.
get
(
sym
)
;
if
(
symId
!==
undefined
)
{
if
(
!
gotoTargets
.
has
(
symId
)
)
gotoTargets
.
set
(
symId
,
[
]
)
;
gotoTargets
.
get
(
symId
)
!
.
push
(
nextState
)
;
}
}
}
}
let
matrixChanged
=
true
;
for
(
let
iter
=
0
;
iter
<
MAX_REACHABILITY_DEPTH
;
iter
++
)
{
if
(
!
matrixChanged
)
break
;
matrixChanged
=
false
;
const
newMatrix
=
new
Uint8Array
(
reachabilityMatrix
)
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
const
actions
=
table
.
actionTable
.
get
(
stateId
)
;
const
gotos
=
table
.
gotoTable
.
get
(
stateId
)
;
if
(
actions
)
{
for
(
const
[
sym
,
acts
]
of
actions
.
entries
(
)
)
{
for
(
const
act
of
acts
)
{
if
(
act
.
type
===
0
&&
act
.
target
!==
undefined
)
{
const
nextState
=
act
.
target
;
for
(
let
t
=
1
;
t
<=
maxTerminalId
;
t
++
)
{
const
altCost
=
1
+
reachabilityMatrix
[
nextState
*
(
maxTerminalId
+
1
)
+
t
]
;
if
(
altCost
<
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
)
{
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
=
altCost
;
matrixChanged
=
true
;
}
}
}
else
if
(
act
.
type
===
1
&&
act
.
target
!==
undefined
)
{
// REDUCE action: cost is 0 GSS transitions (reductions are "free" lookahead steps)
const
prod
=
table
.
grammar
.
productions
[
act
.
target
]
;
const
ruleSymId
=
symToInt
.
get
(
prod
.
left
)
;
if
(
ruleSymId
!==
undefined
&&
gotoTargets
.
has
(
ruleSymId
)
)
{
for
(
const
nextState
of
gotoTargets
.
get
(
ruleSymId
)
!
)
{
for
(
let
t
=
1
;
t
<=
maxTerminalId
;
t
++
)
{
const
altCost
=
reachabilityMatrix
[
nextState
*
(
maxTerminalId
+
1
)
+
t
]
;
if
(
altCost
<
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
)
{
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
=
altCost
;
matrixChanged
=
true
;
}
}
}
}
}
}
}
}
if
(
gotos
)
{
for
(
const
[
sym
,
nextState
]
of
gotos
.
entries
(
)
)
{
const
cost
=
1
;
// GOTO counts as 1 GSS transition (shifting a non-terminal)
for
(
let
t
=
1
;
t
<=
maxTerminalId
;
t
++
)
{
const
altCost
=
cost
+
reachabilityMatrix
[
nextState
*
(
maxTerminalId
+
1
)
+
t
]
;
if
(
altCost
<
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
)
{
newMatrix
[
stateId
*
(
maxTerminalId
+
1
)
+
t
]
=
altCost
;
matrixChanged
=
true
;
}
}
}
}
}
reachabilityMatrix
.
set
(
newMatrix
)
;
}
code
+=
generateStaticArray
(
Array
.
from
(
reachabilityMatrix
)
,
"reachability_matrix"
)
;
// Build Precomputed 1-Token Repair Table (Phase 2)
const
precomputedRepairs
=
new
Uint16Array
(
table
.
actionTable
.
size
*
(
maxTerminalId
+
1
)
)
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
const
actions
=
table
.
actionTable
.
get
(
stateId
)
;
if
(
!
actions
)
continue
;
const
validShiftSyms
:
{
symId
:
number
;
targetState
:
number
}
[
]
=
[
]
;
for
(
const
[
sym
,
acts
]
of
actions
.
entries
(
)
)
{
const
symId
=
symToInt
.
get
(
sym
)
;
if
(
symId
!==
undefined
&&
symId
<=
maxTerminalId
)
{
for
(
const
act
of
acts
)
{
if
(
act
.
type
===
0
&&
act
.
target
!==
undefined
)
{
validShiftSyms
.
push
(
{
symId
,
targetState
:
act
.
target
}
)
;
}
}
}
}
for
(
let
unexpectedTok
=
1
;
unexpectedTok
<=
maxTerminalId
;
unexpectedTok
++
)
{
let
bestRepairTok
=
0
;
let
minCost
=
254
;
for
(
const
{
symId
,
targetState
}
of
validShiftSyms
)
{
const
cost
=
reachabilityMatrix
[
targetState
*
(
maxTerminalId
+
1
)
+
unexpectedTok
]
;
if
(
cost
<
minCost
)
{
minCost
=
cost
;
bestRepairTok
=
symId
;
}
}
precomputedRepairs
[
stateId
*
(
maxTerminalId
+
1
)
+
unexpectedTok
]
=
bestRepairTok
;
}
}
code
+=
generateStaticArray
(
Array
.
from
(
precomputedRepairs
)
,
"precomputed_repairs"
)
;
// Literal terminal strings for keyword/symbol similarity matching
const
tokenStringOffsets
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
-
1
)
;
const
tokenStringBytes
:
number
[
]
=
[
]
;
for
(
const
[
sym
,
symId
]
of
symToInt
.
entries
(
)
)
{
if
(
sym
.
startsWith
(
'"'
)
&&
sym
.
endsWith
(
'"'
)
&&
sym
.
length
>
2
)
{
const
literal
=
sym
.
slice
(
1
,
-
1
)
;
tokenStringOffsets
[
symId
]
=
tokenStringBytes
.
length
;
tokenStringBytes
.
push
(
literal
.
length
)
;
for
(
let
i
=
0
;
i
<
literal
.
length
;
i
++
)
{
tokenStringBytes
.
push
(
literal
.
charCodeAt
(
i
)
)
;
}
}
}
code
+=
generateStaticArray
(
tokenStringOffsets
,
"token_string_offsets"
)
;
code
+=
generateStaticArray
(
tokenStringBytes
,
"token_string_bytes"
)
;
const
syncIds
:
number
[
]
=
[
]
;
for
(
const
t
of
syncTokens
)
{
const
id
=
symToInt
.
get
(
`"
${
t
}
"`
)
||
symToInt
.
get
(
t
)
;
if
(
id
!==
undefined
)
syncIds
.
push
(
id
)
;
}
code
+=
generateStaticArray
(
syncIds
,
"sync_tokens"
)
;
// 1. Identify list separators
const
listSeparators
=
new
Set
<
string
>
(
)
;
for
(
const
p
of
grammar
.
productions
)
{
if
(
p
.
isList
&&
p
.
right
.
length
>=
2
&&
p
.
right
[
0
]
===
p
.
left
)
{
const
potentialSeparator
=
p
.
right
[
1
]
;
if
(
grammar
.
terminals
.
has
(
potentialSeparator
)
)
{
listSeparators
.
add
(
potentialSeparator
)
;
}
}
}
// 2. Generate tokenDeleteCosts
const
tokenDeleteCosts
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
10
)
;
for
(
const
[
sym
,
id
]
of
symToInt
.
entries
(
)
)
{
let
cost
=
10
;
if
(
listSeparators
.
has
(
sym
)
)
{
cost
=
200
;
}
if
(
syncTokens
.
includes
(
sym
.
replace
(
/
^
"
|
"
$
/
g
,
""
)
)
||
syncTokens
.
includes
(
sym
)
)
{
cost
=
1000
;
}
tokenDeleteCosts
[
id
]
=
cost
;
}
const
eofId
=
symToInt
.
get
(
"EOF"
)
;
if
(
eofId
!==
undefined
)
{
tokenDeleteCosts
[
eofId
]
=
5000
;
}
code
+=
generateStaticArray
(
tokenDeleteCosts
,
"token_delete_costs"
)
;
// 3. Compute Minimal Yield Terminal Sequences (Phase 3)
const
minYieldMap
=
new
Map
<
string
,
number
[
]
>
(
)
;
for
(
const
term
of
grammar
.
terminals
)
{
const
id
=
symToInt
.
get
(
term
)
;
if
(
id
!==
undefined
)
{
minYieldMap
.
set
(
term
,
[
id
]
)
;
}
}
let
yieldChanged
=
true
;
while
(
yieldChanged
)
{
yieldChanged
=
false
;
for
(
const
p
of
grammar
.
productions
)
{
const
lhs
=
p
.
left
;
let
valid
=
true
;
let
sequence
:
number
[
]
=
[
]
;
for
(
const
sym
of
p
.
right
)
{
const
symYield
=
minYieldMap
.
get
(
sym
)
;
if
(
!
symYield
)
{
valid
=
false
;
break
;
}
sequence
.
push
(
...
symYield
)
;
}
if
(
valid
)
{
const
existing
=
minYieldMap
.
get
(
lhs
)
;
if
(
!
existing
||
sequence
.
length
<
existing
.
length
)
{
minYieldMap
.
set
(
lhs
,
sequence
)
;
yieldChanged
=
true
;
}
}
}
}
const
minYieldOffsets
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
const
minYieldData
:
number
[
]
=
[
]
;
for
(
let
symId
=
1
;
symId
<=
symToInt
.
size
;
symId
++
)
{
minYieldOffsets
[
symId
]
=
minYieldData
.
length
;
let
symName
=
""
;
for
(
const
[
s
,
id
]
of
symToInt
.
entries
(
)
)
{
if
(
id
===
symId
)
{
symName
=
s
;
break
;
}
}
const
seq
=
symName
?
minYieldMap
.
get
(
symName
)
:
undefined
;
if
(
seq
)
{
minYieldData
.
push
(
seq
.
length
)
;
for
(
const
tId
of
seq
)
{
minYieldData
.
push
(
tId
)
;
}
}
else
{
minYieldData
.
push
(
0
)
;
}
}
code
+=
generateStaticArray
(
minYieldOffsets
,
"min_yield_offsets"
)
;
code
+=
generateStaticArray
(
minYieldData
,
"min_yield_data"
)
;
// 4. Compute Scope Dominator & Boundary Bitmaps (Phase 4)
const
stateScopeBounds
=
new
Uint8Array
(
table
.
actionTable
.
size
*
(
maxTerminalId
+
1
)
)
;
for
(
let
stateId
=
0
;
stateId
<
table
.
actionTable
.
size
;
stateId
++
)
{
const
lrState
=
table
.
automaton
.
states
[
stateId
]
;
if
(
!
lrState
)
continue
;
for
(
const
item
of
lrState
.
items
)
{
for
(
let
pos
=
item
.
dot
;
pos
<
item
.
production
.
right
.
length
;
pos
++
)
{
const
sym
=
item
.
production
.
right
[
pos
]
;
if
(
grammar
.
terminals
.
has
(
sym
)
)
{
const
symId
=
symToInt
.
get
(
sym
)
;
const
cleanSym
=
sym
.
replace
(
/
^
"
|
"
$
/
g
,
""
)
;
const
isBoundary
=
cleanSym
===
"}"
||
cleanSym
===
"]"
||
cleanSym
===
")"
||
cleanSym
===
";"
||
cleanSym
===
"end"
||
cleanSym
===
"else"
||
cleanSym
===
"elseif"
;
if
(
isBoundary
&&
symId
!==
undefined
&&
symId
<=
maxTerminalId
)
{
stateScopeBounds
[
stateId
*
(
maxTerminalId
+
1
)
+
symId
]
=
1
;
}
}
}
if
(
item
.
dot
===
item
.
production
.
right
.
length
)
{
for
(
const
la
of
item
.
lookahead
)
{
const
cleanLa
=
la
.
replace
(
/
^
"
|
"
$
/
g
,
""
)
;
const
isBoundary
=
cleanLa
===
"}"
||
cleanLa
===
"]"
||
cleanLa
===
")"
||
cleanLa
===
";"
||
cleanLa
===
"end"
;
const
symId
=
symToInt
.
get
(
la
)
;
if
(
isBoundary
&&
symId
!==
undefined
&&
symId
<=
maxTerminalId
)
{
stateScopeBounds
[
stateId
*
(
maxTerminalId
+
1
)
+
symId
]
=
1
;
}
}
}
}
}
code
+=
generateStaticArray
(
Array
.
from
(
stateScopeBounds
)
,
"state_scope_bounds"
)
;
const
prodLengths
:
number
[
]
=
[
]
;
const
prodRightOffsets
:
number
[
]
=
[
]
;
const
prodRightSymbols
:
number
[
]
=
[
]
;
const
prodLhs
:
number
[
]
=
[
]
;
const
prodIsStructural
:
number
[
]
=
[
]
;
const
prodIsInvisible
:
number
[
]
=
[
]
;
const
prodIsList
:
number
[
]
=
[
]
;
const
prodDynamicPrec
:
number
[
]
=
[
]
;
const
prodAliases
:
number
[
]
=
[
]
;
const
aliasData
:
number
[
]
=
[
]
;
const
customStructural
=
originalGrammar
.
recovery
?.
structuralRules
||
[
]
;
const
sortedProds
=
[
...
grammar
.
productions
]
.
sort
(
(
a
,
b
)
=>
a
.
id
-
b
.
id
)
;
for
(
const
p
of
sortedProds
)
{
prodRightOffsets
.
push
(
prodRightSymbols
.
length
)
;
for
(
const
sym
of
p
.
right
)
{
prodRightSymbols
.
push
(
symToInt
.
get
(
sym
)
||
0
)
;
}
prodLengths
.
push
(
p
.
right
.
length
)
;
const
lhs
=
symToInt
.
get
(
p
.
left
)
;
if
(
lhs
===
undefined
)
{
console
.
log
(
"prod_lhs is undefined for:"
,
p
.
left
)
;
}
prodLhs
.
push
(
lhs
||
0
)
;
prodIsInvisible
.
push
(
p
.
isInvisible
?
1
:
0
)
;
prodIsList
.
push
(
p
.
isList
?
1
:
0
)
;
prodDynamicPrec
.
push
(
p
.
dynamicPrec
||
0
)
;
let
isStructural
=
0
;
if
(
customStructural
.
length
>
0
)
{
isStructural
=
customStructural
.
includes
(
p
.
left
as
any
)
?
1
:
0
;
}
else
{
if
(
p
.
left
.
endsWith
(
"_list"
)
||
p
.
left
.
endsWith
(
"_clause"
)
||
p
.
left
.
endsWith
(
"_section"
)
||
p
.
left
.
endsWith
(
"_prefixes"
)
||
p
.
left
.
includes
(
"declaration"
)
||
p
.
left
.
includes
(
"definition"
)
||
p
.
left
.
includes
(
"statement"
)
||
p
.
left
.
includes
(
"specifier"
)
||
p
.
left
.
includes
(
"block"
)
||
p
.
left
.
includes
(
"suite"
)
)
{
isStructural
=
1
;
}
if
(
p
.
left
.
includes
(
"expression"
)
||
p
.
left
.
includes
(
"term"
)
||
p
.
left
.
includes
(
"factor"
)
||
p
.
left
.
includes
(
"literal"
)
)
{
isStructural
=
0
;
}
}
prodIsStructural
.
push
(
isStructural
)
;
if
(
p
.
aliases
&&
p
.
aliases
.
length
>
0
)
{
prodAliases
.
push
(
aliasData
.
length
)
;
aliasData
.
push
(
p
.
aliases
.
length
)
;
for
(
const
a
of
p
.
aliases
)
{
aliasData
.
push
(
a
.
index
)
;
aliasData
.
push
(
symToInt
.
get
(
a
.
target
)
||
0
)
;
}
}
else
{
prodAliases
.
push
(
-
1
)
;
}
}
const
maxSymId
=
Math
.
max
(
0
,
...
Array
.
from
(
symToInt
.
values
(
)
)
)
;
const
typeFields
:
number
[
]
=
new
Array
(
maxSymId
+
1
)
.
fill
(
-
1
)
;
const
typeFieldData
:
number
[
]
=
[
]
;
let
maxSyntheticDepth
=
0
;
function
getFieldsForSymbol
(
symName
:
string
,
visited
=
new
Set
<
string
>
(
)
,
depth
=
0
,
)
:
Map
<
number
,
{
index
:
number
;
expectedType
:
number
}
[
]
>
{
if
(
depth
>
maxSyntheticDepth
)
{
maxSyntheticDepth
=
depth
;
}
const
map
=
new
Map
<
number
,
{
index
:
number
;
expectedType
:
number
}
[
]
>
(
)
;
if
(
visited
.
has
(
symName
)
)
return
map
;
visited
.
add
(
symName
)
;
const
symId
=
symToInt
.
get
(
symName
)
||
0
;
for
(
const
p
of
grammar
.
productions
)
{
if
(
(
symToInt
.
get
(
p
.
left
)
||
0
)
===
symId
)
{
if
(
p
.
fields
)
{
for
(
const
f
of
p
.
fields
)
{
if
(
!
map
.
has
(
f
.
fieldId
)
)
map
.
set
(
f
.
fieldId
,
[
]
)
;
const
childSym
=
p
.
right
[
f
.
index
]
;
const
expectedType
=
symToInt
.
get
(
childSym
)
||
0
;
const
list
=
map
.
get
(
f
.
fieldId
)
!
;
if
(
!
list
.
some
(
(
e
)
=>
e
.
index
===
f
.
index
&&
e
.
expectedType
===
expectedType
)
)
{
list
.
push
(
{
index
:
f
.
index
,
expectedType
}
)
;
}
}
}
for
(
let
i
=
0
;
i
<
p
.
right
.
length
;
i
++
)
{
const
childSym
=
p
.
right
[
i
]
;
if
(
childSym
.
startsWith
(
"_"
)
)
{
const
childFields
=
getFieldsForSymbol
(
childSym
,
new
Set
(
visited
)
,
depth
+
1
)
;
const
expectedType
=
symToInt
.
get
(
childSym
)
||
0
;
for
(
const
fieldId
of
childFields
.
keys
(
)
)
{
if
(
!
map
.
has
(
fieldId
)
)
map
.
set
(
fieldId
,
[
]
)
;
const
list
=
map
.
get
(
fieldId
)
!
;
const
idx
=
i
|
0x8000
;
if
(
!
list
.
some
(
(
e
)
=>
e
.
index
===
idx
&&
e
.
expectedType
===
expectedType
)
)
{
list
.
push
(
{
index
:
idx
,
expectedType
}
)
;
}
}
}
}
}
}
return
map
;
}
for
(
const
[
symName
,
symId
]
of
symToInt
.
entries
(
)
)
{
const
fieldsMap
=
getFieldsForSymbol
(
symName
)
;
if
(
fieldsMap
.
size
>
0
)
{
typeFields
[
symId
]
=
typeFieldData
.
length
;
typeFieldData
.
push
(
fieldsMap
.
size
)
;
for
(
const
[
fieldId
,
entries
]
of
fieldsMap
.
entries
(
)
)
{
typeFieldData
.
push
(
fieldId
)
;
typeFieldData
.
push
(
entries
.
length
)
;
for
(
const
entry
of
entries
)
{
typeFieldData
.
push
(
entry
.
index
)
;
typeFieldData
.
push
(
entry
.
expectedType
)
;
}
}
}
}
const
maxFieldCursorDepth
=
Math
.
max
(
16
,
maxSyntheticDepth
+
8
)
;
const
tokenTypesMap
=
new
Map
<
string
,
number
>
(
)
;
const
tokenModifiersMap
=
new
Map
<
string
,
number
>
(
)
;
for
(
const
p
of
sortedProds
)
{
if
(
p
.
semantics
)
{
for
(
const
s
of
p
.
semantics
)
{
if
(
!
tokenTypesMap
.
has
(
s
.
type
)
)
tokenTypesMap
.
set
(
s
.
type
,
tokenTypesMap
.
size
)
;
const
mods
=
Array
.
isArray
(
s
.
modifiers
)
?
s
.
modifiers
:
Object
.
keys
(
s
.
modifiers
||
{
}
)
;
for
(
const
m
of
mods
)
{
if
(
!
tokenModifiersMap
.
has
(
m
)
)
tokenModifiersMap
.
set
(
m
,
tokenModifiersMap
.
size
)
;
}
}
}
}
const
typeSemantics
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
-
1
)
;
const
typeSemanticData
:
number
[
]
=
[
]
;
for
(
let
symId
=
1
;
symId
<=
symToInt
.
size
;
symId
++
)
{
const
semanticsList
=
new
Map
<
number
,
{
type
:
number
;
bitmask
:
number
}
>
(
)
;
for
(
const
p
of
sortedProds
)
{
if
(
(
symToInt
.
get
(
p
.
left
)
||
0
)
===
symId
&&
p
.
semantics
)
{
for
(
const
s
of
p
.
semantics
)
{
let
bitmask
=
0
;
const
mods
=
Array
.
isArray
(
s
.
modifiers
)
?
s
.
modifiers
:
Object
.
keys
(
s
.
modifiers
||
{
}
)
;
for
(
const
m
of
mods
)
{
bitmask
|=
1
<<
tokenModifiersMap
.
get
(
m
)
!
;
}
semanticsList
.
set
(
s
.
index
,
{
type
:
tokenTypesMap
.
get
(
s
.
type
)
!
,
bitmask
}
)
;
}
}
}
if
(
semanticsList
.
size
>
0
)
{
typeSemantics
[
symId
]
=
typeSemanticData
.
length
;
typeSemanticData
.
push
(
semanticsList
.
size
)
;
for
(
const
[
index
,
sem
]
of
semanticsList
.
entries
(
)
)
{
typeSemanticData
.
push
(
index
)
;
typeSemanticData
.
push
(
sem
.
type
)
;
typeSemanticData
.
push
(
sem
.
bitmask
)
;
}
}
}
code
+=
generateStaticArray
(
prodLengths
,
"prod_lengths"
)
;
code
+=
generateStaticArray
(
prodRightOffsets
,
"prod_right_offsets"
)
;
code
+=
generateStaticArray
(
prodRightSymbols
,
"prod_right_symbols"
)
;
code
+=
generateStaticArray
(
prodLhs
,
"prod_lhs"
)
;
code
+=
generateStaticArray
(
prodIsStructural
,
"prod_is_structural"
)
;
code
+=
generateStaticArray
(
prodIsInvisible
,
"prod_is_invisible"
)
;
code
+=
generateStaticArray
(
prodIsList
,
"prod_is_list"
)
;
code
+=
generateStaticArray
(
prodDynamicPrec
,
"prod_dynamic_prec"
)
;
code
+=
generateStaticArray
(
prodAliases
,
"prod_aliases"
)
;
code
+=
generateStaticArray
(
aliasData
.
length
>
0
?
aliasData
:
[
0
]
,
"alias_data"
)
;
code
+=
generateStaticArray
(
typeFields
,
"type_fields"
)
;
code
+=
generateStaticArray
(
typeFieldData
.
length
>
0
?
typeFieldData
:
[
0
]
,
"type_field_data"
)
;
code
+=
generateStaticArray
(
typeSemantics
,
"type_semantics"
)
;
code
+=
generateStaticArray
(
typeSemanticData
.
length
>
0
?
typeSemanticData
:
[
0
]
,
"type_semantic_data"
)
;
const
typeIsList
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
for
(
let
p
=
0
;
p
<
prodLhs
.
length
;
p
++
)
{
if
(
prodIsList
[
p
]
===
1
)
typeIsList
[
prodLhs
[
p
]
]
=
1
;
}
code
+=
generateStaticArray
(
typeIsList
,
"type_is_list"
)
;
const
typeIsFolding
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
if
(
originalGrammar
.
lsp
&&
originalGrammar
.
lsp
.
folding
)
{
for
(
const
f
of
originalGrammar
.
lsp
.
folding
)
{
const
id
=
symToInt
.
get
(
f
)
||
symToInt
.
get
(
`"
${
f
}
"`
)
;
if
(
id
!==
undefined
)
typeIsFolding
[
id
]
=
1
;
}
}
code
+=
generateStaticArray
(
typeIsFolding
,
"type_is_folding"
)
;
const
typeIsOutline
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
if
(
originalGrammar
.
lsp
&&
originalGrammar
.
lsp
.
outline
)
{
for
(
const
f
of
originalGrammar
.
lsp
.
outline
)
{
const
id
=
symToInt
.
get
(
f
)
||
symToInt
.
get
(
`"
${
f
}
"`
)
;
if
(
id
!==
undefined
)
typeIsOutline
[
id
]
=
1
;
}
}
code
+=
generateStaticArray
(
typeIsOutline
,
"type_is_outline"
)
;
const
typeIsSymbol
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
const
symbolNameField
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
const
symbolIsScope
:
number
[
]
=
new
Array
(
symToInt
.
size
+
1
)
.
fill
(
0
)
;
if
(
originalGrammar
.
symbols
)
{
for
(
const
[
ruleName
,
config
]
of
Object
.
entries
(
originalGrammar
.
symbols
)
)
{
const
id
=
symToInt
.
get
(
ruleName
)
||
symToInt
.
get
(
`"
${
ruleName
}
"`
)
;
if
(
id
!==
undefined
&&
config
)
{
typeIsSymbol
[
id
]
=
1
;
if
(
config
.
name
&&
grammar
.
fieldToInt
)
{
const
fieldId
=
grammar
.
fieldToInt
.
get
(
config
.
name
)
||
0
;
symbolNameField
[
id
]
=
fieldId
;
}
symbolIsScope
[
id
]
=
config
.
scope
!==
false
?
1
:
0
;
}
}
}
code
+=
generateStaticArray
(
typeIsSymbol
,
"type_is_symbol"
)
;
code
+=
generateStaticArray
(
symbolNameField
,
"symbol_name_field"
)
;
code
+=
generateStaticArray
(
symbolIsScope
,
"symbol_is_scope"
)
;
code
+=
generateLexer
(
originalGrammar
,
grammar
)
;
code
+=
`\nexport const MAX_TERMINAL_ID =
${
maxTerminalId
}
;\nexport const MAX_SYMBOL_ID =
${
symToInt
.
size
}
;\nexport const MAX_FIELD_CURSOR_DEPTH: i32 =
${
maxFieldCursorDepth
}
;\n`
;
code
+=
`\nexport function invokeLexer(pos: u32): i32 { return
${
LEX_FN
}
(pos); }\n`
;
let
lintSwitchStr
=
""
;
if
(
originalGrammar
.
lints
)
{
const
validLintFns
:
string
[
]
=
[
]
;
let
nextLintId
=
2000
;
const
nodeLints
=
new
Map
<
string
,
string
[
]
>
(
)
;
for
(
const
[
lintName
,
lint
]
of
Object
.
entries
(
originalGrammar
.
lints
)
)
{
const
queryFn
=
typeof
lint
===
"object"
&&
lint
!==
null
&&
(
lint
as
any
)
.
query
?
(
lint
as
any
)
.
query
:
typeof
lint
===
"string"
?
lint
:
null
;
if
(
!
queryFn
)
continue
;
const
lintId
=
typeof
lint
===
"object"
&&
lint
!==
null
&&
(
lint
as
any
)
.
code
?
(
lint
as
any
)
.
code
:
nextLintId
++
;
const
fnName
=
`lint_
${
lintName
}
`
;
validLintFns
.
push
(
fnName
)
;
for
(
const
nodeName
of
(
lint
as
any
)
.
nodes
||
[
]
)
{
if
(
!
nodeLints
.
has
(
nodeName
)
)
nodeLints
.
set
(
nodeName
,
[
]
)
;
nodeLints
.
get
(
nodeName
)
!
.
push
(
`
${
fnName
}
(node,
${
lintId
}
, nodeStart, nodeEnd);`
)
;
}
}
if
(
validLintFns
.
length
>
0
)
{
code
+=
`import {
${
validLintFns
.
join
(
", "
)
}
} from "./graph";\n`
;
}
lintSwitchStr
+=
`\nexport function executeLints(type: u16, node: u32, nodeStart: u32, nodeEnd: u32): void {\n switch (type) {\n`
;
for
(
const
[
nodeName
,
fnCalls
]
of
nodeLints
.
entries
(
)
)
{
const
symId
=
symToInt
.
get
(
nodeName
)
;
if
(
symId
!==
undefined
)
{
lintSwitchStr
+=
` case
${
symId
}
: /*
${
nodeName
}
*/\n`
;
}
else
{
lintSwitchStr
+=
` case <u16>SyntaxType.
${
nodeName
.
toUpperCase
(
)
}
:\n`
;
}
for
(
const
call
of
fnCalls
)
{
lintSwitchStr
+=
`
${
call
}
\n`
;
}
lintSwitchStr
+=
` break;\n`
;
}
lintSwitchStr
+=
" default:\n break;\n }\n}\n"
;
}
else
{
lintSwitchStr
+=
`\nexport function executeLints(type: u16, node: u32, nodeStart: u32, nodeEnd: u32): void {}\n`
;
}
code
+=
lintSwitchStr
;
const
extractExports
=
(
codeStr
:
string
,
moduleName
:
string
)
=>
{
const
exports
:
string
[
]
=
[
]
;
const
regex
=
/
^
e
x
p
o
r
t
\s
+
(?:
@
(?:
u
n
m
a
n
a
g
e
d
|
i
n
l
i
n
e
)
\s
+
)
?
(?:
a
b
s
t
r
a
c
t
\s
+
)
?
(
f
u
n
c
t
i
o
n
|
c
o
n
s
t
|
l
e
t
|
v
a
r
|
c
l
a
s
s
|
e
n
u
m
|
t
y
p
e
|
i
n
t
e
r
f
a
c
e
)
\s
+
(
[
a
-
z
A
-
Z
0
-
9
_
]
+
)
/
gm
;
let
match
;
const
ignoreList
=
new
Set
(
[
"action_offsets"
,
"action_data"
,
"goto_offsets"
,
"goto_data"
,
"mrd_data"
,
"token_insert_costs"
,
"token_delete_costs"
,
"token_is_word"
,
"token_is_operator"
,
"reachability_matrix"
,
"precomputed_repairs"
,
"token_string_offsets"
,
"token_string_bytes"
,
"sorted_insertion_symbols"
,
"prod_lengths"
,
"prod_right_offsets"
,
"prod_right_symbols"
,
"prod_lhs"
,
"prod_is_structural"
,
"prod_is_invisible"
,
"prod_is_list"
,
"prod_dynamic_prec"
,
"prod_aliases"
,
"alias_data"
,
"type_fields"
,
"type_field_data"
,
"type_is_list"
,
"expected_tokens"
,
]
)
;
while
(
(
match
=
regex
.
exec
(
codeStr
)
)
!==
null
)
{
if
(
!
ignoreList
.
has
(
match
[
2
]
)
)
{
exports
.
push
(
match
[
2
]
)
;
}
}
if
(
exports
.
length
>
0
)
{
return
`export {
${
exports
.
join
(
", "
)
}
} from "
${
moduleName
}
";\n`
;
}
return
""
;
}
;
code
+=
"\n"
;
code
+=
extractExports
(
engineCode
,
"./engine"
)
;
code
+=
extractExports
(
lspCode
,
"./lsp"
)
;
code
+=
extractExports
(
generateCodeGraphBridge
(
originalGrammar
)
,
"./graph"
)
;
code
+=
extractExports
(
arenaCode
,
"./arena"
)
;
code
+=
extractExports
(
parserLoopCode
,
"./parser-loop"
)
;
code
+=
extractExports
(
gssCode
,
"./gss"
)
;
code
+=
extractExports
(
recoveryCode
,
"./recovery"
)
;
code
+=
extractExports
(
bltCode
,
"./blt"
)
;
code
+=
extractExports
(
correspondenceCode
,
"./correspondence"
)
;
code
+=
extractExports
(
polyglot_arenaCode
,
"./polyglot_arena"
)
;
if
(
originalGrammar
.
typeSystem
)
{
const
tsCode
=
generateTypeSystem
(
originalGrammar
,
originalGrammar
.
typeSystem
.
customCode
||
""
)
;
code
+=
"\n"
+
extractExports
(
tsCode
,
"./typesys"
)
;
}
if
(
originalGrammar
.
semantics
)
{
const
rsCode
=
generateReasoner
(
originalGrammar
,
grammar
)
;
code
+=
"\n"
+
extractExports
(
rsCode
,
"./reasoner"
)
;
if
(
(
originalGrammar
.
semantics
.
reasoner
as
any
)
?.
smt
)
{
if
(
(
originalGrammar
.
semantics
.
reasoner
as
any
)
?.
smt
?.
theories
?.
includes
(
"LRA"
)
)
{
const
simplexCode
=
generateSimplex
(
originalGrammar
)
;
code
+=
"\n"
+
extractExports
(
simplexCode
,
"./simplex"
)
;
}
const
satCode
=
generateSAT
(
originalGrammar
,
grammar
)
;
code
+=
"\n"
+
extractExports
(
satCode
,
"./sat"
)
;
}
}
if
(
originalGrammar
.
polyglot
)
{
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL