Match multiple patterns

This commit is contained in:
2026-05-19 12:21:20 +02:00
parent 318480f0b5
commit bffc9f073b
4 changed files with 13 additions and 4 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ pub enum Statement {
iterator: Expression, iterator: Expression,
body: Box<Statement>, body: Box<Statement>,
}, },
Match(Expression, Vec<(Pattern, Block)>), Match(Expression, Vec<(Vec<Pattern>, Block)>),
Return(Option<Expression>), Return(Option<Expression>),
Break, Break,
+3 -1
View File
@@ -446,8 +446,10 @@ for_stmt = {
// MATCH // MATCH
// ------------------------------------------------------ // ------------------------------------------------------
pattern_list = { pattern ~ ("|" ~ pattern)* }
match_item = { match_item = {
pattern ~ "=>" ~ block pattern_list ~ "=>" ~ block
} }
match_stmt = { match_stmt = {
+1 -1
View File
@@ -100,7 +100,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
.map(|match_itms| { .map(|match_itms| {
let mut match_inner = match_itms.into_inner(); let mut match_inner = match_itms.into_inner();
Ok(( Ok((
Pattern::try_from(match_inner.next().unwrap()).get()?, collect_recovered(match_inner.next().unwrap().into_inner()).get()?,
Block::try_from(match_inner.next().unwrap()).get()?, Block::try_from(match_inner.next().unwrap()).get()?,
)) ))
}) })
+8 -1
View File
@@ -646,7 +646,14 @@ impl ToRust for Statement {
cg.indent += 1; cg.indent += 1;
for itm in match_items { for itm in match_items {
cg.add_indentedln(&format!("{} =>", itm.0.get_rust())); cg.add_indentedln(&format!(
"{} =>",
itm.0
.into_iter()
.map(Pattern::get_rust)
.collect::<Vec<_>>()
.join(" | ")
));
cg.add_indentedln("{"); cg.add_indentedln("{");
cg.indent += 1; cg.indent += 1;
itm.1.to_rust(cg); itm.1.to_rust(cg);