View Source Sourceror.Zipper (Sourceror v1.0.3)

Tree-like data structure that provides enhanced navigation and modification of an Elixir AST.

This implementation is based on Gérard Huet Functional pearl: the zipper and Clojure's clojure.zip API.

A zipper is a data structure that represents a location in a tree from the perspective of the current node, also called the focus.

It is represented by a struct containing a :node and :path, which is a private field used to track the position of the :node with regards to the entire tree. The :path is an implementation detail that should be considered private.

For more information and examples, see the following guides:

Summary

Functions

Inserts the item as the rightmost child of the node at this zipper, without moving.

Returns a list of children of the node.

Returns the zipper of the leftmost child of the node at this zipper, or nil if no there's no children.

Returns a zipper to the node that satisfies the predicate function, or nil if none is found.

Inserts the item as the leftmost child of the node at this zipper, without moving.

Inserts the item as the left sibling of the node at this zipper, without moving. Raises an ArgumentError when attempting to insert a sibling at the top level.

Inserts the item as the right sibling of the node at this zipper, without moving. Raises an ArgumentError when attempting to insert a sibling at the top level.

Returns the zipper of the left sibling of the node at this zipper, or nil.

Returns the leftmost sibling of the node at this zipper, or itself.

Returns a new branch node, given an existing node and new children.

Returns the following zipper in depth-first pre-order.

Returns the node at the zipper.

Returns the previous zipper in depth-first pre-order. If it's already at the end, it returns nil.

Removes the node at the zipper, returning the zipper that would have preceded it in a depth-first walk.

Replaces the current node in the zipper with a new node.

Returns the zipper of the right sibling of the node at this zipper, or nil.

Returns the rightmost sibling of the node at this zipper, or itself.

Walks the zipper all the way up and returns the root node.

Returns the zipper of the right sibling of the node at this zipper, or the next zipper when no right sibling is available.

Walks the zipper all the way up and returns the top zipper.

Traverses the tree in depth-first pre-order calling the given function for each node. When the traversal is finished, the zipper will be back where it began.

Traverses the tree in depth-first pre-order calling the given function for each node with an accumulator. When the traversal is finished, the zipper will be back where it began.

Traverses the tree in depth-first pre-order calling the given function for each node.

Traverses the tree in depth-first pre-order calling the given function for each node with an accumulator. When the traversal is finished, the zipper will be back where it began.

Returns the zipper of the parent of the node at this zipper, or nil if at the top.

Replaces the current node in the zipper with the result of applying fun to the node.

Creates a zipper from a tree node.

Types

@opaque path()
@type t() :: %Sourceror.Zipper{node: tree(), path: path() | nil}
@type tree() :: Macro.t()

Functions

Link to this function

append_child(zipper, child)

View Source

Inserts the item as the rightmost child of the node at this zipper, without moving.

@spec branch?(tree()) :: boolean()
@spec children(tree()) :: [tree()]

Returns a list of children of the node.

@spec down(t()) :: t() | nil

Returns the zipper of the leftmost child of the node at this zipper, or nil if no there's no children.

Link to this function

find(zipper, direction \\ :next, predicate)

View Source
@spec find(t(), direction :: :prev | :next, predicate :: (tree() -> any())) ::
  t() | nil

Returns a zipper to the node that satisfies the predicate function, or nil if none is found.

The optional second parameters specifies the direction, defaults to :next.

Link to this function

insert_child(zipper, child)

View Source

Inserts the item as the leftmost child of the node at this zipper, without moving.

Link to this function

insert_left(zipper, child)

View Source
@spec insert_left(t(), tree()) :: t()

Inserts the item as the left sibling of the node at this zipper, without moving. Raises an ArgumentError when attempting to insert a sibling at the top level.

Link to this function

insert_right(zipper, child)

View Source
@spec insert_right(t(), tree()) :: t()

Inserts the item as the right sibling of the node at this zipper, without moving. Raises an ArgumentError when attempting to insert a sibling at the top level.

@spec left(t()) :: t() | nil

Returns the zipper of the left sibling of the node at this zipper, or nil.

@spec leftmost(t()) :: t()

Returns the leftmost sibling of the node at this zipper, or itself.

@spec make_node(tree(), [tree()]) :: tree()

Returns a new branch node, given an existing node and new children.

Returns the following zipper in depth-first pre-order.

@spec node(t()) :: tree()

Returns the node at the zipper.

@spec prev(t()) :: t()

Returns the previous zipper in depth-first pre-order. If it's already at the end, it returns nil.

@spec remove(t()) :: t()

Removes the node at the zipper, returning the zipper that would have preceded it in a depth-first walk.

@spec replace(t(), tree()) :: t()

Replaces the current node in the zipper with a new node.

@spec right(t()) :: t() | nil

Returns the zipper of the right sibling of the node at this zipper, or nil.

@spec rightmost(t()) :: t()

Returns the rightmost sibling of the node at this zipper, or itself.

@spec root(t()) :: tree()

Walks the zipper all the way up and returns the root node.

Link to this function

skip(zipper, direction \\ :next)

View Source
@spec skip(t(), direction :: :next | :prev) :: t() | nil

Returns the zipper of the right sibling of the node at this zipper, or the next zipper when no right sibling is available.

This allows to skip subtrees while traversing the siblings of a node.

The optional second parameters specifies the direction, defaults to :next.

If no right/left sibling is available, this function returns the same value as next/1/prev/1.

The function skip/1 behaves like the :skip in traverse_while/2 and traverse_while/3.

@spec top(t()) :: t()

Walks the zipper all the way up and returns the top zipper.

@spec traverse(t(), (t() -> t())) :: t()

Traverses the tree in depth-first pre-order calling the given function for each node. When the traversal is finished, the zipper will be back where it began.

If the zipper is not at the top, just the subtree will be traversed.

The function must return a zipper.

Link to this function

traverse(zipper, acc, fun)

View Source
@spec traverse(t(), term(), (t(), term() -> {t(), term()})) :: {t(), term()}

Traverses the tree in depth-first pre-order calling the given function for each node with an accumulator. When the traversal is finished, the zipper will be back where it began.

If the zipper is not at the top, just the subtree will be traversed.

Link to this function

traverse_while(zipper, fun)

View Source
@spec traverse_while(t(), (t() -> {:cont, t()} | {:halt, t()} | {:skip, t()})) :: t()

Traverses the tree in depth-first pre-order calling the given function for each node.

The traversing will continue if the function returns {:cont, zipper}, skipped for {:skip, zipper} and halted for {:halt, zipper}. When the traversal is finished, the zipper will be back where it began.

If the zipper is not at the top, just the subtree will be traversed.

The function must return a zipper.

Link to this function

traverse_while(zipper, acc, fun)

View Source
@spec traverse_while(
  t(),
  term(),
  (t(), term() ->
     {:cont, t(), term()} | {:halt, t(), term()} | {:skip, t(), term()})
) :: {t(), term()}

Traverses the tree in depth-first pre-order calling the given function for each node with an accumulator. When the traversal is finished, the zipper will be back where it began.

The traversing will continue if the function returns {:cont, zipper, acc}, skipped for {:skip, zipper, acc} and halted for {:halt, zipper, acc}

If the zipper is not at the top, just the subtree will be traversed.

@spec up(t()) :: t() | nil

Returns the zipper of the parent of the node at this zipper, or nil if at the top.

@spec update(t(), (tree() -> tree())) :: t()

Replaces the current node in the zipper with the result of applying fun to the node.

@spec zip(tree()) :: t()

Creates a zipper from a tree node.