From 81061f26a56acb5aea6e466a2d6c08a886ec4eac Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 7 May 2026 21:21:41 +0200 Subject: [PATCH] Enum astgen --- parser/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 8b3783a..5d67068 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -259,6 +259,12 @@ impl From> for TopLevelKind { methods: inner.into_iter().map(FunctionDecl::from).collect(), }, + Rule::enum_decl => TopLevelKind::EnumDecl { + visibility: Visibility::from(&mut inner), + name: Identifier::from(inner.next().unwrap()), + fields: inner.map(EnumItem::from).collect(), + }, + Rule::mod_package => TopLevelKind::Mod(Identifier::from(inner.next().unwrap())), _ => unimplemented!("{rule:#?}"), @@ -266,6 +272,34 @@ impl From> for TopLevelKind { } } +impl From> for EnumItem { + fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { + let rule = pair.as_rule(); + let mut inner = pair.clone().into_inner(); + + match rule { + Rule::enum_named => EnumItem::Named(Identifier::from(inner.next().unwrap())), + + Rule::enum_tuple => EnumItem::Tuple( + Identifier::from(inner.next().unwrap()), + inner + .next() + .unwrap() + .into_inner() + .map(TypeExpr::from) + .collect(), + ), + + Rule::enum_struct => EnumItem::Struct( + Identifier::from(inner.next().unwrap()), + FieldList::from(inner.next().unwrap()), + ), + + _ => unimplemented!("{rule:#?}"), + } + } +} + impl From> for Block { fn from(pair: pest::iterators::Pair) -> Self { let statements = pair