summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
commitd4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch)
tree1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
parent2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff)
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/parserutils/TreeConstructor.java')
-rw-r--r--base/src/main/java/bjc/utils/parserutils/TreeConstructor.java64
1 files changed, 36 insertions, 28 deletions
diff --git a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
index 1ebc2d5..3c7509b 100644
--- a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
+++ b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
@@ -23,21 +23,25 @@ public class TreeConstructor {
* Alias interface for special operator types.
*
* @param <TokenType>
- * The token type of the tree.
+ * The token type of the tree.
*/
- public interface QueueFlattener<TokenType> extends Function<Deque<ITree<TokenType>>, ITree<TokenType>> {
+ public interface QueueFlattener<TokenType>
+ extends Function<Deque<ITree<TokenType>>, ITree<TokenType>> {
/*
* Alias
*/
}
/* Alias for constructor state. */
- static final class ConstructorState<TokenType> extends Pair<Deque<ITree<TokenType>>, ITree<TokenType>> {
- public ConstructorState(final Deque<ITree<TokenType>> left, final ITree<TokenType> right) {
+ static final class ConstructorState<TokenType>
+ extends Pair<Deque<ITree<TokenType>>, ITree<TokenType>> {
+ public ConstructorState(final Deque<ITree<TokenType>> left,
+ final ITree<TokenType> right) {
super(left, right);
}
- public ConstructorState(final IPair<Deque<ITree<TokenType>>, ITree<TokenType>> par) {
+ public ConstructorState(
+ final IPair<Deque<ITree<TokenType>>, ITree<TokenType>> par) {
super(par.getLeft(), par.getRight());
}
}
@@ -48,19 +52,19 @@ public class TreeConstructor {
* Only binary operators are accepted.
*
* @param <TokenType>
- * The elements of the parse tree.
+ * The elements of the parse tree.
*
* @param tokens
- * The list of tokens to build a tree from.
+ * The list of tokens to build a tree from.
*
* @param isOperator
- * The predicate to use to determine if something is a
- * operator.
+ * The predicate to use to determine if something is a
+ * operator.
*
* @return A AST from the expression.
*/
- public static <TokenType> ITree<TokenType> constructTree(final IList<TokenType> tokens,
- final Predicate<TokenType> isOperator) {
+ public static <TokenType> ITree<TokenType> constructTree(
+ final IList<TokenType> tokens, final Predicate<TokenType> isOperator) {
/* Construct a tree with no special operators */
return constructTree(tokens, isOperator, op -> false, null);
}
@@ -68,43 +72,47 @@ public class TreeConstructor {
/**
* Construct a tree from a list of tokens in postfix notation.
*
- * Only binary operators are accepted by default. Use the last two
- * parameters to handle non-binary operators.
+ * Only binary operators are accepted by default. Use the last two parameters to
+ * handle non-binary operators.
*
* @param <TokenType>
- * The elements of the parse tree.
+ * The elements of the parse tree.
*
* @param tokens
- * The list of tokens to build a tree from.
+ * The list of tokens to build a tree from.
*
* @param isOperator
- * The predicate to use to determine if something is a operator.
+ * The predicate to use to determine if something
+ * is a operator.
*
* @param isSpecialOperator
- * The predicate to use to determine if an operator needs special
- * handling.
+ * The predicate to use to determine if an operator
+ * needs special handling.
*
* @param handleSpecialOperator
- * The function to use to handle special case operators.
+ * The function to use to handle special case
+ * operators.
*
* @return A AST from the expression.
*
*/
- public static <TokenType> ITree<TokenType> constructTree(final IList<TokenType> tokens,
- final Predicate<TokenType> isOperator, final Predicate<TokenType> isSpecialOperator,
+ public static <TokenType> ITree<TokenType> constructTree(
+ final IList<TokenType> tokens, final Predicate<TokenType> isOperator,
+ final Predicate<TokenType> isSpecialOperator,
final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) {
/*
* Make sure our parameters are valid
*/
- if(tokens == null)
+ if (tokens == null)
throw new NullPointerException("Tokens must not be null");
- else if(isOperator == null)
+ else if (isOperator == null)
throw new NullPointerException("Operator predicate must not be null");
- else if(isSpecialOperator == null)
- throw new NullPointerException("Special operator determiner must not be null");
+ else if (isSpecialOperator == null)
+ throw new NullPointerException(
+ "Special operator determiner must not be null");
- final ConstructorState<TokenType> cstate = new ConstructorState<>(
- new LinkedList<>(), null);
+ final ConstructorState<TokenType> cstate
+ = new ConstructorState<>(new LinkedList<>(), null);
/* Here is the state for the tree construction */
final IHolder<ConstructorState<TokenType>> initialState = new Identity<>(cstate);
@@ -116,6 +124,6 @@ public class TreeConstructor {
tokens.forEach(trans);
/* Grab the tree from the state */
- return initialState.unwrap(pair -> pair.getRight());
+ return initialState.unwrap(ConstructorState::getRight);
}
}