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
|
const std = @import("std");
const logger = @import("logger.zig");
pub const NodeIndex = u32;
pub const NodeIndexNone = 4294967293;
pub const DataIndex = u16;
pub const DataIndexNone = 65533;
pub const BindingNodeIndex = NodeIndex;
pub const StmtNodeIndex = NodeIndex;
pub const ExprNodeIndex = NodeIndex;
pub const Comment = struct { text: []u8 };
pub const FnBody = struct {
loc: logger.Loc,
stmts: []StmtNodeIndex,
};
pub const Fn = struct {
name: NodeIndex = NodeIndexNone,
open_parens_loc: logger.Loc,
args: []Arg,
body: FnBody,
is_async: bool,
is_generator: bool,
has_rest_arg: bool,
has_if_scope: bool,
// This is true if the function is a method
is_unique_formal_parameters: bool,
};
pub const BindingType = enum {
b_missing,
b_identifier,
b_array,
b_object,
};
pub const Property = struct {
pub const Kind = enum {
normal,
get,
set,
spread,
};
key: NodeIndex,
value: NodeIndex = NodeIndexNone,
initializer: Kind = Kind.normal,
is_computed: bool,
is_method: bool,
is_static: bool,
was_shorthand: bool,
};
pub const Arg = struct {
ts_decorators: []NodeIndex,
binding: Binding,
default: NodeIndex = NodeIndexNone,
// "constructor(public x: boolean) {}"
is_typescript_ctor_field: bool,
};
pub const Try = struct {};
pub const Binding = struct {};
pub const Class = struct {
class_keyword: logger.Range,
ts_decorators: []NodeIndex,
name: logger.Loc,
extends: NodeIndex = NodeIndexNone,
body_loc: logger.Loc,
properties: []Property,
};
pub const Expr = struct {
pub const Array = struct {
items: []ExprNodeIndex,
comma_after_spread: logger.Loc,
is_parenthesized: bool,
};
pub const Unary = struct {
op: Op.Code,
};
// TODO: THIS IS WHERE YOU LEFT OFF!
// pub const Binary = {}
};
pub const Op = struct {
// If you add a new token, remember to add it to "OpTable" too
const Code = enum {
// Prefix
un_pos,
un_neg,
un_cpl,
un_not,
un_void,
un_typeof,
un_delete,
// Prefix update
un_pre_dec,
un_pre_inc,
// Postfix update
un_post_dec,
un_post_inc,
// Left-associative
bin_add,
bin_sub,
bin_mul,
bin_div,
bin_rem,
bin_pow,
bin_lt,
bin_le,
bin_gt,
bin_ge,
bin_in,
bin_instanceof,
bin_shl,
bin_shr,
bin_u_shr,
bin_loose_eq,
bin_loose_ne,
bin_strict_eq,
bin_strict_ne,
bin_nullish_coalescing,
bin_logical_or,
bin_logical_and,
bin_bitwise_or,
bin_bitwise_and,
bin_bitwise_xor,
// Non-associative
bin_comma,
// Right-associative
bin_assign,
bin_add_assign,
bin_sub_assign,
bin_mul_assign,
bin_div_assign,
bin_rem_assign,
bin_pow_assign,
bin_shl_assign,
bin_shr_assign,
bin_u_shr_assign,
bin_bitwise_or_assign,
bin_bitwise_and_assign,
bin_bitwise_xor_assign,
bin_nullish_coalescing_assign,
bin_logical_or_assign,
bin_logical_and_assign,
};
const Level = enum {
lowest,
comma,
spread,
yield,
assign,
conditional,
nullish_coalescing,
logical_or,
logical_and,
bitwise_or,
bitwise_xor,
bitwise_and,
equals,
compare,
shift,
add,
multiply,
exponentiation,
prefix,
postfix,
new,
call,
member,
};
text: string,
level: Level,
is_keyword: bool,
const Table = []Op{
// Prefix
.{ "+", Level.prefix, false },
.{ "-", Level.prefix, false },
.{ "~", Level.prefix, false },
.{ "!", Level.prefix, false },
.{ "void", Level.prefix, true },
.{ "typeof", Level.prefix, true },
.{ "delete", Level.prefix, true },
// Prefix update
.{ "--", Level.prefix, false },
.{ "++", Level.prefix, false },
// Postfix update
.{ "--", Level.postfix, false },
.{ "++", Level.postfix, false },
// Left-associative
.{ "+", Level.add, false },
.{ "-", Level.add, false },
.{ "*", Level.multiply, false },
.{ "/", Level.multiply, false },
.{ "%", Level.multiply, false },
.{ "**", Level.exponentiation, false }, // Right-associative
.{ "<", Level.compare, false },
.{ "<=", Level.compare, false },
.{ ">", Level.compare, false },
.{ ">=", Level.compare, false },
.{ "in", Level.compare, true },
.{ "instanceof", Level.compare, true },
.{ "<<", Level.shift, false },
.{ ">>", Level.shift, false },
.{ ">>>", Level.shift, false },
.{ "==", Level.equals, false },
.{ "!=", Level.equals, false },
.{ "===", Level.equals, false },
.{ "!==", Level.equals, false },
.{ "??", Level.nullish_coalescing, false },
.{ "||", Level.logical_or, false },
.{ "&&", Level.logical_and, false },
.{ "|", Level.bitwise_or, false },
.{ "&", Level.bitwise_and, false },
.{ "^", Level.bitwise_xor, false },
// Non-associative
.{ ",", LComma, false },
// Right-associative
.{ "=", Level.assign, false },
.{ "+=", Level.assign, false },
.{ "-=", Level.assign, false },
.{ "*=", Level.assign, false },
.{ "/=", Level.assign, false },
.{ "%=", Level.assign, false },
.{ "**=", Level.assign, false },
.{ "<<=", Level.assign, false },
.{ ">>=", Level.assign, false },
.{ ">>>=", Level.assign, false },
.{ "|=", Level.assign, false },
.{ "&=", Level.assign, false },
.{ "^=", Level.assign, false },
.{ "??=", Level.assign, false },
.{ "||=", Level.assign, false },
.{ "&&=", Level.assign, false },
};
};
pub const ArrayBinding = struct {
binding: BindingNodeIndex,
default_value: ExprNodeIndex = NodeIndexNone,
};
pub const Node = struct {
pub const Tag = enum {
s_block,
s_comment,
s_debugger,
s_directive,
s_empty,
s_type_script,
s_export_clause,
s_export_from,
s_export_default,
s_export_star,
s_export_equals,
s_lazy_export,
s_expr,
s_enum,
s_namespace,
s_function,
s_class,
s_label,
s_if,
s_for,
s_for_in,
s_for_of,
s_do_while,
s_while,
s_with,
s_try,
s_switch,
s_import,
s_return,
s_throw,
s_local,
s_break,
s_continue,
e_array,
e_unary,
e_binary,
e_boolean,
e_super,
e_null,
e_undefined,
e_this,
e_new,
e_new_target,
e_import_meta,
e_call,
e_dot,
e_index,
e_arrow,
e_function,
e_class,
e_identifier,
e_import_identifier,
e_private_identifier,
ejsx_element,
e_missing,
e_number,
e_big_int,
e_object,
e_spread,
e_string,
e_template,
e_reg_exp,
e_await,
e_yield,
e_if,
e_require,
e_require_resolve,
e_import,
};
// Source code location of the AST node.
loc: logger.Loc,
// this is relatively common.
is_single_line: bool,
//
child: NodeIndex = NodeIndexNone,
extra_data: ?[]NodeIndex,
data_index: u16,
};
pub const AST = struct {
node_tags: std.ArrayList(Node.Tag),
};
pub const Span = struct {
text: []u8,
range: logger.Range,
};
pub const ExportsKind = enum {
// This file doesn't have any kind of export, so it's impossible to say what
// kind of file this is. An empty file is in this category, for example.
none,
// The exports are stored on "module" and/or "exports". Calling "require()"
// on this module returns "module.exports". All imports to this module are
// allowed but may return undefined.
cjs,
// All export names are known explicitly. Calling "require()" on this module
// generates an exports object (stored in "exports") with getters for the
// export names. Named imports to this module are only allowed if they are
// in the set of export names.
esm,
// Some export names are known explicitly, but others fall back to a dynamic
// run-time object. This is necessary when using the "export * from" syntax
// with either a CommonJS module or an external module (i.e. a module whose
// export names are not known at compile-time).
//
// Calling "require()" on this module generates an exports object (stored in
// "exports") with getters for the export names. All named imports to this
// module are allowed. Direct named imports reference the corresponding export
// directly. Other imports go through property accesses on "exports".
esm_with_dyn };
pub fn isDynamicExport(exp: ExportsKind) bool {
return kind == .cjs || kind == .esm_with_dyn;
}
|