fn match_sequential_siblings()

in src/models/concrete_syntax.rs [123:156]


fn match_sequential_siblings(
  cursor: &mut TreeCursor, source_code: &[u8], cs: &ConcreteSyntax,
) -> Option<MatchResult> {
  let parent_node = cursor.node();
  let mut child_seq_match_start = 0;

  if cursor.goto_first_child() {
    // Iterate through siblings to find a match
    loop {
      // Clone the cursor in order to attempt matching the sequence starting at cursor.node
      // Cloning here is necessary other we won't be able to advance to the next sibling if the matching fails
      let mut tmp_cursor = cursor.clone();
      let (mapping, indx) =
        get_matches_for_subsequence_of_nodes(&mut tmp_cursor, source_code, cs, true, &parent_node);

      // If we got the index of the last matched sibling, that means the matching was successful.
      if let Some(last_node_index) = indx {
        // Determine the last matched node. Remember, we are matching subsequences of children [n ... k]
        let last_node = parent_node.child(last_node_index);
        let range = Range::span_ranges(cursor.node().range(), last_node.unwrap().range());
        if last_node_index != child_seq_match_start || parent_node.child_count() == 1 {
          return Some(MatchResult { mapping, range });
        }
        return None;
      }

      child_seq_match_start += 1;
      if !cursor.goto_next_sibling() {
        break;
      }
    }
  } // Not currently handing matching of leaf nodes. Current semantics would never match it anyway.
  None
}