atoxide-parser 0.1.3

Parser for the Ato hardware description language
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
//! Abstract Syntax Tree types for the Ato language.

use atoxide_lexer::Span;
use serde::{Deserialize, Serialize};

/// A complete Ato source file.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct File {
    pub statements: Vec<Statement>,
    pub span: Span,
}

/// A statement in the Ato language.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
    /// `#pragma experiment("NAME")`
    Pragma(PragmaStmt),
    /// `import X` or `from "path" import X`
    Import(ImportStmt),
    /// `import X from "path"` (deprecated form)
    DepImport(DepImportStmt),
    /// `module M:`, `component C:`, `interface I:`
    BlockDef(BlockDef),
    /// `x = value`
    Assignment(Assignment),
    /// `x += value` or `x -= value`
    CumAssignment(CumAssignment),
    /// `x |= value` or `x &= value`
    SetAssignment(SetAssignment),
    /// `a ~ b`
    Connection(Connection),
    /// `a ~> b ~> c` or `a <~ b <~ c`
    DirectedConnection(DirectedConnection),
    /// `x -> Type`
    Retype(Retype),
    /// `pin name`
    PinDeclaration(PinDeclaration),
    /// `signal name`
    SignalDef(SignalDef),
    /// `assert x within 1V to 2V`
    Assert(AssertStmt),
    /// `field: type`
    Declaration(Declaration),
    /// String literal as statement (docstring)
    StringStmt(StringStmt),
    /// `pass`
    Pass(PassStmt),
    /// `trait name<arg=value>`
    Trait(TraitStmt),
    /// `for item in container: body`
    For(ForStmt),
}

impl Statement {
    pub fn span(&self) -> Span {
        match self {
            Statement::Pragma(s) => s.span,
            Statement::Import(s) => s.span,
            Statement::DepImport(s) => s.span,
            Statement::BlockDef(s) => s.span,
            Statement::Assignment(s) => s.span,
            Statement::CumAssignment(s) => s.span,
            Statement::SetAssignment(s) => s.span,
            Statement::Connection(s) => s.span,
            Statement::DirectedConnection(s) => s.span,
            Statement::Retype(s) => s.span,
            Statement::PinDeclaration(s) => s.span,
            Statement::SignalDef(s) => s.span,
            Statement::Assert(s) => s.span,
            Statement::Declaration(s) => s.span,
            Statement::StringStmt(s) => s.span,
            Statement::Pass(s) => s.span,
            Statement::Trait(s) => s.span,
            Statement::For(s) => s.span,
        }
    }
}

// === Pragma ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PragmaStmt {
    pub content: String,
    pub span: Span,
}

// === Imports ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportStmt {
    /// Optional `from "path"` source
    pub from_path: Option<StringLiteral>,
    /// List of imported type references
    pub imports: Vec<TypeRef>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DepImportStmt {
    /// The imported type
    pub type_ref: TypeRef,
    /// The source path
    pub from_path: StringLiteral,
    pub span: Span,
}

// === Block Definitions ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockDef {
    pub kind: BlockKind,
    pub name: Identifier,
    /// Optional `from Base` super type
    pub super_type: Option<TypeRef>,
    pub body: Vec<Statement>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlockKind {
    Module,
    Component,
    Interface,
}

// === Assignments ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Assignment {
    pub target: AssignTarget,
    pub value: Assignable,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AssignTarget {
    FieldRef(FieldRef),
    Declaration(Declaration),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CumAssignment {
    pub target: AssignTarget,
    pub operator: CumOperator,
    pub value: Expression,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CumOperator {
    Add, // +=
    Sub, // -=
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SetAssignment {
    pub target: AssignTarget,
    pub operator: SetOperator,
    pub value: Expression,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SetOperator {
    Or,  // |=
    And, // &=
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Assignable {
    String(StringLiteral),
    New(NewExpr),
    Physical(PhysicalLiteral),
    Arithmetic(Expression),
    Boolean(BoolLiteral),
}

// === Connections ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Connection {
    pub left: Connectable,
    pub right: Connectable,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DirectedConnection {
    pub direction: ConnectionDirection,
    pub elements: Vec<Connectable>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionDirection {
    Forward,  // ~>
    Backward, // <~
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Connectable {
    FieldRef(FieldRef),
    SignalDef(SignalDef),
    PinDef(PinDeclaration),
}

// === Retype ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Retype {
    pub field: FieldRef,
    pub new_type: TypeRef,
    pub span: Span,
}

// === Declarations ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Declaration {
    pub field: FieldRef,
    pub type_info: Identifier,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PinDeclaration {
    pub name: PinName,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PinName {
    Identifier(Identifier),
    Number(NumberLiteral),
    String(StringLiteral),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignalDef {
    pub name: Identifier,
    pub span: Span,
}

// === Assert ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AssertStmt {
    pub comparison: Comparison,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Comparison {
    pub left: Expression,
    pub operations: Vec<CompareOp>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompareOp {
    pub kind: CompareOpKind,
    pub right: Expression,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompareOpKind {
    LessThan,
    GreaterThan,
    LessEq,
    GreaterEq,
    Within,
    Is,
}

// === String/Pass Statements ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StringStmt {
    pub value: StringLiteral,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PassStmt {
    pub span: Span,
}

// === Trait ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TraitStmt {
    /// Optional target field
    pub target: Option<FieldRef>,
    pub type_ref: TypeRef,
    /// Optional constructor name
    pub constructor: Option<Identifier>,
    /// Optional template arguments
    pub template: Option<Template>,
    pub span: Span,
}

// === For Loop ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ForStmt {
    pub variable: Identifier,
    pub iterable: Iterable,
    pub body: Vec<Statement>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum Iterable {
    FieldRef {
        field: FieldRef,
        slice: Option<Slice>,
    },
    List(Vec<FieldRef>),
}

// === Expressions ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum Expression {
    /// Binary operation
    Binary(Box<BinaryExpr>),
    /// Unary operation (e.g., -x)
    Unary(Box<UnaryExpr>),
    /// Literal value
    Literal(Literal),
    /// Field reference (a.b.c, a[0])
    FieldRef(FieldRef),
    /// Grouped expression: (expr)
    Group(Box<Expression>),
    /// Function call: name(args)
    FunctionCall(FunctionCall),
}

impl Expression {
    pub fn span(&self) -> Span {
        match self {
            Expression::Binary(e) => e.span,
            Expression::Unary(e) => e.span,
            Expression::Literal(l) => l.span(),
            Expression::FieldRef(f) => f.span,
            Expression::Group(e) => e.span(),
            Expression::FunctionCall(f) => f.span,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BinaryExpr {
    pub left: Expression,
    pub operator: BinaryOp,
    pub right: Expression,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOp {
    Add,    // +
    Sub,    // -
    Mul,    // *
    Div,    // /
    Power,  // **
    BitOr,  // |
    BitAnd, // &
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnaryExpr {
    pub operator: UnaryOp,
    pub operand: Expression,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOp {
    Neg, // -
    Pos, // +
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionCall {
    pub name: Identifier,
    pub args: Vec<Expression>,
    pub span: Span,
}

// === Literals ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum Literal {
    Number(NumberLiteral),
    String(StringLiteral),
    Bool(BoolLiteral),
    Physical(PhysicalLiteral),
}

impl Literal {
    pub fn span(&self) -> Span {
        match self {
            Literal::Number(n) => n.span,
            Literal::String(s) => s.span,
            Literal::Bool(b) => b.span,
            Literal::Physical(p) => p.span(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NumberLiteral {
    pub value: String,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StringLiteral {
    pub value: String,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BoolLiteral {
    pub value: bool,
    pub span: Span,
}

/// Physical quantity literal (number with optional unit)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PhysicalLiteral {
    /// Simple quantity: `5V`, `10kohm`
    Quantity(Quantity),
    /// Range: `1V to 2V`
    Range(QuantityRange),
    /// Bilateral tolerance: `5V +/- 10%`
    Bilateral(BilateralQuantity),
}

impl PhysicalLiteral {
    pub fn span(&self) -> Span {
        match self {
            PhysicalLiteral::Quantity(q) => q.span,
            PhysicalLiteral::Range(r) => r.span,
            PhysicalLiteral::Bilateral(b) => b.span,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Quantity {
    pub number: NumberLiteral,
    pub unit: Option<Identifier>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct QuantityRange {
    pub from: Quantity,
    pub to: Quantity,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BilateralQuantity {
    pub base: Quantity,
    pub tolerance: Tolerance,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tolerance {
    pub value: String,
    pub is_percent: bool,
    pub unit: Option<Identifier>,
    pub span: Span,
}

// === New Expression ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NewExpr {
    pub type_ref: TypeRef,
    /// Optional array count: `new Type[10]`
    pub count: Option<NumberLiteral>,
    /// Optional template args: `new Type<param=value>`
    pub template: Option<Template>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Template {
    pub args: Vec<TemplateArg>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TemplateArg {
    pub name: Identifier,
    pub value: Literal,
    pub span: Span,
}

// === References ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldRef {
    pub parts: Vec<FieldRefPart>,
    /// Optional trailing pin reference: `.1`
    pub pin_ref: Option<NumberLiteral>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldRefPart {
    pub name: Identifier,
    /// Optional array index: `[0]`
    pub index: Option<NumberLiteral>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TypeRef {
    pub parts: Vec<Identifier>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Slice {
    pub start: Option<NumberLiteral>,
    pub stop: Option<NumberLiteral>,
    pub step: Option<NumberLiteral>,
    pub span: Span,
}

// === Identifier ===

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Identifier {
    pub name: String,
    pub span: Span,
}