summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-31 11:43:21 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-31 11:43:21 -0400
commit8a8b457c98e207d809a7616e73eb59bfe197a7a5 (patch)
tree36fcbb7f10e92adbfb866fced7f27af1ef89f636 /BJC-Utils2/src/main/java/bjc/utils/parserutils
parent32b1b46fcc855fffe6b0dddd10442a9a4f1544d2 (diff)
More code maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java152
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java6
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java57
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java72
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java71
5 files changed, 196 insertions, 162 deletions
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<T> {
/**
* 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<T> con) {
+ public void traverse(TreeLinearizationMethod linearizationMethod,
+ Consumer<T> 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<T> {
* The final value of the collapsed tree
* @param <T2>
*
- * @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, T2> E collapse(Function<T, T2> tokenTransform,
- Function<T, BinaryOperator<T2>> nodeTransform,
- Function<T2, E> resultTransform) {
- return resultTransform
- .apply(internalCollapse(tokenTransform, nodeTransform));
+ public <E, T2> E collapse(Function<T, T2> tokenTransformer,
+ Function<T, BinaryOperator<T2>> nodeTransformer,
+ Function<T2, E> resultTransformer) {
+ return resultTransformer.apply(
+ internalCollapse(tokenTransformer, nodeTransformer));
}
/*
* Internal recursive collapser
*/
- protected <T2> T2 internalCollapse(Function<T, T2> tokenTransform,
- Function<T, BinaryOperator<T2>> nodeTransform) {
+ protected <T2> T2 internalCollapse(Function<T, T2> tokenTransformer,
+ Function<T, BinaryOperator<T2>> 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<T> transformPred,
+ public void selectiveTransform(Predicate<T> transformerPredicate,
UnaryOperator<T> 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<T> {
* @return The AST with transformed tokens
*/
public <E> AST<E> transmuteAST(Function<T, E> tokenTransformer) {
- AST<E> l = null;
- AST<E> r = null;
+ AST<E> leftBranch = null;
+ AST<E> 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<E> {
/**
* 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<FunctionalStringTokenizer, E> pragAct) {
- pragmas.put(pragName, pragAct);
+ public void addPragma(String pragmaName,
+ BiConsumer<FunctionalStringTokenizer, E> 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<E> {
private final class TokenShunter implements Consumer<String> {
- private FunctionalList<E> outp;
+ private FunctionalList<E> output;
private Deque<String> stack;
private Function<String, E> transform;
- public TokenShunter(FunctionalList<E> outp, Deque<String> stack,
+ public TokenShunter(FunctionalList<E> outpt, Deque<String> stack,
Function<String, E> 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<E> {
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<E> {
private final int precedence;
- private Operator(int p) {
- precedence = p;
+ private Operator(int prec) {
+ precedence = prec;
}
/*
@@ -99,70 +99,72 @@ public class ShuntingYard<E> {
/**
* Holds all the shuntable operations
*/
- private Map<String, IPrecedent> ops;
+ private Map<String, IPrecedent> 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<E> postfix(FunctionalList<String> inp,
- Function<String, E> transform) {
- FunctionalList<E> outp = new FunctionalList<>();
+ public FunctionalList<E> postfix(FunctionalList<String> input,
+ Function<String, E> tokenTransformer) {
+ FunctionalList<E> output = new FunctionalList<>();
Deque<String> 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<E> {
* 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 <T>
* 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 <T> AST<T> constructTree(FunctionalList<T> toks,
- Predicate<T> opPredicate) {
- return constructTree(toks, opPredicate, (op) -> false, null);
+ public static <T> AST<T> constructTree(FunctionalList<T> tokens,
+ Predicate<T> operatorPredicate) {
+ return constructTree(tokens, operatorPredicate, (op) -> false,
+ null);
}
/**
@@ -46,15 +47,15 @@ public class TreeConstructor {
*
* @param <T>
* 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 <T> AST<T> constructTree(FunctionalList<T> toks,
- Predicate<T> opPredicate, Predicate<T> isSpecialOp,
- Function<Deque<AST<T>>, AST<T>> handleSpecialOp) {
- GenHolder<Pair<Deque<AST<T>>, AST<T>>> initState =
+ public static <T> AST<T> constructTree(FunctionalList<T> tokens,
+ Predicate<T> operatorPredicate, Predicate<T> isSpecialOperator,
+ Function<Deque<AST<T>>, AST<T>> handleSpecialOperator) {
+ GenHolder<Pair<Deque<AST<T>>, AST<T>>> initialState =
new GenHolder<>(new Pair<>(new LinkedList<>(), null));
- toks.forEach((ele) -> {
- if (opPredicate.test(ele)) {
- initState.transform((par) -> {
- Deque<AST<T>> lft = par.merge((deq, ast) -> deq);
+ tokens.forEach((element) -> {
+ if (operatorPredicate.test(element)) {
+ initialState.transform((pair) -> {
+ Deque<AST<T>> queuedASTs =
+ pair.merge((queue, currentAST) -> queue);
- AST<T> mergedAST = par.merge((deq, ast) -> {
+ AST<T> mergedAST = pair.merge((queue, currentAST) -> {
AST<T> newAST;
- if (isSpecialOp.test(ele)) {
- newAST = handleSpecialOp.apply(deq);
+ if (isSpecialOperator.test(element)) {
+ newAST = handleSpecialOperator.apply(queue);
} else {
- AST<T> right = deq.pop();
- AST<T> left = deq.pop();
- newAST = new AST<>(ele, left, right);
+ AST<T> rightAST = queue.pop();
+ AST<T> leftAST = queue.pop();
+
+ newAST = new AST<>(element, leftAST, rightAST);
}
- deq.push(newAST);
+ queue.push(newAST);
return newAST;
});
Pair<Deque<AST<T>>, AST<T>> newPair =
- new Pair<>(lft, mergedAST);
+ new Pair<>(queuedASTs, mergedAST);
return newPair;
});
} else {
- AST<T> newAST = new AST<>(ele);
+ AST<T> 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<Deque<AST<T>>, AST<T>>) par
- .apply((d) -> d, (a) -> newAST);
+ initialState.transform((pair) -> {
+ return (Pair<Deque<AST<T>>, AST<T>>) pair.apply(
+ (queue) -> queue, (currentAST) -> newAST);
});
}
});
- return initState.unwrap((par) -> par.merge((deq, ast) -> ast));
+ return initialState.unwrap(
+ (pair) -> pair.merge((queue, currentAST) -> currentAST));
}
}