FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/packages/language/src/codegen/types.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
/
types.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
307 lines (286 loc) · 8.69 KB
Breadcrumbs
modelscript
/
packages
/
language
/
src
/
codegen
/
types.ts
Copy path
File metadata and controls
307 lines (286 loc) · 8.69 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
import
{
LanguageOptions
}
from
"../dsl.js"
;
import
{
NormalizedGrammar
}
from
"../grammar.js"
;
/**
* Generates AssemblyScript / TypeScript enum definitions (`SyntaxType`, `NodeFlag`, `FieldId`)
* corresponding to the grammar's symbols, fields, and flag attributes.
*
*
@param
grammar The user-defined language DSL options object.
*
@param
normalized The normalized grammar containing symbol-to-int mapping tables.
*
@returns
The generated TypeScript/AssemblyScript source code string for enum definitions.
*/
export
function
generateTypes
(
grammar
:
LanguageOptions
<
any
>
,
normalized
:
NormalizedGrammar
)
:
string
{
let
typeCode
=
`// Token and Node Types for
${
grammar
.
name
}
\n`
;
typeCode
+=
`export enum SyntaxType {\n`
;
typeCode
+=
` ERROR = 0,\n`
;
// Create reverse mapping from integer ID to symbol string (preferring human-readable names)
const
intToSym
=
new
Map
<
number
,
string
>
(
)
;
for
(
const
[
sym
,
i
]
of
normalized
.
symToInt
.
entries
(
)
)
{
const
existing
=
intToSym
.
get
(
i
)
;
if
(
!
existing
)
{
intToSym
.
set
(
i
,
sym
)
;
}
else
{
const
existingIsLiteral
=
existing
.
startsWith
(
'"'
)
||
existing
.
startsWith
(
"/"
)
;
const
symIsLiteral
=
sym
.
startsWith
(
'"'
)
||
sym
.
startsWith
(
"/"
)
;
if
(
existingIsLiteral
&&
!
symIsLiteral
)
{
intToSym
.
set
(
i
,
sym
)
;
}
}
}
// Output symbols in strict sequential ID order
const
emittedNames
=
new
Set
<
string
>
(
)
;
for
(
let
i
=
1
;
i
<=
intToSym
.
size
;
i
++
)
{
const
sym
=
intToSym
.
get
(
i
)
;
if
(
!
sym
)
continue
;
// Convert symbol strings (e.g. literals or regex) to safe C-style identifiers
let
safeName
=
sym
.
replace
(
/
[
^
a
-
z
A
-
Z
0
-
9
]
/
g
,
"_"
)
;
if
(
sym
.
startsWith
(
'"'
)
||
sym
.
startsWith
(
"/"
)
)
{
safeName
=
"T_"
+
i
;
// Guarantee identifier uniqueness for terminal literals
}
else
{
safeName
=
safeName
.
toUpperCase
(
)
;
}
// Prepend underscore if identifier starts with a digit
if
(
/
^
[
0
-
9
]
/
.
test
(
safeName
)
)
safeName
=
"_"
+
safeName
;
// Deduplicate enum field names using numerical suffixing
let
finalName
=
safeName
;
let
suffix
=
1
;
while
(
emittedNames
.
has
(
finalName
)
)
{
finalName
=
`
${
safeName
}
_
${
suffix
}
`
;
suffix
++
;
}
typeCode
+=
`
${
finalName
}
=
${
i
}
,\n`
;
emittedNames
.
add
(
finalName
)
;
}
// Automatically generate shadow SyntaxTypes for any types defined in `model` or `lints` but not in `rules`
let
shadowIdx
=
10000
;
if
(
grammar
.
model
)
{
// Start shadow types at a high offset to avoid collision with standard rules
for
(
const
modelName
of
Object
.
keys
(
grammar
.
model
)
)
{
let
safeName
=
modelName
.
replace
(
/
[
^
a
-
z
A
-
Z
0
-
9
]
/
g
,
"_"
)
.
toUpperCase
(
)
;
if
(
/
^
[
0
-
9
]
/
.
test
(
safeName
)
)
safeName
=
"_"
+
safeName
;
if
(
!
emittedNames
.
has
(
safeName
)
)
{
typeCode
+=
`
${
safeName
}
=
${
shadowIdx
}
,\n`
;
emittedNames
.
add
(
safeName
)
;
shadowIdx
++
;
}
}
}
if
(
grammar
.
lints
)
{
for
(
const
lint
of
Object
.
values
(
grammar
.
lints
)
as
any
[
]
)
{
if
(
lint
&&
Array
.
isArray
(
lint
.
nodes
)
)
{
for
(
const
nodeName
of
lint
.
nodes
)
{
if
(
typeof
nodeName
===
"string"
)
{
let
safeName
=
nodeName
.
replace
(
/
[
^
a
-
z
A
-
Z
0
-
9
]
/
g
,
"_"
)
.
toUpperCase
(
)
;
if
(
/
^
[
0
-
9
]
/
.
test
(
safeName
)
)
safeName
=
"_"
+
safeName
;
if
(
!
emittedNames
.
has
(
safeName
)
)
{
typeCode
+=
`
${
safeName
}
=
${
shadowIdx
}
,\n`
;
emittedNames
.
add
(
safeName
)
;
shadowIdx
++
;
}
}
}
}
}
}
// Fallback shadow types commonly referenced in codegen/typesys
const
commonFallbacks
=
[
"IDENTIFIER"
,
"NUMBER"
,
"REAL"
,
"STRING"
,
"STRING_LITERAL"
,
"BOOLEAN"
,
"UNSIGNED_INTEGER"
,
"UNSIGNED_REAL"
,
"ARRAY_CONSTRUCTOR"
,
"ARRAY_COMPREHENSION"
,
"EXTERNAL_CLAUSE"
,
"PUBLIC_ELEMENT_LIST"
,
"PROTECTED_ELEMENT_LIST"
,
"TUPLE_EXPRESSION"
,
"RANGE_EXPRESSION"
,
"DIV_EXPRESSION"
,
"ASSIGNMENT_STATEMENT"
,
"ADD_EXPRESSION"
,
"SUB_EXPRESSION"
,
"MUL_EXPRESSION"
,
]
;
for
(
const
fallback
of
commonFallbacks
)
{
if
(
!
emittedNames
.
has
(
fallback
)
)
{
typeCode
+=
`
${
fallback
}
=
${
shadowIdx
}
,\n`
;
emittedNames
.
add
(
fallback
)
;
shadowIdx
++
;
}
}
typeCode
+=
` LIST = 99999,\n`
;
typeCode
+=
`}\n\n`
;
// Synthesize NodeFlag bitmask from `type: "flag"` or `type: "bool"` attributes in the model
let
flagBits
=
0
;
const
flagMap
=
new
Map
<
string
,
number
>
(
)
;
typeCode
+=
`export enum NodeFlag {\n`
;
typeCode
+=
` NONE = 0,\n`
;
if
(
grammar
.
model
)
{
for
(
const
modelKey
of
Object
.
keys
(
grammar
.
model
)
)
{
const
attrs
=
(
grammar
.
model
as
any
)
[
modelKey
]
;
for
(
const
attrKey
of
Object
.
keys
(
attrs
)
)
{
const
attr
=
attrs
[
attrKey
]
;
if
(
typeof
attr
===
"object"
&&
(
attr
?.
type
===
"flag"
||
attr
?.
type
===
"bool"
)
)
{
let
safeName
=
attrKey
.
replace
(
/
(
[
a
-
z
]
)
(
[
A
-
Z
]
)
/
g
,
"$1_$2"
)
.
toUpperCase
(
)
;
safeName
=
safeName
.
replace
(
/
[
^
A
-
Z
0
-
9
_
]
/
g
,
"_"
)
;
if
(
/
^
[
0
-
9
]
/
.
test
(
safeName
)
)
safeName
=
"_"
+
safeName
;
if
(
!
flagMap
.
has
(
safeName
)
&&
flagBits
<
30
)
{
flagMap
.
set
(
safeName
,
1
<<
flagBits
)
;
typeCode
+=
`
${
safeName
}
= 1 <<
${
flagBits
}
,\n`
;
flagBits
++
;
}
}
}
}
}
const
commonFlags
=
[
"IS_PROTECTED"
,
"IS_FINAL"
,
"IS_PARTIAL"
,
"IS_CONNECTOR"
,
"HAS_INNER_MATCH"
,
"IS_REPLACEABLE"
,
"IS_STREAM"
,
"IS_FLOW"
,
"IS_INPUT"
,
"IS_OUTPUT"
,
"IS_PARAMETER"
,
"IS_CONSTANT"
,
"IS_DISCRETE"
,
"IS_CLOCKED"
,
"IS_IMPURE"
,
"IS_PURE"
,
"IS_EXPANDABLE"
,
"IS_ENCAPSULATED"
,
"IS_OPERATOR"
,
"IS_FUNCTION"
,
"IS_BLOCK"
,
"IS_MODEL"
,
"IS_RECORD"
,
"IS_TYPE"
,
"IS_PACKAGE"
,
"IS_CLASS"
,
"IS_ARRAY"
,
"IS_VARIABLE"
,
"IS_PRIMITIVE"
,
"IS_ENUM"
,
"IS_STRUCT"
,
"IS_ROOT"
,
]
;
for
(
const
flag
of
commonFlags
)
{
if
(
!
flagMap
.
has
(
flag
)
&&
flagBits
<
30
)
{
flagMap
.
set
(
flag
,
1
<<
flagBits
)
;
typeCode
+=
`
${
flag
}
= 1 <<
${
flagBits
}
,\n`
;
flagBits
++
;
}
}
typeCode
+=
`}\n\n`
;
// Synthesize Property enum for key-value model property lookups
let
propIdx
=
1
;
const
propMap
=
new
Map
<
string
,
number
>
(
)
;
typeCode
+=
`export enum Property {\n`
;
typeCode
+=
` NONE = 0,\n`
;
if
(
grammar
.
model
)
{
for
(
const
modelKey
of
Object
.
keys
(
grammar
.
model
)
)
{
const
attrs
=
(
grammar
.
model
as
any
)
[
modelKey
]
;
for
(
const
attrKey
of
Object
.
keys
(
attrs
)
)
{
const
attr
=
attrs
[
attrKey
]
;
if
(
typeof
attr
===
"object"
&&
typeof
attr
?.
type
===
"string"
)
{
let
safeName
=
attrKey
.
replace
(
/
(
[
a
-
z
]
)
(
[
A
-
Z
]
)
/
g
,
"$1_$2"
)
.
toUpperCase
(
)
;
safeName
=
safeName
.
replace
(
/
[
^
A
-
Z
0
-
9
_
]
/
g
,
"_"
)
;
if
(
/
^
[
0
-
9
]
/
.
test
(
safeName
)
)
safeName
=
"_"
+
safeName
;
if
(
!
propMap
.
has
(
safeName
)
)
{
propMap
.
set
(
safeName
,
propIdx
)
;
typeCode
+=
`
${
safeName
}
=
${
propIdx
}
,\n`
;
propIdx
++
;
}
}
}
}
}
const
commonProps
=
[
"BASE_TYPE"
,
"FLOW_COUNT"
,
"VALUE"
,
"KIND"
,
"VARIABILITY"
,
"CAUSALITY"
,
"CLOCK_ID"
,
"DIMENSIONS"
,
"SHAPE"
,
"START_VALUE"
,
"BINDING_EXPR"
,
"SOURCE_NODE"
,
"TARGET_NODE"
,
]
;
for
(
const
prop
of
commonProps
)
{
if
(
!
propMap
.
has
(
prop
)
)
{
propMap
.
set
(
prop
,
propIdx
)
;
typeCode
+=
`
${
prop
}
=
${
propIdx
}
,\n`
;
propIdx
++
;
}
}
typeCode
+=
`}\n\n`
;
typeCode
+=
`export enum FieldId {\n`
;
typeCode
+=
` NONE = 0,\n`
;
const
fieldNamesMap
=
new
Map
<
string
,
number
>
(
)
;
let
nextFieldId
=
1
;
for
(
const
[
fieldName
,
id
]
of
normalized
.
fieldToInt
.
entries
(
)
)
{
if
(
typeof
fieldName
!==
"string"
)
continue
;
let
safeName
=
fieldName
.
replace
(
/
(
[
a
-
z
]
)
(
[
A
-
Z
]
)
/
g
,
"$1_$2"
)
.
toUpperCase
(
)
;
safeName
=
safeName
.
replace
(
/
[
^
A
-
Z
0
-
9
_
]
/
g
,
"_"
)
;
if
(
!
fieldNamesMap
.
has
(
safeName
)
)
{
fieldNamesMap
.
set
(
safeName
,
id
)
;
if
(
id
>=
nextFieldId
)
nextFieldId
=
id
+
1
;
}
}
// Pre-seed common AST field names so query AST traversals never crash AssemblyScript compilation
const
commonFields
=
[
"NAME"
,
"END_NAME"
,
"LEFT"
,
"RIGHT"
,
"LHS"
,
"RHS"
,
"OPERAND"
,
"ARGUMENT"
,
"ARGUMENTS"
,
"BINDING"
,
"MODIFICATION"
,
"BODY"
,
"CONDITION"
,
"VALUE"
,
"TYPE"
,
"TYPE_SPECIFIER"
,
"FROM"
,
"TO"
,
"SOURCE"
,
"TARGET"
,
"STATEMENT"
,
"EXPRESSION"
,
"REDECLARE"
,
"STEP"
,
"START"
,
"FACTOR"
,
"CONNECT_EQUATION"
,
"DECLARATION"
,
"DECLARATIONS"
,
"ELEMENT"
,
"ELEMENTS"
,
"EQUATION"
,
"EQUATIONS"
,
]
;
for
(
const
f
of
commonFields
)
{
if
(
!
fieldNamesMap
.
has
(
f
)
)
{
fieldNamesMap
.
set
(
f
,
nextFieldId
++
)
;
}
}
for
(
const
[
safeName
,
id
]
of
fieldNamesMap
.
entries
(
)
)
{
typeCode
+=
`
${
safeName
}
=
${
id
}
,\n`
;
}
typeCode
+=
`}\n\n`
;
return
typeCode
;
}
Back
|
FazBrowse Home
|
New Git URL