Class Root
java.lang.Object
io.github.douira.glsl_transformer.ast.query.Root
Each AST has a root referenced by all its members. This class manages the
indexes of the tree and is updated each time a node is added or removed from
the tree. It also contains methods for indexing nodes for building a tree and
for performing replacement operations on the tree using the stored indexes.
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal IdentifierIndex<?>The identifier index is used to query identifiers by their name or prefixes on their name.final NodeIndexThe node index is used to query nodes by their type. -
Constructor Summary
ConstructorsConstructorDescriptionRoot()Constructs a new root with the default node and identifier indexes which have the least amount of functionality but are also the most efficient.Root(NodeIndex nodeIndex, IdentifierIndex<?> identifierIndex) Constructs a new root with the given node and identifier indexes. -
Method Summary
Modifier and TypeMethodDescriptionstatic RootReturns the currently active build root.static voidindexBuildSession(ASTNode treeMember, Runnable session) Runs the given runnable with the same root as a given tree node as the active build root.static voidindexBuildSession(Root instance, Runnable session) Runs a given runnable with the given root as the active build root.static voidindexBuildSession(Runnable session) Runs the given runnable with a new root as the active build root.static <NodeType extends ASTNode>
NodeTypeindexNodes(ASTNode parentTreeMember, Supplier<NodeType> builder) Runs the given builder supplier with the same root as a given tree node as the active build root.static <NodeType extends ASTNode>
NodeTypeindexNodes(Root instance, Supplier<NodeType> builder) Runs the given builder supplier with the given root as the active build root and then registers the returned node with the root.static <NodeType extends ASTNode>
NodeTypeindexNodes(Supplier<NodeType> builder) Runs the given builder supplier with a new root as the active build root.static <NodeType extends ASTNode>
voidindexSeparateTrees(ASTNode treeMember, Consumer<Passthrough<NodeType>> registerer) Runs the given consumer of a registration pass-through function with the same root as a given tree node as the active build root.static <NodeType extends ASTNode>
voidindexSeparateTrees(Root instance, Consumer<Passthrough<NodeType>> registererConsumer) Runs the given consumer of a registration pass-through function with the given root as the active build root.static <NodeType extends ASTNode>
voidindexSeparateTrees(Consumer<Passthrough<NodeType>> registerer) Runs the given consumer of a registration pass-through function with a new root as the active build root.voidMerges the given root into this root.voidprocessAll(String name, Consumer<Identifier> replacer) Processes all identifiers with the given name with the given consumer.<T extends ASTNode>
voidprocessAll(Stream<T> targets, Consumer<T> replacer) Processes all target nodes from the given stream with the given consumer.voidregisterNode(ASTNode node) Registers the given node with this root.voidRenames all identifiers with the given old name to the given new name.voidreplaceAllExpressions(ASTTransformer<?> transformer, Stream<? extends Expression> targets, String expressionContent) Replaces all expressions from the given stream with the given replacement expression.static voidreplaceAllExpressionsConcurrent(ASTTransformer<?> transformer, List<? extends Expression> targets, String expressionContent) Replaces all expressions from the given list with the given replacement expression but without storing the targets in an intermediary list under the assumption that this list will not be modified by the replacement.voidreplaceAllReferenceExpressions(ASTTransformer<?> transformer, String name, String expressionContent) Replaces all reference expressions containing identifiers with the given name with the given replacement expression.voidreplaceAllReferenceExpressions(ASTTransformer<?> transformer, Stream<Identifier> targets, String expressionContent) Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.voidunregisterNode(ASTNode node) Unregisters the given node from this root.protected static final <R> RwithActiveBuildRoot(Root instance, Function<Root, R> rootConsumer) Runs the given consumer with the given root as the active build root.
-
Field Details
-
nodeIndex
The node index is used to query nodes by their type. There are various available node index implementations that provide different trade-offs between performance, memory consumption and query capabilities. -
identifierIndex
The identifier index is used to query identifiers by their name or prefixes on their name. There are various available identifier index implementations that provide different trade-offs between performance, memory consumption and query capabilities.
-
-
Constructor Details
-
Root
Constructs a new root with the given node and identifier indexes.- Parameters:
nodeIndex- The node indexidentifierIndex- The identifier index
-
Root
public Root()Constructs a new root with the default node and identifier indexes which have the least amount of functionality but are also the most efficient.
-
-
Method Details
-
getActiveBuildRoot
Returns the currently active build root. When nodes are constructed within a build session, this method returns the root of the build session. Nodes can't be constructed with children if they have no root since the children must be registered with the root.- Returns:
- the currently active build root
-
withActiveBuildRoot
Runs the given consumer with the given root as the active build root.- Type Parameters:
R- The return type of the consumer- Parameters:
instance- The root to run the consumer withrootConsumer- The consumer to run- Returns:
- The return value of the consumer
-
indexNodes
public static <NodeType extends ASTNode> NodeType indexNodes(Root instance, Supplier<NodeType> builder) Runs the given builder supplier with the given root as the active build root and then registers the returned node with the root. This is necessary since the node will have the correct root but will not be registered until it is either manually registered as it is being done here or added as a child to another node with the root.- Type Parameters:
NodeType- The type of the node to build- Parameters:
instance- The root to run the builder withbuilder- The builder to run- Returns:
- The built and registered node
-
indexNodes
Runs the given builder supplier with a new root as the active build root.- Type Parameters:
NodeType- The type of the node to build- Parameters:
builder- The builder to run- Returns:
- The built and registered node
-
indexNodes
public static <NodeType extends ASTNode> NodeType indexNodes(ASTNode parentTreeMember, Supplier<NodeType> builder) Runs the given builder supplier with the same root as a given tree node as the active build root.- Type Parameters:
NodeType- The type of the node to build- Parameters:
parentTreeMember- The tree member to get the root frombuilder- The builder to run- Returns:
- The built and registered node
-
indexBuildSession
Runs a given runnable with the given root as the active build root. This is used for constructing nodes with children without registering the constructed root node with the root or for registering it manually.- Parameters:
instance- The root to use as the active build rootsession- The runnable to run
-
indexBuildSession
Runs the given runnable with a new root as the active build root.- Parameters:
session- The runnable to run
-
indexBuildSession
Runs the given runnable with the same root as a given tree node as the active build root.- Parameters:
treeMember- The tree member to get the root fromsession- The runnable to run
-
indexSeparateTrees
public static <NodeType extends ASTNode> void indexSeparateTrees(Root instance, Consumer<Passthrough<NodeType>> registererConsumer) Runs the given consumer of a registration pass-through function with the given root as the active build root. The consumer receives a pass-through function that takes a constructed node and registers it with the root. This is helpful for constructing trees manually and registering them inline.- Type Parameters:
NodeType- The type of the nodes to register- Parameters:
instance- the root to register the nodes withregistererConsumer- The consumer to run
-
indexSeparateTrees
public static <NodeType extends ASTNode> void indexSeparateTrees(Consumer<Passthrough<NodeType>> registerer) Runs the given consumer of a registration pass-through function with a new root as the active build root.- Type Parameters:
NodeType- The type of the nodes to register- Parameters:
registerer- The consumer to run
-
indexSeparateTrees
public static <NodeType extends ASTNode> void indexSeparateTrees(ASTNode treeMember, Consumer<Passthrough<NodeType>> registerer) Runs the given consumer of a registration pass-through function with the same root as a given tree node as the active build root.- Type Parameters:
NodeType- The type of the nodes to register- Parameters:
treeMember- The tree member to get the root fromregisterer- The consumer to run
-
registerNode
Registers the given node with this root.- Parameters:
node- The node to register
-
unregisterNode
Unregisters the given node from this root.- Parameters:
node- The node to unregister
-
merge
Merges the given root into this root. This will merge the node index and the identifier index.- Parameters:
other- The root to merge into this root
-
renameAll
Renames all identifiers with the given old name to the given new name.- Parameters:
oldName- The old namenewName- The new name
-
processAll
Processes all target nodes from the given stream with the given consumer. This method ensures there is no concurrent modification of the node index by collecting the stream into a shared list.- Type Parameters:
T- The type of the target nodes- Parameters:
targets- The stream of target nodes to processreplacer- The consumer to process the target nodes with
-
processAll
Processes all identifiers with the given name with the given consumer.- Parameters:
name- The name of the identifiers to processreplacer- The consumer to process the identifiers with
-
replaceAllReferenceExpressions
public void replaceAllReferenceExpressions(ASTTransformer<?> transformer, String name, String expressionContent) Replaces all reference expressions containing identifiers with the given name with the given replacement expression. Identifiers that are not part of a reference expression are not replaced since an expression would be impossible as a replacement.- Parameters:
transformer- The AST transformername- The name of the identifiers to targetexpressionContent- The content of the replacement expression
-
replaceAllReferenceExpressions
public void replaceAllReferenceExpressions(ASTTransformer<?> transformer, Stream<Identifier> targets, String expressionContent) Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.- Parameters:
transformer- The AST transformertargets- The stream of identifiers to targetexpressionContent- The content of the replacement expression
-
replaceAllExpressions
public void replaceAllExpressions(ASTTransformer<?> transformer, Stream<? extends Expression> targets, String expressionContent) Replaces all expressions from the given stream with the given replacement expression.- Parameters:
transformer- The AST transformertargets- The stream of expressions to targetexpressionContent- The content of the replacement expression
-
replaceAllExpressionsConcurrent
public static void replaceAllExpressionsConcurrent(ASTTransformer<?> transformer, List<? extends Expression> targets, String expressionContent) Replaces all expressions from the given list with the given replacement expression but without storing the targets in an intermediary list under the assumption that this list will not be modified by the replacement.- Parameters:
transformer- The AST transformertargets- The list of expressions to targetexpressionContent- The content of the replacement expression
-