MCPcopy Create free account
hub / github.com/AmrDeveloper/GQL / parse_case_expression

Function parse_case_expression

crates/gitql-parser/src/parser.rs:3552–3664  ·  view source on GitHub ↗
(
    context: &mut ParserContext,
    env: &mut Environment,
    tokens: &[Token],
    position: &mut usize,
)

Source from the content-addressed store, hash-verified

3550}
3551
3552fn 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,

Callers 1

parse_primary_expressionFunction · 0.85

Calls 11

calculate_safe_locationFunction · 0.85
parse_expressionFunction · 0.85
consume_token_or_errorFunction · 0.85
as_boxedMethod · 0.80
with_locationMethod · 0.80
add_noteMethod · 0.80
expr_typeMethod · 0.80
lenMethod · 0.45
is_boolMethod · 0.45
is_emptyMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…