From ce7dc2b9ba54a20565db610b1533d0fb6258d5a3 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Tue, 5 May 2026 14:30:24 +0200 Subject: [PATCH] Updated grammar and AST --- parser/src/ast.rs | 24 ++++++++++-------------- parser/src/grammar.pest | 17 +++++++---------- parser/src/lib.rs | 35 ++--------------------------------- 3 files changed, 19 insertions(+), 57 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index e3e0bcf..05a42f0 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -18,22 +18,18 @@ pub enum TypePostfix { } #[derive(Debug, Clone, Serialize)] -pub struct Attribute { - pub path: StaticPath, - pub kind: MetaItemKind, -} +pub enum Attribute { + /// #[test] + Path(StaticPath), -#[derive(Debug, Clone, Serialize)] -pub enum MetaItemKind { - Word, - NameValue(Literal), - List(Vec), -} + /// #[name = "value"] + NameValue { path: StaticPath, value: Literal }, -#[derive(Debug, Clone, Serialize)] -pub enum NestedMetaItem { - Literal(Literal), - MetaItem(Attribute), + /// #[derive(Clone, Copy)] + List { + path: StaticPath, + items: Vec, + }, } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 8c8aaa0..7b13504 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -20,19 +20,16 @@ var_decl = { type_expr ~ mutable? ~ identifier } // ================= TOP LEVEL ================= attribute = { - "#" ~ "[" ~ (structured_style | list_style | pair_attr | simple_style) ~ "]" + "#" ~ "[" ~ meta ~ "]" } -// Simple: #[test] -simple_style = { static_path } +meta = { + static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")? +} -// #[derive(PartialEq, PartialOrd)] -list_style = { static_path ~ "(" ~ identifier ~ ("," ~ identifier)* ~ ","? ~ ")" } - -// #[link(name = "readline")] -structured_style = { static_path ~ "(" ~ pair_attr ~ ("," ~ pair_attr)* ~ ","? ~ ")" } - -pair_attr = { identifier ~ "=" ~ primary } +meta_list = { + meta ~ ("," ~ meta)* ~ ","? +} top_level = { attribute* ~ (import | function_decl | struct_decl) } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index caddf9d..0e00713 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -128,42 +128,11 @@ impl From> for Attribute { } let mut inner = pair.into_inner(); - // Every style starts with an identifier (the path) - let path = StaticPath::from(inner.next().expect("Path identifier expected")); + let path = StaticPath::from(inner.next().unwrap()); let kind = match rule { - Rule::simple_style => MetaItemKind::Word, - - 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), + _ => unimplemented!("{:?}", rule), }; - - Attribute { path, kind } } }