wirespec-syntax 0.3.0

Parser and AST for the wirespec protocol 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! AST node definitions for wirespec.
//!
//! These correspond to the canonical AST schema defined in `AST_SCHEMA_SPEC.md`.
//! The AST is syntax-oriented: it preserves source structure, declaration order,
//! and field order. It does NOT perform name resolution, type checking, or any
//! semantic analysis.

use crate::span::Span;

// ── Root ──

/// Root of a parsed `.wspec` file.
#[derive(Debug, Clone, PartialEq)]
pub struct AstModule {
    pub module_decl: Option<AstModuleDecl>,
    pub imports: Vec<AstImport>,
    pub annotations: Vec<AstAnnotation>,
    pub items: Vec<AstTopItem>,
    pub span: Option<Span>,
}

// ── Module / Import ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstModuleDecl {
    pub name: String,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstImport {
    pub module: String,
    pub name: Option<String>,
    pub span: Option<Span>,
}

// ── Annotations ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstAnnotation {
    pub name: String,
    pub args: Vec<AstAnnotationArg>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstAnnotationArg {
    Identifier(String),
    Int(i64),
    Bool(bool),
    String(String),
    NamedValue {
        name: String,
        value: AstLiteralValue,
    },
}

// ── Expressions ──

#[derive(Debug, Clone, PartialEq)]
pub enum AstExpr {
    Int {
        value: i64,
        span: Option<Span>,
    },
    Bool {
        value: bool,
        span: Option<Span>,
    },
    Null {
        span: Option<Span>,
    },
    NameRef {
        name: String,
        span: Option<Span>,
    },
    MemberAccess {
        base: Box<AstExpr>,
        field: String,
        span: Option<Span>,
    },
    Binary {
        op: BinOp,
        left: Box<AstExpr>,
        right: Box<AstExpr>,
        span: Option<Span>,
    },
    Unary {
        op: UnaryOp,
        operand: Box<AstExpr>,
        span: Option<Span>,
    },
    Coalesce {
        expr: Box<AstExpr>,
        default: Box<AstExpr>,
        span: Option<Span>,
    },
    InState {
        expr: Box<AstExpr>,
        state_name: String,
        span: Option<Span>,
    },
    Subscript {
        base: Box<AstExpr>,
        index: Box<AstExpr>,
        span: Option<Span>,
    },
    StateConstructor {
        sm_name: String,
        state_name: String,
        args: Vec<AstExpr>,
        span: Option<Span>,
    },
    Fill {
        value: Box<AstExpr>,
        count: Box<AstExpr>,
        span: Option<Span>,
    },
    Slice {
        base: Box<AstExpr>,
        start: Box<AstExpr>,
        end: Box<AstExpr>,
        span: Option<Span>,
    },
    All {
        collection: Box<AstExpr>,
        state_name: String,
        span: Option<Span>,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    BitAnd,
    BitOr,
    BitXor,
    Shl,
    Shr,
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
    And,
    Or,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnaryOp {
    Not,
    Neg,
}

// ── ASN.1 Length ──

#[derive(Debug, Clone, PartialEq)]
pub enum Asn1Length {
    Expr(Box<AstExpr>),
    Remaining,
}

// ── Type Expressions ──

#[derive(Debug, Clone, PartialEq)]
pub enum AstTypeExpr {
    Named {
        name: String,
        span: Option<Span>,
    },
    Bits {
        width: u16,
        span: Option<Span>,
    },
    Match {
        field_name: String,
        branches: Vec<AstMatchBranch>,
        span: Option<Span>,
    },
    Bytes {
        kind: AstBytesKind,
        fixed_size: Option<u64>,
        size_expr: Option<Box<AstExpr>>,
        span: Option<Span>,
    },
    Array {
        element_type: Box<AstTypeExpr>,
        count: AstArrayCount,
        within_expr: Option<Box<AstExpr>>,
        span: Option<Span>,
    },
    Optional {
        condition: AstExpr,
        inner_type: Box<AstTypeExpr>,
        span: Option<Span>,
    },
    Asn1 {
        type_name: String,
        encoding: String,
        length: Asn1Length,
        span: Option<Span>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstMatchBranch {
    pub pattern: AstPattern,
    pub result_type: AstTypeExpr,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AstBytesKind {
    Fixed,
    Length,
    Remaining,
    LengthOrRemaining,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstArrayCount {
    Expr(AstExpr),
    Fill,
}

// ── Patterns ──

#[derive(Debug, Clone, PartialEq)]
pub enum AstPattern {
    Value {
        value: i64,
        span: Option<Span>,
    },
    RangeInclusive {
        start: i64,
        end: i64,
        span: Option<Span>,
    },
    Wildcard {
        span: Option<Span>,
    },
}

// ── Fields ──

#[derive(Debug, Clone, PartialEq)]
pub enum AstFieldItem {
    Field(AstFieldDef),
    Derived(AstDerivedField),
    Require(AstRequireClause),
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstFieldDef {
    pub name: String,
    pub type_expr: AstTypeExpr,
    pub annotations: Vec<AstAnnotation>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstDerivedField {
    pub name: String,
    pub type_name: String,
    pub expr: AstExpr,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstRequireClause {
    pub expr: AstExpr,
    pub span: Option<Span>,
}

// ── Top-Level Items ──

#[derive(Debug, Clone, PartialEq)]
pub enum AstTopItem {
    Const(AstConstDecl),
    Enum(AstEnumDecl),
    Flags(AstFlagsDecl),
    StaticAssert(AstStaticAssertDecl),
    Type(AstTypeDecl),
    Packet(AstPacketDecl),
    Frame(AstFrameDecl),
    Capsule(AstCapsuleDecl),
    ContinuationVarInt(AstContinuationVarIntDecl),
    StateMachine(AstStateMachineDecl),
    ExternAsn1(AstExternAsn1),
}

// ── Extern ASN.1 ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstExternAsn1 {
    pub path: String,
    pub rust_module: Option<String>,
    pub type_names: Vec<String>,
    pub span: Option<Span>,
}

// ── Const / Enum / Flags ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstConstDecl {
    pub name: String,
    pub type_name: String,
    pub value: AstLiteralValue,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstEnumMember {
    pub name: String,
    pub value: i64,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstEnumDecl {
    pub name: String,
    pub underlying_type: String,
    pub members: Vec<AstEnumMember>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstFlagsDecl {
    pub name: String,
    pub underlying_type: String,
    pub members: Vec<AstEnumMember>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstStaticAssertDecl {
    pub expr: AstExpr,
    pub span: Option<Span>,
}

// ── Type / Packet ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstTypeDecl {
    pub name: String,
    pub annotations: Vec<AstAnnotation>,
    pub body: AstTypeDeclBody,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstTypeDeclBody {
    Fields { fields: Vec<AstFieldDef> },
    Alias { target: AstTypeExpr },
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstPacketDecl {
    pub name: String,
    pub fields: Vec<AstFieldItem>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

// ── Frame / Capsule ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstFrameBranch {
    pub pattern: AstPattern,
    pub variant_name: String,
    pub fields: Vec<AstFieldItem>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstFrameDecl {
    pub name: String,
    pub tag_field: String,
    pub tag_type: String,
    pub branches: Vec<AstFrameBranch>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstCapsuleDecl {
    pub name: String,
    pub fields: Vec<AstFieldItem>,
    pub payload_tag: AstPayloadTagSelector,
    pub payload_within: String,
    pub branches: Vec<AstFrameBranch>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstPayloadTagSelector {
    Field { field_name: String },
    Expr { expr: AstExpr },
}

// ── Continuation VarInt ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstContinuationVarIntDecl {
    pub name: String,
    pub annotations: Vec<AstAnnotation>,
    pub continuation_bit: String,
    pub value_bits: u8,
    pub max_bytes: u8,
    pub byte_order: String,
    pub exported: bool,
    pub span: Option<Span>,
}

// ── State Machine ──

#[derive(Debug, Clone, PartialEq)]
pub struct AstStateMachineDecl {
    pub name: String,
    pub states: Vec<AstStateDecl>,
    pub initial_state: String,
    pub transitions: Vec<AstTransitionDecl>,
    pub verify_declarations: Vec<AstVerifyDecl>,
    pub annotations: Vec<AstAnnotation>,
    pub exported: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstVerifyDecl {
    /// `verify NoDeadlock` or `verify AllReachClosed`
    BuiltIn { name: String, span: Option<Span> },
    /// `verify property Name: formula`
    Property {
        name: String,
        formula: AstVerifyFormula,
        span: Option<Span>,
    },
}

/// A verify formula — simplified temporal logic expression
#[derive(Debug, Clone, PartialEq)]
pub enum AstVerifyFormula {
    InState {
        state_name: String,
    },
    Not {
        inner: Box<AstVerifyFormula>,
    },
    And {
        left: Box<AstVerifyFormula>,
        right: Box<AstVerifyFormula>,
    },
    Or {
        left: Box<AstVerifyFormula>,
        right: Box<AstVerifyFormula>,
    },
    Implies {
        left: Box<AstVerifyFormula>,
        right: Box<AstVerifyFormula>,
    },
    Always {
        inner: Box<AstVerifyFormula>,
    },
    Eventually {
        inner: Box<AstVerifyFormula>,
    },
    LeadsTo {
        left: Box<AstVerifyFormula>,
        right: Box<AstVerifyFormula>,
    },
    Compare {
        left: Box<AstVerifyFormula>,
        op: String,
        right: Box<AstVerifyFormula>,
    },
    FieldRef {
        path: Vec<String>,
    },
    Literal {
        value: AstLiteralValue,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstStateDecl {
    pub name: String,
    pub fields: Vec<AstStateFieldDef>,
    pub is_terminal: bool,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstStateFieldDef {
    pub name: String,
    pub type_expr: AstTypeExpr,
    pub default_value: Option<AstLiteralValue>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstEventDecl {
    pub name: String,
    pub params: Vec<AstEventParam>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstEventParam {
    pub name: String,
    pub type_expr: AstTypeExpr,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstTransitionDecl {
    pub src_state: String,
    pub dst_state: String,
    pub events: Vec<AstEventDecl>,
    pub guard: Option<AstExpr>,
    pub actions: Vec<AstAssignment>,
    pub delegate: Option<AstDelegateClause>,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstAssignment {
    pub target: AstExpr,
    pub op: String,
    pub value: AstExpr,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AstDelegateClause {
    pub target: AstExpr,
    pub event_name: String,
    pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AstLiteralValue {
    Int(i64),
    Bool(bool),
    String(String),
    Null,
}