Updated grammar and AST

This commit is contained in:
2026-05-05 14:30:24 +02:00
parent 1d00667b18
commit ce7dc2b9ba
3 changed files with 19 additions and 57 deletions
+10 -14
View File
@@ -18,22 +18,18 @@ pub enum TypePostfix {
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct Attribute { pub enum Attribute {
pub path: StaticPath, /// #[test]
pub kind: MetaItemKind, Path(StaticPath),
}
#[derive(Debug, Clone, Serialize)] /// #[name = "value"]
pub enum MetaItemKind { NameValue { path: StaticPath, value: Literal },
Word,
NameValue(Literal),
List(Vec<NestedMetaItem>),
}
#[derive(Debug, Clone, Serialize)] /// #[derive(Clone, Copy)]
pub enum NestedMetaItem { List {
Literal(Literal), path: StaticPath,
MetaItem(Attribute), items: Vec<Attribute>,
},
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+7 -10
View File
@@ -20,19 +20,16 @@ var_decl = { type_expr ~ mutable? ~ identifier }
// ================= TOP LEVEL ================= // ================= TOP LEVEL =================
attribute = { attribute = {
"#" ~ "[" ~ (structured_style | list_style | pair_attr | simple_style) ~ "]" "#" ~ "[" ~ meta ~ "]"
} }
// Simple: #[test] meta = {
simple_style = { static_path } static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")?
}
// #[derive(PartialEq, PartialOrd)] meta_list = {
list_style = { static_path ~ "(" ~ identifier ~ ("," ~ identifier)* ~ ","? ~ ")" } meta ~ ("," ~ meta)* ~ ","?
}
// #[link(name = "readline")]
structured_style = { static_path ~ "(" ~ pair_attr ~ ("," ~ pair_attr)* ~ ","? ~ ")" }
pair_attr = { identifier ~ "=" ~ primary }
top_level = { attribute* ~ (import | function_decl | struct_decl) } top_level = { attribute* ~ (import | function_decl | struct_decl) }
+2 -33
View File
@@ -128,42 +128,11 @@ impl From<pest::iterators::Pair<'_, Rule>> for Attribute {
} }
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
// Every style starts with an identifier (the path) let path = StaticPath::from(inner.next().unwrap());
let path = StaticPath::from(inner.next().expect("Path identifier expected"));
let kind = match rule { let kind = match rule {
Rule::simple_style => MetaItemKind::Word, _ => unimplemented!("{:?}", rule),
Rule::key_value_style | Rule::pair => {
let lit = Literal::from(inner.next().unwrap());
MetaItemKind::NameValue(lit)
}
Rule::list_style => {
// derive(Debug, Clone) -> NestedMetaItem::MetaItem(Attribute { path: "Debug", kind: Word })
let items = inner
.map(|p| {
NestedMetaItem::MetaItem(Attribute {
path: StaticPath::from(p),
kind: MetaItemKind::Word,
})
})
.collect();
MetaItemKind::List(items)
}
Rule::structured_style => {
// link(name = "readline") -> NestedMetaItem::MetaItem(Attribute { path: "name", kind: NameValue(...) })
let items = inner
.map(|p| NestedMetaItem::MetaItem(Attribute::from(p)))
.collect();
MetaItemKind::List(items)
}
_ => unreachable!("Unexpected rule: {:?}", rule),
}; };
Attribute { path, kind }
} }
} }