summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 16:02:45 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 16:02:45 -0400
commit61fd71f69e080790da722e0e03b71ecd7c2538a2 (patch)
treee5c1150b27b84d550f807e44ac82688216451f00 /BJC-Utils2/src/main/java/bjc/utils/parserutils
parent87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc (diff)
General update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java81
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java16
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java6
3 files changed, 92 insertions, 11 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java
new file mode 100644
index 0000000..9d9d1b1
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java
@@ -0,0 +1,81 @@
+package bjc.utils.parserutils;
+
+import java.util.function.BiConsumer;
+
+import bjc.utils.exceptions.PragmaFormatException;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.funcutils.ListUtils;
+
+/**
+ * Contains factory methods for common pragma types
+ *
+ * @author ben
+ *
+ */
+public class RuleBasedReaderPragmas {
+
+ /**
+ * Creates a pragma that takes any number of arguments and collapses
+ * them all into a single string
+ *
+ * @param <StateType>
+ * The type of state that goes along with this pragma
+ * @param name
+ * The name of this pragma, for error message purpose
+ * @param consumer
+ * The function to invoke with the parsed string
+ * @return A pragma that functions as described above.
+ */
+ public static <StateType>
+ BiConsumer<FunctionalStringTokenizer, StateType>
+ buildStringCollapser(String name,
+ BiConsumer<String, StateType> consumer) {
+ return (tokenizer, state) -> {
+ if (!tokenizer.hasMoreTokens()) {
+ throw new PragmaFormatException("Pragma " + name
+ + " requires one string argument");
+ }
+
+ consumer.accept(ListUtils.collapseTokens(
+ tokenizer.toList((strang) -> strang)), state);
+ };
+ }
+
+ /**
+ * Creates a pragma that takes a single integer argument
+ *
+ * @param <StateType>
+ * The type of state that goes along with this pragma
+ * @param name
+ * The name of this pragma, for error message purpose
+ * @param consumer
+ * The function to invoke with the parsed integer
+ * @return A pragma that functions as described above.
+ */
+ public static <StateType>
+ BiConsumer<FunctionalStringTokenizer, StateType> buildInteger(
+ String name, BiConsumer<Integer, StateType> consumer) {
+ return (tokenizer, state) -> {
+ if (!tokenizer.hasMoreTokens()) {
+ throw new PragmaFormatException("Pragma " + name
+ + " requires one integer argument");
+ }
+
+ String token = tokenizer.nextToken();
+
+ try {
+ consumer.accept(Integer.parseInt(token), state);
+ } catch (NumberFormatException nfex) {
+ PragmaFormatException pfex =
+ new PragmaFormatException("Argument " + token
+ + " to version pragma isn't a valid integer. "
+ + "This pragma requires a integer argument");
+
+ pfex.initCause(nfex);
+
+ throw pfex;
+ }
+ };
+ }
+
+}
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 2ea23c6..8118faa 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
@@ -7,8 +7,8 @@ import java.util.function.Function;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalMap;
-import bjc.utils.funcdata.IFunctionalList;
-import bjc.utils.funcdata.IFunctionalMap;
+import bjc.utils.funcdata.IList;
+import bjc.utils.funcdata.IMap;
import bjc.utils.funcutils.StringUtils;
/**
@@ -63,11 +63,11 @@ public class ShuntingYard<TokenType> {
}
private final class TokenShunter implements Consumer<String> {
- private IFunctionalList<TokenType> output;
+ private IList<TokenType> output;
private Deque<String> stack;
private Function<String, TokenType> transform;
- public TokenShunter(IFunctionalList<TokenType> outpt,
+ public TokenShunter(IList<TokenType> outpt,
Deque<String> stack,
Function<String, TokenType> transform) {
this.output = outpt;
@@ -105,7 +105,7 @@ public class ShuntingYard<TokenType> {
/**
* Holds all the shuntable operations
*/
- private IFunctionalMap<String, IPrecedent> operators;
+ private IMap<String, IPrecedent> operators;
/**
* Create a new shunting yard with a default set of operators
@@ -176,8 +176,8 @@ public class ShuntingYard<TokenType> {
* The function to use to transform strings to tokens
* @return A list of tokens in postfix notation
*/
- public IFunctionalList<TokenType> postfix(
- IFunctionalList<String> input,
+ public IList<TokenType> postfix(
+ IList<String> input,
Function<String, TokenType> tokenTransformer) {
if (input == null) {
throw new NullPointerException("Input must not be null");
@@ -185,7 +185,7 @@ public class ShuntingYard<TokenType> {
throw new NullPointerException("Transformer must not be null");
}
- IFunctionalList<TokenType> output = new FunctionalList<>();
+ IList<TokenType> output = new FunctionalList<>();
Deque<String> stack = new LinkedList<>();
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 283d16e..c703a72 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java
@@ -9,7 +9,7 @@ import bjc.utils.data.IHolder;
import bjc.utils.data.IPair;
import bjc.utils.data.Identity;
import bjc.utils.data.Pair;
-import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.ITree;
/**
@@ -34,7 +34,7 @@ public class TreeConstructor {
* @return A AST from the expression
*/
public static <TokenType> ITree<TokenType> constructTree(
- IFunctionalList<TokenType> tokens,
+ IList<TokenType> tokens,
Predicate<TokenType> operatorPredicate) {
return constructTree(tokens, operatorPredicate, (op) -> false,
null);
@@ -65,7 +65,7 @@ public class TreeConstructor {
* works
*/
public static <TokenType> ITree<TokenType> constructTree(
- IFunctionalList<TokenType> tokens,
+ IList<TokenType> tokens,
Predicate<TokenType> operatorPredicate,
Predicate<TokenType> isSpecialOperator,
Function<TokenType, Function<Deque<ITree<TokenType>>, ITree<TokenType>>> handleSpecialOperator) {