SyntaxTree

public enum SyntaxTree<Element, LeafElement>
extension SyntaxTree: CustomDebugStringConvertible
extension SyntaxTree: CustomStringConvertible

A tree which can store different types of values in its leafs

  • leaf: A leaf
  • node: A node with a key and an arbitrary list of elements
  • A leaf storing a leaf element

    Declaration

    Swift

    case leaf(LeafElement)
  • A node with a key and an arbitrary list of elements

    Declaration

    Swift

    indirect case node(key: Element, children: [SyntaxTree<Element, LeafElement>])
  • Generates a new syntax tree by applying the transform function to every key of the tree

    Declaration

    Swift

    func map<Result>(_ transform: (Element) throws -> Result) rethrows -> SyntaxTree<Result, LeafElement>

    Parameters

    transform

    Transform function

    Return Value

    A tree generated by applying the transform function to every key

  • Generates a new syntax tree by applying the transform function to every leaf of the tree

    Declaration

    Swift

    func mapLeafs<Result>(_ transform: (LeafElement) throws -> Result) rethrows -> SyntaxTree<Element, Result>

    Parameters

    transform

    Transform function

    Return Value

    A tree generated by applying the transform function to every leaf value

  • All leafs of the tree

    Declaration

    Swift

    var leafs: [LeafElement] { get }
  • Filters the tree by removing all nodes and their corresponding subtrees if the given predicate is false

    Declaration

    Swift

    func filter(_ predicate: (Element) throws -> Bool) rethrows -> SyntaxTree<Element, LeafElement>?

    Parameters

    predicate

    Predicate to filter the tree

    Return Value

    A tree generated by filtering out nodes for which the predicate returned false

  • Explodes nodes and passes all child nodes to the parent node if the given closure returns true

    Declaration

    Swift

    func explode(_ shouldExplode: (Element) throws -> Bool) rethrows -> [SyntaxTree<Element, LeafElement>]

    Parameters

    shouldExplode

    Determines if a node should be exploded

    Return Value

    A tree generated by exploding nodes determined by the given predicate

  • Compresses the tree by exploding nodes which have exactly one child node

    Declaration

    Swift

    func compressed() -> SyntaxTree<Element, LeafElement>

    Return Value

    Tree generated by compressing the current tree

  • Returns all nodes which match the given predicate.

    Declaration

    Swift

    func allNodes(where predicate: (Element) throws -> Bool) rethrows -> [SyntaxTree<Element, LeafElement>]

    Parameters

    predicate

    Predicate to match

    Return Value

    A collection of nodes which match the given predicate

  • Declaration

    Swift

    public var debugDescription: String { get }
  • Declaration

    Swift

    public var description: String { get }
  • Creates a new syntax tree node with a given key and a list of children

    Declaration

    Swift

    init(key: Element, children: [SyntaxTree<Element, LeafElement>])

    Parameters

    key

    Root key

    children

    Children of the root node

  • Creates a new syntax tree with a given root key and no children

    Declaration

    Swift

    init(key: Element)

    Parameters

    key

    Root key

  • Creates a new syntax tree with a given leaf value

    Declaration

    Swift

    init(value: LeafElement)

    Parameters

    value

    Leaf value

  • Returns the root key of the tree or nil if no root key exists

    Declaration

    Swift

    var root: Element? { get }
  • Returns the value stored in the current node if the current node is a leaf. Otherwise, nil is returned

    Declaration

    Swift

    var leaf: LeafElement? { get }
  • Returns the direct children of the root node

    Declaration

    Swift

    var children: [SyntaxTree<Element, LeafElement>]? { get }

Available where LeafElement == ()

  • Creates an empty tree

    Declaration

    Swift

    init()