summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-13 16:42:21 -0400
committerEVE <EVE@EVE-PC>2017-03-13 16:42:21 -0400
commit27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd (patch)
tree847fb52acb091c1c613d37b8477094d5762c6988 /BJC-Utils2/src/main/java/bjc/utils/parserutils
parentaa807a96cae2c47259fb38f710640883060339e9 (diff)
Formatting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java2
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java50
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java32
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java43
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java42
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java57
6 files changed, 100 insertions, 126 deletions
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 31ceecd..a9efe9b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java
@@ -12,7 +12,7 @@ public interface IPrecedent {
* Create a new object with set precedence
*
* @param precedence
- * The precedence of the object to handle
+ * The precedence of the object to handle
* @return A new object with set precedence
*/
public static IPrecedent newSimplePrecedent(int 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 9418862..2da4f7e 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java
@@ -16,20 +16,21 @@ import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IMap;
/**
- * This class parses a rules based config file, and uses it to drive a
- * provided set of actions
+ * This class parses a rules based config file, and uses it to drive a provided
+ * set of actions
*
* @author ben
*
* @param <E>
- * The type of the state object to use
+ * The type of the state object to use
*
*/
public class RuleBasedConfigReader<E> {
// Function to execute when starting a rule
- // Takes the tokenizer, and a pair of the read token and application state
+ // Takes the tokenizer, and a pair of the read token and application
+ // state
private BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start;
-
+
// Function to use when continuing a rule
// Takes a tokenizer and application state
private BiConsumer<FunctionalStringTokenizer, E> continueRule;
@@ -46,16 +47,14 @@ public class RuleBasedConfigReader<E> {
* Create a new rule-based config reader
*
* @param start
- * The action to fire when starting a rule
+ * The action to fire when starting a rule
* @param continueRule
- * The action to fire when continuing a rule
+ * The action to fire when continuing a rule
* @param end
- * The action to fire when ending a rule
+ * The action to fire when ending a rule
*/
- public RuleBasedConfigReader(
- BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start,
- BiConsumer<FunctionalStringTokenizer, E> continueRule,
- Consumer<E> end) {
+ public RuleBasedConfigReader(BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start,
+ BiConsumer<FunctionalStringTokenizer, E> continueRule, Consumer<E> end) {
this.start = start;
this.continueRule = continueRule;
this.end = end;
@@ -67,13 +66,11 @@ public class RuleBasedConfigReader<E> {
* Add a pragma to this reader
*
* @param name
- * The name of the pragma to add
+ * The name of the pragma to add
* @param action
- * The function to execute when this pragma is read
+ * The function to execute when this pragma is read
*/
- public void addPragma(
- String name,
- BiConsumer<FunctionalStringTokenizer, E> action) {
+ public void addPragma(String name, BiConsumer<FunctionalStringTokenizer, E> action) {
if (name == null) {
throw new NullPointerException("Pragma name must not be null");
} else if (action == null) {
@@ -116,9 +113,9 @@ public class RuleBasedConfigReader<E> {
* Run a stream through this reader
*
* @param input
- * The stream to get input
+ * The stream to get input
* @param initialState
- * The initial state of the reader
+ * The initial state of the reader
* @return The final state of the reader
*/
public E fromStream(InputStream input, E initialState) {
@@ -143,15 +140,13 @@ public class RuleBasedConfigReader<E> {
return;
} else if (line.equals("")) {
// End the rule
- isRuleOpen.replace(
- endRule(state, isRuleOpen.getValue()));
+ isRuleOpen.replace(endRule(state, isRuleOpen.getValue()));
} else if (line.startsWith("\t")) {
// Continue the rule
continueRule(state, isRuleOpen.getValue(), line);
} else {
// Open a rule
- isRuleOpen.replace(
- startRule(state, isRuleOpen.getValue(), line));
+ isRuleOpen.replace(startRule(state, isRuleOpen.getValue(), line));
}
});
}
@@ -165,10 +160,9 @@ public class RuleBasedConfigReader<E> {
* Set the action to execute when continuing a rule
*
* @param continueRule
- * The action to execute on continuation of a rule
+ * The action to execute on continuation of a rule
*/
- public void setContinueRule(
- BiConsumer<FunctionalStringTokenizer, E> continueRule) {
+ public void setContinueRule(BiConsumer<FunctionalStringTokenizer, E> continueRule) {
this.continueRule = continueRule;
}
@@ -176,7 +170,7 @@ public class RuleBasedConfigReader<E> {
* Set the action to execute when ending a rule
*
* @param end
- * The action to execute on ending of a rule
+ * The action to execute on ending of a rule
*/
public void setEndRule(Consumer<E> end) {
this.end = end;
@@ -186,7 +180,7 @@ public class RuleBasedConfigReader<E> {
* Set the action to execute when starting a rule
*
* @param start
- * The action to execute on starting of a rule
+ * The action to execute on starting of a rule
*/
public void setStartRule(BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) {
if (start == null) {
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java
index eef55a8..6c660c6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java
@@ -18,21 +18,19 @@ public class RuleBasedReaderPragmas {
* Creates a pragma that takes a single integer argument
*
* @param <StateType>
- * The type of state that goes along with this pragma
+ * The type of state that goes along with this pragma
* @param name
- * The name of this pragma, for error message purpose
+ * The name of this pragma, for error message purpose
* @param consumer
- * The function to invoke with the parsed integer
+ * 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) {
+ public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildInteger(String name,
+ BiConsumer<Integer, StateType> consumer) {
return (tokenizer, state) -> {
// Check our input is correct
if (!tokenizer.hasMoreTokens()) {
- throw new PragmaFormatException("Pragma " + name
- + " requires one integer argument");
+ throw new PragmaFormatException("Pragma " + name + " requires one integer argument");
}
// Read the argument
@@ -44,8 +42,7 @@ public class RuleBasedReaderPragmas {
} catch (NumberFormatException nfex) {
// Tell the user their argument isn't correct
PragmaFormatException pfex = new PragmaFormatException(
- "Argument " + token
- + " to " + name + " pragma isn't a valid integer. "
+ "Argument " + token + " to " + name + " pragma isn't a valid integer. "
+ "This pragma requires a integer argument");
pfex.initCause(nfex);
@@ -60,21 +57,20 @@ public class RuleBasedReaderPragmas {
* them all into a single string
*
* @param <StateType>
- * The type of state that goes along with this pragma
+ * The type of state that goes along with this pragma
* @param name
- * The name of this pragma, for error message purpose
+ * The name of this pragma, for error message purpose
* @param consumer
- * The function to invoke with the parsed string
+ * 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) {
+ public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildStringCollapser(String name,
+ BiConsumer<String, StateType> consumer) {
return (tokenizer, state) -> {
// Check our input
if (!tokenizer.hasMoreTokens()) {
- throw new PragmaFormatException("Pragma " + name
- + " requires one or more string arguments");
+ throw new PragmaFormatException(
+ "Pragma " + name + " requires one or more string arguments");
}
// Build our argument
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 fe046f4..19c1971 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
@@ -17,7 +17,7 @@ import bjc.utils.funcutils.StringUtils;
* @author ben
*
* @param <TokenType>
- * The type of tokens being shunted
+ * The type of tokens being shunted
*/
public class ShuntingYard<TokenType> {
/**
@@ -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) {
@@ -73,19 +73,21 @@ public class ShuntingYard<TokenType> {
public void accept(String token) {
// Handle operators
if (operators.containsKey(token)) {
- // Pop operators while there isn't a higher precedence one
- while (!stack.isEmpty()
- && isHigherPrec(token, stack.peek())) {
+ // Pop operators while there isn't a higher
+ // precedence one
+ 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, "\\(")) {
- // Handle groups of parenthesis for multiple nesting levels
+ // Handle groups of parenthesis for multiple
+ // nesting levels
stack.push(token);
} else if (StringUtils.containsOnly(token, "\\)")) {
- // Handle groups of parenthesis for multiple nesting levels
+ // Handle groups of parenthesis for multiple
+ // nesting levels
String swappedToken = token.replace(')', '(');
// Remove tokens up to a matching parenthesis
@@ -111,7 +113,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
+ * Whether or not basic math operators should be provided
*/
public ShuntingYard(boolean configureBasics) {
operators = new FunctionalMap<>();
@@ -129,9 +131,9 @@ public class ShuntingYard<TokenType> {
* Add an operator to the list of shuntable operators
*
* @param operatorToken
- * The token representing the operator
+ * The token representing the operator
* @param precedence
- * The precedence of the operator to add
+ * The precedence of the operator to add
*/
public void addOp(String operator, int precedence) {
// Create the precedence marker
@@ -144,15 +146,15 @@ public class ShuntingYard<TokenType> {
* Add an operator to the list of shuntable operators
*
* @param operatorToken
- * The token representing the operator
+ * The token representing the operator
* @param precedence
- * The precedence of the operator
+ * The precedence of the operator
*/
public void addOp(String operator, IPrecedent precedence) {
// Complain about trying to add an incorrect operator
if (operator == null) {
throw new NullPointerException("Operator must not be null");
- } else if(precedence == null) {
+ } else if (precedence == null) {
throw new NullPointerException("Precedence must not be null");
}
@@ -181,13 +183,12 @@ public class ShuntingYard<TokenType> {
* Transform a string of tokens from infix notation to postfix
*
* @param input
- * The string to transform
+ * The string to transform
* @param transformer
- * The function to use to transform strings to tokens
+ * 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(IList<String> input, Function<String, TokenType> transformer) {
// Check our input
if (input == null) {
throw new NullPointerException("Input must not be null");
@@ -216,8 +217,8 @@ 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
+ * The token representing the operator. If null, remove
+ * all operators
*/
public void removeOp(String operator) {
// Check if we want to remove all operators
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java
index 9953c00..1a4a6c4 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java
@@ -14,8 +14,7 @@ import bjc.utils.data.Tree;
final class TokenTransformer<TokenType> implements Consumer<TokenType> {
// Handle operators
- private final class OperatorHandler implements UnaryOperator<
- IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> {
+ private final class OperatorHandler implements UnaryOperator<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> {
private TokenType element;
public OperatorHandler(TokenType element) {
@@ -25,24 +24,24 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> {
@Override
public IPair<Deque<ITree<TokenType>>, ITree<TokenType>> apply(
IPair<Deque<ITree<TokenType>>, ITree<TokenType>> pair) {
- // Replace the current AST with the result of handling an operator
+ // Replace the current AST with the result of handling
+ // an operator
return pair.bindLeft((queuedASTs) -> {
return handleOperator(queuedASTs);
});
}
- private IPair<Deque<ITree<TokenType>>,
- ITree<TokenType>> handleOperator(
- Deque<ITree<TokenType>> queuedASTs) {
+ private IPair<Deque<ITree<TokenType>>, ITree<TokenType>> handleOperator(
+ 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);
+ newAST = handleSpecialOperator.apply(element).apply(queuedASTs);
} else {
- // Error if we don't have enough for a binary operator
+ // Error if we don't have enough for a binary
+ // operator
if (queuedASTs.size() < 2) {
throw new IllegalStateException(
"Attempted to parse binary operator without enough operands.\n"
@@ -67,25 +66,17 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> {
}
}
- private IHolder<IPair<Deque<ITree<TokenType>>,
- ITree<TokenType>>> initialState;
+ private IHolder<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> initialState;
- private Predicate<
- TokenType> operatorPredicate;
+ private Predicate<TokenType> operatorPredicate;
- private Predicate<
- TokenType> isSpecialOperator;
- private Function<TokenType, Function<Deque<ITree<TokenType>>,
- ITree<TokenType>>> handleSpecialOperator;
+ private Predicate<TokenType> isSpecialOperator;
+ private Function<TokenType, Function<Deque<ITree<TokenType>>, ITree<TokenType>>> handleSpecialOperator;
// Create a new transformer
- public TokenTransformer(
- IHolder<IPair<Deque<ITree<TokenType>>,
- ITree<TokenType>>> initialState,
- Predicate<TokenType> operatorPredicate,
- Predicate<TokenType> isSpecialOperator,
- Function<TokenType, Function<Deque<ITree<TokenType>>,
- ITree<TokenType>>> handleSpecialOperator) {
+ public TokenTransformer(IHolder<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> initialState,
+ Predicate<TokenType> operatorPredicate, Predicate<TokenType> isSpecialOperator,
+ Function<TokenType, Function<Deque<ITree<TokenType>>, ITree<TokenType>>> handleSpecialOperator) {
this.initialState = initialState;
this.operatorPredicate = operatorPredicate;
this.isSpecialOperator = isSpecialOperator;
@@ -102,7 +93,8 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> {
// Insert the new tree into the AST
initialState.transform((pair) -> {
- // Transform the pair, ignoring the current AST in favor of the
+ // Transform the pair, ignoring the current AST
+ // in favor of the
// one consisting of the current element
return pair.bindLeft((queue) -> {
queue.push(newAST);
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 7f933c0..6f4e384 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java
@@ -25,20 +25,18 @@ public class TreeConstructor {
* Only binary operators are accepted.
*
* @param <TokenType>
- * The elements of the parse tree
+ * The elements of the parse tree
* @param tokens
- * The list of tokens to build a tree from
+ * The list of tokens to build a tree from
* @param isOperator
- * The predicate to use to determine if something is a
- * operator
+ * The predicate to use to determine if something is a
+ * operator
* @return A AST from the expression
*/
- public static <TokenType> ITree<TokenType> constructTree(
- IList<TokenType> tokens,
+ public static <TokenType> ITree<TokenType> constructTree(IList<TokenType> tokens,
Predicate<TokenType> isOperator) {
// Construct a tree with no special operators
- return constructTree(tokens, isOperator, (op) -> false,
- null);
+ return constructTree(tokens, isOperator, (op) -> false, null);
}
/**
@@ -48,49 +46,42 @@ public class TreeConstructor {
* parameters to handle non-binary operators
*
* @param <TokenType>
- * The elements of the parse tree
+ * The elements of the parse tree
* @param tokens
- * The list of tokens to build a tree from
+ * The list of tokens to build a tree from
* @param isOperator
- * The predicate to use to determine if something is a
- * operator
+ * The predicate to use to determine if something is a
+ * operator
* @param isSpecialOperator
- * The predicate to use to determine if an operator needs
- * special handling
+ * The predicate to use to determine if an operator needs
+ * special handling
* @param handleSpecialOperator
- * The function to use to handle special case operators
+ * The function to use to handle special case operators
* @return A AST from the expression
*
* FIXME The handleSpecialOp function seems like an ugly
- * interface. Maybe there's a better way to express how
- * that works.
+ * interface. Maybe there's a better way to express how that
+ * works.
*/
- public static <TokenType> ITree<TokenType> constructTree(
- IList<TokenType> tokens,
- Predicate<TokenType> isOperator,
- Predicate<TokenType> isSpecialOperator,
- Function<TokenType, Function<Deque<ITree<TokenType>>,
- ITree<TokenType>>> handleSpecialOperator) {
+ public static <TokenType> ITree<TokenType> constructTree(IList<TokenType> tokens,
+ Predicate<TokenType> isOperator, Predicate<TokenType> isSpecialOperator,
+ Function<TokenType, Function<Deque<ITree<TokenType>>, ITree<TokenType>>> handleSpecialOperator) {
// Make sure our parameters are valid
if (tokens == null) {
throw new NullPointerException("Tokens must not be null");
} else if (isOperator == null) {
- throw new NullPointerException(
- "Operator predicate must not be null");
+ throw new NullPointerException("Operator predicate must not be null");
} else if (isSpecialOperator == null) {
- throw new NullPointerException(
- "Special operator determiner must not be null");
+ throw new NullPointerException("Special operator determiner must not be null");
}
// Here is the state for the tree construction
- IHolder<IPair<Deque<ITree<TokenType>>,
- ITree<TokenType>>> initialState = new Identity<>(
- new Pair<>(new LinkedList<>(), null));
+ IHolder<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> initialState = new Identity<>(
+ new Pair<>(new LinkedList<>(), null));
// Transform each of the tokens
- tokens.forEach(
- new TokenTransformer<>(initialState, isOperator,
- isSpecialOperator, handleSpecialOperator));
+ tokens.forEach(new TokenTransformer<>(initialState, isOperator, isSpecialOperator,
+ handleSpecialOperator));
// Grab the tree from the state
return initialState.unwrap((pair) -> {