From 27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd Mon Sep 17 00:00:00 2001 From: EVE Date: Mon, 13 Mar 2017 16:42:21 -0400 Subject: Formatting --- .../java/bjc/utils/parserutils/IPrecedent.java | 2 +- .../utils/parserutils/RuleBasedConfigReader.java | 50 +++++++++---------- .../utils/parserutils/RuleBasedReaderPragmas.java | 32 ++++++------ .../java/bjc/utils/parserutils/ShuntingYard.java | 43 ++++++++-------- .../bjc/utils/parserutils/TokenTransformer.java | 42 +++++++--------- .../bjc/utils/parserutils/TreeConstructor.java | 57 +++++++++------------- 6 files changed, 100 insertions(+), 126 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils') 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 - * The type of the state object to use + * The type of the state object to use * */ public class RuleBasedConfigReader { // 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> start; - + // Function to use when continuing a rule // Takes a tokenizer and application state private BiConsumer continueRule; @@ -46,16 +47,14 @@ public class RuleBasedConfigReader { * 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> start, - BiConsumer continueRule, - Consumer end) { + public RuleBasedConfigReader(BiConsumer> start, + BiConsumer continueRule, Consumer end) { this.start = start; this.continueRule = continueRule; this.end = end; @@ -67,13 +66,11 @@ public class RuleBasedConfigReader { * 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 action) { + public void addPragma(String name, BiConsumer action) { if (name == null) { throw new NullPointerException("Pragma name must not be null"); } else if (action == null) { @@ -116,9 +113,9 @@ public class RuleBasedConfigReader { * 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 { 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 { * 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 continueRule) { + public void setContinueRule(BiConsumer continueRule) { this.continueRule = continueRule; } @@ -176,7 +170,7 @@ public class RuleBasedConfigReader { * 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 end) { this.end = end; @@ -186,7 +180,7 @@ public class RuleBasedConfigReader { * 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> 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 - * 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 BiConsumer buildInteger(String name, - BiConsumer consumer) { + public static BiConsumer buildInteger(String name, + BiConsumer 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 - * 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 BiConsumer buildStringCollapser(String name, - BiConsumer consumer) { + public static BiConsumer buildStringCollapser(String name, + BiConsumer 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 - * The type of tokens being shunted + * The type of tokens being shunted */ public class ShuntingYard { /** @@ -58,9 +58,9 @@ public class ShuntingYard { } private final class TokenShunter implements Consumer { - private IList output; - private Deque stack; - private Function transformer; + private IList output; + private Deque stack; + private Function transformer; public TokenShunter(IList outpt, Deque stack, Function transformer) { @@ -73,19 +73,21 @@ public class ShuntingYard { 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 { * 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 { * 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 { * 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 { * 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 postfix(IList input, - Function transformer) { + public IList postfix(IList input, Function transformer) { // Check our input if (input == null) { throw new NullPointerException("Input must not be null"); @@ -216,8 +217,8 @@ public class ShuntingYard { * 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 implements Consumer { // Handle operators - private final class OperatorHandler implements UnaryOperator< - IPair>, ITree>> { + private final class OperatorHandler implements UnaryOperator>, ITree>> { private TokenType element; public OperatorHandler(TokenType element) { @@ -25,24 +24,24 @@ final class TokenTransformer implements Consumer { @Override public IPair>, ITree> apply( IPair>, ITree> 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>, - ITree> handleOperator( - Deque> queuedASTs) { + private IPair>, ITree> handleOperator( + Deque> queuedASTs) { // The AST we're going to hand back ITree 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 implements Consumer { } } - private IHolder>, - ITree>> initialState; + private IHolder>, ITree>> initialState; - private Predicate< - TokenType> operatorPredicate; + private Predicate operatorPredicate; - private Predicate< - TokenType> isSpecialOperator; - private Function>, - ITree>> handleSpecialOperator; + private Predicate isSpecialOperator; + private Function>, ITree>> handleSpecialOperator; // Create a new transformer - public TokenTransformer( - IHolder>, - ITree>> initialState, - Predicate operatorPredicate, - Predicate isSpecialOperator, - Function>, - ITree>> handleSpecialOperator) { + public TokenTransformer(IHolder>, ITree>> initialState, + Predicate operatorPredicate, Predicate isSpecialOperator, + Function>, ITree>> handleSpecialOperator) { this.initialState = initialState; this.operatorPredicate = operatorPredicate; this.isSpecialOperator = isSpecialOperator; @@ -102,7 +93,8 @@ final class TokenTransformer implements Consumer { // 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 - * 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 ITree constructTree( - IList tokens, + public static ITree constructTree(IList tokens, Predicate 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 - * 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 ITree constructTree( - IList tokens, - Predicate isOperator, - Predicate isSpecialOperator, - Function>, - ITree>> handleSpecialOperator) { + public static ITree constructTree(IList tokens, + Predicate isOperator, Predicate isSpecialOperator, + Function>, ITree>> 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>, - ITree>> initialState = new Identity<>( - new Pair<>(new LinkedList<>(), null)); + IHolder>, ITree>> 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) -> { -- cgit v1.2.3