Package net.emustudio.edigen.nodes
Class TreeNode
- java.lang.Object
-
- net.emustudio.edigen.nodes.TreeNode
-
- Direct Known Subclasses:
Decoder,Disassembler,Format,Mask,Pattern,Rule,Specification,Subrule,Value,Variant
public abstract class TreeNode extends java.lang.ObjectA node of an abstract syntax tree (AST).The AST will be transformed using the visitor design pattern.
One node can have an unlimited number of children. Insertion order is preserved.
-
-
Constructor Summary
Constructors Constructor Description TreeNode()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaccept(Visitor visitor)Calls the appropriate visitor method.voidacceptChildren(Visitor visitor)Sequentially calls theaccept(Visitor)method for all children.TreeNodeaddChild(TreeNode child)Adds a child to this node, placing it on the end.TreeNodeaddChildren(java.util.List<TreeNode> children)TreeNodeaddChildren(TreeNode... children)Adds multiple children to this node, placing them on the end.intchildCount()Returns the number of all direct children of this node.TreeNodecopy()voiddump(java.io.PrintStream outStream)Prints the whole tree recursively.TreeNodegetChild(int index)Returns the child at given index.java.util.List<TreeNode>getChildren()Returns all children of this node.java.lang.IntegergetLine()Returns the starting line number in the source file from which this node was generated.TreeNodegetParent()Returns the parent of this node.inthashCode()voidremove()Removes this node from the tree.voidsetLine(java.lang.Integer line)Sets the starting source line number of this node.abstract TreeNodeshallowCopy()
-
-
-
Method Detail
-
getParent
public TreeNode getParent()
Returns the parent of this node.- Returns:
- the parent node, or null if this is the root node
-
getChild
public TreeNode getChild(int index)
Returns the child at given index. Note: The average time complexity of this method is O(n).- Parameters:
index- the index, starting at 0- Returns:
- the child node
-
getChildren
public java.util.List<TreeNode> getChildren()
Returns all children of this node. A copy of the collection is returned to allow children removal and insertion during the iteration.- Returns:
- the iterable collection of all children
-
childCount
public int childCount()
Returns the number of all direct children of this node.- Returns:
- the child count
-
addChild
public TreeNode addChild(TreeNode child)
Adds a child to this node, placing it on the end.- Parameters:
child- the child node- Returns:
- this
-
addChildren
public TreeNode addChildren(TreeNode... children)
Adds multiple children to this node, placing them on the end.- Parameters:
children- the child nodes- Returns:
- this
-
shallowCopy
public abstract TreeNode shallowCopy()
-
copy
public TreeNode copy()
-
remove
public void remove()
Removes this node from the tree. This can be described as "tearing off" the node. The link between this node and the parent one is removed bilaterally.
-
accept
public void accept(Visitor visitor) throws SemanticException
Calls the appropriate visitor method. Subclasses should override this method in order to support the visitor design pattern properly.- Parameters:
visitor- the visitor object- Throws:
SemanticException- depends on situation
-
acceptChildren
public void acceptChildren(Visitor visitor) throws SemanticException
Sequentially calls theaccept(Visitor)method for all children.- Parameters:
visitor- the visitor object- Throws:
SemanticException- depends on situation
-
getLine
public java.lang.Integer getLine()
Returns the starting line number in the source file from which this node was generated.- Returns:
- the line number; null if no line was associated with this node
-
setLine
public void setLine(java.lang.Integer line)
Sets the starting source line number of this node. It is the starting position of one of the tokens from which this tree node was generated (usually the first one).- Parameters:
line- the line number
-
dump
public void dump(java.io.PrintStream outStream)
Prints the whole tree recursively.- Parameters:
outStream- the stream to write to
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-