java.lang.Object
io.github.douira.glsl_transformer.ast.node.basic.ASTNode
Direct Known Subclasses:
Identifier, InnerASTNode, VersionStatement

public abstract class ASTNode extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract <R> R
    accept(ASTVisitor<R> visitor)
     
    boolean
    Detaches a node from its parent by using the stored self replacer function and also removes the parent from this node.
    boolean
    Removes this node in the parent and unregisters the subtree.
    void
    Removes 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>
    T
    getAncestor(Class<T> clazz)
    Returns the first ancestor that is an instance of the given class.
    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.
     
     
    Returns the root of this node.
    boolean
    hasAncestor(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.
    boolean
    hasAncestor(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.
    boolean
    Checks if the given node is an ancestor of this node.
    boolean
    hasAncestor(Class<? extends ASTNode> clazz)
    Checks if there is an ancestor of this node that is an instance of the given class.
    boolean
    Checks if there is an ancestor of this node that fulfills the given predicate.
    boolean
     
    boolean
    replaceBy(ASTNode replacement)
    Uses the stored replacer function to remove this node from the parent and remove the parent from this node.
    boolean
    Replaces this node in the parent by the given node and unregisters the subtree.
    boolean
    setParent(ASTNode parent, Consumer<? extends ASTNode> setter)
    Sets the parent of this node and handles registration and unregistration of the subtree with the new parent.
    <NodeType extends ASTNode>
    NodeType
    setup(NodeType node, Consumer<? extends NodeType> setter)
    Adds a newly constructed node to this node as the parent.
    static boolean
    Swaps two nodes in their parents.
    boolean
    This unregisters this node and all its children from its root.
    <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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ASTNode

      public ASTNode()
  • Method Details

    • accept

      public abstract <R> R accept(ASTVisitor<R> visitor)
    • getParent

      public ASTNode getParent()
    • hasParent

      public boolean hasParent()
    • getParentSetter

      public Consumer<ASTNode> getParentSetter()
    • getNthParent

      public ASTNode getNthParent(int n)
      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

      public boolean hasAncestor(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. If the limit is 0, it will only check the current node.
      Parameters:
      limit - the number of parents to check in total
      skip - the number of parents to skip before checking the predicate
      predicate - the predicate to check
      Returns:
      true if there is an ancestor of this node that fulfills the given predicate, false otherwise
    • hasAncestor

      public boolean hasAncestor(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.
      Parameters:
      limit - the number of parents to check in total
      predicate - the predicate to check
      Returns:
      true if there is an ancestor of this node that fulfills the predicate
    • hasAncestor

      public boolean hasAncestor(Predicate<ASTNode> predicate)
      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

      public boolean hasAncestor(Class<? extends ASTNode> clazz)
      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

      public boolean hasAncestor(ASTNode node)
      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

      public ASTNode getAncestor(int limit, int skip, Predicate<ASTNode> predicate)
      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 total
      skip - the number of steps to skip before checking the predicate
      predicate - the predicate to check
      Returns:
      the first ancestor that fulfills the given predicate, or null otherwise
    • getAncestor

      public ASTNode getAncestor(int limit, Predicate<ASTNode> predicate)
      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 total
      predicate - the predicate to check
      Returns:
      the first ancestor that fulfills the given predicate, or null otherwise
    • getAncestor

      public ASTNode getAncestor(Predicate<ASTNode> predicate)
      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

      public <T extends ASTNode> T getAncestor(Class<T> clazz)
      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

      public Stream<ASTNode> getAncestors()
      Returns a lazy stream of all ancestors of this node.
      Returns:
      a stream of the ancestors
    • getRoot

      public Root getRoot()
      Returns the root of this node.
      Returns:
      the root
    • setParent

      public boolean setParent(ASTNode parent, Consumer<? extends ASTNode> setter)
      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:
      true if the parent was changed, false otherwise.
    • replaceBy

      public boolean replaceBy(ASTNode replacement)
      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:
      true if the parent was changed, false otherwise.
    • replaceByAndDelete

      public boolean replaceByAndDelete(ASTNode replacement)
      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:
      true if the parent was changed, false otherwise.
    • 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:
      true if the node was removed, false otherwise.
    • detachAndDelete

      public boolean detachAndDelete()
      Removes this node in the parent and unregisters the subtree.
      Returns:
      true if the parent was changed, false otherwise.
    • 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:
      true if there was unregistering, false otherwise.
    • swap

      public static boolean swap(ASTNode a, ASTNode b)
      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 swap
      b - The second node to swap
      Returns:
      true if the nodes were swapped, false otherwise.
    • 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 add
      setter - 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 node
      newNode - The new node
      setter - The setter to replace the node in this parent (this is usually a method reference to a setter method of the parent, this node)