summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-11 09:32:59 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-11 09:32:59 -0400
commitf9283a20abd9eaed0b0436bc54c60576233121f4 (patch)
treecec1323fb3faf1a4dcee2394a114b821c2366166 /BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java
parent275a627719fc2231b16caea41130ff09f0f2b6a1 (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.java22
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);
+ }
}