///|
/// An immutable cursor pointing at one `SyntaxNode` inside a green CST.
/// Every navigation method returns a new cursor without mutating `self`.
#derive(Eq, Debug)
struct SyntaxCursor {
  current : SyntaxNode
  /// Ancestor chain from root down to immediate parent: `(ancestor, child_index)`.
  ancestors : ReadOnlyArray[(SyntaxNode, Int32)]
}

///|
/// Creates a cursor positioned at the root of a CST.
pub fn SyntaxCursor::root(node : SyntaxNode) -> SyntaxCursor {
  { current: node, ancestors: ReadOnlyArray::from_array([]), }
}

///|
/// The `SyntaxNode` at this cursor position.
pub fn SyntaxCursor::node(self : SyntaxCursor) -> SyntaxNode {
  self.current
}

///|
/// The UTF-16 `SourceSpan` of the current node.
pub fn SyntaxCursor::span(self : SyntaxCursor) -> SourceSpan {
  self.current.span()
}

///|
/// Depth from the root (`0` at the root).
pub fn SyntaxCursor::depth(self : SyntaxCursor) -> Int32 {
  self.ancestors.length()
}

///|
/// Ancestor nodes ordered from the root down to the immediate parent.
pub fn SyntaxCursor::ancestors(
  self : SyntaxCursor,
) -> ReadOnlyArray[SyntaxNode] {
  let out = []
  for pair in self.ancestors {
    out.push(pair.0)
  }
  ReadOnlyArray::from_array(out)
}

///|
/// Moves to the parent node, or returns `None` at the root.
pub fn SyntaxCursor::parent(self : SyntaxCursor) -> SyntaxCursor? {
  let n = self.ancestors.length()
  if n == 0 {
    return None
  }
  let (parent_node, _) = self.ancestors[n - 1]
  let next_ancestors = []
  for i in 0..<(n - 1) {
    next_ancestors.push(self.ancestors[i])
  }
  Some({
    current: parent_node,
    ancestors: ReadOnlyArray::from_array(next_ancestors),
  })
}

///|
fn SyntaxCursor::descend(
  self : SyntaxCursor,
  child_idx : Int32,
  child_node : SyntaxNode,
) -> SyntaxCursor {
  let next_ancestors = []
  for pair in self.ancestors {
    next_ancestors.push(pair)
  }
  next_ancestors.push((self.current, child_idx))
  { current: child_node, ancestors: ReadOnlyArray::from_array(next_ancestors), }
}

///|
/// Moves to the first child `SyntaxNode`, skipping leaf tokens.
pub fn SyntaxCursor::first_child(self : SyntaxCursor) -> SyntaxCursor? {
  let children = self.current.children()
  for i in 0.. SyntaxCursor? {
  let children = self.current.children()
  let mut i = children.length() - 1
  while i >= 0 {
    if children[i] is Node(child) {
      return Some(self.descend(i, child))
    }
    i -= 1
  }
  None
}

///|
/// Moves to the `n`-th child `SyntaxNode` (0-based among node children).
pub fn SyntaxCursor::child_at(
  self : SyntaxCursor,
  index : Int32,
) -> SyntaxCursor? {
  if index < 0 {
    return None
  }
  let children = self.current.children()
  let mut seen = 0
  for i in 0.. SyntaxCursor? {
  let n = self.ancestors.length()
  if n == 0 {
    return None
  }
  let (parent_node, cur_idx) = self.ancestors[n - 1]
  let siblings = parent_node.children()
  for i in (cur_idx + 1).. SyntaxCursor? {
  let n = self.ancestors.length()
  if n == 0 {
    return None
  }
  let (parent_node, cur_idx) = self.ancestors[n - 1]
  let siblings = parent_node.children()
  let mut i = cur_idx - 1
  while i >= 0 {
    if siblings[i] is Node(sib) {
      let next_ancestors = []
      for j in 0..<(n - 1) {
        next_ancestors.push(self.ancestors[j])
      }
      next_ancestors.push((parent_node, i))
      return Some({
        current: sib,
        ancestors: ReadOnlyArray::from_array(next_ancestors),
      })
    }
    i -= 1
  }
  None
}