From 79d3a4a47cbc1fcf17c77c6fc12ff826a3077bac Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 6 Apr 2016 13:50:00 -0400 Subject: Minor bugfixes/changes, as well as beginnings of CLI systems --- .../src/main/java/bjc/utils/parserutils/AST.java | 157 +++++++++++---------- 1 file changed, 81 insertions(+), 76 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 4b00b63..90961d5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -67,6 +67,57 @@ public class AST { right = rght; } + /** + * Apply an action to the head node of this AST + * + * @param + * The type of the returned value + * @param headAction + * The action to apply to the head node + * @return The result of applying the action + */ + public E applyToHead(Function headAction) { + if (headAction == null) { + throw new NullPointerException("Action must not be null"); + } + + return headAction.apply(token); + } + + /** + * Apply an action to the left side of this AST + * + * @param + * The type of the returned value + * @param leftAction + * The action to apply to the left side + * @return The result of applying the action + */ + public E applyToLeft(Function, E> leftAction) { + if (leftAction == null) { + throw new NullPointerException("Action must not be null"); + } + + return leftAction.apply(left); + } + + /** + * Apply an action to the right side of this AST + * + * @param + * The type of the returned value + * @param rightAction + * The action to apply to the right side + * @return The result of applying the action + */ + public E applyToRight(Function, E> rightAction) { + if (rightAction == null) { + throw new NullPointerException("Action must not be null"); + } + + return rightAction.apply(right); + } + /** * Collapse this tree into a single node * @@ -97,12 +148,36 @@ public class AST { if (resultTransformer != null) { return resultTransformer.apply( internalCollapse(tokenTransformer, nodeTransformer)); + } + + // This is valid because if the user passes null as the last + // parameter, E will be inferred as Object, but will actually + // be T2 + return (E) internalCollapse(tokenTransformer, nodeTransformer); + } + + private T2 collapseBranches(Function tokenTransformer, + Function> nodeTransformer) { + T2 leftCollapsed; + + if (left == null) { + leftCollapsed = null; } else { - // This is valid because if the user passes null as the last - // parameter, E will be inferred as Object, but will actually - // be T2 - return (E) internalCollapse(tokenTransformer, nodeTransformer); + leftCollapsed = left.internalCollapse(tokenTransformer, + nodeTransformer); } + + T2 rightCollapsed; + + if (right == null) { + rightCollapsed = null; + } else { + rightCollapsed = right.internalCollapse(tokenTransformer, + nodeTransformer); + } + + return nodeTransformer.apply(token).apply(leftCollapsed, + rightCollapsed); } /** @@ -129,28 +204,9 @@ public class AST { Function> nodeTransformer) { if (left == null && right == null) { return tokenTransformer.apply(token); - } else { - T2 leftCollapsed; - - if (left == null) { - leftCollapsed = null; - } else { - leftCollapsed = left.internalCollapse(tokenTransformer, - nodeTransformer); - } - - T2 rightCollapsed; - - if (right == null) { - rightCollapsed = null; - } else { - rightCollapsed = right.internalCollapse(tokenTransformer, - nodeTransformer); - } - - return nodeTransformer.apply(token).apply(leftCollapsed, - rightCollapsed); } + + return collapseBranches(tokenTransformer, nodeTransformer); } /** @@ -302,55 +358,4 @@ public class AST { action.accept(token); } } - - /** - * Apply an action to the head node of this AST - * - * @param - * The type of the returned value - * @param headAction - * The action to apply to the head node - * @return The result of applying the action - */ - public E applyToHead(Function headAction) { - if (headAction == null) { - throw new NullPointerException("Action must not be null"); - } - - return headAction.apply(token); - } - - /** - * Apply an action to the left side of this AST - * - * @param - * The type of the returned value - * @param leftAction - * The action to apply to the left side - * @return The result of applying the action - */ - public E applyToLeft(Function, E> leftAction) { - if (leftAction == null) { - throw new NullPointerException("Action must not be null"); - } - - return leftAction.apply(left); - } - - /** - * Apply an action to the right side of this AST - * - * @param - * The type of the returned value - * @param rightAction - * The action to apply to the right side - * @return The result of applying the action - */ - public E applyToRight(Function, E> rightAction) { - if (rightAction == null) { - throw new NullPointerException("Action must not be null"); - } - - return rightAction.apply(right); - } } -- cgit v1.2.3