summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
diff options
context:
space:
mode:
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.java82
1 files changed, 41 insertions, 41 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 b30a69c..44744f5 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
@@ -1,16 +1,16 @@
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.
*
@@ -47,7 +47,7 @@ public class ShuntingYard<TokenType> {
private final int precedence;
- private Operator(int prec) {
+ private Operator(final int prec) {
precedence = prec;
}
@@ -58,40 +58,40 @@ 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 final IList<TokenType> output;
+ private final Deque<String> stack;
+ private final Function<String, TokenType> transformer;
- public TokenShunter(IList<TokenType> outpt, Deque<String> stack,
- Function<String, TokenType> transformer) {
+ public TokenShunter(final IList<TokenType> outpt, final Deque<String> stack,
+ final Function<String, TokenType> transformer) {
this.output = outpt;
this.stack = stack;
this.transformer = transformer;
}
@Override
- public void accept(String token) {
+ public void accept(final 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(')', '(');
+ final 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()));
}
@@ -116,11 +116,11 @@ public class ShuntingYard<TokenType> {
* Whether or not basic math operators should be
* provided.
*/
- public ShuntingYard(boolean configureBasics) {
+ public ShuntingYard(final boolean configureBasics) {
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);
@@ -133,15 +133,15 @@ public class ShuntingYard<TokenType> {
*
* @param operator
* The token representing the operator.
- *
+ *
* @param precedence
* The precedence of the operator to add.
*/
- public void addOp(String operator, int precedence) {
+ public void addOp(final String operator, final int precedence) {
/*
* Create the precedence marker
*/
- IPrecedent prec = IPrecedent.newSimplePrecedent(precedence);
+ final IPrecedent prec = IPrecedent.newSimplePrecedent(precedence);
this.addOp(operator, prec);
}
@@ -151,17 +151,17 @@ public class ShuntingYard<TokenType> {
*
* @param operator
* The token representing the operator.
- *
+ *
* @param precedence
* The precedence of the operator.
*/
- public void addOp(String operator, IPrecedent precedence) {
+ public void addOp(final String operator, final 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
@@ -169,16 +169,16 @@ public class ShuntingYard<TokenType> {
operators.put(operator, precedence);
}
- private boolean isHigherPrec(String left, String right) {
+ private boolean isHigherPrec(final String left, final String right) {
// Check if the right operator exists
- boolean exists = operators.containsKey(right);
+ final 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();
- int leftPrecedence = operators.get(left).getPrecedence();
+ final int rightPrecedence = operators.get(right).getPrecedence();
+ final int leftPrecedence = operators.get(left).getPrecedence();
// Evaluate what we were asked
return rightPrecedence >= leftPrecedence;
@@ -189,23 +189,23 @@ public class ShuntingYard<TokenType> {
*
* @param input
* The string to transform.
- *
+ *
* @param transformer
* The function to use to transform strings to tokens.
- *
+ *
* @return A list of tokens in postfix notation.
*/
- public IList<TokenType> postfix(IList<String> input, Function<String, TokenType> transformer) {
+ public IList<TokenType> postfix(final IList<String> input, final 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<>();
+ final IList<TokenType> output = new FunctionalList<>();
// The stack to put operators on
- Deque<String> stack = new LinkedList<>();
+ final Deque<String> stack = new LinkedList<>();
// Shunt the tokens
input.forEach(new TokenShunter(output, stack, transformer));
@@ -225,9 +225,9 @@ public class ShuntingYard<TokenType> {
* The token representing the operator. If null, remove
* all operators.
*/
- public void removeOp(String operator) {
+ public void removeOp(final String operator) {
// Check if we want to remove all operators
- if(operator == null) {
+ if (operator == null) {
operators = new FunctionalMap<>();
} else {
operators.remove(operator);