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-06 13:50:00 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-06 13:50:00 -0400
commit79d3a4a47cbc1fcf17c77c6fc12ff826a3077bac (patch)
treea69e533c558326d583b3aee891fc815208c7b650 /BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java
parent4355418164c44170cfb329fcbb7e6f1358c0e314 (diff)
Minor bugfixes/changes, as well as beginnings of CLI systems
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.java157
1 files changed, 81 insertions, 76 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 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
@@ -68,6 +68,57 @@ public class AST<T> {
}
/**
+ * Apply an action to the head node of this AST
+ *
+ * @param <E>
+ * 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> E applyToHead(Function<T, E> 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 <E>
+ * 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> E applyToLeft(Function<AST<T>, 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 <E>
+ * 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> E applyToRight(Function<AST<T>, E> rightAction) {
+ if (rightAction == null) {
+ throw new NullPointerException("Action must not be null");
+ }
+
+ return rightAction.apply(right);
+ }
+
+ /**
* Collapse this tree into a single node
*
* @param <E>
@@ -97,12 +148,36 @@ public class AST<T> {
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> T2 collapseBranches(Function<T, T2> tokenTransformer,
+ Function<T, BinaryOperator<T2>> 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<T> {
Function<T, BinaryOperator<T2>> 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<T> {
action.accept(token);
}
}
-
- /**
- * Apply an action to the head node of this AST
- *
- * @param <E>
- * 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> E applyToHead(Function<T, E> 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 <E>
- * 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> E applyToLeft(Function<AST<T>, 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 <E>
- * 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> E applyToRight(Function<AST<T>, E> rightAction) {
- if (rightAction == null) {
- throw new NullPointerException("Action must not be null");
- }
-
- return rightAction.apply(right);
- }
}