From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../src/main/java/bjc/utils/parserutils/AST.java | 152 ++++++++++++--------- .../java/bjc/utils/parserutils/IPrecedent.java | 6 +- .../utils/parserutils/RuleBasedConfigReader.java | 57 ++++---- .../java/bjc/utils/parserutils/ShuntingYard.java | 72 +++++----- .../bjc/utils/parserutils/TreeConstructor.java | 71 +++++----- 5 files changed, 196 insertions(+), 162 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 9ced090..4bfb469 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -6,7 +6,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; -import bjc.utils.funcdata.ITreePart.TreeLinearizationMethod; +import bjc.utils.funcdata.bst.ITreePart.TreeLinearizationMethod; /** * A simple binary tree meant for use as an AST @@ -55,36 +55,37 @@ public class AST { /** * Traverse an AST * - * @param tlm + * @param linearizationMethod * The way to traverse the tree - * @param con + * @param action * The function to call on each traversed element */ - public void traverse(TreeLinearizationMethod tlm, Consumer con) { + public void traverse(TreeLinearizationMethod linearizationMethod, + Consumer action) { if (left != null && right != null) { - switch (tlm) { + switch (linearizationMethod) { case INORDER: - left.traverse(tlm, con); - con.accept(token); - right.traverse(tlm, con); + left.traverse(linearizationMethod, action); + action.accept(token); + right.traverse(linearizationMethod, action); break; case POSTORDER: - left.traverse(tlm, con); - right.traverse(tlm, con); - con.accept(token); + left.traverse(linearizationMethod, action); + right.traverse(linearizationMethod, action); + action.accept(token); break; case PREORDER: - con.accept(token); - left.traverse(tlm, con); - right.traverse(tlm, con); + action.accept(token); + left.traverse(linearizationMethod, action); + right.traverse(linearizationMethod, action); break; default: throw new IllegalArgumentException( - "Got a invalid tree linearizer " + tlm - + ". WAT"); + "Got a invalid tree linearizer " + + linearizationMethod + ". WAT"); } } else { - con.accept(token); + action.accept(token); } } @@ -95,107 +96,131 @@ public class AST { * The final value of the collapsed tree * @param * - * @param tokenTransform + * @param tokenTransformer * The function to transform nodes into data - * @param nodeTransform + * @param nodeTransformer * A map of functions for operator collapsing - * @param resultTransform + * @param resultTransformer * The function for transforming the result * @return The collapsed value of the tree */ - public E collapse(Function tokenTransform, - Function> nodeTransform, - Function resultTransform) { - return resultTransform - .apply(internalCollapse(tokenTransform, nodeTransform)); + public E collapse(Function tokenTransformer, + Function> nodeTransformer, + Function resultTransformer) { + return resultTransformer.apply( + internalCollapse(tokenTransformer, nodeTransformer)); } /* * Internal recursive collapser */ - protected T2 internalCollapse(Function tokenTransform, - Function> nodeTransform) { + protected T2 internalCollapse(Function tokenTransformer, + Function> nodeTransformer) { if (left == null && right == null) { - return tokenTransform.apply(token); + return tokenTransformer.apply(token); } else { - T2 leftCollapsed = left.internalCollapse(tokenTransform, - nodeTransform); - T2 rightCollapsed = right.internalCollapse(tokenTransform, - nodeTransform); + 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 nodeTransform.apply(token).apply(leftCollapsed, + return nodeTransformer.apply(token).apply(leftCollapsed, rightCollapsed); } } @Override public String toString() { - StringBuilder sb = new StringBuilder(); + StringBuilder builder = new StringBuilder(); - internalToString(sb, -1); + internalToString(builder, -1); - return sb.toString(); + return builder.toString(); } /** * Internal version of toString for proper rendering * - * @param sb + * @param builder * The string rendering being built * @param indentLevel * The current level to indent the tree */ - protected void internalToString(StringBuilder sb, int indentLevel) { - indentNLevels(sb, indentLevel); + protected void internalToString(StringBuilder builder, + int indentLevel) { + indentNLevels(builder, indentLevel); if (left == null && right == null) { - sb.append("Node: "); - sb.append(token.toString()); - sb.append("\n"); + builder.append("Node: "); + builder.append(token.toString()); + builder.append("\n"); } else { - sb.append("Node: "); - sb.append(token.toString()); - sb.append("\n"); - - left.internalToString(sb, indentLevel + 2); - right.internalToString(sb, indentLevel + 2); + builder.append("Node: "); + builder.append(token.toString()); + builder.append("\n"); + + if (left != null) { + left.internalToString(builder, indentLevel + 2); + } else { + indentNLevels(builder, indentLevel + 2); + builder.append("No left node\n"); + } + if (right != null) { + right.internalToString(builder, indentLevel + 2); + } else { + indentNLevels(builder, indentLevel + 2); + builder.append("No right node\n"); + } } } /** * Indent a string n levels * - * @param sb + * @param builder * The string to indent - * @param n + * @param levels * The number of levels to indent */ - protected static void indentNLevels(StringBuilder sb, int n) { - for (int i = 0; i <= n; i++) { - sb.append("\t"); + protected static void indentNLevels(StringBuilder builder, + int levels) { + for (int i = 0; i <= levels; i++) { + builder.append("\t"); } } /** * Execute a transform on selective nodes of the tree * - * @param transformPred + * @param transformerPredicate * The predicate to pick nodes to transform * @param transformer * The thing to use to transform the nodes */ - public void selectiveTransform(Predicate transformPred, + public void selectiveTransform(Predicate transformerPredicate, UnaryOperator transformer) { - if (transformPred.test(token)) { + if (transformerPredicate.test(token)) { token = transformer.apply(token); } if (left != null) { - left.selectiveTransform(transformPred, transformer); + left.selectiveTransform(transformerPredicate, transformer); } if (right != null) { - right.selectiveTransform(transformPred, transformer); + right.selectiveTransform(transformerPredicate, transformer); } } @@ -210,17 +235,18 @@ public class AST { * @return The AST with transformed tokens */ public AST transmuteAST(Function tokenTransformer) { - AST l = null; - AST r = null; + AST leftBranch = null; + AST rightBranch = null; if (left != null) { - l = left.transmuteAST(tokenTransformer); + leftBranch = left.transmuteAST(tokenTransformer); } if (right != null) { - r = right.transmuteAST(tokenTransformer); + rightBranch = right.transmuteAST(tokenTransformer); } - return new AST<>(tokenTransformer.apply(token), l, r); + return new AST<>(tokenTransformer.apply(token), leftBranch, + rightBranch); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java index 95ced43..287c8a9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java @@ -11,12 +11,12 @@ public interface IPrecedent { /** * Create a new object with set precedence * - * @param prec + * @param precedence * The precedence of the object to handle * @return A new object with set precedence */ - public static IPrecedent newSimplePrecedent(int prec) { - return () -> prec; + public static IPrecedent newSimplePrecedent(int precedence) { + return () -> precedence; } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java index 12f6891..46f5f1d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -51,62 +51,63 @@ public class RuleBasedConfigReader { /** * Add a pragma to this reader * - * @param pragName + * @param pragmaName * The name of the pragma to add - * @param pragAct + * @param pragmaAction * The function to execute when this pragma is read */ - public void addPragma(String pragName, - BiConsumer pragAct) { - pragmas.put(pragName, pragAct); + public void addPragma(String pragmaName, + BiConsumer pragmaAction) { + pragmas.put(pragmaName, pragmaAction); } /** * Run a stream through this reader * - * @param is + * @param inputStream * The stream to get input - * @param initState + * @param initialState * The initial state of the reader * @return The final state of the reader */ - public E fromStream(InputStream is, E initState) { - Scanner scn = new Scanner(is); + public E fromStream(InputStream inputStream, E initialState) { + Scanner inputSource = new Scanner(inputStream); - E stat = initState; + E state = initialState; - while (scn.hasNextLine()) { - String ln = scn.nextLine(); + while (inputSource.hasNextLine()) { + String line = inputSource.nextLine(); - if (ln.equals("")) { - endRule.accept(stat); + if (line.equals("")) { + endRule.accept(state); continue; - } else if (ln.startsWith("\t")) { + } else if (line.startsWith("\t")) { continueRule.accept(new FunctionalStringTokenizer( - ln.substring(1), " "), stat); + line.substring(1), " "), state); } else { - FunctionalStringTokenizer stk = - new FunctionalStringTokenizer(ln, " "); + FunctionalStringTokenizer tokenizer = + new FunctionalStringTokenizer(line, " "); - String nxtToken = stk.nextToken(); - if (nxtToken.equals("#")) { + String nextToken = tokenizer.nextToken(); + if (nextToken.equals("#")) { // Do nothing, this is a comment - } else if (nxtToken.equals("pragma")) { - String tk = stk.nextToken(); + } else if (nextToken.equals("pragma")) { + String token = tokenizer.nextToken(); - pragmas.getOrDefault(tk, (strk, ras) -> { + pragmas.getOrDefault(token, (tokenzer, stat) -> { throw new UnknownPragmaException( - "Unknown pragma " + tk); - }).accept(stk, stat); + "Unknown pragma " + token); + }).accept(tokenizer, state); } else { - startRule.accept(stk, new Pair<>(nxtToken, stat)); + startRule.accept(tokenizer, + new Pair<>(nextToken, state)); } } } - scn.close(); + inputSource.close(); - return stat; + return state; } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java index 3bb6bed..0ca1879 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -21,23 +21,23 @@ import bjc.utils.funcutils.StringUtils; public class ShuntingYard { private final class TokenShunter implements Consumer { - private FunctionalList outp; + private FunctionalList output; private Deque stack; private Function transform; - public TokenShunter(FunctionalList outp, Deque stack, + public TokenShunter(FunctionalList outpt, Deque stack, Function transform) { - this.outp = outp; + this.output = outpt; this.stack = stack; this.transform = transform; } @Override public void accept(String token) { - if (ops.containsKey(token)) { + if (operators.containsKey(token)) { while (!stack.isEmpty() && isHigherPrec(token, stack.peek())) { - outp.add(transform.apply(stack.pop())); + output.add(transform.apply(stack.pop())); } stack.push(token); @@ -45,12 +45,12 @@ public class ShuntingYard { stack.push(token); } else if (StringUtils.containsOnly(token, "\\)")) { while (stack.peek().equals(token.replace(')', '('))) { - outp.add(transform.apply(stack.pop())); + output.add(transform.apply(stack.pop())); } stack.pop(); } else { - outp.add(transform.apply(token)); + output.add(transform.apply(token)); } } } @@ -81,8 +81,8 @@ public class ShuntingYard { private final int precedence; - private Operator(int p) { - precedence = p; + private Operator(int prec) { + precedence = prec; } /* @@ -99,70 +99,72 @@ public class ShuntingYard { /** * Holds all the shuntable operations */ - private Map ops; + private Map operators; /** * Create a new shunting yard with a default set of operators */ public ShuntingYard() { - ops = new HashMap<>(); + operators = new HashMap<>(); - ops.put("+", Operator.ADD); - ops.put("-", Operator.SUBTRACT); - ops.put("*", Operator.MULTIPLY); - ops.put("/", Operator.DIVIDE); + operators.put("+", Operator.ADD); + operators.put("-", Operator.SUBTRACT); + operators.put("*", Operator.MULTIPLY); + operators.put("/", Operator.DIVIDE); } /** * Add an operator to the list of shuntable operators * - * @param tok + * @param operatorToken * The token representing the operator - * @param i + * @param precedence * The precedence of the operator to add */ - public void addOp(String tok, int i) { - this.addOp(tok, IPrecedent.newSimplePrecedent(i)); + public void addOp(String operatorToken, int precedence) { + this.addOp(operatorToken, + IPrecedent.newSimplePrecedent(precedence)); } /** * Add an operator to the list of shuntable operators * - * @param tok + * @param token * The token representing the operator - * @param prec + * @param precedence * The precedence of the operator */ - public void addOp(String tok, IPrecedent prec) { - ops.put(tok, prec); + public void addOp(String token, IPrecedent precedence) { + operators.put(token, precedence); } - private boolean isHigherPrec(String op, String sub) { - return (ops.containsKey(sub) && ops.get(sub).getPrecedence() >= ops - .get(op).getPrecedence()); + private boolean isHigherPrec(String operator, String rightOperator) { + return (operators.containsKey(rightOperator) && operators + .get(rightOperator).getPrecedence() >= operators + .get(operator).getPrecedence()); } /** * Transform a string of tokens from infix notation to postfix * - * @param inp + * @param input * The string to transform - * @param transform + * @param tokenTransformer * The function to use to transform strings to tokens * @return A list of tokens in postfix notation */ - public FunctionalList postfix(FunctionalList inp, - Function transform) { - FunctionalList outp = new FunctionalList<>(); + public FunctionalList postfix(FunctionalList input, + Function tokenTransformer) { + FunctionalList output = new FunctionalList<>(); Deque stack = new LinkedList<>(); - inp.forEach(new TokenShunter(outp, stack, transform)); + input.forEach(new TokenShunter(output, stack, tokenTransformer)); while (!stack.isEmpty()) { - outp.add(transform.apply(stack.pop())); + output.add(tokenTransformer.apply(stack.pop())); } - return outp; + return output; } /** @@ -172,6 +174,6 @@ public class ShuntingYard { * The token representing the operator */ public void removeOp(String tok) { - ops.remove(tok); + operators.remove(tok); } } \ No newline at end of file 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 6339d8c..42d5a9d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -23,9 +23,9 @@ public class TreeConstructor { * * @param * The elements of the parse tree - * @param toks + * @param tokens * The list of tokens to build a tree from - * @param opPredicate + * @param operatorPredicate * The predicate to use to determine if something is a * operator * @return A AST from the expression @@ -34,9 +34,10 @@ public class TreeConstructor { * {@link TreeConstructor#constructTree(FunctionalList, Predicate, Predicate, Function)} * instead */ - public static AST constructTree(FunctionalList toks, - Predicate opPredicate) { - return constructTree(toks, opPredicate, (op) -> false, null); + public static AST constructTree(FunctionalList tokens, + Predicate operatorPredicate) { + return constructTree(tokens, operatorPredicate, (op) -> false, + null); } /** @@ -46,15 +47,15 @@ public class TreeConstructor { * * @param * The elements of the parse tree - * @param toks + * @param tokens * The list of tokens to build a tree from - * @param opPredicate + * @param operatorPredicate * The predicate to use to determine if something is a * operator - * @param isSpecialOp + * @param isSpecialOperator * The predicate to use to determine if an operator needs * special handling - * @param handleSpecialOp + * @param handleSpecialOperator * The function to use to handle special case operators * @return A AST from the expression * @@ -62,51 +63,55 @@ public class TreeConstructor { * interface. Maybe there's a better way to express how that * works */ - public static AST constructTree(FunctionalList toks, - Predicate opPredicate, Predicate isSpecialOp, - Function>, AST> handleSpecialOp) { - GenHolder>, AST>> initState = + public static AST constructTree(FunctionalList tokens, + Predicate operatorPredicate, Predicate isSpecialOperator, + Function>, AST> handleSpecialOperator) { + GenHolder>, AST>> initialState = new GenHolder<>(new Pair<>(new LinkedList<>(), null)); - toks.forEach((ele) -> { - if (opPredicate.test(ele)) { - initState.transform((par) -> { - Deque> lft = par.merge((deq, ast) -> deq); + tokens.forEach((element) -> { + if (operatorPredicate.test(element)) { + initialState.transform((pair) -> { + Deque> queuedASTs = + pair.merge((queue, currentAST) -> queue); - AST mergedAST = par.merge((deq, ast) -> { + AST mergedAST = pair.merge((queue, currentAST) -> { AST newAST; - if (isSpecialOp.test(ele)) { - newAST = handleSpecialOp.apply(deq); + if (isSpecialOperator.test(element)) { + newAST = handleSpecialOperator.apply(queue); } else { - AST right = deq.pop(); - AST left = deq.pop(); - newAST = new AST<>(ele, left, right); + AST rightAST = queue.pop(); + AST leftAST = queue.pop(); + + newAST = new AST<>(element, leftAST, rightAST); } - deq.push(newAST); + queue.push(newAST); return newAST; }); Pair>, AST> newPair = - new Pair<>(lft, mergedAST); + new Pair<>(queuedASTs, mergedAST); return newPair; }); } else { - AST newAST = new AST<>(ele); + AST newAST = new AST<>(element); - initState.doWith((par) -> par.doWith((deq, ast) -> { - deq.push(newAST); - })); + initialState.doWith( + (pair) -> pair.doWith((queue, currentAST) -> { + queue.push(newAST); + })); - initState.transform((par) -> { - return (Pair>, AST>) par - .apply((d) -> d, (a) -> newAST); + initialState.transform((pair) -> { + return (Pair>, AST>) pair.apply( + (queue) -> queue, (currentAST) -> newAST); }); } }); - return initState.unwrap((par) -> par.merge((deq, ast) -> ast)); + return initialState.unwrap( + (pair) -> pair.merge((queue, currentAST) -> currentAST)); } } -- cgit v1.2.3