basic parsing of alternative cases in when .. in

This commit is contained in:
Stoeffel 2020-01-08 08:53:48 +01:00
parent 3a1d905b66
commit 505effd1d9
2 changed files with 14 additions and 7 deletions

View file

@ -889,6 +889,11 @@ pub fn case_branches<'a>(
space1_before(loc!(pattern(min_indent)), min_indent).parse(arena, state)?; space1_before(loc!(pattern(min_indent)), min_indent).parse(arena, state)?;
let original_indent = state.indent_col; let original_indent = state.indent_col;
let indented_more = original_indent + 1; let indented_more = original_indent + 1;
let (loc_first_pattern_alt, state) = zero_or_more!(skip_first!(
and!(space1(min_indent), char('|')),
space1_before(loc!(pattern(min_indent)), min_indent)
))
.parse(arena, state)?;
let (spaces_before_arrow, state) = space0(min_indent).parse(arena, state)?; let (spaces_before_arrow, state) = space0(min_indent).parse(arena, state)?;
// Record the spaces before the first "->", if any. // Record the spaces before the first "->", if any.
@ -912,10 +917,7 @@ pub fn case_branches<'a>(
.parse(arena, state)?; .parse(arena, state)?;
// Record this as the first branch, then optionally parse additional branches. // Record this as the first branch, then optionally parse additional branches.
branches.push(arena.alloc(( branches.push(arena.alloc(((loc_first_pattern, loc_first_pattern_alt), loc_first_expr)));
(loc_first_pattern, Vec::with_capacity_in(0, arena)),
loc_first_expr,
)));
let branch_parser = and!( let branch_parser = and!(
then( then(

View file

@ -1361,10 +1361,15 @@ mod test_parse {
let newlines = bumpalo::vec![in &arena; Newline]; let newlines = bumpalo::vec![in &arena; Newline];
let pattern1 = let pattern1 =
Pattern::SpaceBefore(arena.alloc(StrLiteral("blah")), newlines.into_bump_slice()); Pattern::SpaceBefore(arena.alloc(StrLiteral("blah")), newlines.into_bump_slice());
let pattern1_alt = StrLiteral("blop"); // TODO retain spacebefore
let loc_pattern1 = Located::new(1, 1, 1, 7, pattern1); let loc_pattern1 = Located::new(1, 1, 1, 7, pattern1);
let loc_pattern1_alt = Located::new(1, 1, 10, 16, pattern1_alt);
let expr1 = Int("1"); let expr1 = Int("1");
let loc_expr1 = Located::new(1, 1, 11, 12, expr1); let loc_expr1 = Located::new(1, 1, 20, 21, expr1);
let branch1 = &*arena.alloc(((loc_pattern1, bumpalo::vec![in &arena;]), loc_expr1)); let branch1 = &*arena.alloc((
(loc_pattern1, bumpalo::vec![in &arena;loc_pattern1_alt]),
loc_expr1,
));
let newlines = bumpalo::vec![in &arena; Newline]; let newlines = bumpalo::vec![in &arena; Newline];
let pattern2 = let pattern2 =
Pattern::SpaceBefore(arena.alloc(StrLiteral("mise")), newlines.into_bump_slice()); Pattern::SpaceBefore(arena.alloc(StrLiteral("mise")), newlines.into_bump_slice());
@ -1380,7 +1385,7 @@ mod test_parse {
indoc!( indoc!(
r#" r#"
when x is when x is
"blah" -> 1 "blah" | "blop" -> 1
"mise" -> 2 "mise" -> 2
"# "#
), ),