Class TreeNode
- java.lang.Object
-
- org.jparsec.TreeNode
-
final class TreeNode extends java.lang.Object
A TreeNode remembers it's parent (which corresponds to a parent parser that syntactically encloses this parter), it's previous node (which is the parser at the same syntactical level and had just succeeded before this parser started). It also keeps all the children.Once constructed, a node's 'parent' and 'previous' references are immutable. The list of children nodes however can change. When the alternative parsers in an
or
parser are attempted one after another, they each generate new child node of the parent node. These "alternative" nodes all point to the same parent and same "previous" node.When exception is to be thrown, the most relevant error is picked, along with the tree node that was recorded at time of
ParseContext.raise(org.jparsec.ParseContext.ErrorType, java.lang.Object)
. That tree node is thenfrozen
by setting its parent'slatestChild
to this error node's "previous" successful node, and that of its grandparent's to its parent node, all the way up to the root. This essentially freezes and collapse the "multi universes" into a single error state, with all other "potential" error state destroyed and forgotten.
-
-
Field Summary
Fields Modifier and Type Field Description private int
beginIndex
private int
endIndex
(package private) TreeNode
latestChild
private java.lang.String
name
private TreeNode
parent
private TreeNode
previous
private java.lang.Object
result
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) TreeNode
addChild(java.lang.String childName, int childIndex)
(package private) TreeNode
freeze(int index)
Freezes the current tree node to make it the latest child of its parent (discarding nodes that have been tacked on after it in the same hierarchy level); and recursively apply to all of its ancestors.(package private) TreeNode
orphanize()
When this leaf node has errors, it didn't complete and shouldn't be part of the parse tree that is the current partial parse result with all successful matches.(package private) TreeNode
parent()
(package private) void
setEndIndex(int index)
(package private) void
setResult(java.lang.Object result)
(package private) ParseTree
toParseTree()
Converts this node into aParseTree
representation.java.lang.String
toString()
-
-
-
Method Detail
-
setEndIndex
void setEndIndex(int index)
-
setResult
void setResult(java.lang.Object result)
-
parent
TreeNode parent()
-
addChild
TreeNode addChild(java.lang.String childName, int childIndex)
-
orphanize
TreeNode orphanize()
When this leaf node has errors, it didn't complete and shouldn't be part of the parse tree that is the current partial parse result with all successful matches. In that case, return the parent node, by setting itslatestChild
toprevious
.
-
freeze
TreeNode freeze(int index)
Freezes the current tree node to make it the latest child of its parent (discarding nodes that have been tacked on after it in the same hierarchy level); and recursively apply to all of its ancestors.This is because it's only called at time of error. If an ancestor node has a child node that was added during the process of trying other alternatives and then failed, those paths don't matter. So we should restore the tree back to when this most relevant error happened.
Returns the root node, which can then be used to
toParseTree()
.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-