Visibility And Mutability AST

This commit is contained in:
2026-05-05 22:00:39 +02:00
parent 5be6ecb44e
commit ef1585f9fb
2 changed files with 22 additions and 27 deletions
+5 -5
View File
@@ -1,7 +1,7 @@
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Clone, 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)] #[derive(Debug, Clone, Serialize)]
pub struct ParamList(pub Vec<VarDecl>); pub struct ParamList(pub Vec<VarDecl>);
@@ -69,13 +69,13 @@ pub enum TopLevelKind {
ModAttribute, ModAttribute,
Include(Path), Include(Path),
StructDecl { StructDecl {
export: bool, export: Visibility,
name: String, name: String,
fields: FieldList, fields: FieldList,
}, },
FunctionDecl(FunctionDecl), FunctionDecl(FunctionDecl),
ClassDecl { ClassDecl {
export: bool, export: Visibility,
name: String, name: String,
fields: Vec<VarDeclStmt>, fields: Vec<VarDeclStmt>,
constructor: ClassConstructor, constructor: ClassConstructor,
@@ -85,14 +85,14 @@ pub enum TopLevelKind {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor { pub struct ClassConstructor {
pub export: bool, pub export: Visibility,
pub params: ParamList, pub params: ParamList,
pub body: Block, pub body: Block,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct FunctionDecl { pub struct FunctionDecl {
pub export: bool, pub export: Visibility,
pub name: String, pub name: String,
pub params: ParamList, pub params: ParamList,
pub return_type: TypeExpr, pub return_type: TypeExpr,
+17 -22
View File
@@ -546,12 +546,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
Some(TypeExpr::from(pair)) Some(TypeExpr::from(pair))
} }
}); });
let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable { let mutable = listen_rule(&mut inner, Rule::mutable);
inner.next();
true
} else {
false
};
let name = inner.next().unwrap().as_str().to_string(); let name = inner.next().unwrap().as_str().to_string();
VarDecl { VarDecl {
@@ -588,23 +584,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
let mut param = inner.next().unwrap().into_inner(); let mut param = inner.next().unwrap().into_inner();
let name = format!("self"); let name = format!("self");
let mutable = param let mutable = listen_rule(&mut param, Rule::mutable);
.peek()
.map(|p| p.as_rule() == Rule::mutable)
.unwrap_or_default();
if mutable { let is_ref = listen_rule(&mut param, Rule::mutable);
param.next();
}
let is_ref = param
.peek()
.map(|p| p.as_rule() == Rule::deref_px)
.unwrap_or_default();
if is_ref {
param.next();
}
Some(VarDecl { Some(VarDecl {
mutable: mutable && !is_ref, mutable: mutable && !is_ref,
@@ -647,3 +629,16 @@ impl From<pest::iterators::Pair<'_, Rule>> 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
}