From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../src/main/java/bjc/utils/parserutils/AST.java | 5 +++- .../utils/parserutils/RuleBasedConfigReader.java | 32 +++++++++++++--------- .../java/bjc/utils/parserutils/ShuntingYard.java | 17 ++++++------ .../bjc/utils/parserutils/TreeConstructor.java | 6 ++-- 4 files changed, 35 insertions(+), 25 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils') 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 90961d5..88b4862 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -183,11 +183,14 @@ public class AST { /** * Expand the nodes in an AST * + * This is actually equivalent to converting the tree into an ordered + * list, doing a flatMap, and then reconstructing the tree + * * @param expander * The function to use for expanding nodes * @return The expanded AST */ - public AST expand(Function> expander) { + public AST flatMapTree(Function> expander) { if (expander == null) { throw new NullPointerException("Expander must not be null"); } 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 b6162e5..999503c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -8,6 +8,7 @@ import java.util.Scanner; import java.util.function.BiConsumer; import java.util.function.Consumer; +import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.exceptions.UnknownPragmaException; import bjc.utils.funcdata.FunctionalStringTokenizer; @@ -20,9 +21,10 @@ import bjc.utils.funcdata.FunctionalStringTokenizer; * * @param * The type of the state object to use + * */ public class RuleBasedConfigReader { - private BiConsumer> startRule; + private BiConsumer> startRule; private BiConsumer continueRule; private Consumer endRule; @@ -39,7 +41,7 @@ public class RuleBasedConfigReader { * The action to fire when ending a rule */ public RuleBasedConfigReader( - BiConsumer> startRule, + BiConsumer> startRule, BiConsumer continueRule, Consumer endRule) { this.startRule = startRule; @@ -90,20 +92,26 @@ public class RuleBasedConfigReader { state = initialState; boolean ruleOpen = false; + while (inputSource.hasNextLine()) { String line = inputSource.nextLine(); - if (line.equals("")) { + if (line.startsWith("#") || line.startsWith("//")) { + // It's a comment + continue; + } else if (line.equals("")) { if (ruleOpen == false) { // Ignore blank line without an open rule - } - - if (endRule == null) { - // Nothing happens on rule end } else { - endRule.accept(state); - } + if (endRule == null) { + // Nothing happens on rule end + ruleOpen = false; + } else { + endRule.accept(state); + } + ruleOpen = false; + } continue; } else if (line.startsWith("\t")) { if (ruleOpen == false) { @@ -125,9 +133,7 @@ public class RuleBasedConfigReader { String nextToken = tokenizer.nextToken(); - if (nextToken.equals("#") || nextToken.equals("//")) { - // Do nothing, this is a comment - } else if (nextToken.equals("pragma")) { + if (nextToken.equals("pragma")) { String token = tokenizer.nextToken(); pragmas.getOrDefault(token, (tokenzer, stat) -> { @@ -181,7 +187,7 @@ public class RuleBasedConfigReader { * The action to execute on starting of a rule */ public void setStartRule( - BiConsumer> startRule) { + BiConsumer> startRule) { if (startRule == null) { throw new NullPointerException( "Action on rule start must be non-null"); 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 1e5d487..a60cb01 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -1,13 +1,14 @@ package bjc.utils.parserutils; import java.util.Deque; -import java.util.HashMap; import java.util.LinkedList; -import java.util.Map; import java.util.function.Consumer; 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.funcutils.StringUtils; /** @@ -61,11 +62,11 @@ public class ShuntingYard { } private final class TokenShunter implements Consumer { - private FunctionalList output; + private IFunctionalList output; private Deque stack; private Function transform; - public TokenShunter(FunctionalList outpt, Deque stack, + public TokenShunter(IFunctionalList outpt, Deque stack, Function transform) { this.output = outpt; this.stack = stack; @@ -115,13 +116,13 @@ public class ShuntingYard { /** * Holds all the shuntable operations */ - private Map operators; + private IFunctionalMap operators; /** * Create a new shunting yard with a default set of operators */ public ShuntingYard() { - operators = new HashMap<>(); + operators = new FunctionalMap<>(); if (shouldConfigureBasicOperators) { operators.put("+", Operator.ADD); @@ -175,7 +176,7 @@ public class ShuntingYard { * The function to use to transform strings to tokens * @return A list of tokens in postfix notation */ - public FunctionalList postfix(FunctionalList input, + public IFunctionalList postfix(IFunctionalList input, Function tokenTransformer) { if (input == null) { throw new NullPointerException("Input must not be null"); @@ -183,7 +184,7 @@ public class ShuntingYard { throw new NullPointerException("Transformer must not be null"); } - FunctionalList output = new FunctionalList<>(); + IFunctionalList output = new FunctionalList<>(); Deque 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 6fedf87..46e4da4 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 java.util.function.Predicate; import bjc.utils.data.GenHolder; import bjc.utils.data.IPair; import bjc.utils.data.Pair; -import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; /** * Creates a parse tree from a postfix expression @@ -119,7 +119,7 @@ public class TreeConstructor { * operator * @return A AST from the expression */ - public static AST constructTree(FunctionalList tokens, + public static AST constructTree(IFunctionalList tokens, Predicate operatorPredicate) { return constructTree(tokens, operatorPredicate, (op) -> false, null); @@ -149,7 +149,7 @@ public class TreeConstructor { * interface. Maybe there's a better way to express how that * works */ - public static AST constructTree(FunctionalList tokens, + public static AST constructTree(IFunctionalList tokens, Predicate operatorPredicate, Predicate isSpecialOperator, Function>, AST> handleSpecialOperator) { if (tokens == null) { -- cgit v1.2.3