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 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. 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)
    • hasAncestor

      public boolean hasAncestor(Predicate<ASTNode> predicate)
    • hasAncestor

      public boolean hasAncestor(Class<? extends ASTNode> clazz)
    • hasAncestor

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

      public ASTNode getAncestor(Predicate<ASTNode> predicate)
    • getAncestor

      public ASTNode getAncestor(Class<? extends ASTNode> clazz)
    • getAncestors

      public Stream<ASTNode> getAncestors()
    • getRoot

      public Root getRoot()
    • 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)