Class ASTNode
java.lang.Object
io.github.douira.glsl_transformer.ast.node.basic.ASTNode
- Direct Known Subclasses:
Identifier,InnerASTNode,VersionStatement
The AST node represents a node in the abstract syntax tree. Each AST node has
a reference to the shared root object that contains the indexes for querying
the tree.
Invariants:
1. The root must contain exactly the nodes that are accessible by
traversing the tree downwards at any time.
2. Each contained node must have a reference to the root node it is part of.
3. The node must have a reference to its parent if it's not the root of the
tree and it must have the same root reference as its parent.
4. The selfReplacer function replaces the current node in the referenced
parent with a new one.
5. An AST node may only ever be in a tree once. Attempting to insert it
multiple times will cause undefined behavior. Moving a node requires removing
it from one parent and then adding it to another.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <R> Raccept(ASTVisitor<R> visitor) booleandetach()Detaches a node from its parent by using the stored self replacer function and also removes the parent from this node.booleanRemoves this node in the parent and unregisters the subtree.voidRemoves the parent from this node.getAncestor(int limit, int skip, Predicate<ASTNode> predicate) Returns the first ancestor that fulfills the given predicate, limited to a certain number of steps.getAncestor(int limit, Predicate<ASTNode> predicate) Returns the first ancestor that fulfills the given predicate, limited to a certain number of steps.<T extends ASTNode>
TgetAncestor(Class<T> clazz) Returns the first ancestor that is an instance of the given class.getAncestor(Predicate<ASTNode> predicate) Returns the first ancestor that fulfills the given predicate.Returns a lazy stream of all ancestors of this node.getNthParent(int n) Gets the nth parent of this node.getRoot()Returns the root of this node.booleanhasAncestor(int limit, int skip, Predicate<ASTNode> predicate) Checks if there is an ancestor of this node that fulfills the given predicate within a limited nubmer of steps after a number of steps skipped.booleanhasAncestor(int limit, Predicate<ASTNode> predicate) Checks if there is an ancestor of this node that fulfills the given predicate within a limited nubmer of steps.booleanhasAncestor(ASTNode node) Checks if the given node is an ancestor of this node.booleanhasAncestor(Class<? extends ASTNode> clazz) Checks if there is an ancestor of this node that is an instance of the given class.booleanhasAncestor(Predicate<ASTNode> predicate) Checks if there is an ancestor of this node that fulfills the given predicate.booleanbooleanUses the stored replacer function to remove this node from the parent and remove the parent from this node.booleanreplaceByAndDelete(ASTNode replacement) Replaces this node in the parent by the given node and unregisters the subtree.booleanSets the parent of this node and handles registration and unregistration of the subtree with the new parent.<NodeType extends ASTNode>
NodeTypeAdds a newly constructed node to this node as the parent.static booleanSwaps two nodes in their parents.booleanThis unregisters this node and all its children from its root.<NodeType extends ASTNode>
voidupdateParents(NodeType currentNode, NodeType newNode, Consumer<? extends NodeType> setter) Updates the parents of two nodes where one is replacing the other in this node.
-
Constructor Details
-
ASTNode
public ASTNode()
-
-
Method Details
-
accept
-
getParent
-
hasParent
public boolean hasParent() -
getParentSetter
-
getNthParent
Gets the nth parent of this node. The 0th parent is this node. The 1st parent is the parent of this node.- Parameters:
n- the number of parents to go up- Returns:
- the nth parent of this node
-
hasAncestor
Checks if there is an ancestor of this node that fulfills the given predicate within a limited nubmer of steps after a number of steps skipped. If the limit is 0, it will only check the current node.- Parameters:
limit- the number of parents to check in totalskip- the number of parents to skip before checking the predicatepredicate- the predicate to check- Returns:
- true if there is an ancestor of this node that fulfills the given predicate, false otherwise
-
hasAncestor
Checks if there is an ancestor of this node that fulfills the given predicate within a limited nubmer of steps.- Parameters:
limit- the number of parents to check in totalpredicate- the predicate to check- Returns:
- true if there is an ancestor of this node that fulfills the predicate
-
hasAncestor
Checks if there is an ancestor of this node that fulfills the given predicate.- Parameters:
predicate- the predicate to check- Returns:
- true if there is an ancestor of this node that fulfills the predicate
-
hasAncestor
Checks if there is an ancestor of this node that is an instance of the given class.- Parameters:
clazz- the class to check- Returns:
- true if there is an ancestor that is an instance of the given class
-
hasAncestor
Checks if the given node is an ancestor of this node.- Parameters:
node- the node to check- Returns:
- true if the given node is an ancestor of this node
-
getAncestor
Returns the first ancestor that fulfills the given predicate, limited to a certain number of steps. If the limit is 0, it will only check the current node.- Parameters:
limit- the number of parents to check in totalskip- the number of steps to skip before checking the predicatepredicate- the predicate to check- Returns:
- the first ancestor that fulfills the given predicate, or null otherwise
-
getAncestor
Returns the first ancestor that fulfills the given predicate, limited to a certain number of steps.- Parameters:
limit- the number of parents to check in totalpredicate- the predicate to check- Returns:
- the first ancestor that fulfills the given predicate, or null otherwise
-
getAncestor
Returns the first ancestor that fulfills the given predicate.- Parameters:
predicate- the predicate to check- Returns:
- the first ancestor that fulfills the given predicate, or null otherwise
-
getAncestor
Returns the first ancestor that is an instance of the given class.- Parameters:
clazz- the class to check- Returns:
- the first ancestor that is an instance of the given class, or null otherwise
-
getAncestors
Returns a lazy stream of all ancestors of this node.- Returns:
- a stream of the ancestors
-
getRoot
Returns the root of this node.- Returns:
- the root
-
setParent
Sets the parent of this node and handles registration and unregistration of the subtree with the new parent.- Parameters:
parent- The parent value to set, cannot be null.- Returns:
trueif the parent was changed,falseotherwise.
-
replaceBy
Uses the stored replacer function to remove this node from the parent and remove the parent from this node. It does not unregister the subtree.- Parameters:
replacement- The node to replace this node with- Returns:
trueif the parent was changed,falseotherwise.
-
replaceByAndDelete
Replaces this node in the parent by the given node and unregisters the subtree. This fully removes the node from the tree.- Parameters:
replacement- The node to replace this node with- Returns:
trueif the parent was changed,falseotherwise.
-
detach
public boolean detach()Detaches a node from its parent by using the stored self replacer function and also removes the parent from this node. Does not unregister the subtree.- Returns:
trueif the node was removed,falseotherwise.
-
detachAndDelete
public boolean detachAndDelete()Removes this node in the parent and unregisters the subtree.- Returns:
trueif the parent was changed,falseotherwise.
-
detachParent
public void detachParent()Removes the parent from this node. This is useful if the node has already been (efficiently) removed from the parent. -
unregisterSubtree
public boolean unregisterSubtree()This unregisters this node and all its children from its root.- Returns:
trueif there was unregistering,falseotherwise.
-
swap
Swaps two nodes in their parents. Throws if the nodes or their parents are null in which case calling this method is pointless.- Parameters:
a- The first node to swapb- The second node to swap- Returns:
trueif the nodes were swapped,falseotherwise.
-
setup
public <NodeType extends ASTNode> NodeType setup(NodeType node, Consumer<? extends NodeType> setter) Adds a newly constructed node to this node as the parent. The node will already have a reference to the current root but will not be registered to it yet.- Type Parameters:
NodeType- Type of the node for passthrough- Parameters:
node- The node to addsetter- The setter to replace the node in this parent- Returns:
- The node itself
-
updateParents
public <NodeType extends ASTNode> void updateParents(NodeType currentNode, NodeType newNode, Consumer<? extends NodeType> setter) Updates the parents of two nodes where one is replacing the other in this node. Both of them may be null if either the existing value is being removed or a new value is being set.- Type Parameters:
NodeType- The type of the nodes for pass-through- Parameters:
currentNode- The current nodenewNode- The new nodesetter- The setter to replace the node in this parent (this is usually a method reference to a setter method of the parent, this node)
-