From 3e25bf04561d045ec6a620161347e649506b570b Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 10 May 2026 00:35:14 +0200 Subject: [PATCH] Refrence lifetimes --- parser/src/ast.rs | 2 ++ parser/src/grammar.pest | 2 +- parser/src/lib.rs | 20 ++++++++++++++++---- src/codegen.rs | 2 ++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 85c3713..db081d9 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -16,6 +16,8 @@ pub struct Block(pub Vec); pub enum TypePostfix { Ref, RefMut, + RefLifetime(Identifier), + RefMutLifetime(Identifier), } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 5a31967..16903b5 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -71,7 +71,7 @@ path_type = { } ref_type = { - mutable? ~ "*" + mutable? ~ lifetime? ~ "*" } type_expr = { diff --git a/parser/src/lib.rs b/parser/src/lib.rs index caf663f..7c90682 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -49,14 +49,26 @@ impl From> for TypeExpr { impl From> for TypePostfix { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); - let inner = pair.into_inner(); + let mut inner = pair.into_inner(); match rule { Rule::ref_type => { - if inner.peek().is_some() { - TypePostfix::RefMut + let mutable = listen_rule(&mut inner, Rule::mutable); + let lifetime = consume_rule(&mut inner, Rule::lifetime) + .map(|pair| Identifier::from(pair.into_inner().next().unwrap())); + + if mutable { + if let Some(lifetime) = lifetime { + TypePostfix::RefMutLifetime(lifetime) + } else { + TypePostfix::Ref + } } else { - TypePostfix::Ref + if let Some(lifetime) = lifetime { + TypePostfix::RefLifetime(lifetime) + } else { + TypePostfix::RefMut + } } } _ => unimplemented!("{rule:#?}"), diff --git a/src/codegen.rs b/src/codegen.rs index 7030837..702f3e1 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -634,6 +634,8 @@ impl GetRust for TypePostfix { match self { TypePostfix::Ref => format!("&"), TypePostfix::RefMut => format!("&mut "), + TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), + TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), } } }