nargo-types 0.0.1

Nargo common types and error handling
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
//! Intermediate Representation (IR) types for Nargo.
//!
//! This module provides the IR types used for parsing and compilation.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Source span for tracking locations in code.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Span {
    /// Start position.
    pub start: Position,
    /// End position.
    pub end: Position,
}

impl Span {
    /// Creates a new span.
    pub fn new(start: u32, end: u32) -> Self {
        Self {
            start: Position {
                offset: start,
                line: 1,
                column: 0,
            },
            end: Position {
                offset: end,
                line: 1,
                column: 0,
            },
        }
    }

    /// Creates an unknown span.
    pub fn unknown() -> Self {
        Self::default()
    }

    /// Checks if this span is unknown.
    pub fn is_unknown(&self) -> bool {
        self.start.offset == 0 && self.end.offset == 0
    }
}

/// Position in source code.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Position {
    /// Byte offset.
    pub offset: u32,
    /// Line number (1-based).
    pub line: u32,
    /// Column number (0-based).
    pub column: u32,
}

/// Trivia (comments and whitespace).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct Trivia {
    /// Leading comments.
    pub leading: Vec<String>,
    /// Trailing comments.
    pub trailing: Vec<String>,
}

/// Comment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Comment {
    /// Comment content.
    pub content: String,
    /// Whether this is a block comment.
    pub is_block: bool,
}

/// IR Module representing a parsed single-file component.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct IRModule {
    /// URI of the source file.
    pub uri: String,
    /// Name of the module.
    pub name: String,
    /// Template section.
    pub template: Option<TemplateIR>,
    /// Script section.
    pub script: Option<JsProgram>,
    /// Style sections.
    pub styles: Vec<StyleIR>,
    /// Custom blocks.
    pub custom_blocks: Vec<CustomBlock>,
}

/// Template IR for the template section.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TemplateIR {
    /// Template nodes.
    pub nodes: Vec<TemplateNodeIR>,
    /// Span of the template.
    pub span: Span,
}

/// Template node IR.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TemplateNodeIR {
    /// Element node.
    Element(ElementIR),
    /// Text node.
    Text(String, Span),
    /// Interpolation node.
    Interpolation(ExpressionIR),
    /// Comment node.
    Comment(String, Span),
}

/// Element IR.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ElementIR {
    /// Tag name.
    pub tag: String,
    /// Attributes.
    pub attributes: Vec<AttributeIR>,
    /// Children.
    pub children: Vec<TemplateNodeIR>,
    /// Span.
    pub span: Span,
    /// Trivia.
    pub trivia: Trivia,
}

/// Attribute IR.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AttributeIR {
    /// Attribute name.
    pub name: String,
    /// Attribute value.
    pub value: Option<String>,
    /// Parsed value AST.
    pub value_ast: Option<JsExpr>,
    /// Whether this is a directive.
    pub is_directive: bool,
    /// Whether this is dynamic.
    pub is_dynamic: bool,
    /// Span.
    pub span: Span,
}

/// Expression IR for interpolations.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExpressionIR {
    /// Raw code.
    pub code: String,
    /// Span.
    pub span: Span,
    /// Parsed AST.
    pub ast: Option<JsExpr>,
}

/// Style IR.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StyleIR {
    /// Style content.
    pub content: String,
    /// Language (css, scss, etc.).
    pub lang: String,
    /// Span.
    pub span: Span,
    /// Whether scoped.
    pub scoped: bool,
    /// Whether module.
    pub module: bool,
}

/// Custom block IR.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CustomBlock {
    /// Block name.
    pub name: String,
    /// Block content.
    pub content: String,
    /// Block attributes.
    pub attributes: HashMap<String, String>,
}

/// JavaScript program IR.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct JsProgram {
    /// Statement body.
    pub body: Vec<JsStmt>,
    /// Span.
    pub span: Span,
}

/// JavaScript statement IR.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JsStmt {
    /// Expression statement.
    Expr(JsExpr, Span, Trivia),
    /// Variable declaration.
    VariableDecl {
        /// Declaration kind (let, const, var).
        kind: String,
        /// Variable name.
        id: String,
        /// Initializer.
        init: Option<JsExpr>,
        /// Span.
        span: Span,
        /// Type annotation.
        type_ann: Option<String>,
    },
    /// Function declaration.
    FunctionDecl {
        /// Function name.
        id: String,
        /// Parameters.
        params: Vec<String>,
        /// Function body.
        body: Vec<JsStmt>,
        /// Span.
        span: Span,
        /// Return type.
        return_type: Option<String>,
        /// Whether async.
        is_async: bool,
        /// Whether generator.
        is_generator: bool,
    },
    /// Import statement.
    Import {
        /// Import specifiers.
        specifiers: Vec<ImportSpecifier>,
        /// Source module.
        source: String,
        /// Span.
        span: Span,
    },
    /// Export statement.
    Export {
        /// Exported declaration.
        decl: Box<JsStmt>,
        /// Span.
        span: Span,
        /// Whether type export.
        is_type: bool,
    },
    /// Export default statement.
    ExportDefault {
        /// Exported declaration.
        decl: Box<JsStmt>,
        /// Span.
        span: Span,
    },
    /// Export all statement.
    ExportAll {
        /// Source module.
        source: String,
        /// Span.
        span: Span,
    },
    /// Export named statement.
    ExportNamed {
        /// Exported names.
        specifiers: Vec<String>,
        /// Source module.
        source: Option<String>,
        /// Span.
        span: Span,
    },
    /// Return statement.
    Return(JsExpr, Span, Trivia),
    /// If statement.
    If {
        /// Condition.
        test: JsExpr,
        /// Consequent branch.
        consequent: Vec<JsStmt>,
        /// Alternate branch.
        alternate: Option<Vec<JsStmt>>,
        /// Span.
        span: Span,
    },
    /// While statement.
    While {
        /// Condition.
        test: JsExpr,
        /// Body.
        body: Vec<JsStmt>,
        /// Span.
        span: Span,
    },
    /// For statement.
    For {
        /// Initializer.
        init: Box<JsStmt>,
        /// Condition.
        test: Option<JsExpr>,
        /// Update.
        update: Option<JsExpr>,
        /// Body.
        body: Vec<JsStmt>,
        /// Span.
        span: Span,
    },
    /// Block statement.
    Block(Vec<JsStmt>, Span, Trivia),
    /// Break statement.
    Break(Span, Trivia),
    /// Continue statement.
    Continue(Span, Trivia),
    /// Other statement.
    Other(String, Span, Trivia),
}

impl JsStmt {
    /// Returns the span of the statement.
    pub fn span(&self) -> Span {
        match self {
            JsStmt::Expr(_, span, _) => *span,
            JsStmt::VariableDecl { span, .. } => *span,
            JsStmt::FunctionDecl { span, .. } => *span,
            JsStmt::Import { span, .. } => *span,
            JsStmt::Export { span, .. } => *span,
            JsStmt::ExportDefault { span, .. } => *span,
            JsStmt::ExportAll { span, .. } => *span,
            JsStmt::ExportNamed { span, .. } => *span,
            JsStmt::Return(_, span, _) => *span,
            JsStmt::If { span, .. } => *span,
            JsStmt::While { span, .. } => *span,
            JsStmt::For { span, .. } => *span,
            JsStmt::Block(_, span, _) => *span,
            JsStmt::Break(span, _) => *span,
            JsStmt::Continue(span, _) => *span,
            JsStmt::Other(_, span, _) => *span,
        }
    }

    /// Returns a clone of the trivia of the statement.
    pub fn trivia(&self) -> Trivia {
        match self {
            JsStmt::Expr(_, _, trivia) => trivia.clone(),
            JsStmt::Return(_, _, trivia) => trivia.clone(),
            JsStmt::Block(_, _, trivia) => trivia.clone(),
            JsStmt::Break(_, trivia) => trivia.clone(),
            JsStmt::Continue(_, trivia) => trivia.clone(),
            JsStmt::Other(_, _, trivia) => trivia.clone(),
            _ => Trivia::default(),
        }
    }
}

/// Import specifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ImportSpecifier {
    /// Default import.
    Default {
        /// Name.
        name: String,
    },
    /// Named import.
    Named {
        /// Import name.
        name: String,
        /// Local alias.
        alias: Option<String>,
    },
    /// Namespace import.
    Namespace {
        /// Local alias.
        alias: Option<String>,
    },
}

/// JavaScript expression IR.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JsExpr {
    /// Literal value.
    Literal(NargoValue, Span, Trivia),
    /// Identifier.
    Identifier(String, Span, Trivia),
    /// Unary expression.
    Unary {
        /// Operator.
        op: String,
        /// Argument.
        argument: Box<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Binary expression.
    Binary {
        /// Left operand.
        left: Box<JsExpr>,
        /// Operator.
        op: String,
        /// Right operand.
        right: Box<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Call expression.
    Call {
        /// Callee.
        callee: Box<JsExpr>,
        /// Arguments.
        args: Vec<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Member expression.
    Member {
        /// Object.
        object: Box<JsExpr>,
        /// Property name.
        property: String,
        /// Whether computed.
        computed: bool,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Object expression.
    Object(HashMap<String, JsExpr>, Span, Trivia),
    /// Array expression.
    Array(Vec<JsExpr>, Span, Trivia),
    /// Arrow function.
    ArrowFunction {
        /// Parameters.
        params: Vec<String>,
        /// Body.
        body: Box<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Conditional expression.
    Conditional {
        /// Condition.
        test: Box<JsExpr>,
        /// Consequent.
        consequent: Box<JsExpr>,
        /// Alternate.
        alternate: Box<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Template literal.
    TemplateLiteral {
        /// Quasi strings.
        quasis: Vec<String>,
        /// Expressions.
        expressions: Vec<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// TSE element.
    TseElement {
        /// Tag name.
        tag: String,
        /// Attributes.
        attributes: Vec<AttributeIR>,
        /// Children.
        children: Vec<JsExpr>,
        /// Span.
        span: Span,
        /// Trivia.
        trivia: Trivia,
    },
    /// Other expression.
    Other(String, Span, Trivia),
}

impl JsExpr {
    /// Returns the span of the expression.
    pub fn span(&self) -> Span {
        match self {
            JsExpr::Literal(_, span, _) => *span,
            JsExpr::Identifier(_, span, _) => *span,
            JsExpr::Unary { span, .. } => *span,
            JsExpr::Binary { span, .. } => *span,
            JsExpr::Call { span, .. } => *span,
            JsExpr::Member { span, .. } => *span,
            JsExpr::Object(_, span, _) => *span,
            JsExpr::Array(_, span, _) => *span,
            JsExpr::ArrowFunction { span, .. } => *span,
            JsExpr::Conditional { span, .. } => *span,
            JsExpr::TemplateLiteral { span, .. } => *span,
            JsExpr::TseElement { span, .. } => *span,
            JsExpr::Other(_, span, _) => *span,
        }
    }

    /// Returns a clone of the trivia of the expression.
    pub fn trivia(&self) -> Trivia {
        match self {
            JsExpr::Literal(_, _, trivia) => trivia.clone(),
            JsExpr::Identifier(_, _, trivia) => trivia.clone(),
            JsExpr::Unary { trivia, .. } => trivia.clone(),
            JsExpr::Binary { trivia, .. } => trivia.clone(),
            JsExpr::Call { trivia, .. } => trivia.clone(),
            JsExpr::Member { trivia, .. } => trivia.clone(),
            JsExpr::Object(_, _, trivia) => trivia.clone(),
            JsExpr::Array(_, _, trivia) => trivia.clone(),
            JsExpr::ArrowFunction { trivia, .. } => trivia.clone(),
            JsExpr::Conditional { trivia, .. } => trivia.clone(),
            JsExpr::TemplateLiteral { trivia, .. } => trivia.clone(),
            JsExpr::TseElement { trivia, .. } => trivia.clone(),
            JsExpr::Other(_, _, trivia) => trivia.clone(),
        }
    }
}

/// Nargo value for literals.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NargoValue {
    /// Null value.
    Null,
    /// Boolean value.
    Bool(bool),
    /// Number value.
    Number(f64),
    /// String value.
    String(String),
    /// Array value.
    Array(Vec<NargoValue>),
    /// Object value.
    Object(HashMap<String, NargoValue>),
    /// Signal reference.
    Signal(String),
    /// Raw code.
    Raw(String),
    /// Other value.
    Other(String),
}

impl Default for NargoValue {
    fn default() -> Self {
        NargoValue::Null
    }
}