From d2af58b0f68ebfbba2be7e7679efec6c8c0af12f Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 9 Feb 2017 11:50:31 -0500 Subject: Update --- .../java/bjc/utils/parserutils/IPrecedent.java | 2 +- .../java/bjc/utils/parserutils/ShuntingYard.java | 55 ++++++++++------------ .../bjc/utils/parserutils/TokenTransformer.java | 6 +-- .../bjc/utils/parserutils/TreeConstructor.java | 18 +++---- 4 files changed, 38 insertions(+), 43 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils') 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 287c8a9..31ceecd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java @@ -25,4 +25,4 @@ public interface IPrecedent { * @return The precedence of the attached object */ public int getPrecedence(); -} \ No newline at end of file +} 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 99e3e60..fe046f4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -51,11 +51,6 @@ public class ShuntingYard { precedence = prec; } - /* - * (non-Javadoc) - * - * @see bjc.utils.parserutils.IPrecedent#getPrecedence() - */ @Override public int getPrecedence() { return precedence; @@ -65,13 +60,13 @@ public class ShuntingYard { private final class TokenShunter implements Consumer { private IList output; private Deque stack; - private Function transform; + private Function transformer; public TokenShunter(IList outpt, Deque stack, - Function transform) { + Function transformer) { this.output = outpt; this.stack = stack; - this.transform = transform; + this.transformer = transformer; } @Override @@ -81,7 +76,7 @@ public class ShuntingYard { // Pop operators while there isn't a higher precedence one while (!stack.isEmpty() && isHigherPrec(token, stack.peek())) { - output.add(transform.apply(stack.pop())); + output.add(transformer.apply(stack.pop())); } // Put this operator onto the stack @@ -95,19 +90,19 @@ public class ShuntingYard { // Remove tokens up to a matching parenthesis while (!stack.peek().equals(swappedToken)) { - output.add(transform.apply(stack.pop())); + output.add(transformer.apply(stack.pop())); } // Remove the parenthesis stack.pop(); } else { // Just add the transformed token - output.add(transform.apply(token)); + output.add(transformer.apply(token)); } } } - /** + /* * Holds all the shuntable operations */ private IMap operators; @@ -138,11 +133,11 @@ public class ShuntingYard { * @param precedence * The precedence of the operator to add */ - public void addOp(String operatorToken, int precedence) { + public void addOp(String operator, int precedence) { // Create the precedence marker IPrecedent prec = IPrecedent.newSimplePrecedent(precedence); - this.addOp(operatorToken, prec); + this.addOp(operator, prec); } /** @@ -153,30 +148,30 @@ public class ShuntingYard { * @param precedence * The precedence of the operator */ - public void addOp(String operatorToken, IPrecedent precedence) { + public void addOp(String operator, IPrecedent precedence) { // Complain about trying to add an incorrect operator - if (operatorToken == null) { + if (operator == null) { throw new NullPointerException("Operator must not be null"); } else if(precedence == null) { throw new NullPointerException("Precedence must not be null"); } // Add the operator to the ones we handle - operators.put(operatorToken, precedence); + operators.put(operator, precedence); } - private boolean isHigherPrec(String leftOperator, String rightOperator) { + private boolean isHigherPrec(String left, String right) { // Check if the right operator exists - boolean operatorExists = operators.containsKey(rightOperator); + boolean exists = operators.containsKey(right); // If it doesn't, the left is higher precedence. - if (!operatorExists) { + if (!exists) { return false; } // Get the precedence of operators - int rightPrecedence = operators.get(rightOperator).getPrecedence(); - int leftPrecedence = operators.get(leftOperator).getPrecedence(); + int rightPrecedence = operators.get(right).getPrecedence(); + int leftPrecedence = operators.get(left).getPrecedence(); // Evaluate what we were asked return rightPrecedence >= leftPrecedence; @@ -187,16 +182,16 @@ public class ShuntingYard { * * @param input * The string to transform - * @param tokenTransformer + * @param transformer * The function to use to transform strings to tokens * @return A list of tokens in postfix notation */ public IList postfix(IList input, - Function tokenTransformer) { + Function transformer) { // Check our input if (input == null) { throw new NullPointerException("Input must not be null"); - } else if (tokenTransformer == null) { + } else if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -207,11 +202,11 @@ public class ShuntingYard { Deque stack = new LinkedList<>(); // Shunt the tokens - input.forEach(new TokenShunter(output, stack, tokenTransformer)); + input.forEach(new TokenShunter(output, stack, transformer)); // Transform any resulting tokens stack.forEach((token) -> { - output.add(tokenTransformer.apply(token)); + output.add(transformer.apply(token)); }); return output; @@ -224,12 +219,12 @@ public class ShuntingYard { * The token representing the operator. If null, remove all * operators */ - public void removeOp(String token) { + public void removeOp(String operator) { // Check if we want to remove all operators - if (token == null) { + if (operator == null) { operators = new FunctionalMap<>(); } else { - operators.remove(token); + operators.remove(operator); } } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java index ff3a6b9..9953c00 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -52,11 +52,11 @@ final class TokenTransformer implements Consumer { } // Grab the two operands - ITree rightAST = queuedASTs.pop(); - ITree leftAST = queuedASTs.pop(); + ITree right = queuedASTs.pop(); + ITree left = queuedASTs.pop(); // Create a new AST - newAST = new Tree<>(element, leftAST, rightAST); + newAST = new Tree<>(element, left, right); } // Stick it onto the stack 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 0b61363..7f933c0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -28,16 +28,16 @@ public class TreeConstructor { * The elements of the parse tree * @param tokens * The list of tokens to build a tree from - * @param operatorPredicate + * @param isOperator * The predicate to use to determine if something is a * operator * @return A AST from the expression */ public static ITree constructTree( IList tokens, - Predicate operatorPredicate) { + Predicate isOperator) { // Construct a tree with no special operators - return constructTree(tokens, operatorPredicate, (op) -> false, + return constructTree(tokens, isOperator, (op) -> false, null); } @@ -51,7 +51,7 @@ public class TreeConstructor { * The elements of the parse tree * @param tokens * The list of tokens to build a tree from - * @param operatorPredicate + * @param isOperator * The predicate to use to determine if something is a * operator * @param isSpecialOperator @@ -62,19 +62,19 @@ public class TreeConstructor { * @return A AST from the expression * * FIXME The handleSpecialOp function seems like an ugly - * interface. Maybe there's a better way to express how that - * works + * interface. Maybe there's a better way to express how + * that works. */ public static ITree constructTree( IList tokens, - Predicate operatorPredicate, + Predicate isOperator, Predicate isSpecialOperator, Function>, ITree>> handleSpecialOperator) { // Make sure our parameters are valid if (tokens == null) { throw new NullPointerException("Tokens must not be null"); - } else if (operatorPredicate == null) { + } else if (isOperator == null) { throw new NullPointerException( "Operator predicate must not be null"); } else if (isSpecialOperator == null) { @@ -89,7 +89,7 @@ public class TreeConstructor { // Transform each of the tokens tokens.forEach( - new TokenTransformer<>(initialState, operatorPredicate, + new TokenTransformer<>(initialState, isOperator, isSpecialOperator, handleSpecialOperator)); // Grab the tree from the state -- cgit v1.2.3