fn match_literal()

in crates/concrete-syntax/src/models/concrete_syntax/interpreter.rs [199:231]


fn match_literal(
  ctx: &mut MatchingContext<'_>, literal_text: &str, remaining_elements: &[ResolvedCsElement],
) -> PatternMatchResult {
  // We match literals against leaves
  while ctx.cursor.node().child_count() != 0 {
    ctx.cursor.goto_first_child();
  }

  let node_code = ctx.cursor.node().utf8_text(ctx.source_code).unwrap().trim();

  if literal_text.starts_with(node_code) && !node_code.is_empty() {
    let advance_by = node_code.len();
    // Can only advance if there is still enough chars to consume
    if advance_by > literal_text.len() {
      return PatternMatchResult::failed();
    }

    let should_match = CursorNavigator::find_next_sibling_or_ancestor_sibling(&mut ctx.cursor);

    // If we consumed the entire literal, continue with remaining elements
    return if advance_by == literal_text.len() {
      match_cs_pattern(ctx, remaining_elements, should_match)
    } else {
      // If we only consumed part of the literal, create a new literal with the remaining text
      let remaining_literal = &literal_text[advance_by..];
      let mut new_elements = vec![ResolvedCsElement::Literal(remaining_literal.to_string())];
      new_elements.extend_from_slice(remaining_elements);
      match_cs_pattern(ctx, &new_elements, should_match)
    };
  }

  PatternMatchResult::failed()
}