diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java index 88b4862..b6c4d8c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -361,4 +361,26 @@ public class AST<T> { action.accept(token); } } + + /** + * Change the type of nodes in the tree without changing the structure + * + * @param <E> + * The new node type + * @param nodeTransform + * The transform to apply to leaf nodes + * @param operatorTransform + * The transform to apply to operator nodes + * @return An AST with the node types transformed + */ + public <E> AST<E> rebuildTree(Function<T, E> nodeTransform, + Function<T, E> operatorTransform) { + return collapse((leafNode) -> { + E transformedNode = nodeTransform.apply(leafNode); + return new AST<>(transformedNode); + }, (operator) -> (AST<E> newLeft, AST<E> newRight) -> { + return new AST<>(operatorTransform.apply(operator), newLeft, + newRight); + }, (resultValue) -> resultValue); + } } |
