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 ++++++++ .../bjc/utils/parserutils/TreeConstructor.java | 62 +++++++++++----------- 2 files changed, 52 insertions(+), 32 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils') 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); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index 46e4da4..68ec70e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -28,40 +28,38 @@ public class TreeConstructor { } @Override - public IPair>, AST> - apply(IPair>, AST> pair) { - Deque> queuedASTs = - pair.merge((queue, currentAST) -> queue); - - AST mergedAST = pair.merge((queue, currentAST) -> { - AST newAST; - - if (isSpecialOperator.test(element)) { - newAST = handleSpecialOperator.apply(queue); - } else { - if (queue.size() < 2) { - throw new IllegalStateException( - "Attempted to parse binary operator without enough operands.\n" - + "Problem operator is " - + element - + "\nPossible operand is: \n\t" - + queue.peek()); - } - - AST rightAST = queue.pop(); - AST leftAST = queue.pop(); - - newAST = new AST<>(element, leftAST, rightAST); + public IPair>, AST> apply( + IPair>, AST> pair) { + return pair.bind((queuedASTs, currentAST) -> { + return handleOperator(queuedASTs); + }); + } + + private IPair>, AST> handleOperator( + Deque> queuedASTs) { + AST newAST; + + if (isSpecialOperator.test(element)) { + newAST = handleSpecialOperator.apply(queuedASTs); + } else { + if (queuedASTs.size() < 2) { + throw new IllegalStateException( + "Attempted to parse binary operator without enough operands.\n" + + "Problem operator is " + + element + + "\nPossible operand is: \n\t" + + queuedASTs.peek()); } - queue.push(newAST); - return newAST; - }); + AST rightAST = queuedASTs.pop(); + AST leftAST = queuedASTs.pop(); + + newAST = new AST<>(element, leftAST, rightAST); + } - Pair>, AST> newPair = - new Pair<>(queuedASTs, mergedAST); + queuedASTs.push(newAST); - return newPair; + return new Pair<>(queuedASTs, newAST); } } @@ -162,8 +160,8 @@ public class TreeConstructor { "Special operator determiner must not be null"); } - GenHolder>, AST>> initialState = - new GenHolder<>(new Pair<>(new LinkedList<>(), null)); + GenHolder>, AST>> initialState = new GenHolder<>( + new Pair<>(new LinkedList<>(), null)); tokens.forEach( new TokenTransformer<>(initialState, operatorPredicate, -- cgit v1.2.3