-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathnodes.ts
More file actions
570 lines (492 loc) · 17.8 KB
/
nodes.ts
File metadata and controls
570 lines (492 loc) · 17.8 KB
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
import {TokenObject} from "../compiler_tokenizer/tokenObject";
import {TokenRange} from "./tokenRange";
export enum AccessModifier {
Private = 'Private',
Protected = 'Protected',
}
export enum TypeModifier {
In = 'In',
Out = 'Out',
InOut = 'InOut',
}
export enum ReferenceModifier {
At = 'At',
AtConst = 'AtConst',
}
export interface EntityAttribute {
readonly isShared: boolean,
readonly isExternal: boolean,
readonly isAbstract: boolean,
readonly isFinal: boolean,
}
export interface FunctionAttribute {
readonly isOverride: boolean,
readonly isFinal: boolean,
readonly isExplicit: boolean,
readonly isProperty: boolean
}
export enum NodeName {
NodeName = 'NodeName',
Namespace = 'Namespace',
Enum = 'Enum',
Class = 'Class',
TypeDef = 'TypeDef',
Func = 'Func',
Interface = 'Interface',
Var = 'Var',
Import = 'Import',
FuncDef = 'FuncDef',
VirtualProp = 'VirtualProp',
Mixin = 'Mixin',
IntfMethod = 'IntfMethod',
StatBlock = 'StatBlock',
ParamList = 'ParamList',
TypeMod = 'TypeMod',
Type = 'Type',
InitList = 'InitList',
Scope = 'Scope',
DataType = 'DataType',
PrimType = 'PrimType',
FuncAttr = 'FuncAttr',
Statement = 'Statement',
Switch = 'Switch',
Break = 'Break',
For = 'For',
While = 'While',
DoWhile = 'DoWhile',
If = 'If',
Continue = 'Continue',
ExprStat = 'ExprStat',
Try = 'Try',
Return = 'Return',
Case = 'Case',
Expr = 'Expr',
ExprTerm = 'ExprTerm',
ExprValue = 'ExprValue',
ConstructCall = 'ConstructCall',
ExprPreOp = 'ExprPreOp',
ExprPostOp = 'ExprPostOp',
Cast = 'Cast',
Lambda = 'Lambda',
Literal = 'Literal',
FuncCall = 'FuncCall',
VarAccess = 'VarAccess',
ArgList = 'ArgList',
Assign = 'Assign',
Condition = 'Condition',
ExprOp = 'ExprOp',
BitOp = 'BitOp',
MathOp = 'MathOp',
CompOp = 'CompOp',
LogicOp = 'LogicOp',
AssignOp = 'AssignOp',
Identifier = 'Identifier',
Number = 'Number',
String = 'String',
Bits = 'Bits',
Comment = 'Comment',
Whitespace = 'Whitespace',
}
export interface NodesBase {
readonly nodeName: NodeName;
readonly nodeRange: TokenRange;
}
// SCRIPT ::= {IMPORT | ENUM | TYPEDEF | CLASS | MIXIN | INTERFACE | FUNCDEF | VIRTPROP | VAR | FUNC | NAMESPACE | ';'}
export type NodeScript = NodeScriptMember[];
export type NodeScriptMember =
NodeImport
| NodeEnum
| NodeTypeDef
| NodeClass
| NodeMixin
| NodeInterface
| NodeFuncDef
| NodeVirtualProp
| NodeVar
| NodeFunc
| NodeNamespace;
// NAMESPACE ::= 'namespace' IDENTIFIER {'::' IDENTIFIER} '{' SCRIPT '}'
export interface NodeNamespace extends NodesBase {
readonly nodeName: NodeName.Namespace
readonly namespaceList: TokenObject[],
readonly script: NodeScript
}
// ENUM ::= {'shared' | 'external'} 'enum' IDENTIFIER (';' | ('{' IDENTIFIER ['=' EXPR] {',' IDENTIFIER ['=' EXPR]} '}'))
export interface NodeEnum extends NodesBase {
readonly nodeName: NodeName.Enum;
readonly scopeRange: TokenRange;
readonly entity: EntityAttribute | undefined;
readonly identifier: TokenObject;
readonly memberList: ParsedEnumMember[];
}
export interface ParsedEnumMember {
readonly identifier: TokenObject,
readonly expr: NodeExpr | undefined
}
// CLASS ::= {'shared' | 'abstract' | 'final' | 'external'} 'class' IDENTIFIER (';' | ([':' IDENTIFIER {',' IDENTIFIER}] '{' {VIRTPROP | FUNC | VAR | FUNCDEF} '}'))
export interface NodeClass extends NodesBase {
readonly nodeName: NodeName.Class;
readonly scopeRange: TokenRange;
readonly metadata: TokenObject[][];
readonly entity: EntityAttribute | undefined;
readonly identifier: TokenObject;
readonly typeTemplates: NodeType[] | undefined;
readonly baseList: TokenObject[];
readonly memberList: (NodeVirtualProp | NodeVar | NodeFunc | NodeFuncDef)[];
}
// TYPEDEF ::= 'typedef' PRIMTYPE IDENTIFIER ';'
export interface NodeTypeDef extends NodesBase {
readonly nodeName: NodeName.TypeDef;
readonly type: TokenObject;
readonly identifier: TokenObject;
}
// FUNC ::= {'shared' | 'external'} ['private' | 'protected'] [((TYPE ['&']) | '~')] IDENTIFIER PARAMLIST ['const'] FUNCATTR (';' | STATBLOCK)
export interface NodeFunc extends NodesBase {
readonly nodeName: NodeName.Func;
readonly entity: EntityAttribute | undefined;
readonly accessor: AccessModifier | undefined;
readonly head: FuncHead;
readonly identifier: TokenObject;
readonly paramList: NodeParamList;
readonly isConst: boolean;
readonly funcAttr: FunctionAttribute | undefined;
readonly statBlock: NodeStatBlock;
}
export interface FuncHeadReturnValue {
readonly returnType: NodeType;
readonly isRef: boolean;
}
export const funcHeadDestructor = Symbol();
export type FuncHeadDestructor = typeof funcHeadDestructor;
export const funcHeadConstructor = Symbol();
export type FuncHeadConstructor = typeof funcHeadConstructor;
export type FuncHead = FuncHeadReturnValue | FuncHeadDestructor | FuncHeadConstructor;
export function isFuncHeadReturnValue(head: FuncHead): head is FuncHeadReturnValue {
return head !== funcHeadDestructor && head !== funcHeadConstructor;
}
// INTERFACE ::= {'external' | 'shared'} 'interface' IDENTIFIER (';' | ([':' IDENTIFIER {',' IDENTIFIER}] '{' {VIRTPROP | INTFMTHD} '}'))
export interface NodeInterface extends NodesBase {
readonly nodeName: NodeName.Interface;
readonly entity: EntityAttribute | undefined;
readonly identifier: TokenObject;
readonly baseList: TokenObject[];
readonly memberList: (NodeVirtualProp | NodeIntfMethod)[];
}
// VAR ::= ['private'|'protected'] TYPE IDENTIFIER [( '=' (INITLIST | ASSIGN)) | ARGLIST] {',' IDENTIFIER [( '=' (INITLIST | ASSIGN)) | ARGLIST]} ';'
export interface NodeVar extends NodesBase {
readonly nodeName: NodeName.Var
readonly accessor: AccessModifier | undefined,
readonly type: NodeType,
readonly variables: ParsedVariableInit[];
}
export interface ParsedVariableInit {
readonly identifier: TokenObject;
readonly initializer: NodeInitList | NodeAssign | NodeArgList | undefined;
}
// IMPORT ::= 'import' TYPE ['&'] IDENTIFIER PARAMLIST FUNCATTR 'from' STRING ';'
export interface NodeImport extends NodesBase {
readonly nodeName: NodeName.Import;
readonly type: NodeType;
readonly isRef: boolean;
readonly identifier: TokenObject;
readonly paramList: NodeParamList;
readonly funcAttr: FunctionAttribute | undefined;
readonly path: TokenObject;
}
// FUNCDEF ::= {'external' | 'shared'} 'funcdef' TYPE ['&'] IDENTIFIER PARAMLIST ';'
export interface NodeFuncDef extends NodesBase {
readonly nodeName: NodeName.FuncDef;
readonly entity: EntityAttribute | undefined;
readonly returnType: NodeType;
readonly isRef: boolean;
readonly identifier: TokenObject;
readonly paramList: NodeParamList;
}
// VIRTPROP ::= ['private' | 'protected'] TYPE ['&'] IDENTIFIER '{' {('get' | 'set') ['const'] FUNCATTR (STATBLOCK | ';')} '}'
export interface NodeVirtualProp extends NodesBase {
readonly nodeName: NodeName.VirtualProp
readonly accessor: AccessModifier | undefined,
readonly type: NodeType,
readonly isRef: boolean,
readonly identifier: TokenObject,
readonly getter: ParsedGetterSetter | undefined,
readonly setter: ParsedGetterSetter | undefined
}
export interface ParsedGetterSetter {
readonly isConst: boolean,
readonly funcAttr: FunctionAttribute | undefined,
readonly statBlock: NodeStatBlock | undefined
}
// MIXIN ::= 'mixin' CLASS
export interface NodeMixin extends NodesBase {
readonly nodeName: NodeName.Mixin;
readonly mixinClass: NodeClass;
}
// INTFMTHD ::= TYPE ['&'] IDENTIFIER PARAMLIST ['const'] ';'
export interface NodeIntfMethod extends NodesBase {
readonly nodeName: NodeName.IntfMethod;
readonly returnType: NodeType;
readonly isRef: boolean;
readonly identifier: TokenObject;
readonly paramList: NodeParamList;
readonly isConst: boolean;
}
// STATBLOCK ::= '{' {VAR | STATEMENT} '}'
export interface NodeStatBlock extends NodesBase {
readonly nodeName: NodeName.StatBlock;
readonly statementList: (NodeVar | NodeStatement)[];
}
// PARAMLIST ::= '(' ['void' | (TYPE TYPEMOD [IDENTIFIER] ['=' EXPR] {',' TYPE TYPEMOD [IDENTIFIER] ['=' EXPR]})] ')'
export type NodeParamList = ParsedTypeIdentifier[];
export interface ParsedTypeIdentifier {
readonly type: NodeType,
readonly modifier: TypeModifier | undefined,
readonly identifier: TokenObject | undefined
readonly defaultExpr: NodeExpr | undefined
}
// TYPEMOD ::= ['&' ['in' | 'out' | 'inout']]
// TYPE ::= ['const'] SCOPE DATATYPE ['<' TYPE {',' TYPE} '>'] { ('[' ']') | ('@' ['const']) }
export interface NodeType extends NodesBase {
readonly nodeName: NodeName.Type
readonly isConst: boolean,
readonly scope: NodeScope | undefined,
readonly dataType: NodeDataType,
readonly typeTemplates: NodeType[],
readonly isArray: boolean,
readonly refModifier: ReferenceModifier | undefined,
}
// INITLIST ::= '{' [ASSIGN | INITLIST] {',' [ASSIGN | INITLIST]} '}'
export interface NodeInitList extends NodesBase {
readonly nodeName: NodeName.InitList;
readonly initList: (NodeAssign | NodeInitList)[];
}
// SCOPE ::= ['::'] {IDENTIFIER '::'} [IDENTIFIER ['<' TYPE {',' TYPE} '>'] '::']
export interface NodeScope extends NodesBase {
readonly nodeName: NodeName.Scope
readonly isGlobal: boolean,
readonly scopeList: TokenObject[],
readonly typeTemplates: NodeType[]
}
// DATATYPE ::= (IDENTIFIER | PRIMTYPE | '?' | 'auto')
export interface NodeDataType extends NodesBase {
readonly nodeName: NodeName.DataType;
readonly identifier: TokenObject;
}
// PRIMTYPE ::= 'void' | 'int' | 'int8' | 'int16' | 'int32' | 'int64' | 'uint' | 'uint8' | 'uint16' | 'uint32' | 'uint64' | 'float' | 'double' | 'bool'
// FUNCATTR ::= {'override' | 'final' | 'explicit' | 'property'}
// STATEMENT ::= (IF | FOR | WHILE | RETURN | STATBLOCK | BREAK | CONTINUE | DOWHILE | SWITCH | EXPRSTAT | TRY)
export type NodeStatement =
NodeIf
| NodeFor
| NodeWhile
| NodeReturn
| NodeStatBlock
| NodeBreak
| NodeContinue
| NodeDoWhile
| NodeSwitch
| NodeExprStat
| NodeTry;
// SWITCH ::= 'switch' '(' ASSIGN ')' '{' {CASE} '}'
export interface NodeSwitch extends NodesBase {
readonly nodeName: NodeName.Switch
readonly assign: NodeAssign,
readonly caseList: NodeCase[]
}
// BREAK ::= 'break' ';'
export interface NodeBreak extends NodesBase {
readonly nodeName: NodeName.Break;
}
// FOR ::= 'for' '(' (VAR | EXPRSTAT) EXPRSTAT [ASSIGN {',' ASSIGN}] ')' STATEMENT
export interface NodeFor extends NodesBase {
readonly nodeName: NodeName.For
readonly initial: NodeVar | NodeExprStat,
readonly condition: NodeExprStat | undefined
readonly incrementList: NodeAssign[],
readonly statement: NodeStatement | undefined
}
// WHILE ::= 'while' '(' ASSIGN ')' STATEMENT
export interface NodeWhile extends NodesBase {
readonly nodeName: NodeName.While
readonly assign: NodeAssign,
readonly statement: NodeStatement | undefined
}
// DOWHILE ::= 'do' STATEMENT 'while' '(' ASSIGN ')' ';'
export interface NodeDoWhile extends NodesBase {
readonly nodeName: NodeName.DoWhile
readonly statement: NodeStatement,
readonly assign: NodeAssign | undefined
}
// IF ::= 'if' '(' ASSIGN ')' STATEMENT ['else' STATEMENT]
export interface NodeIf extends NodesBase {
readonly nodeName: NodeName.If
readonly condition: NodeAssign,
readonly thenStat: NodeStatement | undefined,
readonly elseStat: NodeStatement | undefined
}
// CONTINUE ::= 'continue' ';'
export interface NodeContinue extends NodesBase {
readonly nodeName: NodeName.Continue;
}
// EXPRSTAT ::= [ASSIGN] ';'
export interface NodeExprStat extends NodesBase {
readonly nodeName: NodeName.ExprStat,
readonly assign: NodeAssign | undefined
}
// TRY ::= 'try' STATBLOCK 'catch' STATBLOCK
export interface NodeTry extends NodesBase {
readonly nodeName: NodeName.Try;
readonly tryBlock: NodeStatBlock,
readonly catchBlock: NodeStatBlock | undefined
}
// RETURN ::= 'return' [ASSIGN] ';'
export interface NodeReturn extends NodesBase {
readonly nodeName: NodeName.Return;
readonly assign: NodeAssign | undefined;
}
// CASE ::= (('case' EXPR) | 'default') ':' {STATEMENT}
export interface NodeCase extends NodesBase {
readonly nodeName: NodeName.Case
readonly expr: NodeExpr | undefined,
readonly statementList: NodeStatement[]
}
// EXPR ::= EXPRTERM {EXPROP EXPRTERM}
export interface NodeExpr extends NodesBase {
readonly nodeName: NodeName.Expr
readonly head: NodeExprTerm,
readonly tail: ParsedOpExpr | undefined
}
export interface ParsedOpExpr {
readonly operator: TokenObject,
readonly expression: NodeExpr
}
// EXPRTERM ::= ([TYPE '='] INITLIST) | ({EXPRPREOP} EXPRVALUE {EXPRPOSTOP})
export type NodeExprTerm = NodeExprTerm1 | NodeExprTerm2;
// ([TYPE '='] INITLIST)
export interface NodeExprTerm1 extends NodesBase {
readonly nodeName: NodeName.ExprTerm
readonly exprTerm: 1
readonly type: NodeType | undefined,
readonly initList: NodeInitList
}
// ({EXPRPREOP} EXPRVALUE {EXPRPOSTOP})
export interface NodeExprTerm2 extends NodesBase {
readonly nodeName: NodeName.ExprTerm
readonly exprTerm: 2,
readonly preOps: TokenObject[],
readonly value: NodeExprValue,
readonly postOps: NodeExprPostOp[]
}
// EXPRVALUE ::= 'void' | CONSTRUCTCALL | FUNCCALL | VARACCESS | CAST | LITERAL | '(' ASSIGN ')' | LAMBDA
export type NodeExprValue =
NodeConstructCall
| NodeFuncCall
| NodeVarAccess
| NodeCast
| NodeLiteral
| NodeAssign
| NodeLambda;
// CONSTRUCTCALL ::= TYPE ARGLIST
export interface NodeConstructCall extends NodesBase {
readonly nodeName: NodeName.ConstructCall;
readonly type: NodeType;
readonly argList: NodeArgList;
}
// EXPRPREOP ::= '-' | '+' | '!' | '++' | '--' | '~' | '@'
// EXPRPOSTOP ::= ('.' (FUNCCALL | IDENTIFIER)) | ('[' [IDENTIFIER ':'] ASSIGN {',' [IDENTIFIER ':' ASSIGN} ']') | ARGLIST | '++' | '--'
export type NodeExprPostOp = NodeExprPostOp1 | NodeExprPostOp2 | NodeExprPostOp3 | NodeExprPostOp4;
// ('.' (FUNCCALL | IDENTIFIER))
export interface NodeExprPostOp1 extends NodesBase {
readonly nodeName: NodeName.ExprPostOp;
readonly postOp: 1;
readonly member: NodeFuncCall | TokenObject | undefined;
}
export function isMemberMethodInPostOp(member: NodeFuncCall | TokenObject | undefined): member is NodeFuncCall {
return member !== undefined && 'nodeName' in member;
}
// ('[' [IDENTIFIER ':'] ASSIGN {',' [IDENTIFIER ':' ASSIGN} ']')
export interface NodeExprPostOp2 extends NodesBase {
readonly nodeName: NodeName.ExprPostOp;
readonly postOp: 2;
readonly indexingList: ParsedPostIndexing[];
}
export interface ParsedPostIndexing {
readonly identifier: TokenObject | undefined,
readonly assign: NodeAssign
}
// ARGLIST
export interface NodeExprPostOp3 extends NodesBase {
readonly nodeName: NodeName.ExprPostOp;
readonly postOp: 3;
readonly args: NodeArgList;
}
// ++ | --
export interface NodeExprPostOp4 extends NodesBase {
readonly nodeName: NodeName.ExprPostOp;
readonly postOp: 4;
readonly operator: '++' | '--';
}
// CAST ::= 'cast' '<' TYPE '>' '(' ASSIGN ')'
export interface NodeCast extends NodesBase {
readonly nodeName: NodeName.Cast;
readonly type: NodeType;
readonly assign: NodeAssign;
}
// LAMBDA ::= 'function' '(' [[TYPE TYPEMOD] [IDENTIFIER] {',' [TYPE TYPEMOD] [IDENTIFIER]}] ')' STATBLOCK
export interface NodeLambda extends NodesBase {
readonly nodeName: NodeName.Lambda;
readonly paramList: ParsedLambdaParams[],
readonly statBlock: NodeStatBlock | undefined
}
export interface ParsedLambdaParams {
readonly type: NodeType | undefined,
readonly typeMod: TypeModifier | undefined,
readonly identifier: TokenObject | undefined
}
// LITERAL ::= NUMBER | STRING | BITS | 'true' | 'false' | 'null'
export interface NodeLiteral extends NodesBase {
readonly nodeName: NodeName.Literal;
readonly value: TokenObject;
}
// FUNCCALL ::= SCOPE IDENTIFIER ARGLIST
export interface NodeFuncCall extends NodesBase {
readonly nodeName: NodeName.FuncCall
readonly scope: NodeScope | undefined,
readonly identifier: TokenObject,
readonly argList: NodeArgList
}
// VARACCESS ::= SCOPE IDENTIFIER
export interface NodeVarAccess extends NodesBase {
readonly nodeName: NodeName.VarAccess;
readonly scope: NodeScope | undefined;
readonly identifier: TokenObject | undefined;
}
// ARGLIST ::= '(' [IDENTIFIER ':'] ASSIGN {',' [IDENTIFIER ':'] ASSIGN} ')'
export interface NodeArgList extends NodesBase {
readonly nodeName: NodeName.ArgList;
readonly argList: ParsedArgument[];
}
export interface ParsedArgument {
readonly identifier: TokenObject | undefined,
readonly assign: NodeAssign
}
// ASSIGN ::= CONDITION [ ASSIGNOP ASSIGN ]
export interface NodeAssign extends NodesBase {
readonly nodeName: NodeName.Assign;
readonly condition: NodeCondition;
readonly tail: ParsedAssignTail | undefined;
}
export interface ParsedAssignTail {
readonly operator: TokenObject,
readonly assign: NodeAssign
}
// CONDITION ::= EXPR ['?' ASSIGN ':' ASSIGN]
export interface NodeCondition extends NodesBase {
readonly nodeName: NodeName.Condition
readonly expr: NodeExpr,
readonly ternary: ParsedTernary | undefined
}
export interface ParsedTernary {
readonly trueAssign: NodeAssign,
readonly falseAssign: NodeAssign
}