From f9283a20abd9eaed0b0436bc54c60576233121f4 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 11 Apr 2016 09:32:59 -0400 Subject: Added new method to pairs and holders --- .../src/main/java/bjc/utils/parserutils/AST.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java') 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 { action.accept(token); } } + + /** + * Change the type of nodes in the tree without changing the structure + * + * @param + * 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 AST rebuildTree(Function nodeTransform, + Function operatorTransform) { + return collapse((leafNode) -> { + E transformedNode = nodeTransform.apply(leafNode); + return new AST<>(transformedNode); + }, (operator) -> (AST newLeft, AST newRight) -> { + return new AST<>(operatorTransform.apply(operator), newLeft, + newRight); + }, (resultValue) -> resultValue); + } } -- cgit v1.2.3