Unsafe pointer

This commit is contained in:
2026-06-28 05:24:30 +02:00
parent bf5d243036
commit 3ddb1bc601
6 changed files with 44 additions and 68 deletions
+1 -7
View File
@@ -17,16 +17,10 @@ pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>);
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum Lifetime {
Lifetime(Identifier),
Unsafe,
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum TypeExpr {
Ref {
lifetime: Option<Lifetime>,
lifetime: Option<Identifier>,
mutable: bool,
ty: Box<TypeExpr>,
},
+6 -5
View File
@@ -81,7 +81,6 @@ module_program = {
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
mutable = { "mut" }
unsafe_kw = { "unsafe" }
// ======================================================
// GENERICS
@@ -104,10 +103,12 @@ path_type = {
static_path ~ generics?
}
ref_lifetime = { lifetime | unsafe_kw }
unsafe_ref_type = {
mutable? ~ "unsafe" ~ "*"
}
ref_type = {
mutable? ~ ref_lifetime? ~ "&"
mutable? ~ lifetime? ~ "&"
}
dyn_type = {
@@ -115,7 +116,7 @@ dyn_type = {
}
type_expr = {
(path_type | tuple_type | dyn_type) ~ ref_type*
(path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type)*
}
lifetime = {
@@ -467,7 +468,7 @@ match_stmt = {
closure = {
("(" ~ param_list ~ ")" ~ "=>" ~ expr)
| (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr)
| (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr)
}
// ------------------------------------------------------
+22 -9
View File
@@ -21,16 +21,29 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
let mut ty = TypeExpr::try_from(inner.next().unwrap())?;
for ref_pair in inner {
let mut ref_inner = ref_pair.into_inner();
let mut ref_inner = ref_pair.clone().into_inner();
ty = TypeExpr::Ref {
lifetime: consume_rule(&mut ref_inner, Rule::ref_lifetime)
.map(|v| v.into_inner().next().map(Lifetime::try_from))
.unwrap_or_default()
.transpose()?,
mutable: listen_rule(&mut ref_inner, Rule::mutable),
ty: Box::new(ty),
};
match ref_pair.as_rule() {
Rule::ref_type => {
ty = TypeExpr::Ref {
mutable: listen_rule(&mut ref_inner, Rule::mutable),
lifetime: consume_rule(&mut ref_inner, Rule::lifetime)
.map(|v| v.into_inner().next().map(Identifier::try_from))
.unwrap_or_default()
.transpose()?,
ty: Box::new(ty),
};
}
Rule::unsafe_ref_type => {
ty = TypeExpr::UnsafePtr {
mutable: listen_rule(&mut ref_inner, Rule::mutable),
ty: Box::new(ty),
};
}
_ => AstError::bug_unimplemented(ref_pair)?,
}
}
Ok(ty)
+1 -16
View File
@@ -2,7 +2,7 @@ use crate::{
Rule,
ast::*,
ast_ensure,
error::{self, AstError, AstResult},
error::{AstError, AstResult},
parser::{consume_rule, consume_rule_map, listen_rule},
};
@@ -66,18 +66,3 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Override {
})
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Lifetime {
type Error = AstError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
match pair.as_rule() {
Rule::ref_lifetime => inner.next().unwrap().try_into(),
Rule::lifetime => Ok(Lifetime::Lifetime(inner.next().unwrap().try_into()?)),
Rule::unsafe_kw => Ok(Lifetime::Unsafe),
_ => error::AstError::bug_unimplemented(pair),
}
}
}