org.parboiled.support
Class ParseTreeUtils

java.lang.Object
  extended by org.parboiled.support.ParseTreeUtils

public final class ParseTreeUtils
extends java.lang.Object

General utility methods for operating on parse trees.


Method Summary
static
<V,C extends java.util.Collection<Node<V>>>
C
collectNodes(java.util.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.
static
<V,C extends java.util.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.
static
<V,C extends java.util.Collection<Node<V>>>
C
collectNodesByPath(java.util.List<Node<V>> parents, java.lang.String path, C collection)
          Collects all nodes underneath the given parents that match the given path.
static
<V,C extends java.util.Collection<Node<V>>>
C
collectNodesByPath(Node<V> parent, java.lang.String path, C collection)
          Collects all nodes underneath the given parent that match the given path.
static
<V> Node<V>
findLastNode(java.util.List<Node<V>> parents, Predicate<Node<V>> predicate)
          Returns the last node underneath the given parents for which the given predicate evaluates to true.
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.
static
<V> Node<V>
findNode(java.util.List<Node<V>> parents, Predicate<Node<V>> predicate)
          Returns the first node underneath the given parents for which the given predicate evaluates to true.
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.
static
<V> Node<V>
findNodeByLabel(java.util.List<Node<V>> parents, java.lang.String labelPrefix)
          Returns the first node underneath the given parents which matches the given label prefix.
static
<V> Node<V>
findNodeByLabel(Node<V> parent, java.lang.String labelPrefix)
          Returns the first node underneath the given parent for which matches the given label prefix.
static
<V> Node<V>
findNodeByPath(java.util.List<Node<V>> parents, java.lang.String path)
          Returns the node underneath the given parents that matches the given path.
static
<V> Node<V>
findNodeByPath(Node<V> parent, java.lang.String path)
          Returns the parse tree node underneath the given parent that matches the given path.
static java.lang.String getNodeText(Node<?> node, InputBuffer inputBuffer)
          Returns the input text matched by the given node, with error correction.
static java.lang.String getRawNodeText(Node<?> node, InputBuffer inputBuffer)
          Returns the raw input text matched by the given node, without error correction.
static
<V> java.lang.String
printNodeTree(ParsingResult<V> parsingResult)
          Creates a readable string represenation of the parse tree in the given ParsingResult object.
static
<V> java.lang.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

findNodeByPath

public static <V> Node<V> findNodeByPath(Node<V> parent,
                                         java.lang.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(java.util.List<Node<V>> parents,
                                         java.lang.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 java.util.Collection<Node<V>>> C collectNodesByPath(Node<V> parent,
                                                                               java.lang.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 java.util.Collection<Node<V>>> C collectNodesByPath(java.util.List<Node<V>> parents,
                                                                               java.lang.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(java.util.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,
                                          java.lang.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(java.util.List<Node<V>> parents,
                                          java.lang.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(java.util.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 java.util.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 java.lang.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 java.util.Collection<Node<V>>> C collectNodes(java.util.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

getRawNodeText

public static java.lang.String getRawNodeText(Node<?> node,
                                              InputBuffer inputBuffer)
Returns the raw input text matched by the given node, without 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)

printNodeTree

public static <V> java.lang.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> java.lang.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