greenwood/zipper

Zipper (cursor) operations for Greenwood syntax trees.

This module provides all navigation, mutation, and query operations for the Zipper type. The zipper implements a Huet zipper providing a focused view into the tree with O(1) local moves and edits.

Values

pub fn delete(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Delete the focused node. Focus shifts to:

  1. Right sibling (if exists)
  2. Left sibling (if exists)
  3. Parent with focus removed from children

Returns None if the focus is the root.

cursor

pub fn down(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Move focus to the first child that is a Node.

cursor

pub fn down_last(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Move focus to the last child that is a Node.

cursor

pub fn down_last_where(
  zipper zipper: greenwood.Zipper(kind),
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus to the last child Node matching a predicate.

cursor

pub fn down_where(
  zipper zipper: greenwood.Zipper(kind),
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus to the first child Node matching a predicate.

cursor

pub fn find_descendant(
  zipper zipper: greenwood.Zipper(kind),
  where predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Depth-first search below the focus for a descendant matching a predicate. Returns a zipper focused on the match, or None.

cursor

pub fn insert_down(
  zipper zipper: greenwood.Zipper(kind),
  child child: greenwood.Node(kind),
) -> greenwood.Zipper(kind)

Insert a child as the first child of the focus and move focus to it. Returns None if the focus has no children list (impossible for Node, but kept as Option for API consistency).

cursor

pub fn insert_left(
  zipper zipper: greenwood.Zipper(kind),
  element element: greenwood.Element(kind),
) -> option.Option(greenwood.Zipper(kind))

Insert an element to the left of the focus. Focus does not move. Returns None if the focus is the root (no parent to hold the sibling).

cursor

pub fn insert_right(
  zipper zipper: greenwood.Zipper(kind),
  element element: greenwood.Element(kind),
) -> option.Option(greenwood.Zipper(kind))

Insert an element to the right of the focus. Focus does not move. Returns None if the focus is the root.

cursor

pub fn left(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Move focus to the nearest sibling Node to the left.

cursor

pub fn left_n(
  zipper zipper: greenwood.Zipper(kind),
  by n: Int,
) -> option.Option(greenwood.Zipper(kind))

Move focus n sibling Nodes to the left. Negative n flips direction.

cursor

pub fn left_n_until(
  zipper zipper: greenwood.Zipper(kind),
  n n: Int,
  target target: fn(greenwood.Node(kind)) -> Bool,
  stop stop: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Scan n target-matching left siblings, stopping early if stop fires. left_n_until(zipper:, n: 0, ..) returns Some(zipper).

cursor

pub fn left_n_where(
  zipper zipper: greenwood.Zipper(kind),
  by n: Int,
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus n sibling Nodes to the left matching a predicate. Negative n flips direction.

cursor

pub fn left_until(
  zipper zipper: greenwood.Zipper(kind),
  target target: fn(greenwood.Node(kind)) -> Bool,
  stop stop: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Scan left siblings for target, aborting early if stop matches first.

cursor

pub fn left_where(
  zipper zipper: greenwood.Zipper(kind),
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus to the nearest sibling Node to the left matching a predicate.

cursor

pub fn map_focus(
  zipper zipper: greenwood.Zipper(kind),
  with f: fn(greenwood.Node(kind)) -> greenwood.Node(kind),
) -> greenwood.Zipper(kind)

Apply a transform to the focused node.

cursor

pub fn nth_child(
  zipper zipper: greenwood.Zipper(kind),
  n n: Int,
) -> option.Option(greenwood.Zipper(kind))

Descend to the nth child Node (1-indexed). Returns None if fewer than n Node children exist.

cursor

pub fn right(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Move focus to the nearest sibling Node to the right.

cursor

pub fn right_n(
  zipper zipper: greenwood.Zipper(kind),
  by n: Int,
) -> option.Option(greenwood.Zipper(kind))

Move focus n sibling Nodes to the right. Negative n flips direction.

cursor

pub fn right_n_until(
  zipper zipper: greenwood.Zipper(kind),
  n n: Int,
  target target: fn(greenwood.Node(kind)) -> Bool,
  stop stop: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Scan n target-matching right siblings, stopping early if stop fires. right_n_until(zipper:, n: 0, ..) returns Some(zipper).

cursor

pub fn right_n_where(
  zipper zipper: greenwood.Zipper(kind),
  by n: Int,
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus n sibling Nodes to the right matching a predicate. Negative n flips direction.

cursor

pub fn right_until(
  zipper zipper: greenwood.Zipper(kind),
  target target: fn(greenwood.Node(kind)) -> Bool,
  stop stop: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Scan right siblings for target, aborting early if stop matches first. Returns None if siblings are exhausted or stop fires.

cursor

pub fn right_where(
  zipper zipper: greenwood.Zipper(kind),
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Move focus to the nearest sibling Node to the right matching a predicate.

cursor

pub fn set_focus(
  zipper zipper: greenwood.Zipper(kind),
  node node: greenwood.Node(kind),
) -> greenwood.Zipper(kind)

Replace the focused node.

cursor

pub fn unzip(
  zipper: greenwood.Zipper(kind),
) -> greenwood.Node(kind)

Reconstruct the full tree from a zipper by moving up to the root.

cursor

pub fn up(
  zipper: greenwood.Zipper(kind),
) -> option.Option(greenwood.Zipper(kind))

Move focus back up to the parent.

cursor

pub fn up_n(
  zipper zipper: greenwood.Zipper(kind),
  by n: Int,
) -> option.Option(greenwood.Zipper(kind))

Move focus up n parents. Returns None if n is negative or would move above the root.

up_n(z, by: 0) returns Some(z).

cursor

pub fn up_until(
  zipper zipper: greenwood.Zipper(kind),
  predicate predicate: fn(greenwood.Node(kind)) -> Bool,
) -> option.Option(greenwood.Zipper(kind))

Ascend until the focused node’s parent matches a predicate. Returns the zipper focused on the first ancestor matching predicate, or None if the root is reached without a match.

cursor

pub fn zip(root: greenwood.Node(kind)) -> greenwood.Zipper(kind)

Create a zipper focused on the root node.

cursor

Search Document