diff options
| author | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
| commit | 504ca816530efdff06bc202e0432ebd354aec304 (patch) | |
| tree | 4836932fb81d1d625470502c78c94d202c9a7420 /BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java | |
| parent | 5c1163df17c46f7d3e15b6c7949c38843ec56146 (diff) | |
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java | 60 |
1 files changed, 27 insertions, 33 deletions
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 19c1971..2a0bdff 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -1,19 +1,19 @@ package bjc.utils.parserutils; -import java.util.Deque; -import java.util.LinkedList; -import java.util.function.Consumer; -import java.util.function.Function; - import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.StringUtils; +import java.util.Deque; +import java.util.LinkedList; +import java.util.function.Consumer; +import java.util.function.Function; + /** * Utility to run the shunting yard algorithm on a bunch of tokens - * + * * @author ben * * @param <TokenType> @@ -22,7 +22,7 @@ import bjc.utils.funcutils.StringUtils; public class ShuntingYard<TokenType> { /** * A enum representing the fundamental operator types - * + * * @author ben * */ @@ -58,9 +58,9 @@ public class ShuntingYard<TokenType> { } private final class TokenShunter implements Consumer<String> { - private IList<TokenType> output; - private Deque<String> stack; - private Function<String, TokenType> transformer; + private IList<TokenType> output; + private Deque<String> stack; + private Function<String, TokenType> transformer; public TokenShunter(IList<TokenType> outpt, Deque<String> stack, Function<String, TokenType> transformer) { @@ -72,26 +72,26 @@ public class ShuntingYard<TokenType> { @Override public void accept(String token) { // Handle operators - if (operators.containsKey(token)) { + if(operators.containsKey(token)) { // Pop operators while there isn't a higher // precedence one - while (!stack.isEmpty() && isHigherPrec(token, stack.peek())) { + while(!stack.isEmpty() && isHigherPrec(token, stack.peek())) { output.add(transformer.apply(stack.pop())); } // Put this operator onto the stack stack.push(token); - } else if (StringUtils.containsOnly(token, "\\(")) { + } else if(StringUtils.containsOnly(token, "\\(")) { // Handle groups of parenthesis for multiple // nesting levels stack.push(token); - } else if (StringUtils.containsOnly(token, "\\)")) { + } else if(StringUtils.containsOnly(token, "\\)")) { // Handle groups of parenthesis for multiple // nesting levels String swappedToken = token.replace(')', '('); // Remove tokens up to a matching parenthesis - while (!stack.peek().equals(swappedToken)) { + while(!stack.peek().equals(swappedToken)) { output.add(transformer.apply(stack.pop())); } @@ -111,7 +111,7 @@ public class ShuntingYard<TokenType> { /** * Create a new shunting yard with a default set of operators - * + * * @param configureBasics * Whether or not basic math operators should be provided */ @@ -119,7 +119,7 @@ public class ShuntingYard<TokenType> { operators = new FunctionalMap<>(); // Add basic operators if we're configured to do so - if (configureBasics) { + if(configureBasics) { operators.put("+", Operator.ADD); operators.put("-", Operator.SUBTRACT); operators.put("*", Operator.MULTIPLY); @@ -129,7 +129,7 @@ public class ShuntingYard<TokenType> { /** * Add an operator to the list of shuntable operators - * + * * @param operatorToken * The token representing the operator * @param precedence @@ -144,7 +144,7 @@ public class ShuntingYard<TokenType> { /** * Add an operator to the list of shuntable operators - * + * * @param operatorToken * The token representing the operator * @param precedence @@ -152,11 +152,9 @@ public class ShuntingYard<TokenType> { */ public void addOp(String operator, IPrecedent precedence) { // Complain about trying to add an incorrect operator - if (operator == null) { + if(operator == null) throw new NullPointerException("Operator must not be null"); - } else if (precedence == null) { - throw new NullPointerException("Precedence 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(operator, precedence); @@ -167,9 +165,7 @@ public class ShuntingYard<TokenType> { boolean exists = operators.containsKey(right); // If it doesn't, the left is higher precedence. - if (!exists) { - return false; - } + if(!exists) return false; // Get the precedence of operators int rightPrecedence = operators.get(right).getPrecedence(); @@ -181,7 +177,7 @@ public class ShuntingYard<TokenType> { /** * Transform a string of tokens from infix notation to postfix - * + * * @param input * The string to transform * @param transformer @@ -190,11 +186,9 @@ public class ShuntingYard<TokenType> { */ public IList<TokenType> postfix(IList<String> input, Function<String, TokenType> transformer) { // Check our input - if (input == null) { + if(input == null) throw new NullPointerException("Input must not be null"); - } else if (transformer == null) { - throw new NullPointerException("Transformer must not be null"); - } + else if(transformer == null) throw new NullPointerException("Transformer must not be null"); // Here's what we're handing back IList<TokenType> output = new FunctionalList<>(); @@ -215,14 +209,14 @@ public class ShuntingYard<TokenType> { /** * Remove an operator from the list of shuntable operators - * + * * @param token * The token representing the operator. If null, remove * all operators */ public void removeOp(String operator) { // Check if we want to remove all operators - if (operator == null) { + if(operator == null) { operators = new FunctionalMap<>(); } else { operators.remove(operator); |
