diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-11 09:32:59 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-11 09:32:59 -0400 |
| commit | f9283a20abd9eaed0b0436bc54c60576233121f4 (patch) | |
| tree | cec1323fb3faf1a4dcee2394a114b821c2366166 /BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java | |
| parent | 275a627719fc2231b16caea41130ff09f0f2b6a1 (diff) | |
Added new method to pairs and holders
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); + } } |
