Skip to main content

synapse_parser/
ast.rs

1// ── Public types ──────────────────────────────────────────────────────────────
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct SynFile {
5    pub items: Vec<Item>,
6}
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum Item {
10    Namespace(NamespaceDecl),
11    Import(ImportDecl),
12    Const(ConstDecl),
13    Enum(EnumDef),
14    Struct(StructDef),
15    Table(StructDef),
16    Command(MessageDef),
17    Telemetry(MessageDef),
18    Message(MessageDef),
19}
20
21#[derive(Debug, Clone, PartialEq)]
22pub struct NamespaceDecl {
23    pub name: ScopedIdent,
24}
25
26#[derive(Debug, Clone, PartialEq)]
27pub struct ImportDecl {
28    pub path: String,
29}
30
31#[derive(Debug, Clone, PartialEq)]
32pub struct ConstDecl {
33    pub name: String,
34    pub ty: TypeExpr,
35    pub value: Literal,
36    pub doc: Vec<String>,
37    pub attrs: Vec<Attribute>,
38}
39
40#[derive(Debug, Clone, PartialEq)]
41pub struct EnumDef {
42    pub name: String,
43    pub repr: Option<PrimitiveType>,
44    pub variants: Vec<EnumVariant>,
45    pub doc: Vec<String>,
46    pub attrs: Vec<Attribute>,
47}
48
49#[derive(Debug, Clone, PartialEq)]
50pub struct EnumVariant {
51    pub name: String,
52    pub value: Option<i64>,
53    pub doc: Vec<String>,
54}
55
56#[derive(Debug, Clone, PartialEq)]
57pub struct StructDef {
58    pub name: String,
59    pub fields: Vec<FieldDef>,
60    pub doc: Vec<String>,
61    pub attrs: Vec<Attribute>,
62}
63
64#[derive(Debug, Clone, PartialEq)]
65pub struct MessageDef {
66    pub kind: PacketKind,
67    pub name: String,
68    pub fields: Vec<FieldDef>,
69    pub doc: Vec<String>,
70    pub attrs: Vec<Attribute>,
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum PacketKind {
75    /// Legacy generic Software Bus packet. cFS codegen rejects this in favor of explicit packet kinds.
76    Message,
77    /// cFS Software Bus command packet.
78    Command,
79    /// cFS Software Bus telemetry packet.
80    Telemetry,
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub struct FieldDef {
85    pub name: String,
86    pub optional: bool,
87    pub ty: TypeExpr,
88    pub default: Option<Literal>,
89    pub doc: Vec<String>,
90}
91
92#[derive(Debug, Clone, PartialEq)]
93pub struct TypeExpr {
94    pub base: BaseType,
95    pub array: Option<ArraySuffix>,
96}
97
98#[derive(Debug, Clone, PartialEq)]
99pub enum BaseType {
100    Primitive(PrimitiveType),
101    String,
102    Ref(ScopedIdent),
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum PrimitiveType {
107    F32,
108    F64,
109    I8,
110    I16,
111    I32,
112    I64,
113    U8,
114    U16,
115    U32,
116    U64,
117    Bool,
118    Bytes,
119}
120
121#[derive(Debug, Clone, PartialEq, Eq)]
122pub enum ArraySuffix {
123    /// `[]` — unbounded dynamic (Vec<T>)
124    Dynamic,
125    /// `[N]` — fixed size ([T; N])
126    Fixed(u64),
127    /// `[<=N]` — bounded dynamic (Vec<T> with max N)
128    Bounded(u64),
129}
130
131/// A namespace-qualified identifier, stored as individual segments.
132/// `geometry::Point` → `["geometry", "Point"]`
133pub type ScopedIdent = Vec<String>;
134
135#[derive(Debug, Clone, PartialEq)]
136pub enum Literal {
137    Float(f64),
138    Int(i64),
139    Hex(u64),
140    Bool(bool),
141    Str(String),
142    /// Enum variant or constant reference, e.g. `DriveMode::Idle`
143    Ident(ScopedIdent),
144}
145
146/// A declaration attribute, e.g. `@mid(0x0801)`.
147#[derive(Debug, Clone, PartialEq)]
148pub struct Attribute {
149    pub name: String,
150    pub value: Literal,
151}
152
153mod builder;
154
155pub use builder::parse;
156
157#[cfg(test)]
158mod tests;