This commit is contained in:
2026-05-05 22:34:30 +02:00
parent 2b1069d2e8
commit d958057502
+32 -31
View File
@@ -211,11 +211,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
let visibility = Visibility::from(&mut inner); let visibility = Visibility::from(&mut inner);
let params = if inner.peek().unwrap().as_rule() == Rule::param_list { let params = consume_rule(&mut inner, Rule::param_list)
ParamList::from(inner.next().unwrap()) .map(ParamList::from)
} else { .unwrap_or_else(|| ParamList(Vec::new()));
ParamList(Vec::new())
};
Self { Self {
visibility, visibility,
@@ -394,18 +392,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
} }
Rule::primary => Expression::from(inner.next().unwrap()), Rule::primary => Expression::from(inner.next().unwrap()),
Rule::static_path => Expression::Path(Path::from(pair)), Rule::static_path => Expression::Path(Path::from(pair)),
Rule::integer => { Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => {
Expression::Literal(Literal::Int(pair.as_str().parse::<i64>().unwrap())) Expression::Literal(Literal::from(pair))
}
Rule::float => {
Expression::Literal(Literal::Float(pair.as_str().parse::<f64>().unwrap()))
}
Rule::boolean => {
Expression::Literal(Literal::Bool(pair.as_str().parse::<bool>().unwrap()))
}
Rule::string_lit => Expression::Literal(Literal::String(inner.as_str().to_string())),
Rule::tuple => {
Expression::Literal(Literal::Tuple(inner.map(Expression::from).collect()))
} }
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
@@ -539,15 +527,15 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
let return_type = TypeExpr::from(inner.next().unwrap()); let return_type = TypeExpr::from(inner.next().unwrap());
let name = inner.next().unwrap().as_str().to_string(); let name = inner.next().unwrap().as_str().to_string();
let self_param = if inner.peek().unwrap().as_rule() == Rule::self_param { let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
let mut param = inner.next().unwrap().into_inner(); let mut param_inner = param.into_inner();
let name = format!("self"); let name = format!("self");
let mutable = listen_rule(&mut param, Rule::mutable); let mutable = listen_rule(&mut param_inner, Rule::mutable);
let is_ref = listen_rule(&mut param, Rule::deref_px); let is_ref = listen_rule(&mut param_inner, Rule::deref_px);
Some(VarDecl { VarDecl {
mutable: mutable && !is_ref, mutable: mutable && !is_ref,
name, name,
type_: Some(TypeExpr( type_: Some(TypeExpr(
@@ -562,20 +550,21 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
Vec::new() Vec::new()
}, },
)), )),
}) }
} else { });
None
};
let params = if inner.peek().unwrap().as_rule() == Rule::param_list { let params = consume_rule(&mut inner, Rule::param_list)
let mut params = ParamList::from(inner.next().unwrap()); .map({
let self_param = self_param.clone();
|params_pair| {
let mut params = ParamList::from(params_pair);
if let Some(x) = self_param { if let Some(x) = self_param {
params.0.insert(0, x); params.0.insert(0, x);
} }
params params
} else { }
ParamList(self_param.into_iter().collect()) })
}; .unwrap_or_else(|| ParamList(self_param.into_iter().collect()));
let body = Block::from(inner.next().unwrap()); let body = Block::from(inner.next().unwrap());
@@ -611,3 +600,15 @@ pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) ->
consumed consumed
} }
pub fn consume_rule<'a>(
pairs: &mut pest::iterators::Pairs<'a, Rule>,
rule: Rule,
) -> Option<pest::iterators::Pair<'a, Rule>> {
let consumed = pairs
.peek()
.map(|p| p.as_rule() == rule)
.unwrap_or_default();
if consumed { pairs.next() } else { None }
}