diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
4 files changed, 82 insertions, 16 deletions
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 f390155..943d177 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -1,6 +1,5 @@ package bjc.utils.parserutils; -import java.util.Map; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; @@ -41,9 +40,9 @@ public class AST<T> { * * @param tokn * The token in this node - * @param left + * @param lft * The left child of this AST - * @param right + * @param rght * The right child of this AST */ public AST(T tokn, AST<T> lft, AST<T> rght) { @@ -53,6 +52,14 @@ public class AST<T> { right = rght; } + /** + * Traverse an AST + * + * @param tlm + * The way to traverse the tree + * @param con + * The function to call on each traversed element + */ public void traverse(TreeLinearizationMethod tlm, Consumer<T> con) { if (left != null && right != null) { switch (tlm) { @@ -83,6 +90,8 @@ public class AST<T> { /** * Collapse this tree into a single node + * @param <E> The final value of the collapsed tree + * @param <T2> * * @param tokenTransform * The function to transform nodes into data @@ -159,7 +168,7 @@ public class AST<T> { * @param n * The number of levels to indent */ - private void indentNLevels(StringBuilder sb, int n) { + private static void indentNLevels(StringBuilder sb, int n) { for (int i = 0; i <= n; i++) { sb.append("\t"); } @@ -190,6 +199,7 @@ public class AST<T> { /** * Transmute the tokens in an AST into a different sort of token + * @param <E> The type of the transformed tokens * * @param tokenTransformer * The transform to run on the tokens @@ -207,6 +217,6 @@ public class AST<T> { r = right.transmuteAST(tokenTransformer); } - return new AST<E>(tokenTransformer.apply(token), l, r); + return new AST<>(tokenTransformer.apply(token), l, r); } } 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 963437e..12f6891 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -17,8 +17,8 @@ import bjc.utils.funcdata.FunctionalStringTokenizer; * * @author ben * - * @param <E>The - * type of the state object to use + * @param <E> + * The type of the state object to use */ public class RuleBasedConfigReader<E> { private BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule; @@ -48,11 +48,28 @@ public class RuleBasedConfigReader<E> { this.pragmas = new HashMap<>(); } + /** + * Add a pragma to this reader + * + * @param pragName + * The name of the pragma to add + * @param pragAct + * The function to execute when this pragma is read + */ public void addPragma(String pragName, BiConsumer<FunctionalStringTokenizer, E> pragAct) { pragmas.put(pragName, pragAct); } + /** + * Run a stream through this reader + * + * @param is + * The stream to get input + * @param initState + * The initial state of the reader + * @return The final state of the reader + */ public E fromStream(InputStream is, E initState) { Scanner scn = new Scanner(is); @@ -68,8 +85,8 @@ public class RuleBasedConfigReader<E> { continueRule.accept(new FunctionalStringTokenizer( ln.substring(1), " "), stat); } else { - FunctionalStringTokenizer stk = new FunctionalStringTokenizer( - ln, " "); + FunctionalStringTokenizer stk = + new FunctionalStringTokenizer(ln, " "); String nxtToken = stk.nextToken(); if (nxtToken.equals("#")) { @@ -82,8 +99,7 @@ public class RuleBasedConfigReader<E> { "Unknown pragma " + tk); }).accept(stk, stat); } else { - startRule.accept(stk, - new Pair<String, E>(nxtToken, stat)); + startRule.accept(stk, new Pair<>(nxtToken, stat)); } } } @@ -93,15 +109,33 @@ public class RuleBasedConfigReader<E> { return stat; } + /** + * Set the action to execute when continuing a rule + * + * @param continueRule + * The action to execute on continuation of a rule + */ public void setContinueRule( BiConsumer<FunctionalStringTokenizer, E> continueRule) { this.continueRule = continueRule; } + /** + * Set the action to execute when ending a rule + * + * @param endRule + * The action to execute on ending of a rule + */ public void setEndRule(Consumer<E> endRule) { this.endRule = endRule; } + /** + * Set the action to execute when starting a rule + * + * @param startRule + * The action to execute on starting of a rule + */ public void setStartRule( BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule) { this.startRule = startRule; 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 8a036ce..b5f48e7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -19,8 +19,29 @@ import bjc.utils.funcdata.FunctionalList; */ public class ShuntingYard<E> { + /** + * A enum representing the fundamental operator types + * + * @author ben + * + */ public static enum Operator implements IPrecedent { - ADD(1), DIVIDE(4), MULTIPLY(3), SUBTRACT(2); + /** + * Represents addition + */ + ADD(1), + /** + * Represents division + */ + DIVIDE(4), + /** + * Represents multiplication + */ + MULTIPLY(3), + /** + * Represents subtraction + */ + SUBTRACT(2); private final int precedence; @@ -61,6 +82,7 @@ public class ShuntingYard<E> { * * @param tok * The token representing the operator + * @param i The precedence of the operator to add */ public void addOp(String tok, int i) { this.addOp(tok, IPrecedent.newSimplePrecedent(i)); 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 e1a03f1..30c147e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -13,8 +13,6 @@ import bjc.utils.funcdata.FunctionalList; * * @author ben * - * @param <T> - * The elements of the parse tree */ public class TreeConstructor { /** @@ -22,6 +20,8 @@ public class TreeConstructor { * * Only binary operators are accepted. * + * @param <T> + * The elements of the parse tree * @param toks * The list of tokens to build a tree from * @param opPredicate @@ -43,7 +43,7 @@ public class TreeConstructor { AST<T> right = deq.pop(); AST<T> left = deq.pop(); - AST<T> newAST = new AST<T>(ele, left, right); + AST<T> newAST = new AST<>(ele, left, right); deq.push(newAST); @@ -61,7 +61,7 @@ public class TreeConstructor { initState.doWith((par) -> par.doWith((deq, ast) -> { deq.push(newAST); })); - + initState.transform((par) -> { return (Pair<Deque<AST<T>>, AST<T>>) par .apply((d) -> d, (a) -> newAST); |
