molecule_codegen/ast/raw/
mod.rs1use std::path::PathBuf;
2
3#[cfg(feature = "compiler-plugin")]
4use serde::{Deserialize, Serialize};
5
6use property::Property;
7
8mod utils;
9
10#[derive(Debug, Default, Property)]
11pub(crate) struct Ast {
12 syntax_version: Option<SyntaxVersion>,
13 namespace: String,
14 imports: Vec<ImportStmt>,
15 decls: Vec<TopDecl>,
16}
17
18impl Default for SyntaxVersion {
19 fn default() -> Self {
20 Self { version: 1 }
21 }
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Hash, Property)]
25#[property(get(public))]
26#[cfg_attr(
27 feature = "compiler-plugin",
28 derive(Deserialize, Serialize),
29 serde(deny_unknown_fields)
30)]
31pub struct SyntaxVersion {
32 version: usize,
33}
34
35#[derive(Debug, Clone, Property)]
36pub(crate) struct ImportStmt {
37 name: String,
38 paths: Vec<String>,
39 path_supers: usize,
40 imported_base: PathBuf,
41 imported_depth: usize,
42}
43
44#[derive(Debug)]
45pub(crate) enum TopDecl {
46 Option_(OptionDecl),
47 Union(UnionDecl),
48 Array(ArrayDecl),
49 Struct(StructDecl),
50 Vector(VectorDecl),
51 Table(TableDecl),
52}
53
54#[derive(Debug, Property)]
55pub(crate) struct OptionDecl {
56 name: String,
57 item: ItemDecl,
58 imported_depth: usize,
59}
60
61#[derive(Debug, Property)]
62pub(crate) struct UnionDecl {
63 name: String,
64 items: Vec<CustomUnionItemDecl>,
65 imported_depth: usize,
66}
67
68#[derive(Debug, Property)]
69pub(crate) struct ArrayDecl {
70 name: String,
71 item: ItemDecl,
72 item_count: usize,
73 imported_depth: usize,
74}
75
76#[derive(Debug, Property)]
77pub(crate) struct StructDecl {
78 name: String,
79 fields: Vec<FieldDecl>,
80 imported_depth: usize,
81}
82
83#[derive(Debug, Property)]
84pub(crate) struct VectorDecl {
85 name: String,
86 item: ItemDecl,
87 imported_depth: usize,
88}
89
90#[derive(Debug, Property)]
91pub(crate) struct TableDecl {
92 name: String,
93 fields: Vec<FieldDecl>,
94 imported_depth: usize,
95}
96
97#[derive(Debug, Property)]
98pub(crate) struct ItemDecl {
99 typ: String,
100}
101
102#[derive(Debug, Property)]
103pub(crate) struct CustomUnionItemDecl {
104 typ: String,
105 id: usize,
106}
107
108#[derive(Debug, Property)]
109pub(crate) struct FieldDecl {
110 name: String,
111 typ: String,
112}
113
114impl Ast {
115 pub(crate) fn add_import(&mut self, stmt: ImportStmt) {
116 self.imports.push(stmt);
117 }
118
119 pub(crate) fn add_decl(&mut self, decl: impl Into<TopDecl>) {
120 self.decls.push(decl.into());
121 }
122}
123
124impl TopDecl {
125 pub(crate) fn name(&self) -> &str {
126 match self {
127 TopDecl::Option_(inner) => inner.name(),
128 TopDecl::Union(inner) => inner.name(),
129 TopDecl::Array(inner) => inner.name(),
130 TopDecl::Struct(inner) => inner.name(),
131 TopDecl::Vector(inner) => inner.name(),
132 TopDecl::Table(inner) => inner.name(),
133 }
134 }
135}
136
137macro_rules! impl_into_top_decl_for {
138 ($item:ident, $decl:ident) => {
139 impl From<$decl> for TopDecl {
140 fn from(decl: $decl) -> Self {
141 TopDecl::$item(decl)
142 }
143 }
144 };
145}
146
147impl_into_top_decl_for!(Option_, OptionDecl);
148impl_into_top_decl_for!(Union, UnionDecl);
149impl_into_top_decl_for!(Array, ArrayDecl);
150impl_into_top_decl_for!(Struct, StructDecl);
151impl_into_top_decl_for!(Vector, VectorDecl);
152impl_into_top_decl_for!(Table, TableDecl);