Improved match item span

This commit is contained in:
2026-05-28 12:16:28 +02:00
parent 7a2ba74d9c
commit ad11a3bd6a
3 changed files with 24 additions and 12 deletions
+5 -1
View File
@@ -59,7 +59,11 @@ impl GenRust for Statement {
cg.add(" {"); cg.add(" {");
cg.indent += 1; cg.indent += 1;
for (pat, body) in match_items { for match_item in match_items {
cg.add_indentedln(&match_item.get_comment());
let MatchItem(pat, body) = &match_item.item;
for (i, p) in pat.iter().enumerate() { for (i, p) in pat.iter().enumerate() {
cg.addln(""); cg.addln("");
cg.add_indented(""); cg.add_indented("");
+4 -1
View File
@@ -35,7 +35,7 @@ pub enum Statement {
iterator: Expression, iterator: Expression,
body: StatementBody, body: StatementBody,
}, },
Match(Expression, Vec<(Vec<Pattern>, Expression)>), Match(Expression, Vec<Spanned<MatchItem>>),
VarDecl(VarDeclStmt), VarDecl(VarDeclStmt),
Return(Option<Expression>), Return(Option<Expression>),
@@ -43,6 +43,9 @@ pub enum Statement {
Continue, Continue,
} }
#[derive(Debug, Clone, Serialize)]
pub struct MatchItem(pub Vec<Pattern>, pub Expression);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub name: Pattern, pub name: Pattern,
+15 -10
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, IntoErr, collect_recovered}, error::{AstError, IntoErr, collect_recovered},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
@@ -103,18 +103,23 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
Rule::match_stmt => ast_expr!(Statement::Match( Rule::match_stmt => ast_expr!(Statement::Match(
inner.next().unwrap().try_into(), inner.next().unwrap().try_into(),
inner collect_recovered(inner),
.map(|match_itms| {
let mut match_inner = match_itms.into_inner();
Ok((
collect_recovered(match_inner.next().unwrap().into_inner()).get()?,
Expression::try_from(match_inner.next().unwrap()).get()?,
))
})
.collect::<AstResult<'a, Vec<_>>>(),
)), )),
_ => AstError::bug_unimplemented(pair), _ => AstError::bug_unimplemented(pair),
} }
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for MatchItem {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut match_inner = pair.into_inner();
Ok(MatchItem(
collect_recovered(match_inner.next().unwrap().into_inner()).get()?,
Expression::try_from(match_inner.next().unwrap()).get()?,
))
}
}