From ef1585f9fb053dfdf64588769594cc3757669301 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Tue, 5 May 2026 22:00:29 +0200 Subject: [PATCH] Visibility And Mutability AST --- parser/src/ast.rs | 10 +++++----- parser/src/lib.rs | 39 +++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 7e4dc1b..2b7a613 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -1,7 +1,7 @@ use serde::Serialize; #[derive(Debug, Clone, Serialize)] -pub struct FieldList(pub Vec<(String, bool, TypeExpr)>); +pub struct FieldList(pub Vec<(String, Visibility, TypeExpr)>); #[derive(Debug, Clone, Serialize)] pub struct ParamList(pub Vec); @@ -69,13 +69,13 @@ pub enum TopLevelKind { ModAttribute, Include(Path), StructDecl { - export: bool, + export: Visibility, name: String, fields: FieldList, }, FunctionDecl(FunctionDecl), ClassDecl { - export: bool, + export: Visibility, name: String, fields: Vec, constructor: ClassConstructor, @@ -85,14 +85,14 @@ pub enum TopLevelKind { #[derive(Debug, Clone, Serialize)] pub struct ClassConstructor { - pub export: bool, + pub export: Visibility, pub params: ParamList, pub body: Block, } #[derive(Debug, Clone, Serialize)] pub struct FunctionDecl { - pub export: bool, + pub export: Visibility, pub name: String, pub params: ParamList, pub return_type: TypeExpr, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index b39ea1d..ec2605c 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -546,12 +546,8 @@ impl From> for VarDecl { Some(TypeExpr::from(pair)) } }); - let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable { - inner.next(); - true - } else { - false - }; + let mutable = listen_rule(&mut inner, Rule::mutable); + let name = inner.next().unwrap().as_str().to_string(); VarDecl { @@ -588,23 +584,9 @@ impl From> for FunctionDecl { let mut param = inner.next().unwrap().into_inner(); let name = format!("self"); - let mutable = param - .peek() - .map(|p| p.as_rule() == Rule::mutable) - .unwrap_or_default(); + let mutable = listen_rule(&mut param, Rule::mutable); - if mutable { - param.next(); - } - - let is_ref = param - .peek() - .map(|p| p.as_rule() == Rule::deref_px) - .unwrap_or_default(); - - if is_ref { - param.next(); - } + let is_ref = listen_rule(&mut param, Rule::mutable); Some(VarDecl { mutable: mutable && !is_ref, @@ -647,3 +629,16 @@ impl From> for FunctionDecl { } } } + +pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool { + let consumed = pairs + .peek() + .map(|p| p.as_rule() == rule) + .unwrap_or_default(); + + if consumed { + pairs.next(); + } + + consumed +}