presolve-parser 0.1.0-alpha.8

Parser implementation used by the Presolve compiler.
Documentation
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
use std::collections::BTreeMap;

use std::path::PathBuf;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedFile {
    pub path: PathBuf,
    pub classes: Vec<ParsedClass>,
    pub type_aliases: Vec<ParsedTypeAlias>,
    /// Module-local declarations that bind a name in TypeScript's type
    /// namespace. The compiler uses this normalized fact to distinguish its
    /// built-in marker types from authored lookalikes.
    pub local_type_bindings: Vec<String>,
    /// Module-local declarations in the value namespace. I6 uses this
    /// normalized fact to reject authored functions that shadow compiler-owned
    /// validation intrinsics.
    pub local_value_bindings: Vec<String>,
    pub imports: Vec<ParsedImport>,
    pub exports: Vec<ParsedExport>,
    pub diagnostics: Vec<ParseDiagnostic>,
}

/// Authored type alias retained for canonical semantic type lowering.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedTypeAlias {
    pub name: String,
    pub type_text: String,
    pub span: SourceSpan,
    pub type_span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedImport {
    pub source: String,
    pub specifiers: Vec<ParsedImportSpecifier>,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedImportSpecifier {
    pub imported: String,
    pub local: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedExport {
    pub kind: ParsedExportKind,
    pub source: Option<String>,
    pub specifiers: Vec<ParsedExportSpecifier>,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedExportKind {
    Named,
    Default,
    All,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedExportSpecifier {
    pub local: Option<String>,
    pub exported: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedClass {
    pub name: String,
    pub span: SourceSpan,
    pub heritage: Option<ParsedClassHeritage>,
    pub decorators: Vec<ParsedDecorator>,
    pub properties: Vec<ParsedProperty>,
    pub methods: Vec<ParsedMethod>,
}

/// Source-faithful class heritage retained for component-inheritance diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedClassHeritage {
    pub base: String,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedDecorator {
    pub name: String,
    pub is_invoked: bool,
    /// Source-normalized static-string values for every decorator argument.
    /// Existing compiler consumers continue to use `argument` for their
    /// one-argument contracts; multi-argument semantics must opt in explicitly.
    pub arguments: Vec<Option<String>>,
    pub argument: Option<String>,
    pub argument_count: usize,
    pub argument_spans: Vec<SourceSpan>,
    pub static_member_argument: Option<ParsedStaticMemberDesignator>,
    pub this_member_argument: Option<ParsedThisMemberDesignator>,
    /// Normalized syntax for the sole outer argument of `@validate(...)`.
    /// Semantic rule classification remains a core lowering responsibility.
    pub validation_rule_expression: Option<ParsedValidationRuleExpression>,
    pub span: SourceSpan,
}

/// Parser-owned syntax facts for one authored validation rule expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedValidationRuleExpression {
    pub kind: ParsedValidationRuleExpressionKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedValidationRuleExpressionKind {
    Call {
        callee: Option<String>,
        arguments: Vec<ParsedValidationRuleArgument>,
    },
    Identifier(String),
    Unsupported,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedValidationRuleArgument {
    pub kind: ParsedValidationRuleArgumentKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedValidationRuleArgumentKind {
    StringLiteral(String),
    Constant(ParsedConstantExpression),
    ThisMember(ParsedThisMemberDesignator),
    Unsupported,
}

/// An exact direct `this.<identifier>` decorator argument.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedThisMemberDesignator {
    pub member: String,
    pub span: SourceSpan,
    pub this_span: SourceSpan,
    pub member_span: SourceSpan,
}

/// A source-faithful `ComponentSymbol.contextField` decorator argument.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedStaticMemberDesignator {
    pub object: String,
    pub member: String,
    pub span: SourceSpan,
    pub object_span: SourceSpan,
    pub member_span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedProperty {
    pub name: String,
    pub is_identifier_name: bool,
    pub decorators: Vec<ParsedDecorator>,
    pub initializer: Option<String>,
    pub initializer_literal: Option<ParsedSerializableValue>,
    pub initializer_expression: Option<ParsedComputedExpression>,
    pub initializer_constant_expression: Option<ParsedConstantExpression>,
    pub initializer_span: Option<SourceSpan>,
    pub state_initial_value: Option<ParsedSerializableValue>,
    pub state_initial_expression: Option<ParsedConstantExpression>,
    pub state_type_annotation: Option<ParsedTypeAnnotation>,
    pub type_annotation: Option<ParsedTypeAnnotation>,
    pub name_span: SourceSpan,
    pub is_static: bool,
    pub is_definite_assignment: bool,
    pub is_declare: bool,
    pub span: SourceSpan,
}

/// Authored TypeScript annotation retained for a state field without type checking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedTypeAnnotation {
    pub text: String,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedSerializableValue {
    Null,
    Number(String),
    String(String),
    Boolean(bool),
    Array(Vec<ParsedSerializableValue>),
    Object(BTreeMap<String, ParsedSerializableValue>),
}

/// A compiler-owned numeric arithmetic expression accepted in `state(...)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedArithmeticExpression {
    pub kind: ParsedArithmeticExpressionKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedArithmeticExpressionKind {
    Number(String),
    Binary {
        operator: ParsedArithmeticOperator,
        left: Box<ParsedArithmeticExpression>,
        right: Box<ParsedArithmeticExpression>,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedArithmeticOperator {
    Add,
    Subtract,
    Multiply,
    Divide,
    Remainder,
}

/// A compiler-owned constant expression accepted in `state(...)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedConstantExpression {
    pub kind: ParsedConstantExpressionKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedConstantExpressionKind {
    Primitive(ParsedSerializableValue),
    Boolean(bool),
    Arithmetic(ParsedArithmeticExpression),
    Comparison {
        operator: ParsedComparisonOperator,
        left: ParsedArithmeticExpression,
        right: ParsedArithmeticExpression,
    },
    Logical {
        operator: ParsedLogicalOperator,
        left: Box<ParsedConstantExpression>,
        right: Box<ParsedConstantExpression>,
    },
    NullishCoalescing {
        left: Box<ParsedConstantExpression>,
        right: Box<ParsedConstantExpression>,
    },
    Unary {
        operator: ParsedUnaryOperator,
        operand: Box<ParsedConstantExpression>,
    },
}

/// A supported expression retained from one `@computed()` getter body.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedComputedExpression {
    pub kind: ParsedComputedExpressionKind,
    pub span: SourceSpan,
}

/// Parsed computed getter expression forms accepted by the E2 lowering slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedComputedExpressionKind {
    Literal(ParsedSerializableValue),
    ThisMember(String),
    MemberAccess {
        object: Box<ParsedComputedExpression>,
        property: String,
        optional: bool,
    },
    /// A statically bounded property or array-element read.
    ///
    /// The parser admits only string and non-negative integer literal indices;
    /// dynamic JavaScript property lookup remains outside the semantic subset.
    IndexAccess {
        object: Box<ParsedComputedExpression>,
        index: Box<ParsedComputedExpression>,
    },
    Conditional {
        condition: Box<ParsedComputedExpression>,
        when_true: Box<ParsedComputedExpression>,
        when_false: Box<ParsedComputedExpression>,
    },
    Template {
        quasis: Vec<String>,
        expressions: Vec<ParsedComputedExpression>,
    },
    Call {
        callee: String,
        arguments: Vec<ParsedComputedExpression>,
    },
    Arithmetic {
        left: Box<ParsedComputedExpression>,
        right: Box<ParsedComputedExpression>,
        operator: ParsedArithmeticOperator,
    },
    Comparison {
        left: Box<ParsedComputedExpression>,
        right: Box<ParsedComputedExpression>,
        operator: ParsedComparisonOperator,
    },
    Logical {
        left: Box<ParsedComputedExpression>,
        right: Box<ParsedComputedExpression>,
        operator: ParsedLogicalOperator,
    },
    NullishCoalescing {
        left: Box<ParsedComputedExpression>,
        right: Box<ParsedComputedExpression>,
    },
    Unary {
        operand: Box<ParsedComputedExpression>,
        operator: ParsedUnaryOperator,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedComparisonOperator {
    Equal,
    NotEqual,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedLogicalOperator {
    And,
    Or,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedUnaryOperator {
    Not,
    Plus,
    Minus,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedMethod {
    pub name: String,
    pub span: SourceSpan,
    pub decorators: Vec<ParsedDecorator>,
    pub is_getter: bool,
    pub is_setter: bool,
    pub is_async: bool,
    pub is_static: bool,
    pub jsx_roots: Vec<ParsedJsxNode>,
    pub bindings: Vec<String>,
    pub state_updates: Vec<ParsedStateUpdate>,
    pub local_variables: Vec<ParsedLocalVariable>,
    pub parameters: Vec<ParsedMethodParameter>,
    pub return_type_annotation: Option<ParsedTypeAnnotation>,
    pub return_values: Vec<ParsedSerializableValue>,
    pub computed_expression: Option<ParsedComputedExpression>,
    pub effect_body: Option<ParsedEffectBody>,
    pub calls: Vec<ParsedMethodCall>,
}

/// Ordered syntax retained from one `@effect()` method body.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedEffectBody {
    pub statements: Vec<ParsedEffectStatement>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedEffectStatement {
    pub kind: ParsedEffectStatementKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedEffectStatementKind {
    StaticMemberAssignment {
        target: ParsedEffectExpression,
        value: ParsedEffectExpression,
    },
    CapabilityCall {
        callee: ParsedEffectExpression,
        arguments: Vec<ParsedEffectExpression>,
    },
    EffectReturn {
        value: Option<ParsedEffectExpression>,
    },
    Empty,
    Unsupported(ParsedUnsupportedEffectStatementKind),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedUnsupportedEffectStatementKind {
    LocalDeclaration,
    Branch,
    Loop,
    NestedBlock,
    ExceptionHandling,
    AsyncOperation,
    CompoundAssignment,
    CleanupReturnCandidate,
    UnsupportedExpression,
}

/// Expression syntax accepted as an operand of a lowered effect statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedEffectExpression {
    pub kind: ParsedEffectExpressionKind,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedEffectExpressionKind {
    Literal(ParsedSerializableValue),
    Identifier(String),
    ThisMember(String),
    MemberAccess {
        object: Box<ParsedEffectExpression>,
        property: String,
    },
    Arithmetic {
        left: Box<ParsedEffectExpression>,
        right: Box<ParsedEffectExpression>,
        operator: ParsedArithmeticOperator,
    },
    Comparison {
        left: Box<ParsedEffectExpression>,
        right: Box<ParsedEffectExpression>,
        operator: ParsedComparisonOperator,
    },
    Logical {
        left: Box<ParsedEffectExpression>,
        right: Box<ParsedEffectExpression>,
        operator: ParsedLogicalOperator,
    },
    NullishCoalescing {
        left: Box<ParsedEffectExpression>,
        right: Box<ParsedEffectExpression>,
    },
    Unary {
        operand: Box<ParsedEffectExpression>,
        operator: ParsedUnaryOperator,
    },
}

/// One directly authored method call retained for computed-purity analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedMethodCall {
    pub callee: String,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedMethodParameter {
    pub name: String,
    pub decorators: Vec<ParsedDecorator>,
    pub span: SourceSpan,
    pub type_annotation: Option<ParsedTypeAnnotation>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedLocalVariable {
    pub name: String,
    pub value: ParsedSerializableValue,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedStateUpdate {
    pub field: String,
    pub operation: ParsedStateOperation,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedStateOperation {
    Increment,
    Decrement,
    AddAssign(ParsedSerializableValue),
    SubtractAssign(ParsedSerializableValue),
    Assign(ParsedSerializableValue),
    AssignParameter(String),
    Toggle,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedJsxChild {
    Text {
        value: String,
        span: SourceSpan,
    },
    Binding {
        expression: String,
        span: SourceSpan,
    },
    Element(ParsedJsxElement),
    Fragment(ParsedJsxFragment),
    Conditional(ParsedJsxConditional),
    List(ParsedJsxList),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedJsxNode {
    Element(ParsedJsxElement),
    Fragment(ParsedJsxFragment),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedJsxElement {
    pub name: String,
    pub name_span: SourceSpan,
    pub span: SourceSpan,
    pub attributes: Vec<ParsedJsxAttribute>,
    pub event_handlers: Vec<ParsedEventHandler>,
    pub children: Vec<ParsedJsxChild>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedJsxFragment {
    pub span: SourceSpan,
    pub children: Vec<ParsedJsxChild>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedJsxConditional {
    pub condition: String,
    pub span: SourceSpan,
    pub when_true: ParsedJsxNode,
    pub when_false: Option<ParsedJsxNode>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedJsxList {
    pub iterable: String,
    pub item_variable: String,
    pub index_variable: Option<String>,
    pub key_expression: String,
    pub span: SourceSpan,
    pub item_template: ParsedJsxNode,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedJsxAttribute {
    pub name: String,
    pub value: ParsedJsxAttributeValue,
    pub name_span: SourceSpan,
    pub value_span: Option<SourceSpan>,
    pub expression_span: Option<SourceSpan>,
    pub this_member: Option<ParsedThisMemberDesignator>,
    pub constant_value: Option<ParsedSerializableValue>,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedJsxAttributeValue {
    Boolean,
    Static(String),
    Expression(Option<String>),
    Spread(Option<String>),
    Unsupported,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedEventHandler {
    pub event: String,
    pub handler: String,
    pub arguments: Vec<ParsedSerializableValue>,
    pub span: SourceSpan,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseDiagnostic {
    pub message: String,
    pub severity: ParseSeverity,
    pub labels: Vec<ParseLabel>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseSeverity {
    Info,
    Warning,
    Error,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseLabel {
    pub span: SourceSpan,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceSpan {
    pub start: usize,
    pub end: usize,
    pub line: usize,
    pub column: usize,
}