Class ParseTreeUtils


  • public final class ParseTreeUtils
    extends Object
    General utility methods for operating on parse trees.
    • Method Detail

      • findNodeByPath

        public static <V> Node<V> findNodeByPath​(Node<V> parent,
                                                 String path)

        Returns the parse tree node underneath the given parent that matches the given path.

        The path is a '/' separated list of node label prefixes describing the ancestor chain of the node to look for relative to the given parent node. If there are several nodes that match the given path the method returns the first one unless the respective path segments has the special prefix "last:". In this case the last matching node is returned.

        Example: "per/last:so/fix" will return the first node, whose label starts with "fix" under the last node, whose label starts with "so" under the first node, whose label starts with "per".

        If parent is null or no node is found the method returns null.
        Parameters:
        parent - the parent Node
        path - the path to the Node being searched for
        Returns:
        the Node if found or null if not found
      • findNodeByPath

        public static <V> Node<V> findNodeByPath​(List<Node<V>> parents,
                                                 String path)
        Returns the node underneath the given parents that matches the given path. See findNodeByPath(org.parboiled.Node, String) )} for a description of the path argument. If the given collections of parents is null or empty or no node is found the method returns null.
        Parameters:
        parents - the parent Nodes to look through
        path - the path to the Node being searched for
        Returns:
        the Node if found or null if not found
      • collectNodesByPath

        public static <V,​C extends Collection<Node<V>>> C collectNodesByPath​(Node<V> parent,
                                                                                   String path,
                                                                                   C collection)
        Collects all nodes underneath the given parent that match the given path. The path is a '/' separated list of node label prefixes describing the ancestor chain of the node to look for relative to the given parent node.
        Parameters:
        parent - the parent Node
        path - the path to the Nodes being searched for
        collection - the collection to collect the found Nodes into
        Returns:
        the same collection instance passed as a parameter
      • collectNodesByPath

        public static <V,​C extends Collection<Node<V>>> C collectNodesByPath​(List<Node<V>> parents,
                                                                                   String path,
                                                                                   C collection)
        Collects all nodes underneath the given parents that match the given path. The path is a '/' separated list of node label prefixes describing the ancestor chain of the node to look for relative to the given parent nodes.
        Parameters:
        parents - the parent Nodes to look through
        path - the path to the Nodes being searched for
        collection - the collection to collect the found Nodes into
        Returns:
        the same collection instance passed as a parameter
      • findNode

        public static <V> Node<V> findNode​(Node<V> parent,
                                           Predicate<Node<V>> predicate)
        Returns the first node underneath the given parent for which the given predicate evaluates to true. If parent is null or no node is found the method returns null.
        Parameters:
        parent - the parent Node
        predicate - the predicate
        Returns:
        the Node if found or null if not found
      • findNode

        public static <V> Node<V> findNode​(List<Node<V>> parents,
                                           Predicate<Node<V>> predicate)
        Returns the first node underneath the given parents for which the given predicate evaluates to true. If parents is null or empty or no node is found the method returns null.
        Parameters:
        parents - the parent Nodes to look through
        predicate - the predicate
        Returns:
        the Node if found or null if not found
      • findNodeByLabel

        public static <V> Node<V> findNodeByLabel​(Node<V> parent,
                                                  String labelPrefix)
        Returns the first node underneath the given parent for which matches the given label prefix. If parents is null or empty or no node is found the method returns null.
        Parameters:
        parent - the parent node
        labelPrefix - the label prefix to look for
        Returns:
        the Node if found or null if not found
      • findNodeByLabel

        public static <V> Node<V> findNodeByLabel​(List<Node<V>> parents,
                                                  String labelPrefix)
        Returns the first node underneath the given parents which matches the given label prefix. If parents is null or empty or no node is found the method returns null.
        Parameters:
        parents - the parent Nodes to look through
        labelPrefix - the label prefix to look for
        Returns:
        the Node if found or null if not found
      • findLastNode

        public static <V> Node<V> findLastNode​(Node<V> parent,
                                               Predicate<Node<V>> predicate)
        Returns the last node underneath the given parent for which the given predicate evaluates to true. If parent is null or no node is found the method returns null.
        Parameters:
        parent - the parent Node
        predicate - the predicate
        Returns:
        the Node if found or null if not found
      • findLastNode

        public static <V> Node<V> findLastNode​(List<Node<V>> parents,
                                               Predicate<Node<V>> predicate)
        Returns the last node underneath the given parents for which the given predicate evaluates to true. If parents is null or empty or no node is found the method returns null.
        Parameters:
        parents - the parent Nodes to look through
        predicate - the predicate
        Returns:
        the Node if found or null if not found
      • collectNodes

        public static <V,​C extends Collection<Node<V>>> C collectNodes​(Node<V> parent,
                                                                             Predicate<Node<V>> predicate,
                                                                             C collection)
        Collects all nodes underneath the given parent for which the given predicate evaluates to true.
        Parameters:
        parent - the parent Node
        predicate - the predicate
        collection - the collection to collect the found Nodes into
        Returns:
        the same collection instance passed as a parameter
      • getNodeText

        public static String getNodeText​(Node<?> node,
                                         InputBuffer inputBuffer)
        Returns the input text matched by the given node, with error correction.
        Parameters:
        node - the node
        inputBuffer - the underlying inputBuffer
        Returns:
        null if node is null otherwise a string with the matched input text (which can be empty)
      • collectNodes

        public static <V,​C extends Collection<Node<V>>> C collectNodes​(List<Node<V>> parents,
                                                                             Predicate<Node<V>> predicate,
                                                                             C collection)
        Collects all nodes underneath the given parents for which the given predicate evaluates to true.
        Parameters:
        parents - the parent Nodes to look through
        predicate - the predicate
        collection - the collection to collect the found Nodes into
        Returns:
        the same collection instance passed as a parameter
      • printNodeTree

        public static <V> String printNodeTree​(ParsingResult<V> parsingResult)
        Creates a readable string represenation of the parse tree in the given ParsingResult object.
        Parameters:
        parsingResult - the parsing result containing the parse tree
        Returns:
        a new String
      • printNodeTree

        public static <V> String printNodeTree​(ParsingResult<V> parsingResult,
                                               Predicate<Node<V>> nodeFilter,
                                               Predicate<Node<V>> subTreeFilter)
        Creates a readable string represenation of the parse tree in thee given ParsingResult object. The given filter predicate determines whether a particular node (incl. its subtree) is printed or not.
        Parameters:
        parsingResult - the parsing result containing the parse tree
        nodeFilter - the predicate selecting the nodes to print
        subTreeFilter - the predicate determining whether to descend into a given nodes subtree or not
        Returns:
        a new String