(
context: &mut ParserContext,
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 3550 | } |
| 3551 | |
| 3552 | fn parse_case_expression( |
| 3553 | context: &mut ParserContext, |
| 3554 | env: &mut Environment, |
| 3555 | tokens: &[Token], |
| 3556 | position: &mut usize, |
| 3557 | ) -> Result<Box<dyn Expr>, Box<Diagnostic>> { |
| 3558 | let mut conditions: Vec<Box<dyn Expr>> = vec![]; |
| 3559 | let mut values: Vec<Box<dyn Expr>> = vec![]; |
| 3560 | let mut default_value: Option<Box<dyn Expr>> = None; |
| 3561 | |
| 3562 | // Consume `case` keyword |
| 3563 | let case_location = tokens[*position].location; |
| 3564 | *position += 1; |
| 3565 | |
| 3566 | let mut has_else_branch = false; |
| 3567 | |
| 3568 | while *position < tokens.len() && tokens[*position].kind != TokenKind::End { |
| 3569 | // Else branch |
| 3570 | if tokens[*position].kind == TokenKind::Else { |
| 3571 | if has_else_branch { |
| 3572 | return Err( |
| 3573 | Diagnostic::error("This `CASE` expression already has else branch") |
| 3574 | .add_note("`CASE` expression can has only one `ELSE` branch") |
| 3575 | .with_location(calculate_safe_location(tokens, *position)) |
| 3576 | .as_boxed(), |
| 3577 | ); |
| 3578 | } |
| 3579 | |
| 3580 | // Consume `ELSE` keyword |
| 3581 | *position += 1; |
| 3582 | |
| 3583 | let default_value_expr = parse_expression(context, env, tokens, position)?; |
| 3584 | default_value = Some(default_value_expr); |
| 3585 | has_else_branch = true; |
| 3586 | continue; |
| 3587 | } |
| 3588 | |
| 3589 | // Consume `WHEN` keyword |
| 3590 | consume_token_or_error( |
| 3591 | tokens, |
| 3592 | position, |
| 3593 | TokenKind::When, |
| 3594 | "Expect `when` before case condition", |
| 3595 | )?; |
| 3596 | |
| 3597 | let condition = parse_expression(context, env, tokens, position)?; |
| 3598 | if !condition.expr_type().is_bool() { |
| 3599 | return Err(Diagnostic::error("Case condition must be a boolean type") |
| 3600 | .with_location(calculate_safe_location(tokens, *position)) |
| 3601 | .as_boxed()); |
| 3602 | } |
| 3603 | |
| 3604 | conditions.push(condition); |
| 3605 | |
| 3606 | // Consume `THEN` keyword |
| 3607 | consume_token_or_error( |
| 3608 | tokens, |
| 3609 | position, |
no test coverage detected
searching dependent graphs…