GroteTrap-0.5.1: Parser and selection library for expression languages.

Safe HaskellSafe-Inferred
LanguageHaskell98

Language.GroteTrap.Trees

Contents

Description

A class for tree types and representations of selections on tree types, as well as functions for converting between text and tree selections.

Synopsis

Paths and navigation

type Path = [Int] Source

A path in a tree. Each integer denotes the selection of a child; these indices are 0-relative.

root :: Path Source

root is the empty path.

type Nav = Path -> Path Source

Navigation transforms one path to another.

up :: Nav Source

Move up to parent node. Moving up from root has no effect.

into :: Int -> Nav Source

Move down into the nth child node. If n is negative, the leftmost child is selected.

down :: Nav Source

Move down into first child node.

left :: Nav Source

Move left one sibling.

right :: Nav Source

Move right one sibling.

sibling :: Int -> Nav Source

Move n siblings to the right. n can be negative. If the new child index becomes negative, the leftmost child is selected.

Tree types

class Tree p where Source

Tree types.

Methods

children :: p -> [p] Source

Yields this tree's subtrees.

Instances

depth :: Tree t => t -> Int Source

Yields the depth of the tree.

selectDepth :: Tree t => Int -> t -> [t] Source

Yields all ancestors at the specified depth.

flatten :: Tree t => t -> [t] Source

Pre-order depth-first traversal.

follow :: (Monad m, Tree t) => t -> Path -> m t Source

Follows a path in a tree, returning the result in a monad.

child :: (Monad m, Tree t) => t -> Int -> m t Source

Moves down into a child.

Tree selections

class Tree t => Selectable t where Source

Selectable trees.

Methods

allowSubranges :: t -> Bool Source

Tells whether complete subranges of children may be selected in this tree node. If not, valid TreeSelections in this tree always have a second element 0.

type TreeSelection = (Path, Int) Source

Selection in a tree. The path indicates the left side of the selection; the int tells how many siblings to the right are included in the selection.

select :: (Monad m, Tree t) => t -> TreeSelection -> m [t] Source

Selects part of a tree.

allSelections :: Selectable a => a -> [TreeSelection] Source

Enumerates all possible selections of a tree.

selectionToRange :: (Monad m, Tree a, Ranged a) => a -> TreeSelection -> m Range Source

Computes the range of a valid selection.

rangeToSelection :: (Monad m, Selectable a, Ranged a) => a -> Range -> m TreeSelection Source

Converts a specified range to a corresponding selection and returns it in a monad.

posToPath :: (Monad m, Tree a, Ranged a) => a -> Pos -> m Path Source

Returns the path to the deepest descendant whose range contains the specified position.

isValidRange :: (Ranged a, Selectable a) => a -> Range -> Bool Source

Tells whether the text selection corresponds to a tree selection.

Suggesting and fixing

suggestBy :: (Selectable a, Ranged a) => (Range -> Range -> Int) -> a -> Range -> [TreeSelection] Source

Yields all possible selections, ordered by distance to the specified range, closest first.

suggest :: (Selectable a, Ranged a) => a -> Range -> [TreeSelection] Source

suggest uses distRange as cost function.

repairBy :: (Ranged a, Selectable a) => (Range -> Range -> Int) -> a -> Range -> Range Source

Takes suggestBy's first suggestion and yields its range.

repair :: (Ranged a, Selectable a) => a -> Range -> Range Source

repair uses distRange as cost function.