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    /// Logical command topic containing this command.
68    ///
69    /// Telemetry and legacy messages do not belong to command groups.
70    pub command_group: Option<String>,
71    pub name: String,
72    pub fields: Vec<FieldDef>,
73    pub doc: Vec<String>,
74    pub attrs: Vec<Attribute>,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub enum PacketKind {
79    /// Legacy generic Software Bus packet. cFS codegen rejects this in favor of explicit packet kinds.
80    Message,
81    /// cFS Software Bus command packet.
82    Command,
83    /// cFS Software Bus telemetry packet.
84    Telemetry,
85}
86
87#[derive(Debug, Clone, PartialEq)]
88pub struct FieldDef {
89    pub name: String,
90    pub optional: bool,
91    pub ty: TypeExpr,
92    pub default: Option<Literal>,
93    pub doc: Vec<String>,
94}
95
96#[derive(Debug, Clone, PartialEq)]
97pub struct TypeExpr {
98    pub base: BaseType,
99    pub array: Option<ArraySuffix>,
100}
101
102#[derive(Debug, Clone, PartialEq)]
103pub enum BaseType {
104    Primitive(PrimitiveType),
105    String,
106    Ref(ScopedIdent),
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub enum PrimitiveType {
111    F32,
112    F64,
113    I8,
114    I16,
115    I32,
116    I64,
117    U8,
118    U16,
119    U32,
120    U64,
121    Bool,
122    Bytes,
123}
124
125#[derive(Debug, Clone, PartialEq, Eq)]
126pub enum ArraySuffix {
127    /// `[]` — unbounded dynamic (Vec<T>)
128    Dynamic,
129    /// `[N]` — fixed size ([T; N])
130    Fixed(u64),
131    /// `[<=N]` — bounded dynamic (Vec<T> with max N)
132    Bounded(u64),
133}
134
135/// A namespace-qualified identifier, stored as individual segments.
136/// `geometry::Point` → `["geometry", "Point"]`
137pub type ScopedIdent = Vec<String>;
138
139#[derive(Debug, Clone, PartialEq)]
140pub enum Literal {
141    Float(f64),
142    Int(i64),
143    Hex(u64),
144    Bool(bool),
145    Str(String),
146    /// Enum variant or constant reference, e.g. `DriveMode::Idle`
147    Ident(ScopedIdent),
148}
149
150/// A declaration attribute, e.g. `@cc(1)`.
151#[derive(Debug, Clone, PartialEq)]
152pub struct Attribute {
153    pub name: String,
154    pub value: Literal,
155}
156
157mod builder;
158
159pub use builder::parse;
160
161#[cfg(test)]
162mod tests;