Skip to content

Commit c7bdf6f

Browse files
committed
wip: f me up
1 parent 5ae27cc commit c7bdf6f

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

xparser/src/constructs.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,18 +763,26 @@ pub fn block(input: ParseInput) -> ParseResult<ParseInput, Ast> {
763763
let input = next(input);
764764
let (input, start_loc) = position(input)?;
765765

766-
if let Ok((input, _)) = tokens::if_tok(input) {
767-
unit_if(input, start_loc.into())
766+
let (input, stmt) = if let Ok((input, _)) = tokens::if_tok(input) {
767+
unit_if(input, start_loc.into())?
768768
} else if let Ok((input, kind)) =
769769
alt((tokens::func_tok, tokens::test_tok, tokens::mock_tok))(input)
770770
{
771-
unit_func(input, kind, start_loc.into())
771+
unit_func(input, kind, start_loc.into())?
772772
} else if tokens::left_curly_bracket(input).is_ok() {
773+
// TODO: This does not work to have a block as the last expression of another block
774+
// `stmt` will parse it and return it as a statement
775+
// we need to do something smarted like lifting the last statement to an expression if there was no semicolon
776+
773777
// we don't update `input` here so that we can just call `block`
774-
block(input)
778+
block(input)?
775779
} else {
776-
expr_semicolon(input)
777-
}
780+
expr_semicolon(input)?
781+
};
782+
783+
let (input, _) = opt(tokens::semicolon)(input)?;
784+
785+
Ok((input, stmt))
778786
};
779787

780788
let (input, stmts) = opt(many1(stmt))(input)?;
@@ -2329,4 +2337,37 @@ mod tests {
23292337

23302338
assert!(expr(input).is_ok());
23312339
}
2340+
2341+
#[test]
2342+
fn early_return395() {
2343+
let input = span!("func halloween(b: bool) -> int { if b { return 14 }; 15 }");
2344+
2345+
assert!(expr(input).is_ok());
2346+
}
2347+
2348+
#[test]
2349+
fn inner_block_with_semi395() {
2350+
let input = span!(
2351+
"func halloween() -> int { func inner() -> int { { { { { return 14; } } } } }; 15 }"
2352+
);
2353+
2354+
assert!(expr(input).is_ok())
2355+
}
2356+
2357+
#[test]
2358+
fn inner_nested_function395() {
2359+
let input = span! {r#"
2360+
func halloween() -> string {
2361+
func inner() -> string {
2362+
{ { { {
2363+
return "boo";
2364+
} } } }
2365+
};
2366+
2367+
"different string"
2368+
}
2369+
"#};
2370+
2371+
assert!(expr(input).is_ok())
2372+
}
23322373
}

0 commit comments

Comments
 (0)