summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc')
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/DefineCLI.java8
-rw-r--r--base/src/main/java/bjc/utils/funcutils/ListUtils.java40
-rw-r--r--base/src/main/java/bjc/utils/funcutils/SetUtils.java4
-rw-r--r--base/src/main/java/bjc/utils/funcutils/StringUtils.java74
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TestUtils.java4
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TreeUtils.java4
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedRandom.java19
-rw-r--r--base/src/main/java/bjc/utils/graph/AdjacencyMap.java23
-rw-r--r--base/src/main/java/bjc/utils/graph/Edge.java33
-rw-r--r--base/src/main/java/bjc/utils/graph/Graph.java38
-rw-r--r--base/src/main/java/bjc/utils/parserutils/ShuntingYard.java25
-rw-r--r--base/src/main/java/bjc/utils/parserutils/StringDescaper.java2
-rw-r--r--base/src/main/java/bjc/utils/parserutils/TokenTransformer.java148
-rw-r--r--base/src/main/java/bjc/utils/parserutils/TokenUtils.java28
-rw-r--r--base/src/main/java/bjc/utils/parserutils/TreeConstructor.java156
15 files changed, 253 insertions, 353 deletions
diff --git a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
index d2595d6..613b223 100644
--- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
@@ -14,7 +14,7 @@ import java.util.logging.Logger;
import java.util.regex.Pattern;
import bjc.utils.cli.objects.Command.CommandStatus;
-import bjc.utils.funcutils.StringUtils;
+import bjc.utils.parserutils.*;
/*
* @TODO 10/09/17 :DefineCLIFinish
@@ -130,7 +130,7 @@ public class DefineCLI {
}
private CommandStatus defString(Command com) {
- List<String> arguments = StringUtils.processArguments(com.remn);
+ List<String> arguments = TokenUtils.processArguments(com.remn);
if (arguments.size() < 1) {
LOGGER.severe(com.error(
@@ -158,7 +158,7 @@ public class DefineCLI {
}
private CommandStatus defFormat(Command com) {
- List<String> arguments = StringUtils.processArguments(com.remn);
+ List<String> arguments = TokenUtils.processArguments(com.remn);
if (arguments.size() < 1) {
LOGGER.severe(com.error(
@@ -186,7 +186,7 @@ public class DefineCLI {
}
private CommandStatus bindFormat(Command com) {
- List<String> strings = StringUtils.processArguments(com.remn);
+ List<String> strings = TokenUtils.processArguments(com.remn);
if (strings.size() < 2) {
LOGGER.severe(com.error(
diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
index 672a12d..e3662af 100644
--- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
@@ -28,8 +28,7 @@ public class ListUtils {
* @return The collapsed string of tokens.
*/
public static String collapseTokens(final IList<String> input) {
- if (input == null)
- throw new NullPointerException("Input must not be null");
+ if (input == null) throw new NullPointerException("Input must not be null");
return collapseTokens(input, "");
}
@@ -48,11 +47,8 @@ public class ListUtils {
*/
public static String collapseTokens(final IList<String> input,
final String seperator) {
- if (input == null) {
- throw new NullPointerException("Input must not be null");
- } else if (seperator == null) {
- throw new NullPointerException("Seperator must not be null");
- }
+ if (input == null) throw new NullPointerException("Input must not be null");
+ else if (seperator == null) throw new NullPointerException("Seperator must not be null");
if (input.getSize() < 1) {
return "";
@@ -65,9 +61,7 @@ public class ListUtils {
for (final String itm : input.toIterable()) {
state.append(itm);
- if (i != input.getSize()) {
- state.append(seperator);
- }
+ if (i != input.getSize()) state.append(seperator);
i += 1;
}
@@ -143,9 +137,7 @@ public class ListUtils {
final Function<Integer, Integer> rng) {
final IList<E> selected = new FunctionalList<>(new ArrayList<>(number));
- for (int i = 0; i < number; i++) {
- selected.add(list.randItem(rng));
- }
+ for (int i = 0; i < number; i++) selected.add(list.randItem(rng));
return selected;
}
@@ -198,10 +190,8 @@ public class ListUtils {
numberOfIterations++) {
input.forEach(it);
- if (rejected.isEmpty()) {
- /* Nothing was rejected, so we're done. */
- return returned;
- }
+ /* Nothing was rejected, so we're done. */
+ if (rejected.isEmpty()) return returned;
}
final String fmt
@@ -229,9 +219,7 @@ public class ListUtils {
final IList<E> returned = new FunctionalList<>();
for (final IList<E> list : lists) {
- for (final E itm : list.toIterable()) {
- returned.add(itm);
- }
+ for (final E itm : list.toIterable()) returned.add(itm);
}
return returned;
@@ -343,9 +331,7 @@ public class ListUtils {
/*
* Special-case small usages.
*/
- if (list.size() == 0) {
- return permutes;
- }
+ if (list.size() == 0) return permutes;
if (list.size() == 1) {
permutes.add(list);
@@ -383,9 +369,7 @@ public class ListUtils {
}
List<T> currentPermute = new ArrayList<>(list.size());
- for (T elm : list) {
- currentPermute.add(elm);
- }
+ for (T elm : list) currentPermute.add(elm);
permutes.add(currentPermute);
int j = list.size() - 1;
@@ -421,9 +405,7 @@ public class ListUtils {
auxC[j] = q;
currentPermute = new ArrayList<>(list.size());
- for (T elm : list) {
- currentPermute.add(elm);
- }
+ for (T elm : list) currentPermute.add(elm);
permutes.add(currentPermute);
j = list.size() - 1;
diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java
index 83c191b..babdb8e 100644
--- a/base/src/main/java/bjc/utils/funcutils/SetUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java
@@ -63,9 +63,7 @@ public class SetUtils {
public static <T> Set<T> toSet(T... elms) {
Set<T> set = new HashSet<>();
- for (T elm : elms) {
- set.add(elm);
- }
+ for (T elm : elms) set.add(elm);
return set;
}
diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
index b7a6835..b029e1d 100644
--- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
@@ -1,19 +1,12 @@
package bjc.utils.funcutils;
-import java.util.ArrayList;
-import java.util.Deque;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Scanner;
-
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.ibm.icu.text.BreakIterator;
-import bjc.data.BooleanToggle;
import bjc.utils.ioutils.LevelSplitter;
-import bjc.utils.parserutils.TokenUtils;
/**
* Utility methods for operations on strings.
@@ -35,10 +28,8 @@ public class StringUtils {
* provided regex.
*/
public static boolean containsOnly(final String input, final String rRegex) {
- if (input == null)
- throw new NullPointerException("Input must not be null");
- else if (rRegex == null)
- throw new NullPointerException("Regex must not be null");
+ if (input == null) throw new NullPointerException("Input must not be null");
+ else if (rRegex == null) throw new NullPointerException("Regex must not be null");
/*
* This regular expression is fairly simple.
@@ -60,9 +51,7 @@ public class StringUtils {
* The number of levels to indent.
*/
public static void indentNLevels(final StringBuilder builder, final int levels) {
- for (int i = 0; i < levels; i++) {
- builder.append("\t");
- }
+ for (int i = 0; i < levels; i++) builder.append("\t");
}
/**
@@ -168,11 +157,8 @@ public class StringUtils {
* @return The sequence as an English list.
*/
public static String toEnglishList(final Object[] objects, final boolean and) {
- if (and) {
- return toEnglishList(objects, "and");
- }
-
- return toEnglishList(objects, "or");
+ if (and) return toEnglishList(objects, "and");
+ else return toEnglishList(objects, "or");
}
/**
@@ -188,9 +174,7 @@ public class StringUtils {
it.setText(value);
int count = 0;
- while (it.next() != BreakIterator.DONE) {
- count++;
- }
+ while (it.next() != BreakIterator.DONE) count++;
return count;
}
@@ -209,8 +193,7 @@ public class StringUtils {
Matcher mat = Pattern.compile(pattern).matcher(value);
int num = 0;
- while (mat.find())
- num += 1;
+ while (mat.find()) num += 1;
return num;
}
@@ -246,41 +229,13 @@ public class StringUtils {
int idx = strang.indexOf(vx);
if (idx == -1) {
- if (allowFail)
- return strang;
-
- return null;
+ if (allowFail) return strang;
+ else return null;
}
return strang.substring(0, strang.indexOf(vx));
}
- /**
- * Split a line into a series of space-separated arguments, including string
- * literals.
- *
- * @param com
- * The command to split from
- * @return The split arguments.
- */
- public static List<String> processArguments(String com) {
- List<String> strings = new ArrayList<>();
-
- BooleanToggle togg = new BooleanToggle();
-
- for (String strang : TokenUtils.removeDQuotedStrings(com)) {
- if (togg.get()) {
- strings.add(TokenUtils.descapeString(strang));
- } else {
- for (String strung : strang.split("\\s+")) {
- strings.add(strung);
- }
- }
- }
-
- return strings;
- }
-
private static class LineIterator implements Iterator<String> {
private Scanner scn;
@@ -306,16 +261,13 @@ public class StringUtils {
boolean doLoop = true;
do {
- if (!scn.hasNextLine())
- break;
+ if (!scn.hasNextLine()) break;
tmp = scn.nextLine().trim();
// Skip blank lines
- if (skipBlanks && tmp.equals(""))
- continue;
- if (processComments && tmp.startsWith(commentInd))
- continue;
+ if (skipBlanks && tmp.equals("")) continue;
+ if (processComments && tmp.startsWith(commentInd)) continue;
doLoop = tmp.endsWith("\\") && !tmp.endsWith("\\\\");
diff --git a/base/src/main/java/bjc/utils/funcutils/TestUtils.java b/base/src/main/java/bjc/utils/funcutils/TestUtils.java
index 3aa20a2..c7aeec1 100644
--- a/base/src/main/java/bjc/utils/funcutils/TestUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/TestUtils.java
@@ -22,9 +22,7 @@ public class TestUtils {
*/
@SafeVarargs
public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) {
- for (T val : vals) {
- assertEquals(val, src.next());
- }
+ for (T val : vals) assertEquals(val, src.next());
}
/**
diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
index 7f54f73..1b5c821 100644
--- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
@@ -39,9 +39,7 @@ public class TreeUtils {
/* We're at a matching leaf node. Add it. */
IList<T> finalPath = new FunctionalList<>();
- for (T ePath : path) {
- finalPath.add(ePath);
- }
+ for (T ePath : path) finalPath.add(ePath);
finalPath.add(subtree.getHead());
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
index b17017b..969020e 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -38,8 +38,7 @@ public class WeightedRandom<E> {
public WeightedRandom(Random src) {
values = new FunctionalList<>();
- if (src == null)
- throw new NullPointerException("Source of randomness must not be null");
+ if (src == null) throw new NullPointerException("Source of randomness must not be null");
source = src;
}
@@ -156,12 +155,10 @@ public class WeightedRandom<E> {
* @return A random value.
*/
public E getDescent(int factor, Random rn) {
- if (values.getSize() == 0)
- return null;
+ if (values.getSize() == 0) return null;
for (IPair<Integer, E> val : values) {
- if (rn.nextInt(factor) == 0)
- continue;
+ if (rn.nextInt(factor) == 0) continue;
if (exhaust) {
totalChance -= val.getLeft();
@@ -173,8 +170,7 @@ public class WeightedRandom<E> {
}
IPair<Integer, E> val = values.getByIndex(values.getSize() - 1);
- if (exhaust)
- values.removeMatching(val);
+ if (exhaust) values.removeMatching(val);
return val.getRight();
}
@@ -214,8 +210,7 @@ public class WeightedRandom<E> {
* @return The value at the index corresponding to the number of successes.
*/
public E getBinomial(int target, int bound, int trials, Random rn) {
- if (values.getSize() == 0)
- return null;
+ if (values.getSize() == 0) return null;
int numSuc = 0;
@@ -254,9 +249,7 @@ public class WeightedRandom<E> {
*/
public WeightedRandom<E> exhaustible() {
IList<IPair<Integer, E>> lst = new FunctionalList<>();
- for (IPair<Integer, E> val : values) {
- lst.add(val);
- }
+ for (IPair<Integer, E> val : values) lst.add(val);
WeightedRandom<E> res = new WeightedRandom<>(source, lst, totalChance);
diff --git a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
index e046fb5..804e232 100644
--- a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
+++ b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java
@@ -32,8 +32,7 @@ public class AdjacencyMap<T> {
* @return An adjacency map defined by the text
*/
public static AdjacencyMap<Integer> fromStream(final InputStream stream) {
- if (stream == null)
- throw new NullPointerException("Input source must not be null");
+ if (stream == null) throw new NullPointerException("Input source must not be null");
/* Create the adjacency map. */
AdjacencyMap<Integer> adjacency;
@@ -60,8 +59,7 @@ public class AdjacencyMap<T> {
throw imex;
}
- if (vertexCount <= 0)
- throw new InputMismatchException(
+ if (vertexCount <= 0) throw new InputMismatchException(
"The number of vertices must be greater than 0");
final IList<Integer> vertices = new FunctionalList<>();
@@ -126,8 +124,7 @@ public class AdjacencyMap<T> {
* The set of vertices to create a map from
*/
public AdjacencyMap(final IList<T> vertices) {
- if (vertices == null)
- throw new NullPointerException("Vertices must not be null");
+ if (vertices == null) throw new NullPointerException("Vertices must not be null");
vertices.forEach(vertex -> {
final IMap<T, Integer> row = new FunctionalMap<>();
@@ -152,9 +149,7 @@ public class AdjacencyMap<T> {
sourceValue.forEach((targetKey, targetValue) -> {
final int inverseValue = adjacency.get(targetKey).get(sourceKey);
- if (targetValue != inverseValue) {
- result.replace(false);
- }
+ if (targetValue != inverseValue) result.replace(false);
});
});
@@ -172,11 +167,8 @@ public class AdjacencyMap<T> {
* The weight of the edge.
*/
public void setWeight(final T source, final T target, final int weight) {
- if (source == null) {
- throw new NullPointerException("Source vertex must not be null");
- } else if (target == null) {
- throw new NullPointerException("Target vertex must not be null");
- }
+ if (source == null) throw new NullPointerException("Source vertex must not be null");
+ else if (target == null) throw new NullPointerException("Target vertex must not be null");
if (!adjacency.containsKey(source)) {
String msg = String.format("Source vertex %s isn't present in map", source);
@@ -215,8 +207,7 @@ public class AdjacencyMap<T> {
* The stream to convert to.
*/
public void toStream(final OutputStream sink) {
- if (sink == null)
- throw new NullPointerException("Output source must not be null");
+ if (sink == null) throw new NullPointerException("Output source must not be null");
final PrintStream outputPrinter = new PrintStream(sink);
diff --git a/base/src/main/java/bjc/utils/graph/Edge.java b/base/src/main/java/bjc/utils/graph/Edge.java
index fe3d891..b48fcd0 100644
--- a/base/src/main/java/bjc/utils/graph/Edge.java
+++ b/base/src/main/java/bjc/utils/graph/Edge.java
@@ -28,11 +28,8 @@ public class Edge<T> {
* The distance between initial and terminal edge.
*/
public Edge(final T initial, final T terminal, final int distance) {
- if (initial == null) {
- throw new NullPointerException("Initial node must not be null");
- } else if (terminal == null) {
- throw new NullPointerException("Terminal node must not be null");
- }
+ if (initial == null) throw new NullPointerException("Initial node must not be null");
+ else if (terminal == null) throw new NullPointerException("Terminal node must not be null");
this.source = initial;
this.target = terminal;
@@ -41,27 +38,23 @@ public class Edge<T> {
@Override
public boolean equals(final Object obj) {
- if (this == obj)
- return true;
- else if (obj == null)
- return false;
- else if (getClass() != obj.getClass())
- return false;
+ if (this == obj) return true;
+ else if (obj == null) return false;
+ else if (getClass() != obj.getClass()) return false;
else {
final Edge<?> other = (Edge<?>) obj;
- if (distance != other.distance)
+ if (distance != other.distance) {
return false;
- else if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
+ } else if (source == null) {
+ if (other.source != null) return false;
+ } else if (!source.equals(other.source)) {
return false;
- else if (target == null) {
- if (other.target != null)
- return false;
- } else if (!target.equals(other.target))
+ } else if (target == null) {
+ if (other.target != null) return false;
+ } else if (!target.equals(other.target)) {
return false;
+ }
return true;
}
diff --git a/base/src/main/java/bjc/utils/graph/Graph.java b/base/src/main/java/bjc/utils/graph/Graph.java
index 8ff5647..1e81fcf 100644
--- a/base/src/main/java/bjc/utils/graph/Graph.java
+++ b/base/src/main/java/bjc/utils/graph/Graph.java
@@ -72,16 +72,11 @@ public class Graph<T> {
public void addEdge(final T source, final T target, final int distance,
final boolean directed) {
/* Can't add edges with a null source or target. */
- if (source == null) {
- throw new NullPointerException("The source vertex cannot be null");
- } else if (target == null) {
- throw new NullPointerException("The target vertex cannot be null");
- }
+ if (source == null) throw new NullPointerException("The source vertex cannot be null");
+ else if (target == null) throw new NullPointerException("The target vertex cannot be null");
/* Initialize adjacency list for vertices if necessary. */
- if (!backing.containsKey(source)) {
- backing.put(source, new FunctionalMap<T, Integer>());
- }
+ if (!backing.containsKey(source)) backing.put(source, new FunctionalMap<T, Integer>());
/* Add the edge to the graph. */
backing.get(source).put(target, distance);
@@ -110,16 +105,11 @@ public class Graph<T> {
*/
public void forAllEdgesMatchingAt(final T source,
final BiPredicate<T, Integer> matcher, final BiConsumer<T, Integer> action) {
- if (matcher == null) {
- throw new NullPointerException("Matcher must not be null");
- } else if (action == null) {
- throw new NullPointerException("Action must not be null");
- }
+ if (matcher == null) throw new NullPointerException("Matcher must not be null");
+ else if (action == null) throw new NullPointerException("Action must not be null");
getEdges(source).forEach((target, weight) -> {
- if (matcher.test(target, weight)) {
- action.accept(target, weight);
- }
+ if (matcher.test(target, weight)) action.accept(target, weight);
});
}
@@ -132,10 +122,11 @@ public class Graph<T> {
*/
public IMap<T, Integer> getEdges(final T source) {
/* Can't find edges for a null source. */
- if (source == null)
+ if (source == null) {
throw new NullPointerException("The source cannot be null.");
- else if (!backing.containsKey(source))
+ } else if (!backing.containsKey(source)) {
throw new IllegalArgumentException("Vertex " + source + " is not in graph");
+ }
return backing.get(source);
}
@@ -176,11 +167,14 @@ public class Graph<T> {
while (visited.size() != getVertexCount()) {
/* Grab all edges adjacent to the provided edge. */
- forAllEdgesMatchingAt(source.getValue(), (target, weight) -> !visited.contains(target), (target, weight) -> {
- final T vert = source.unwrap(vertex -> vertex);
+ forAllEdgesMatchingAt(source.getValue(),
+ (target, weight) -> !visited.contains(target),
+ (target, weight) -> {
+ final T vert = source.unwrap(vertex -> vertex);
- available.add(new Edge<>(vert, target, weight));
- });
+ available.add(new Edge<>(vert, target, weight));
+ }
+ );
/* Get the edge with the minimum distance. */
final IHolder<Edge<T>> minimum = new Identity<>(available.poll());
diff --git a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java
index 2418517..8d61b4a 100644
--- a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java
+++ b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java
@@ -113,10 +113,8 @@ public class ShuntingYard<TokenType> {
/*
* Complain about trying to add an incorrect operator
*/
- if (operator == null)
- throw new NullPointerException("Operator must not be null");
- else if (precedence == null)
- throw new NullPointerException("Precedence must not be 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
@@ -141,7 +139,7 @@ public class ShuntingYard<TokenType> {
* Get the precedence of operators
*/
final int rightPrecedence = operators.get(right).getPrecedence();
- final int leftPrecedence = operators.get(left).getPrecedence();
+ final int leftPrecedence = operators.get(left).getPrecedence();
/*
* Evaluate what we were asked
@@ -165,10 +163,8 @@ public class ShuntingYard<TokenType> {
/*
* Check our input
*/
- if (input == null)
- throw new NullPointerException("Input must not be null");
- else if (transformer == null)
- throw new NullPointerException("Transformer must not be null");
+ if (input == null) throw new NullPointerException("Input must not be null");
+ else if (transformer == null) throw new NullPointerException("Transformer must not be null");
/*
* Here's what we're handing back
@@ -226,9 +222,7 @@ public class ShuntingYard<TokenType> {
}
}
- for (String token : stack) {
- output.add(transformer.apply(token));
- }
+ for (String token : stack) output.add(transformer.apply(token));
return output;
}
@@ -244,10 +238,7 @@ public class ShuntingYard<TokenType> {
/*
* Check if we want to remove all operators
*/
- if (operator == null) {
- operators = new FunctionalMap<>();
- } else {
- operators.remove(operator);
- }
+ if (operator == null) operators = new FunctionalMap<>();
+ else operators.remove(operator);
}
}
diff --git a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
index f8868e6..7052588 100644
--- a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
+++ b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
@@ -12,7 +12,7 @@ import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
- * <<<<<<< Updated upstream Customizable string escapes.
+ * Customizable string escapes.
*
* @author Benjamin Culkin
*/
diff --git a/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java
deleted file mode 100644
index 6cf2da5..0000000
--- a/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java
+++ /dev/null
@@ -1,148 +0,0 @@
-package bjc.utils.parserutils;
-
-import java.util.Deque;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.function.UnaryOperator;
-
-import bjc.data.IHolder;
-import bjc.data.ITree;
-import bjc.data.Pair;
-import bjc.data.Tree;
-import bjc.utils.parserutils.TreeConstructor.ConstructorState;
-import bjc.utils.parserutils.TreeConstructor.QueueFlattener;
-
-/*
- * Handle creating ASTs from tokens.
- */
-final class TokenTransformer<TokenType> implements Consumer<TokenType> {
- /*
- * Handle operators
- */
- private final class OperatorHandler
- implements UnaryOperator<ConstructorState<TokenType>> {
- /* The handled element. */
- private final TokenType element;
-
- /* Create a new operator handler. */
- public OperatorHandler(final TokenType element) {
- this.element = element;
- }
-
- @Override
- public ConstructorState<TokenType> apply(final ConstructorState<TokenType> pair) {
- /*
- * Replace the current AST with the result of handling an operator
- */
- return new ConstructorState<>(
- pair.bindLeft(queuedASTs -> handleOperator(queuedASTs)));
- }
-
- private ConstructorState<TokenType>
- handleOperator(final Deque<ITree<TokenType>> queuedASTs) {
- /*
- * The AST we're going to hand back
- */
- ITree<TokenType> newAST;
-
- /*
- * Handle special operators
- */
- if (isSpecialOperator.test(element)) {
- newAST = handleSpecialOperator.apply(element).apply(queuedASTs);
- } else {
- /*
- * Error if we don't have enough for a binary operator
- */
- if (queuedASTs.size() < 2) {
- final String msg = String.format(
- "Attempted to parse binary operator without enough operands\n\tProblem operator is: %s\n\tPossible operand is: %s",
- element.toString(), queuedASTs.peek().toString());
-
- throw new IllegalStateException(msg);
- }
-
- /*
- * Grab the two operands
- */
- final ITree<TokenType> right = queuedASTs.pop();
- final ITree<TokenType> left = queuedASTs.pop();
-
- /*
- * Create a new AST
- */
- newAST = new Tree<>(element, left, right);
- }
-
- /*
- * Stick it onto the stack
- */
- queuedASTs.push(newAST);
-
- /*
- * Hand back the state
- */
- return new ConstructorState<>(queuedASTs, newAST);
- }
- }
-
- /* The initial state of the transformer. */
- private final IHolder<ConstructorState<TokenType>> initialState;
-
- /* The predicate tot use to detect operators. */
- private final Predicate<TokenType> operatorPredicate;
-
- /* The predicate for detecting special operators. */
- private final Predicate<TokenType> isSpecialOperator;
- /* The function for handling special operators. */
- private final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator;
-
- /**
- * Create a new transformer
- *
- * @param initialState
- * The initial state of the transformer.
- *
- * @param operatorPredicate
- * The predicate to use to identify operators.
- *
- * @param isSpecialOperator
- * The predicate used to identify special
- * operators.
- *
- * @param handleSpecialOperator
- * The function used for handling special
- * operators.
- */
- public TokenTransformer(final IHolder<ConstructorState<TokenType>> initialState,
- final Predicate<TokenType> operatorPredicate,
- final Predicate<TokenType> isSpecialOperator,
- final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) {
- this.initialState = initialState;
- this.operatorPredicate = operatorPredicate;
- this.isSpecialOperator = isSpecialOperator;
- this.handleSpecialOperator = handleSpecialOperator;
- }
-
- @Override
- public void accept(final TokenType element) {
- /*
- * Handle operators
- */
- if (operatorPredicate.test(element)) {
- initialState.transform(new OperatorHandler(element));
- } else {
- final ITree<TokenType> newAST = new Tree<>(element);
-
- /*
- * Insert the new tree into the AST
- */
- initialState.transform(pair -> new ConstructorState<>(pair.bindLeft(queue -> {
- queue.push(newAST);
-
- return new Pair<>(queue, newAST);
- })));
- }
- }
-}
diff --git a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
index 81a7ba0..c6fdf5e 100644
--- a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
+++ b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
@@ -4,11 +4,11 @@ import static bjc.utils.misc.PropertyDB.applyFormat;
import static bjc.utils.misc.PropertyDB.getCompiledRegex;
import static bjc.utils.misc.PropertyDB.getRegex;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import bjc.data.*;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.IList;
import bjc.utils.parserutils.splitter.TokenSplitter;
@@ -316,4 +316,28 @@ public class TokenUtils {
return false;
}
}
+
+ /**
+ * Split a line into a series of space-separated arguments, including string
+ * literals.
+ *
+ * @param com
+ * The command to split from
+ * @return The split arguments.
+ */
+ public static List<String> processArguments(String com) {
+ List<String> strings = new ArrayList<>();
+
+ BooleanToggle togg = new BooleanToggle();
+
+ for (String strang : removeDQuotedStrings(com)) {
+ if (togg.get()) {
+ strings.add(descapeString(strang));
+ } else {
+ for (String strung : strang.split("\\s+")) strings.add(strung);
+ }
+ }
+
+ return strings;
+ }
}
diff --git a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
index 3c7509b..6780768 100644
--- a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
+++ b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java
@@ -2,15 +2,11 @@ package bjc.utils.parserutils;
import java.util.Deque;
import java.util.LinkedList;
-import java.util.function.Function;
-import java.util.function.Predicate;
-
-import bjc.data.IHolder;
-import bjc.data.IPair;
-import bjc.data.ITree;
-import bjc.data.Identity;
-import bjc.data.Pair;
+import java.util.function.*;
+
+import bjc.data.*;
import bjc.funcdata.IList;
+import bjc.utils.parserutils.TreeConstructor.*;
/**
* Creates a parse tree from a postfix expression.
@@ -103,13 +99,14 @@ public class TreeConstructor {
/*
* Make sure our parameters are valid
*/
- if (tokens == null)
+ if (tokens == null) {
throw new NullPointerException("Tokens must not be null");
- else if (isOperator == null)
+ } else if (isOperator == null) {
throw new NullPointerException("Operator predicate must not be null");
- else if (isSpecialOperator == null)
+ } else if (isSpecialOperator == null) {
throw new NullPointerException(
"Special operator determiner must not be null");
+ }
final ConstructorState<TokenType> cstate
= new ConstructorState<>(new LinkedList<>(), null);
@@ -127,3 +124,140 @@ public class TreeConstructor {
return initialState.unwrap(ConstructorState::getRight);
}
}
+
+/*
+ * Transform function on tokens
+ */
+class TokenTransformer<TokenType> implements Consumer<TokenType> {
+ /*
+ * Handle operators
+ */
+ private final class OperatorHandler
+ implements UnaryOperator<ConstructorState<TokenType>> {
+ /* The handled element. */
+ private final TokenType element;
+
+ /* Create a new operator handler. */
+ public OperatorHandler(final TokenType element) {
+ this.element = element;
+ }
+
+ @Override
+ public ConstructorState<TokenType> apply(final ConstructorState<TokenType> pair) {
+ /*
+ * Replace the current AST with the result of handling an operator
+ */
+ return new ConstructorState<>(
+ pair.bindLeft(queuedASTs -> handleOperator(queuedASTs)));
+ }
+
+ private ConstructorState<TokenType>
+ handleOperator(final Deque<ITree<TokenType>> queuedASTs) {
+ /*
+ * The AST we're going to hand back
+ */
+ ITree<TokenType> newAST;
+
+ /*
+ * Handle special operators
+ */
+ if (isSpecialOperator.test(element)) {
+ newAST = handleSpecialOperator.apply(element).apply(queuedASTs);
+ } else {
+ /*
+ * Error if we don't have enough for a binary operator
+ */
+ if (queuedASTs.size() < 2) {
+ final String msg = String.format(
+ "Attempted to parse binary operator without enough operands\n\tProblem operator is: %s\n\tPossible operand is: %s",
+ element.toString(), queuedASTs.peek().toString());
+
+ throw new IllegalStateException(msg);
+ }
+
+ /*
+ * Grab the two operands
+ */
+ final ITree<TokenType> right = queuedASTs.pop();
+ final ITree<TokenType> left = queuedASTs.pop();
+
+ /*
+ * Create a new AST
+ */
+ newAST = new Tree<>(element, left, right);
+ }
+
+ /*
+ * Stick it onto the stack
+ */
+ queuedASTs.push(newAST);
+
+ /*
+ * Hand back the state
+ */
+ return new ConstructorState<>(queuedASTs, newAST);
+ }
+ }
+
+ /* The initial state of the transformer. */
+ private final IHolder<ConstructorState<TokenType>> initialState;
+
+ /* The predicate tot use to detect operators. */
+ private final Predicate<TokenType> operatorPredicate;
+
+ /* The predicate for detecting special operators. */
+ private final Predicate<TokenType> isSpecialOperator;
+ /* The function for handling special operators. */
+ private final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator;
+
+ /**
+ * Create a new transformer
+ *
+ * @param initialState
+ * The initial state of the transformer.
+ *
+ * @param operatorPredicate
+ * The predicate to use to identify operators.
+ *
+ * @param isSpecialOperator
+ * The predicate used to identify special
+ * operators.
+ *
+ * @param handleSpecialOperator
+ * The function used for handling special
+ * operators.
+ */
+ public TokenTransformer(final IHolder<ConstructorState<TokenType>> initialState,
+ final Predicate<TokenType> operatorPredicate,
+ final Predicate<TokenType> isSpecialOperator,
+ final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) {
+ this.initialState = initialState;
+ this.operatorPredicate = operatorPredicate;
+ this.isSpecialOperator = isSpecialOperator;
+ this.handleSpecialOperator = handleSpecialOperator;
+ }
+
+ @Override
+ public void accept(final TokenType element) {
+ /*
+ * Handle operators
+ */
+ if (operatorPredicate.test(element)) {
+ initialState.transform(new OperatorHandler(element));
+ } else {
+ final ITree<TokenType> newAST = new Tree<>(element);
+
+ /*
+ * Insert the new tree into the AST
+ */
+ initialState.transform(pair -> new ConstructorState<>(
+ pair.bindLeft(queue -> {
+ queue.push(newAST);
+
+ return new Pair<>(queue, newAST);
+ })
+ )
+ );
+ }
+ }
+} \ No newline at end of file