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 ExternalDeclarationIndex<?,?> The external declaration index indexes the external declarations by their name.final IdentifierIndex<?,?> The identifier index is used to query identifiers by their name or prefixes on their name.final NodeIndex<?>The node index is used to query nodes by their type. -
Constructor Summary
ConstructorsConstructorDescriptionRoot(NodeIndex<?> nodeIndex, IdentifierIndex<?, ?> identifierIndex, ExternalDeclarationIndex<?, ?> externalDeclarationIndex) Constructs a new root with the given node and identifier indexes. -
Method Summary
Modifier and TypeMethodDescriptionstatic RootReturns the currently active build root.Returns the identifier index as a prefix identifier index if it is one.voidindexBuildSession(Runnable session) Runs a given runnable with the given root as the active build root.voidindexBuildSession(Consumer<Root> session) <N extends ASTNode>
NindexNodes(Supplier<N> builder) Runs the given builder supplier with the given root as the active build root and then registers the returned node with the root.<N extends ASTNode>
voidindexSeparateTrees(Consumer<Passthrough<N>> registererConsumer) Runs the given consumer of a registration pass-through function with the given root as the active build root.booleanprocess(String name, Consumer<Identifier> replacer) Processes all identifiers with the given name with the given consumer.<N extends ASTNode>
booleanProcesses all target nodes from the given stream with the given consumer.<N extends ASTNode>
booleanprocessMatches(ASTParser t, HintedMatcher<N> hintedMatcher, Consumer<? super N> replacer) Processes all matches of nodes in the tree that match the given hinted matcher.<N extends ASTNode>
booleanprocessMatches(ASTParser t, Stream<? extends ASTNode> matchTargetChildren, Matcher<N> matcher, Consumer<? super N> replacer) Processes all matches of nodes from the given stream matched with the given matcher.voidregisterFastRename(ASTNode identifier) voidregisterIdentifierRename(Identifier identifier) voidregisterNode(ASTNode node, boolean isSubtreeRoot) Registers the given node with this root.booleanRenames all identifiers with the given old name to the given new name.<N extends Expression>
booleanreplaceExpressionMatches(ASTParser t, HintedMatcher<N> hintedMatcher, String expression) Replaces expressions all matches of expression nodes in the tree that match the given hinted matcher with new expressions created from the given string.<N extends Expression>
booleanreplaceExpressionMatches(ASTParser t, Stream<? extends ASTNode> matchTargetChildren, Matcher<N> matcher, String expression) Replaces expressions from the given stream that match the given matcher with new expressions created from the given string.booleanreplaceExpressions(ASTParser t, Stream<? extends Expression> targets, String expression) Replaces all expressions from the given stream with the given replacement expression.static booleanreplaceExpressionsConcurrent(ASTParser t, List<? extends Expression> targets, String expression) 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.voidreplaceReferenceExpressions(ASTParser t, String name, String expression) Replaces all reference expressions containing identifiers with the given name with the given replacement expression.voidreplaceReferenceExpressions(ASTParser t, Stream<Identifier> targets, String expression) Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.booleanreplaceReferenceExpressionsReport(ASTParser t, String name, String expression) Replaces all reference expressions containing identifiers with the given name with the given replacement expression.booleanreplaceReferenceExpressionsReport(ASTParser t, Stream<Identifier> targets, String expression) Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.voidunregisterFastRename(ASTNode identifier) voidunregisterIdentifierRename(Identifier identifier) voidunregisterNode(ASTNode node, boolean isSubtreeRoot) Unregisters the given node from this root.protected final <R> RwithActiveBuildRoot(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. -
externalDeclarationIndex
The external declaration index indexes the external declarations by their name. Each node may appear multiple times if it contains multiple members with different names.
-
-
Constructor Details
-
Root
public Root(NodeIndex<?> nodeIndex, IdentifierIndex<?, ?> identifierIndex, ExternalDeclarationIndex<?, ?> externalDeclarationIndex) Constructs a new root with the given node and identifier indexes.- Parameters:
nodeIndex- The node indexidentifierIndex- The identifier indexexternalDeclarationIndex- The external declaration index
-
-
Method Details
-
getPrefixIdentifierIndex
Returns the identifier index as a prefix identifier index if it is one. Otherwise, it throws.- Returns:
- The prefix identifier index
-
getPrefixExternalDeclarationIndex
-
registerNode
Registers the given node with this root.- Parameters:
node- The node to registerisSubtreeRoot- Whether the node is the root of a subtree that is being added
-
unregisterNode
Unregisters the given node from this root.- Parameters:
node- The node to unregisterisSubtreeRoot- Whether the node is the root of a subtree that is being removed
-
unregisterIdentifierRename
-
unregisterFastRename
-
registerIdentifierRename
-
registerFastRename
-
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:
rootConsumer- The consumer to run- Returns:
- The return value of the consumer
-
indexNodes
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:
N- The type of the node to build- Parameters:
builder- 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:
session- The runnable to run
-
indexBuildSession
-
indexSeparateTrees
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:
N- The type of the nodes to register- Parameters:
registererConsumer- The consumer to run
-
rename
Renames all identifiers with the given old name to the given new name.- Parameters:
oldName- The old namenewName- The new name- Returns:
- Whether anything was renamed
-
process
public <N extends ASTNode> boolean process(Stream<? extends N> targets, Consumer<? super N> replacer) 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:
N- The type of the target nodes- Parameters:
targets- The stream of target nodes to processreplacer- The consumer to process the target nodes with- Returns:
- Whether anything was processed
-
process
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- Returns:
- Whether anything was processed
-
replaceReferenceExpressions
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:
t- The AST transformername- The name of the identifiers to targetexpression- The content of the replacement expression
-
replaceReferenceExpressionsReport
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:
t- The AST transformername- The name of the identifiers to targetexpression- The content of the replacement expression- Returns:
- Whether any replacements were made
-
replaceReferenceExpressions
Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.- Parameters:
t- The AST transformertargets- The stream of identifiers to targetexpression- The content of the replacement expression
-
replaceReferenceExpressionsReport
public boolean replaceReferenceExpressionsReport(ASTParser t, Stream<Identifier> targets, String expression) Replaces all reference expressions containing the given identifiers from the given stream with the given replacement expression.- Parameters:
t- The AST transformertargets- The stream of identifiers to targetexpression- The content of the replacement expression- Returns:
- Whether any replacements were made
-
replaceExpressions
public boolean replaceExpressions(ASTParser t, Stream<? extends Expression> targets, String expression) Replaces all expressions from the given stream with the given replacement expression.- Parameters:
t- The AST transformertargets- The stream of expressions to targetexpression- The content of the replacement expression- Returns:
- Whether any replacements were made
-
replaceExpressionsConcurrent
public static boolean replaceExpressionsConcurrent(ASTParser t, List<? extends Expression> targets, String expression) 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:
t- The AST transformertargets- The list of expressions to targetexpression- The content of the replacement expression- Returns:
- Whether any replacements were made
-
processMatches
public <N extends ASTNode> boolean processMatches(ASTParser t, Stream<? extends ASTNode> matchTargetChildren, Matcher<N> matcher, Consumer<? super N> replacer) Processes all matches of nodes from the given stream matched with the given matcher.- Type Parameters:
N- The type of the matched nodes- Parameters:
t- The AST transformermatchTargetChildren- The stream of nodes to matchmatcher- The matcher to match the nodes withreplacer- The consumer to process the matched nodes with- Returns:
- Whether anything was processed
-
processMatches
public <N extends ASTNode> boolean processMatches(ASTParser t, HintedMatcher<N> hintedMatcher, Consumer<? super N> replacer) Processes all matches of nodes in the tree that match the given hinted matcher. The hint is used to identify the nodes to match.- Type Parameters:
N- The type of the matched nodes- Parameters:
t- The AST transformerhintedMatcher- The matcher to match the nodes withreplacer- The consumer to process the matched nodes with- Returns:
- Whether anything was processed
-
replaceExpressionMatches
public <N extends Expression> boolean replaceExpressionMatches(ASTParser t, Stream<? extends ASTNode> matchTargetChildren, Matcher<N> matcher, String expression) Replaces expressions from the given stream that match the given matcher with new expressions created from the given string.- Type Parameters:
N- The type of the matched expression nodes- Parameters:
t- The AST transformermatchTargetChildren- The stream of nodes to matchmatcher- The matcher to match the nodes withexpression- The content of the replacement expression- Returns:
- Whether anything was processed
-
replaceExpressionMatches
public <N extends Expression> boolean replaceExpressionMatches(ASTParser t, HintedMatcher<N> hintedMatcher, String expression) Replaces expressions all matches of expression nodes in the tree that match the given hinted matcher with new expressions created from the given string.- Type Parameters:
N- The type of the matched expression nodes- Parameters:
t- The AST transformerhintedMatcher- The matcher to match the nodes withexpression- The content of the replacement expression- Returns:
- Whether anything was processed
-