diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:49:54 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:49:54 -0400 |
| commit | 56f07e9a3aaa873fe385d224f088f048dbafa8f7 (patch) | |
| tree | 64fae78f95fd1c233689ecf3dda2e2b645bb8d33 | |
| parent | 251419e1f0ab8eb04d21287b708b06a552f4c58a (diff) | |
Cleanup
42 files changed, 582 insertions, 553 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java index 45aac1a..f3cc003 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java @@ -9,14 +9,15 @@ import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; class AssignCommand extends NonBinaryCommand<String, String, TestContext> { - public AssignCommand(int prec) { + public AssignCommand(final int prec) { super(prec); } @Override - public ITree<Token<String, String>> denote(ITree<Token<String, String>> operand, Token<String, String> operator, - ParserContext<String, String, TestContext> ctx) throws ParserException { - Token<String, String> name = operand.getHead(); + public ITree<Token<String, String>> denote(final ITree<Token<String, String>> operand, + final Token<String, String> operator, final ParserContext<String, String, TestContext> ctx) + throws ParserException { + final Token<String, String> name = operand.getHead(); switch (name.getKey()) { case "(literal)": @@ -26,7 +27,7 @@ class AssignCommand extends NonBinaryCommand<String, String, TestContext> { throw new ParserException("Variable name must be simple"); } - ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); + final ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); ctx.state.scopes.top().putKey(name.getValue(), body); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java b/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java index 67e560c..092c6b2 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java @@ -1,19 +1,19 @@ package bjc.pratt.examples; +import java.util.function.UnaryOperator; + import bjc.pratt.Token; import bjc.utils.data.ITree; import bjc.utils.esodata.Directory; import bjc.utils.esodata.Stack; -import java.util.function.UnaryOperator; - final class BlockEnter implements UnaryOperator<TestContext> { @Override - public TestContext apply(TestContext state) { - Directory<String, ITree<Token<String, String>>> enclosing = state.scopes.top(); - Stack<Integer> blockCount = state.blockCount; + public TestContext apply(final TestContext state) { + final Directory<String, ITree<Token<String, String>>> enclosing = state.scopes.top(); + final Stack<Integer> blockCount = state.blockCount; - int currBlockNumber = blockCount.pop(); + final int currBlockNumber = blockCount.pop(); state.scopes.push(enclosing.newSubdirectory("block" + currBlockNumber)); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/BlockExit.java b/JPratt/src/examples/java/bjc/pratt/examples/BlockExit.java index 1cca6d5..ea9526d 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/BlockExit.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/BlockExit.java @@ -4,7 +4,7 @@ import java.util.function.UnaryOperator; final class BlockExit implements UnaryOperator<TestContext> { @Override - public TestContext apply(TestContext state) { + public TestContext apply(final TestContext state) { state.scopes.pop(); state.blockCount.pop(); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java index b7d31eb..e504048 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java @@ -1,5 +1,30 @@ package bjc.pratt.examples; +import static bjc.pratt.commands.InitialCommands.delimited; +import static bjc.pratt.commands.InitialCommands.grouping; +import static bjc.pratt.commands.InitialCommands.leaf; +import static bjc.pratt.commands.InitialCommands.preTernary; +import static bjc.pratt.commands.InitialCommands.unary; +import static bjc.pratt.commands.NonInitialCommands.chain; +import static bjc.pratt.commands.NonInitialCommands.infixLeft; +import static bjc.pratt.commands.NonInitialCommands.infixNon; +import static bjc.pratt.commands.NonInitialCommands.infixRight; +import static bjc.pratt.commands.NonInitialCommands.postCircumfix; +import static bjc.pratt.commands.NonInitialCommands.postfix; +import static bjc.pratt.commands.NonInitialCommands.ternary; +import static bjc.pratt.tokens.StringToken.litToken; +import static bjc.utils.functypes.ID.id; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; +import java.util.Set; +import java.util.function.UnaryOperator; + import bjc.pratt.InitialCommand; import bjc.pratt.NonInitialCommand; import bjc.pratt.PrattParser; @@ -14,39 +39,24 @@ import bjc.utils.parserutils.splitterv2.ConfigurableTokenSplitter; import bjc.utils.parserutils.splitterv2.ExcludingTokenSplitter; import bjc.utils.parserutils.splitterv2.TokenSplitter; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Scanner; -import java.util.Set; -import java.util.function.UnaryOperator; - -import static bjc.pratt.commands.InitialCommands.*; -import static bjc.pratt.commands.NonInitialCommands.*; -import static bjc.pratt.tokens.StringToken.litToken; -import static bjc.utils.functypes.ID.id; - /** * Simple test for Pratt parser. - * + * * @author EVE * */ public class PrattParserTest { /** * Main method. - * + * * @param args * Unused CLI arguments. */ - public static void main(String[] args) { + public static void main(final String[] args) { /* * Use a linked hash set to preserve insertion order. */ - Set<String> ops = new LinkedHashSet<>(); + final Set<String> ops = new LinkedHashSet<>(); ops.add("!!!"); @@ -55,7 +65,7 @@ public class PrattParserTest { ops.addAll(Arrays.asList("||", "&&")); ops.addAll(Arrays.asList("<=", ">=")); - ops.addAll(Arrays.asList("±")); + ops.addAll(Arrays.asList("�")); ops.addAll(Arrays.asList(".", ",", ";", ":")); ops.addAll(Arrays.asList("=", "<", ">")); ops.addAll(Arrays.asList("+", "-", "*", "/")); @@ -67,7 +77,7 @@ public class PrattParserTest { /* * Reserved words that represent themselves, not literals. */ - Set<String> reserved = new LinkedHashSet<>(); + final Set<String> reserved = new LinkedHashSet<>(); reserved.addAll(Arrays.asList("if", "then", "else")); reserved.addAll(Arrays.asList("and", "or")); reserved.addAll(Arrays.asList("begin", "end")); @@ -75,17 +85,17 @@ public class PrattParserTest { reserved.addAll(Arrays.asList("sqrt", "cbrt", "root")); reserved.add("var"); - ChainTokenSplitter nsplit = new ChainTokenSplitter(); + final ChainTokenSplitter nsplit = new ChainTokenSplitter(); - ConfigurableTokenSplitter hi = new ConfigurableTokenSplitter(true); - ConfigurableTokenSplitter lo = new ConfigurableTokenSplitter(true); + final ConfigurableTokenSplitter hi = new ConfigurableTokenSplitter(true); + final ConfigurableTokenSplitter lo = new ConfigurableTokenSplitter(true); hi.addSimpleDelimiters("->"); hi.addSimpleDelimiters(":="); hi.addSimpleDelimiters("||", "&&"); hi.addSimpleDelimiters("<=", ">="); - lo.addSimpleDelimiters("±"); + lo.addSimpleDelimiters("�"); lo.addSimpleDelimiters(".", ",", ";", ":"); lo.addSimpleDelimiters("=", "<", ">"); lo.addSimpleDelimiters("+", "-", "*", "/"); @@ -101,38 +111,40 @@ public class PrattParserTest { nsplit.appendSplitters(hi, lo); - ExcludingTokenSplitter excluder = new ExcludingTokenSplitter(nsplit); + final ExcludingTokenSplitter excluder = new ExcludingTokenSplitter(nsplit); excluder.addLiteralExclusions(reserved.toArray(new String[0])); - PrattParser<String, String, TestContext> parser = createParser(); + final PrattParser<String, String, TestContext> parser = createParser(); - TestContext ctx = new TestContext(); + final TestContext ctx = new TestContext(); - Scanner scn = new Scanner(System.in); + final Scanner scn = new Scanner(System.in); System.out.print("Enter a command (blank line to exit): "); String ln = scn.nextLine(); while (!ln.trim().equals("")) { - Iterator<Token<String, String>> tokens = preprocessInput(ops, excluder, ln, reserved, ctx); + final Iterator<Token<String, String>> tokens = preprocessInput(ops, excluder, ln, reserved, + ctx); try { - StringTokenStream tokenStream = new StringTokenStream(tokens); + final StringTokenStream tokenStream = new StringTokenStream(tokens); /* * Prime stream. */ tokenStream.next(); - ITree<Token<String, String>> tree = parser.parseExpression(0, tokenStream, ctx, true); + final ITree<Token<String, String>> tree = parser.parseExpression(0, tokenStream, ctx, + true); if (!tokenStream.headIs("(end)")) { System.out.println("\nMultiple expressions on line"); } System.out.println("\nParsed expression:\n" + tree); - } catch (ParserException pex) { + } catch (final ParserException pex) { pex.printStackTrace(); } @@ -146,16 +158,16 @@ public class PrattParserTest { scn.close(); } - private static Iterator<Token<String, String>> preprocessInput(Set<String> ops, TokenSplitter split, String ln, - Set<String> reserved, TestContext ctx) { - String[] rawTokens = ln.split("\\s+"); + private static Iterator<Token<String, String>> preprocessInput(final Set<String> ops, final TokenSplitter split, + final String ln, final Set<String> reserved, final TestContext ctx) { + final String[] rawTokens = ln.split("\\s+"); - List<String> splitTokens = new LinkedList<>(); + final List<String> splitTokens = new LinkedList<>(); - for (String raw : rawTokens) { + for (final String raw : rawTokens) { boolean doSplit = false; - for (String op : ops) { + for (final String op : ops) { if (raw.contains(op)) { doSplit = true; break; @@ -163,7 +175,7 @@ public class PrattParserTest { } if (doSplit) { - String[] strangs = split.split(raw).toArray(new String[0]); + final String[] strangs = split.split(raw).toArray(new String[0]); splitTokens.addAll(Arrays.asList(strangs)); } else { @@ -173,11 +185,11 @@ public class PrattParserTest { System.out.println("\nSplit string: " + splitTokens); - Iterator<String> source = splitTokens.iterator(); + final Iterator<String> source = splitTokens.iterator(); - Tokenizer tokenzer = new Tokenizer(ops, reserved, ctx); + final Tokenizer tokenzer = new Tokenizer(ops, reserved, ctx); - Iterator<Token<String, String>> tokens = new TransformIterator<>(source, tokenzer); + final Iterator<Token<String, String>> tokens = new TransformIterator<>(source, tokenzer); return tokens; } @@ -186,86 +198,88 @@ public class PrattParserTest { /* * Set of which relational operators chain with each other. */ - HashSet<String> relChain = new HashSet<>(); + final HashSet<String> relChain = new HashSet<>(); relChain.addAll(Arrays.asList("=", "<", ">", "<=", ">=")); /* * Token for marking chains. */ - StringToken chainToken = litToken("and"); + final StringToken chainToken = litToken("and"); /* * ID function. */ - UnaryOperator<TestContext> idfun = id(); + final UnaryOperator<TestContext> idfun = id(); - PrattParser<String, String, TestContext> parser = new PrattParser<>(); + final PrattParser<String, String, TestContext> parser = new PrattParser<>(); parser.addNonInitialCommand("!!!", postfix(0)); parser.addNonInitialCommand(":", infixNon(3)); - NonInitialCommand<String, String, TestContext> ifElse = ternary(5, 0, "else", litToken("cond"), false); + final NonInitialCommand<String, String, TestContext> ifElse = ternary(5, 0, "else", litToken("cond"), + false); parser.addNonInitialCommand("if", ifElse); parser.addNonInitialCommand(":=", new AssignCommand(10)); parser.addNonInitialCommand("->", infixRight(11)); - NonInitialCommand<String, String, TestContext> nonSSRelJoin = infixLeft(13); + final NonInitialCommand<String, String, TestContext> nonSSRelJoin = infixLeft(13); parser.addNonInitialCommand("and", nonSSRelJoin); parser.addNonInitialCommand("or", nonSSRelJoin); - NonInitialCommand<String, String, TestContext> chainRelOp = chain(15, relChain, chainToken); + final NonInitialCommand<String, String, TestContext> chainRelOp = chain(15, relChain, chainToken); parser.addNonInitialCommand("=", chainRelOp); parser.addNonInitialCommand("<", chainRelOp); parser.addNonInitialCommand(">", chainRelOp); parser.addNonInitialCommand("<=", chainRelOp); parser.addNonInitialCommand(">=", chainRelOp); - NonInitialCommand<String, String, TestContext> ssRelJoin = infixRight(17); + final NonInitialCommand<String, String, TestContext> ssRelJoin = infixRight(17); parser.addNonInitialCommand("&&", ssRelJoin); parser.addNonInitialCommand("||", ssRelJoin); - NonInitialCommand<String, String, TestContext> addSub = infixLeft(20); + final NonInitialCommand<String, String, TestContext> addSub = infixLeft(20); parser.addNonInitialCommand("+", addSub); parser.addNonInitialCommand("-", addSub); - parser.addNonInitialCommand("±", addSub); + parser.addNonInitialCommand("�", addSub); - NonInitialCommand<String, String, TestContext> mulDiv = infixLeft(30); + final NonInitialCommand<String, String, TestContext> mulDiv = infixLeft(30); parser.addNonInitialCommand("*", mulDiv); parser.addNonInitialCommand("/", mulDiv); parser.addNonInitialCommand("!", postfix(40)); - NonInitialCommand<String, String, TestContext> expon = infixRight(50); + final NonInitialCommand<String, String, TestContext> expon = infixRight(50); parser.addNonInitialCommand("^", expon); parser.addNonInitialCommand("root", expon); - NonInitialCommand<String, String, TestContext> superexpon = postfix(50); + final NonInitialCommand<String, String, TestContext> superexpon = postfix(50); parser.addNonInitialCommand("(superexp)", superexpon); parser.addNonInitialCommand(".", infixLeft(60)); - NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", litToken("idx")); + final NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", + litToken("idx")); parser.addNonInitialCommand("[", arrayIdx); - InitialCommand<String, String, TestContext> ifThenElse = preTernary(0, 0, 0, "then", "else", + final InitialCommand<String, String, TestContext> ifThenElse = preTernary(0, 0, 0, "then", "else", litToken("ifelse")); parser.addInitialCommand("if", ifThenElse); - InitialCommand<String, String, TestContext> parens = grouping(0, ")", litToken("parens")); + final InitialCommand<String, String, TestContext> parens = grouping(0, ")", litToken("parens")); parser.addInitialCommand("(", parens); - InitialCommand<String, String, TestContext> scoper = delimited(0, ";", "end", litToken("block"), + final InitialCommand<String, String, TestContext> scoper = delimited(0, ";", "end", litToken("block"), new BlockEnter(), idfun, new BlockExit(), true); parser.addInitialCommand("begin", scoper); - InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", litToken("array"), - idfun, idfun, idfun, false); + final InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", + litToken("array"), idfun, idfun, idfun, false); parser.addInitialCommand("[", arrayLiteral); - InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), + final InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), idfun, idfun, idfun, false); parser.addInitialCommand("{", jsonLiteral); @@ -273,11 +287,11 @@ public class PrattParserTest { parser.addInitialCommand("-", unary(30)); - InitialCommand<String, String, TestContext> root = unary(50); + final InitialCommand<String, String, TestContext> root = unary(50); parser.addInitialCommand("sqrt", root); parser.addInitialCommand("cbrt", root); - InitialCommand<String, String, TestContext> leaf = leaf(); + final InitialCommand<String, String, TestContext> leaf = leaf(); parser.addInitialCommand("(literal)", leaf); parser.addInitialCommand("(vref)", leaf); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java index 4a5bd22..6c4e2b6 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java @@ -10,11 +10,11 @@ import bjc.utils.parserutils.ParserException; class SwitchCommand implements InitialCommand<String, String, TestContext> { @Override - public ITree<Token<String, String>> denote(Token<String, String> operator, - ParserContext<String, String, TestContext> ctx) throws ParserException { - ITree<Token<String, String>> object = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); + public ITree<Token<String, String>> denote(final Token<String, String> operator, + final ParserContext<String, String, TestContext> ctx) throws ParserException { + final ITree<Token<String, String>> object = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); - ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); + final ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); return new Tree<>(new StringToken("switch", "switch"), object, body); } diff --git a/JPratt/src/examples/java/bjc/pratt/examples/TestContext.java b/JPratt/src/examples/java/bjc/pratt/examples/TestContext.java index b5e8448..e5583b4 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/TestContext.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/TestContext.java @@ -9,7 +9,7 @@ import bjc.utils.esodata.Stack; /** * Simple context for the parser. - * + * * @author EVE * */ diff --git a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java index 0fccc60..9b3fa74 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java @@ -1,37 +1,46 @@ package bjc.pratt.examples; -import bjc.pratt.Token; -import bjc.pratt.tokens.StringToken; +import static bjc.pratt.tokens.StringToken.litToken; import java.util.Set; import java.util.function.Function; -import static bjc.pratt.tokens.StringToken.litToken; +import bjc.pratt.Token; +import bjc.pratt.tokens.StringToken; final class Tokenizer implements Function<String, Token<String, String>> { - private Set<String> ops; - private Set<String> reserved; - private TestContext ctx; + private final Set<String> ops; + private final Set<String> reserved; + private final TestContext ctx; - public Tokenizer(Set<String> operators, Set<String> reservedWords, TestContext context) { + public Tokenizer(final Set<String> operators, final Set<String> reservedWords, final TestContext context) { ops = operators; reserved = reservedWords; ctx = context; } @Override - public Token<String, String> apply(String strang) { - if (ops.contains(strang) || reserved.contains(strang)) { + public Token<String, String> apply(final String strang) { + if (ops.contains(strang) || reserved.contains(strang)) return litToken(strang); - } else if (ctx.scopes.top().containsKey(strang)) { + else if (ctx.scopes.top().containsKey(strang)) return new StringToken("(vref)", strang); - } else if(strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) { - /* - * This regular expression matches series of unicode super-scripts 1-9. - */ + else if (strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) /* + * This + * regular + * expression + * matches + * series + * of + * unicode + * super + * - + * scripts + * 1 + * - + * 9. + */ return new StringToken("(superexp)", strang); - } else { - return new StringToken("(literal)", strang); - } + else return new StringToken("(literal)", strang); } } diff --git a/JPratt/src/examples/java/bjc/pratt/examples/VarCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/VarCommand.java index dd04a59..a194b72 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/VarCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/VarCommand.java @@ -11,11 +11,11 @@ import bjc.utils.parserutils.ParserException; class VarCommand extends AbstractInitialCommand<String, String, TestContext> { @Override - protected ITree<Token<String, String>> intNullDenotation(Token<String, String> operator, - ParserContext<String, String, TestContext> ctx) throws ParserException { - Token<String, String> name = ctx.tokens.current(); + protected ITree<Token<String, String>> intNullDenotation(final Token<String, String> operator, + final ParserContext<String, String, TestContext> ctx) throws ParserException { + final Token<String, String> name = ctx.tokens.current(); - switch(name.getKey()) { + switch (name.getKey()) { case "(literal)": case "(vref)": ctx.tokens.next(); @@ -26,7 +26,7 @@ class VarCommand extends AbstractInitialCommand<String, String, TestContext> { ctx.tokens.expect("="); - ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); + final ITree<Token<String, String>> body = ctx.parse.parseExpression(0, ctx.tokens, ctx.state, false); ctx.state.scopes.top().putKey(name.getValue(), body); diff --git a/JPratt/src/main/java/bjc/pratt/InitialCommand.java b/JPratt/src/main/java/bjc/pratt/InitialCommand.java index 470ef3b..403cf24 100644 --- a/JPratt/src/main/java/bjc/pratt/InitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/InitialCommand.java @@ -5,32 +5,32 @@ import bjc.utils.parserutils.ParserException; /** * Represents an initial command in parsing. - * + * * @author EVE - * + * * @param <K> * The key type for the tokens. - * + * * @param <V> * The value type for the tokens. - * + * * @param <C> * The state type of the parser. - * + * * */ @FunctionalInterface public interface InitialCommand<K, V, C> { /** * Construct the null denotation of this command. - * + * * @param operator * The operator for this command. * @param ctx * The context for the command. - * + * * @return The tree for this command. - * + * * @throws ParserException * If something goes wrong during parsing. */ diff --git a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java index 3f6512d..bb1aa5b 100644 --- a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java @@ -5,15 +5,15 @@ import bjc.utils.parserutils.ParserException; /** * Represents a non-initial command in parsing. - * + * * @author EVE - * + * * @param <K> * The key type for the tokens. - * + * * @param <V> * The value type for the tokens. - * + * * @param <C> * The state type of the parser. * @@ -21,17 +21,17 @@ import bjc.utils.parserutils.ParserException; public abstract class NonInitialCommand<K, V, C> { /** * Construct the left denotation of this command. - * + * * @param operand * The left-hand operand of this command. * @param operator * The operator for this command. - * + * * @param ctx * The state needed for commands. - * + * * @return The tree this command forms. - * + * * @throws ParserException * If something went wrong during parsing. */ @@ -40,21 +40,21 @@ public abstract class NonInitialCommand<K, V, C> { /** * Get the left-binding power of this command. - * + * * This represents the general precedence of this command. - * + * * @return The left-binding power of this command. */ public abstract int leftBinding(); /** * Get the next-binding power of this command. - * + * * This represents the highest precedence of command this command can be * the left operand of. - * + * * This is the same as the left-binding power by default. - * + * * @return The next-binding power of this command. */ public int nextBinding() { diff --git a/JPratt/src/main/java/bjc/pratt/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/ParseBlock.java index cf707ed..28caf43 100644 --- a/JPratt/src/main/java/bjc/pratt/ParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/ParseBlock.java @@ -5,15 +5,15 @@ import bjc.utils.parserutils.ParserException; /** * Represents a embedded block in an expression. - * + * * @author bjculkin * * @param <K> * The key type of the token. - * + * * @param <V> * The value type of the token. - * + * * @param <C> * The state type of the parser. */ @@ -22,12 +22,12 @@ public interface ParseBlock<K, V, C> { /** * Parse the block this represents. - * + * * @param ctx * The context for parsing. - * + * * @return A AST for this block. - * + * * @throws ParserException * If something goes wrong during parsing, or the block * fails validation. diff --git a/JPratt/src/main/java/bjc/pratt/ParserContext.java b/JPratt/src/main/java/bjc/pratt/ParserContext.java index 1697a12..162c5cf 100644 --- a/JPratt/src/main/java/bjc/pratt/ParserContext.java +++ b/JPratt/src/main/java/bjc/pratt/ParserContext.java @@ -2,14 +2,14 @@ package bjc.pratt; /** * Represents the contextual state passed to a command. - * + * * @author EVE * * @param <K> * The key type of the tokens. * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ @@ -29,17 +29,17 @@ public class ParserContext<K, V, C> { /** * Create a new parser context. - * + * * @param tokns * The source of tokens. - * + * * @param prse * The parser to call for sub expressions. - * + * * @param stte * Any state needing to be kept during parsing. */ - public ParserContext(TokenStream<K, V> tokns, PrattParser<K, V, C> prse, C stte) { + public ParserContext(final TokenStream<K, V> tokns, final PrattParser<K, V, C> prse, final C stte) { this.tokens = tokns; this.parse = prse; this.state = stte; diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index c36cabc..b3ce4a7 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -1,28 +1,28 @@ package bjc.pratt; +import java.util.HashMap; +import java.util.Map; + import bjc.pratt.commands.DefaultInitialCommand; import bjc.pratt.commands.DefaultNonInitialCommand; import bjc.utils.data.ITree; import bjc.utils.funcutils.NumberUtils; import bjc.utils.parserutils.ParserException; -import java.util.HashMap; -import java.util.Map; - /** * A configurable Pratt parser for expressions. - * + * * @author EVE - * + * * @param <K> * The key type for the tokens. - * + * * @param <V> * The value type for the tokens. - * + * * @param <C> * The state type of the parser. - * + * * */ public class PrattParser<K, V, C> { @@ -35,24 +35,24 @@ public class PrattParser<K, V, C> { /* * Left-commands that depend on what the null command was. */ - private Map<K, Map<K, NonInitialCommand<K, V, C>>> dependantLeftCommands; + private final Map<K, Map<K, NonInitialCommand<K, V, C>>> dependantLeftCommands; /* * The left commands. */ - private Map<K, NonInitialCommand<K, V, C>> leftCommands; + private final Map<K, NonInitialCommand<K, V, C>> leftCommands; /* * The initial commands. */ - private Map<K, InitialCommand<K, V, C>> nullCommands; + private final Map<K, InitialCommand<K, V, C>> nullCommands; /* * Initial commands only checked for statements. */ - private Map<K, InitialCommand<K, V, C>> statementCommands; + private final Map<K, InitialCommand<K, V, C>> statementCommands; /** * Create a new Pratt parser. - * + * */ public PrattParser() { dependantLeftCommands = new HashMap<>(); @@ -64,34 +64,32 @@ public class PrattParser<K, V, C> { /** * Parse an expression. - * + * * @param precedence * The initial precedence for the expression. - * + * * @param tokens * The tokens for the expression. - * + * * @param state * The state of the parser. - * + * * @param isStatement * Whether or not to parse statements. - * + * * @return The expression as an AST. - * + * * @throws ParserException * If something goes wrong during parsing. */ - public ITree<Token<K, V>> parseExpression(int precedence, TokenStream<K, V> tokens, C state, - boolean isStatement) throws ParserException { - if (precedence < 0) { - throw new IllegalArgumentException("Precedence must be greater than zero"); - } + public ITree<Token<K, V>> parseExpression(final int precedence, final TokenStream<K, V> tokens, final C state, + final boolean isStatement) throws ParserException { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be greater than zero"); - Token<K, V> initToken = tokens.current(); + final Token<K, V> initToken = tokens.current(); tokens.next(); - K initKey = initToken.getKey(); + final K initKey = initToken.getKey(); ITree<Token<K, V>> ast; @@ -106,9 +104,9 @@ public class PrattParser<K, V, C> { int rightPrec = Integer.MAX_VALUE; while (true) { - Token<K, V> tok = tokens.current(); + final Token<K, V> tok = tokens.current(); - K key = tok.getKey(); + final K key = tok.getKey(); NonInitialCommand<K, V, C> command = leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); @@ -116,7 +114,7 @@ public class PrattParser<K, V, C> { command = dependantLeftCommands.get(initKey).getOrDefault(key, command); } - int leftBind = command.leftBinding(); + final int leftBind = command.leftBinding(); if (NumberUtils.between(precedence, rightPrec, leftBind)) { tokens.next(); @@ -133,63 +131,63 @@ public class PrattParser<K, V, C> { /** * Add a non-initial command to this parser. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addNonInitialCommand(K marker, NonInitialCommand<K, V, C> comm) { + public void addNonInitialCommand(final K marker, final NonInitialCommand<K, V, C> comm) { leftCommands.put(marker, comm); } /** * Add a initial command to this parser. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addInitialCommand(K marker, InitialCommand<K, V, C> comm) { + public void addInitialCommand(final K marker, final InitialCommand<K, V, C> comm) { nullCommands.put(marker, comm); } /** * Add a statement command to this parser. - * + * * The difference between statements and initial commands is that * statements can only appear at the start of the expression. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addStatementCommand(K marker, InitialCommand<K, V, C> comm) { + public void addStatementCommand(final K marker, final InitialCommand<K, V, C> comm) { statementCommands.put(marker, comm); } /** * Add a dependent non-initial command to this parser. - * + * * @param dependant * The dependent that precedes the command. - * + * * @param marker * The token key that marks the command. - * + * * @param comm * The command. */ - public void addDependantCommand(K dependant, K marker, NonInitialCommand<K, V, C> comm) { + public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand<K, V, C> comm) { if (dependantLeftCommands.containsKey(dependant)) { dependantLeftCommands.get(dependant).put(marker, comm); } else { - Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>(); + final Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>(); comms.put(marker, comm); diff --git a/JPratt/src/main/java/bjc/pratt/Token.java b/JPratt/src/main/java/bjc/pratt/Token.java index 1f2616b..d49dee5 100644 --- a/JPratt/src/main/java/bjc/pratt/Token.java +++ b/JPratt/src/main/java/bjc/pratt/Token.java @@ -2,12 +2,12 @@ package bjc.pratt; /** * Represents a simple parsing token. - * + * * @author EVE - * + * * @param <K> * The key type of this token. Represents the type of the token. - * + * * @param <V> * The value type of this token. Represents any additional data * for the token. @@ -16,14 +16,14 @@ package bjc.pratt; public interface Token<K, V> { /** * Get the key for this token. - * + * * @return The key for this token */ K getKey(); /** * Get the value for this token. - * + * * @return The value for this token. */ V getValue(); diff --git a/JPratt/src/main/java/bjc/pratt/TokenStream.java b/JPratt/src/main/java/bjc/pratt/TokenStream.java index e377352..7390e5f 100644 --- a/JPratt/src/main/java/bjc/pratt/TokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/TokenStream.java @@ -1,27 +1,28 @@ package bjc.pratt; -import bjc.utils.funcutils.StringUtils; -import bjc.utils.parserutils.ParserException; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import bjc.utils.funcutils.StringUtils; +import bjc.utils.parserutils.ParserException; + /** * A stream of tokens. - * + * * @author EVE * * @param <K> * The key type of the token. - * + * * @param <V> * The value type of the token. */ public abstract class TokenStream<K, V> implements Iterator<Token<K, V>> { /** * The exception thrown when an expectation fails. - * + * * @author EVE * */ @@ -30,18 +31,18 @@ public abstract class TokenStream<K, V> implements Iterator<Token<K, V>> { /** * Create a new exception with the specified message. - * + * * @param msg * The message of the exception. */ - public ExpectationException(String msg) { + public ExpectationException(final String msg) { super(msg); } } /** * Get the current token. - * + * * @return The current token. */ public abstract Token<K, V> current(); @@ -55,18 +56,18 @@ public abstract class TokenStream<K, V> implements Iterator<Token<K, V>> { /** * Utility method for checking that the next token is one of a specific * set of types, and then consuming it. - * + * * @param expectedKeys * The expected values - * + * * @throws ExpectationException * If the token is not one of the expected types. */ - public void expect(Set<K> expectedKeys) throws ExpectationException { - K curKey = current().getKey(); + public void expect(final Set<K> expectedKeys) throws ExpectationException { + final K curKey = current().getKey(); if (!expectedKeys.contains(curKey)) { - String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false); + final String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false); throw new ExpectationException("One of '" + expectedList + "' was expected, not " + curKey); } @@ -77,27 +78,27 @@ public abstract class TokenStream<K, V> implements Iterator<Token<K, V>> { /** * Utility method for checking that the next token is one of a specific * set of types, and then consuming it. - * + * * @param expectedKeys * The expected values - * + * * @throws ExpectationException * If the token is not one of the expected types. */ @SafeVarargs - public final void expect(K... expectedKeys) throws ExpectationException { + public final void expect(final K... expectedKeys) throws ExpectationException { expect(new HashSet<>(Arrays.asList(expectedKeys))); } /** * Check whether the head token is a certain type. - * + * * @param val * The type to check for. - * + * * @return Whether or not the head token is of that type. */ - public boolean headIs(K val) { + public boolean headIs(final K val) { return current().getKey().equals(val); } } diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java index e0dea48..d236a71 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -9,76 +9,76 @@ import bjc.utils.data.ITree; /** * Utility class for creating common implementations of {@link ParseBlock} - * + * * @author bjculkin * */ public class ParseBlocks { /** * Create a new repeating parse block. - * + * * @param inner * The parse block to repeat. - * + * * @param delim * The token type that seperates repetitions. - * + * * @param term * The token type that terminates repititions. - * + * * @param mark * The token to use as the node in the AST. - * + * * @param action * The action to perform on the state after every * repitition. - * + * * @return A configured repeating parse block. */ - public static <K, V, C> ParseBlock<K, V, C> repeating(ParseBlock<K, V, C> inner, K delim, K term, - Token<K, V> mark, UnaryOperator<C> action) { + public static <K, V, C> ParseBlock<K, V, C> repeating(final ParseBlock<K, V, C> inner, final K delim, + final K term, final Token<K, V> mark, final UnaryOperator<C> action) { return new RepeatingParseBlock<>(inner, delim, term, mark, action); } /** * Create a new triggered parse block. - * + * * @param source * The block to trigger around. - * + * * @param onEnter * The action to perform upon the state before entering * the block. - * + * * @param onExit * The action to perform upon the state after exiting the * block. - * + * * @return A configured trigger parse block. */ - public static <K, V, C> ParseBlock<K, V, C> trigger(ParseBlock<K, V, C> source, UnaryOperator<C> onEnter, - UnaryOperator<C> onExit) { + public static <K, V, C> ParseBlock<K, V, C> trigger(final ParseBlock<K, V, C> source, + final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit) { return new TriggeredParseBlock<>(onEnter, onExit, source); } /** * Create a new simple parse block. - * + * * @param precedence * The precedence of the expression inside the block. - * + * * @param terminator * The key type of the token expected after this block, * or null if none is expected. - * + * * @param validator * The predicate to use to validate parsed expressions, * or null if none is used. - * + * * @return A configured simple parse block. */ - public static <K, V, C> ParseBlock<K, V, C> simple(int precedence, K terminator, - Predicate<ITree<Token<K, V>>> validator) { + public static <K, V, C> ParseBlock<K, V, C> simple(final int precedence, final K terminator, + final Predicate<ITree<Token<K, V>>> validator) { return new SimpleParseBlock<>(precedence, terminator, validator); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java index 1c82b36..7791ea9 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java @@ -12,49 +12,49 @@ import bjc.utils.parserutils.ParserException; /** * A parse block that can parse a sequnce of zero or more occurances of another * block. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private ParseBlock<K, V, C> innerBlock; + private final ParseBlock<K, V, C> innerBlock; - private K delim; - private K term; + private final K delim; + private final K term; - private UnaryOperator<C> onDelim; + private final UnaryOperator<C> onDelim; - private Token<K, V> mark; + private final Token<K, V> mark; /** * Create a new repeating block. - * + * * @param inner * The inner block for elements. - * + * * @param delimiter * The token that delimits elements in the sequence. - * + * * @param terminator * The token that terminates the sequence. - * + * * @param marker * The token to use as the node in the AST. - * + * * @param action * The action to apply to the state after every * delimiter. */ - public RepeatingParseBlock(ParseBlock<K, V, C> inner, K delimiter, K terminator, Token<K, V> marker, - UnaryOperator<C> action) { + public RepeatingParseBlock(final ParseBlock<K, V, C> inner, final K delimiter, final K terminator, + final Token<K, V> marker, final UnaryOperator<C> action) { super(); if (inner == null) @@ -74,20 +74,22 @@ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> ret = new Tree<>(mark); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> ret = new Tree<>(mark); Token<K, V> tok = ctx.tokens.current(); while (!tok.getKey().equals(term)) { - ITree<Token<K, V>> kid = innerBlock.parse(ctx); + final ITree<Token<K, V>> kid = innerBlock.parse(ctx); ret.addChild(kid); tok = ctx.tokens.current(); ctx.tokens.expect(delim, term); - if (onDelim != null) ctx.state = onDelim.apply(ctx.state); + if (onDelim != null) { + ctx.state = onDelim.apply(ctx.state); + } } return ret; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index acddd3b..db94034 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -10,39 +10,40 @@ import bjc.utils.parserutils.ParserException; /** * Simple implementation of {@link ParseBlock} - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private int pow; + private final int pow; - private K term; + private final K term; - private Predicate<ITree<Token<K, V>>> validatr; + private final Predicate<ITree<Token<K, V>>> validatr; /** * Create a new block. - * + * * @param precedence * The precedence of this block. - * + * * @param terminator * The token type that terminates the block. If this is * null, don't check for a terminator. - * + * * @param validator * The predicate to apply to blocks. */ - public SimpleParseBlock(int precedence, K terminator, Predicate<ITree<Token<K, V>>> validator) { + public SimpleParseBlock(final int precedence, final K terminator, + final Predicate<ITree<Token<K, V>>> validator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -51,16 +52,14 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); if (term != null) { ctx.tokens.expect(term); } - if (validatr == null || validatr.test(res)) { - return res; - } + if (validatr == null || validatr.test(res)) return res; throw new ParserException("Block failed validation"); } @@ -72,18 +71,18 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { int result = 1; result = prime * result + pow; - result = prime * result + ((term == null) ? 0 : term.hashCode()); + result = prime * result + (term == null ? 0 : term.hashCode()); return result; } @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof SimpleParseBlock)) return false; - SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj; + final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj; if (pow != other.pow) return false; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index 76d99de..bb3d6f7 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -10,7 +10,7 @@ import bjc.utils.parserutils.ParserException; /** * A parse block that can adjust the state before handling its context. - * + * * @author bjculkin * * @param <K> @@ -21,36 +21,37 @@ import bjc.utils.parserutils.ParserException; * The state type of the parser. */ public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private UnaryOperator<C> onEntr; - private UnaryOperator<C> onExt; + private final UnaryOperator<C> onEntr; + private final UnaryOperator<C> onExt; - private ParseBlock<K, V, C> sourc; + private final ParseBlock<K, V, C> sourc; /** * Create a new triggered parse block. - * + * * @param onEnter * The action to fire before parsing the block. - * + * * @param onExit * The action to fire after parsing the block. - * + * * @param source * The block to use for parsing. */ - public TriggeredParseBlock(UnaryOperator<C> onEnter, UnaryOperator<C> onExit, ParseBlock<K, V, C> source) { + public TriggeredParseBlock(final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit, + final ParseBlock<K, V, C> source) { onEntr = onEnter; onExt = onExit; sourc = source; } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - C newState = onEntr.apply(ctx.state); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final C newState = onEntr.apply(ctx.state); - ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - ITree<Token<K, V>> res = sourc.parse(newCtx); + final ITree<Token<K, V>> res = sourc.parse(newCtx); ctx.state = onExt.apply(newState); diff --git a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java index b5a789c..9fc4fbc 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java @@ -8,21 +8,22 @@ import bjc.utils.parserutils.ParserException; /** * Abstract base for initial commands. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public abstract class AbstractInitialCommand<K, V, C> implements InitialCommand<K, V, C> { @Override - public ITree<Token<K, V>> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { + public ITree<Token<K, V>> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) + throws ParserException { return intNullDenotation(operator, ctx); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java index 44cb5f8..ef81d3f 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java @@ -8,35 +8,35 @@ import bjc.utils.parserutils.ParserException; /** * A binary operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public abstract class BinaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> { /** * Create a new binary operator with the specified precedence. - * + * * @param precedence * The precedence of the operator. */ - public BinaryCommand(int precedence) { + public BinaryCommand(final int precedence) { super(precedence); } protected abstract int rightBinding(); @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) - throws ParserException { - ITree<Token<K, V>> opr = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false); + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> opr = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false); return new Tree<>(operator, operand, opr); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java index 18a5584..316f0fe 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java @@ -4,15 +4,15 @@ import bjc.pratt.NonInitialCommand; /** * A operator with fixed precedence. - * + * * @author bjculkin - * + * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ @@ -21,14 +21,12 @@ public abstract class BinaryPostCommand<K, V, C> extends NonInitialCommand<K, V, /** * Create a new operator with fixed precedence. - * + * * @param precedence * The precedence of the operator. */ - public BinaryPostCommand(int precedence) { - if (precedence < 0) { - throw new IllegalArgumentException("Precedence must be non-negative"); - } + public BinaryPostCommand(final int precedence) { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); leftPower = precedence; } diff --git a/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java b/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java index 6ab126a..4e471d0 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java @@ -1,45 +1,45 @@ package bjc.pratt.commands; +import java.util.Set; + import bjc.pratt.ParserContext; import bjc.pratt.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; -import java.util.Set; - /** * Create a new chained operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private Set<K> chainWith; + private final Set<K> chainWith; - private Token<K, V> chain; + private final Token<K, V> chain; /** * Create a new chained operator. - * + * * @param precedence * The precedence of this operator. - * + * * @param chainSet * The operators to chain with. - * + * * @param chainMarker * The token to use as the node in the AST. */ - public ChainCommand(int precedence, Set<K> chainSet, Token<K, V> chainMarker) { + public ChainCommand(final int precedence, final Set<K> chainSet, final Token<K, V> chainMarker) { super(precedence); chainWith = chainSet; @@ -47,17 +47,18 @@ public class ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> { } @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) - throws ParserException { - ITree<Token<K, V>> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false); + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, + false); - ITree<Token<K, V>> res = new Tree<>(operator, operand, tree); + final ITree<Token<K, V>> res = new Tree<>(operator, operand, tree); if (chainWith.contains(ctx.tokens.current().getKey())) { - Token<K, V> tok = ctx.tokens.current(); + final Token<K, V> tok = ctx.tokens.current(); ctx.tokens.next(); - ITree<Token<K, V>> other = denote(tree, tok, + final ITree<Token<K, V>> other = denote(tree, tok, new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); return new Tree<>(chain, res, other); diff --git a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java b/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java index 86a172f..9b12cda 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java @@ -8,33 +8,34 @@ import bjc.utils.parserutils.ParserException; /** * A command that represents a specific tree. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class ConstantCommand<K, V, C> implements InitialCommand<K, V, C> { - private ITree<Token<K, V>> val; + private final ITree<Token<K, V>> val; /** * Create a new constant. - * + * * @param con * The tree this constant represents. */ - public ConstantCommand(ITree<Token<K, V>> con) { + public ConstantCommand(final ITree<Token<K, V>> con) { val = con; } @Override - public ITree<Token<K, V>> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { + public ITree<Token<K, V>> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) + throws ParserException { return val; } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java index 717444a..ff40ef2 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java @@ -8,21 +8,22 @@ import bjc.utils.parserutils.ParserException; /** * Default implementation of an initial command. - * + * * @author EVE * * @param <K> * The key type of the token. - * + * * @param <V> * The value type of the token. - * + * * @param <C> * The state type of the parser. */ public class DefaultInitialCommand<K, V, C> implements InitialCommand<K, V, C> { @Override - public ITree<Token<K, V>> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { + public ITree<Token<K, V>> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) + throws ParserException { throw new ParserException("Unexpected token " + operator); } } diff --git a/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java index aeb337d..6f590c9 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java @@ -7,21 +7,22 @@ import bjc.utils.data.ITree; /** * Default implementation of a non-initial command. - * + * * @author EVE * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class DefaultNonInitialCommand<K, V, C> extends NonInitialCommand<K, V, C> { @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) { + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) { throw new UnsupportedOperationException("Default command has no left denotation"); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java index 9b4518f..e3b1d3c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java @@ -8,37 +8,37 @@ import bjc.utils.parserutils.ParserException; /** * A command that denests a input tree. - * + * * Useful for processing the result of passing a complex parse group to a * command. - * + * * @author bjculkin - * + * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. * */ public class DenestingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private InitialCommand<K, V, C> wrapped; + private final InitialCommand<K, V, C> wrapped; /** * Create a new transforming initial command. - * + * * @param internal * The initial command to delegate to. */ - public DenestingCommand(InitialCommand<K, V, C> internal) { + public DenestingCommand(final InitialCommand<K, V, C> internal) { wrapped = internal; } @Override - protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) throws ParserException { return wrapped.denote(operator, ctx).getChild(0); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java index 1a8d3c8..f4288d0 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java @@ -9,42 +9,42 @@ import bjc.utils.parserutils.ParserException; /** * A grouping operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class GroupingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private ParseBlock<K, V, C> innerBlock; + private final ParseBlock<K, V, C> innerBlock; - private Token<K, V> mark; + private final Token<K, V> mark; /** * Create a new grouping command. - * + * * @param inner * The inner block. - * + * * @param marker * The token to use as the node in the AST. */ - public GroupingCommand(ParseBlock<K, V, C> inner, Token<K, V> marker) { + public GroupingCommand(final ParseBlock<K, V, C> inner, final Token<K, V> marker) { innerBlock = inner; mark = marker; } @Override - protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> opr = innerBlock.parse(ctx); + final ITree<Token<K, V>> opr = innerBlock.parse(ctx); return new Tree<>(mark, opr); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java index 3ece14c..5710277 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java @@ -1,57 +1,60 @@ package bjc.pratt.commands; +import static bjc.pratt.blocks.ParseBlocks.repeating; +import static bjc.pratt.blocks.ParseBlocks.simple; +import static bjc.pratt.blocks.ParseBlocks.trigger; + +import java.util.function.UnaryOperator; + import bjc.pratt.InitialCommand; import bjc.pratt.ParseBlock; import bjc.pratt.Token; import bjc.utils.data.ITree; -import java.util.function.UnaryOperator; - -import static bjc.pratt.blocks.ParseBlocks.*; - /** * * Contains factory methods for producing common implementations of * {@link InitialCommand} - * + * * @author EVE * */ public class InitialCommands { /** * Create a new unary operator. - * + * * @param precedence * The precedence of the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> InitialCommand<K, V, C> unary(int precedence) { + public static <K, V, C> InitialCommand<K, V, C> unary(final int precedence) { return new UnaryCommand<>(precedence); } /** * Create a new grouping operator. - * + * * @param precedence * The precedence of the expression in the operator. - * + * * @param term * The type that closes the group. - * + * * @param mark * The token for the AST node of the group. - * + * * @return A command implementing the operator. */ - public static <K, V, C> InitialCommand<K, V, C> grouping(int precedence, K term, Token<K, V> mark) { - ParseBlock<K, V, C> innerBlock = simple(precedence, term, null); + public static <K, V, C> InitialCommand<K, V, C> grouping(final int precedence, final K term, + final Token<K, V> mark) { + final ParseBlock<K, V, C> innerBlock = simple(precedence, term, null); return new GroupingCommand<>(innerBlock, mark); } /** * Create a new leaf operator. - * + * * @return A command implementing the operator. */ public static <K, V, C> InitialCommand<K, V, C> leaf() { @@ -60,89 +63,89 @@ public class InitialCommands { /** * Create a new pre-ternary operator, like an if-then-else statement. - * + * * @param cond1 * The priority of the first block. - * + * * @param block1 * The priority of the second block. - * + * * @param block2 * The priority of the third block. - * + * * @param mark1 * The marker that ends the first block. - * + * * @param mark2 * The marker that ends the second block. - * + * * @param term * The token for the AST node of the group. - * + * * @return A command implementing the operator. */ - public static <K, V, C> InitialCommand<K, V, C> preTernary(int cond1, int block1, int block2, K mark1, K mark2, - Token<K, V> term) { - ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null); - ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null); - ParseBlock<K, V, C> opblock2 = simple(block2, null, null); + public static <K, V, C> InitialCommand<K, V, C> preTernary(final int cond1, final int block1, final int block2, + final K mark1, final K mark2, final Token<K, V> term) { + final ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null); + final ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null); + final ParseBlock<K, V, C> opblock2 = simple(block2, null, null); return new PreTernaryCommand<>(condBlock, opblock1, opblock2, term); } /** * Create a new named constant. - * + * * @param val * The value of the constant. - * + * * @return A command implementing the constant. */ - public static <K, V, C> InitialCommand<K, V, C> constant(ITree<Token<K, V>> val) { + public static <K, V, C> InitialCommand<K, V, C> constant(final ITree<Token<K, V>> val) { return new ConstantCommand<>(val); } /** * Create a new delimited command. This is for block-like constructs. - * + * * @param inner * The precedence of the inner blocks. - * + * * @param delim * The marker between sub-blocks. - * + * * @param mark * The block terminator. - * + * * @param term * The token for the AST node of the group. - * + * * @param onEnter * The function to apply to the state on entering the * block. - * + * * @param onDelim * The function to apply to the state on finishing a * sub-block. - * + * * @param onExit * The function to apply to the state on exiting the * block. - * + * * @param statement * Whether or not the sub-blocks are statements or * expressions. - * + * * @return A command implementing the operator. */ - public static <K, V, C> InitialCommand<K, V, C> delimited(int inner, K delim, K mark, Token<K, V> term, - UnaryOperator<C> onEnter, UnaryOperator<C> onDelim, UnaryOperator<C> onExit, - boolean statement) { - ParseBlock<K, V, C> innerBlock = simple(inner, null, null); - ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); - ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit); + public static <K, V, C> InitialCommand<K, V, C> delimited(final int inner, final K delim, final K mark, + final Token<K, V> term, final UnaryOperator<C> onEnter, final UnaryOperator<C> onDelim, + final UnaryOperator<C> onExit, final boolean statement) { + final ParseBlock<K, V, C> innerBlock = simple(inner, null, null); + final ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); + final ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit); - GroupingCommand<K, V, C> command = new GroupingCommand<>(scopedBlock, term); + final GroupingCommand<K, V, C> command = new GroupingCommand<>(scopedBlock, term); /* * Remove the wrapper layer from grouping-command on top of @@ -153,16 +156,16 @@ public class InitialCommands { /** * Create a new denesting command. - * + * * This removes one tree-level, and is useful when combining complex * parse blocks with commands. - * + * * @param comm * The command to denest. - * + * * @return A command that denests the result of the provided command. */ - public static <K, V, C> InitialCommand<K, V, C> denest(InitialCommand<K, V, C> comm) { + public static <K, V, C> InitialCommand<K, V, C> denest(final InitialCommand<K, V, C> comm) { return new DenestingCommand<>(comm); } } diff --git a/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java b/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java index 798b9ce..9b4480d 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java @@ -9,21 +9,22 @@ import bjc.utils.parserutils.ParserException; /** * A operator that stands for itself. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class LeafCommand<K, V, C> implements InitialCommand<K, V, C> { @Override - public ITree<Token<K, V>> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { + public ITree<Token<K, V>> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) + throws ParserException { return new Tree<>(operator); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java index 4a481d7..adf98a1 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java @@ -2,26 +2,26 @@ package bjc.pratt.commands; /** * A left-associative operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class LeftBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { /** * Create a new left-associative operator. - * + * * @param precedence * The precedence of the operator. */ - public LeftBinaryCommand(int precedence) { + public LeftBinaryCommand(final int precedence) { super(precedence); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java index d1208bd..54ab98b 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java @@ -2,26 +2,26 @@ package bjc.pratt.commands; /** * A non-associative operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class NonBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { /** * Create a new non-associative operator. - * + * * @param precedence * The precedence of the operator. */ - public NonBinaryCommand(int precedence) { + public NonBinaryCommand(final int precedence) { super(precedence); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java index b7eda95..48922b7 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java @@ -1,139 +1,140 @@ package bjc.pratt.commands; +import java.util.Set; + import bjc.pratt.NonInitialCommand; import bjc.pratt.ParseBlock; import bjc.pratt.Token; import bjc.pratt.blocks.SimpleParseBlock; -import java.util.Set; - /** * Contains factory methods for producing common implementations of * {@link NonInitialCommand} - * + * * @author EVE * */ public class NonInitialCommands { /** * Create a left-associative infix operator. - * + * * @param precedence * The precedence of the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> infixLeft(int precedence) { + public static <K, V, C> NonInitialCommand<K, V, C> infixLeft(final int precedence) { return new LeftBinaryCommand<>(precedence); } /** * Create a right-associative infix operator. - * + * * @param precedence * The precedence of the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> infixRight(int precedence) { + public static <K, V, C> NonInitialCommand<K, V, C> infixRight(final int precedence) { return new RightBinaryCommand<>(precedence); } /** * Create a non-associative infix operator. - * + * * @param precedence * The precedence of the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> infixNon(int precedence) { + public static <K, V, C> NonInitialCommand<K, V, C> infixNon(final int precedence) { return new NonBinaryCommand<>(precedence); } /** * Create a chained operator. - * + * * @param precedence * The precedence of the operator. - * + * * @param chainSet * The operators it forms a chain with. - * + * * @param marker * The token to use as the AST node for the chained * operators. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> chain(int precedence, Set<K> chainSet, Token<K, V> marker) { + public static <K, V, C> NonInitialCommand<K, V, C> chain(final int precedence, final Set<K> chainSet, + final Token<K, V> marker) { return new ChainCommand<>(precedence, chainSet, marker); } /** * Create a postfix operator. - * + * * @param precedence * The precedence of the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> postfix(int precedence) { + public static <K, V, C> NonInitialCommand<K, V, C> postfix(final int precedence) { return new PostfixCommand<>(precedence); } /** * Create a post-circumfix operator. - * + * * This is an operator in form similar to array indexing. - * + * * @param precedence * The precedence of this operator - * + * * @param insidePrecedence * The precedence of the expression inside the operator - * + * * @param closer * The token that closes the circumfix. - * + * * @param marker * The token to use as the AST node for the operator. - * + * * @return A command implementing that operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> postCircumfix(int precedence, int insidePrecedence, K closer, - Token<K, V> marker) { - ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); + public static <K, V, C> NonInitialCommand<K, V, C> postCircumfix(final int precedence, + final int insidePrecedence, final K closer, final Token<K, V> marker) { + final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); return new PostCircumfixCommand<>(precedence, innerBlock, marker); } /** * Create a ternary operator. - * + * * This is like C's ?: operator. - * + * * @param precedence * The precedence of the operator. - * + * * @param insidePrecedence * The precedence of the inner section of the operator. - * + * * @param closer * The token that marks the end of the inner section. - * + * * @param marker * The token to use as the AST node for the operator. - * + * * @param nonassoc * True if the command is non-associative, false * otherwise. - * + * * @return A command implementing this operator. */ - public static <K, V, C> NonInitialCommand<K, V, C> ternary(int precedence, int insidePrecedence, K closer, - Token<K, V> marker, boolean nonassoc) { - ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); + public static <K, V, C> NonInitialCommand<K, V, C> ternary(final int precedence, final int insidePrecedence, + final K closer, final Token<K, V> marker, final boolean nonassoc) { + final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java index 6bed0ff..e7d2eea 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java @@ -9,41 +9,39 @@ import bjc.utils.parserutils.ParserException; /** * A post-circumfix operator, like array indexing. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class PostCircumfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private ParseBlock<K, V, C> innerBlock; + private final ParseBlock<K, V, C> innerBlock; - private Token<K, V> mark; + private final Token<K, V> mark; /** * Create a new post-circumfix operator. - * + * * @param precedence * The precedence of the operator. - * + * * @param inner * The block inside the expression. - * + * * @param marker * The token to use as the node for the AST. */ - public PostCircumfixCommand(int precedence, ParseBlock<K, V, C> inner, Token<K, V> marker) { + public PostCircumfixCommand(final int precedence, final ParseBlock<K, V, C> inner, final Token<K, V> marker) { super(precedence); - if (inner == null) { - throw new NullPointerException("Inner block must not be null"); - } + if (inner == null) throw new NullPointerException("Inner block must not be null"); innerBlock = inner; @@ -51,9 +49,9 @@ public class PostCircumfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> { } @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) - throws ParserException { - ITree<Token<K, V>> inside = innerBlock.parse(ctx); + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> inside = innerBlock.parse(ctx); return new Tree<>(mark, operand, inside); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java index 5e2ce28..3276d09 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java @@ -8,32 +8,32 @@ import bjc.utils.parserutils.ParserException; /** * A postfix operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class PostfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> { /** * Create a new postfix operator. - * + * * @param precedence * The precedence of the operator. */ - public PostfixCommand(int precedence) { + public PostfixCommand(final int precedence) { super(precedence); } @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) - throws ParserException { + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) throws ParserException { return new Tree<>(operator, operand); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java index 072c58c..d8304e2 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java @@ -9,43 +9,43 @@ import bjc.utils.parserutils.ParserException; /** * A prefix ternary operator, like an if/then/else group. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private Token<K, V> trm; + private final Token<K, V> trm; - private ParseBlock<K, V, C> condBlock; + private final ParseBlock<K, V, C> condBlock; - private ParseBlock<K, V, C> opblock1; - private ParseBlock<K, V, C> opblock2; + private final ParseBlock<K, V, C> opblock1; + private final ParseBlock<K, V, C> opblock2; /** * Create a new ternary statement. - * + * * @param cond * The block for handling the condition. - * + * * @param op1 * The block for handling the first operator. - * + * * @param op2 * The block for handling the second operator. - * + * * @param term * The token to use as the node for the AST. */ - public PreTernaryCommand(ParseBlock<K, V, C> cond, ParseBlock<K, V, C> op1, ParseBlock<K, V, C> op2, - Token<K, V> term) { + public PreTernaryCommand(final ParseBlock<K, V, C> cond, final ParseBlock<K, V, C> op1, + final ParseBlock<K, V, C> op2, final Token<K, V> term) { super(); if (cond == null) @@ -62,13 +62,13 @@ public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> } @Override - protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> cond = condBlock.parse(ctx); + final ITree<Token<K, V>> cond = condBlock.parse(ctx); - ITree<Token<K, V>> op1 = opblock1.parse(ctx); + final ITree<Token<K, V>> op1 = opblock1.parse(ctx); - ITree<Token<K, V>> op2 = opblock2.parse(ctx); + final ITree<Token<K, V>> op2 = opblock2.parse(ctx); return new Tree<>(trm, cond, op1, op2); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java index 8ddab06..35c828f 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java @@ -2,7 +2,7 @@ package bjc.pratt.commands; /** * A right-associative binary operator. - * + * * @author bjculkin * * @param <K> @@ -15,11 +15,11 @@ package bjc.pratt.commands; public class RightBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { /** * Create a new right-associative operator. - * + * * @param precedence * The precedence of the operator. */ - public RightBinaryCommand(int precedence) { + public RightBinaryCommand(final int precedence) { super(precedence); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java index 0152471..2e12050 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java @@ -9,42 +9,43 @@ import bjc.utils.parserutils.ParserException; /** * A ternary command, like C's ?: - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class TernaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private ParseBlock<K, V, C> innerBlck; + private final ParseBlock<K, V, C> innerBlck; - private Token<K, V> mark; + private final Token<K, V> mark; - private boolean nonassoc; + private final boolean nonassoc; /** * Create a new ternary command. - * + * * @param precedence * The precedence of this operator. - * + * * @param innerBlock * The representation of the inner block of the * expression. - * + * * @param marker * The token to use as the root of the AST node. - * + * * @param isNonassoc * Whether or not the conditional is associative. */ - public TernaryCommand(int precedence, ParseBlock<K, V, C> innerBlock, Token<K, V> marker, boolean isNonassoc) { + public TernaryCommand(final int precedence, final ParseBlock<K, V, C> innerBlock, final Token<K, V> marker, + final boolean isNonassoc) { super(precedence); if (innerBlock == null) @@ -57,20 +58,19 @@ public class TernaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> { } @Override - public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx) - throws ParserException { - ITree<Token<K, V>> inner = innerBlck.parse(ctx); + public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator, + final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> inner = innerBlck.parse(ctx); - ITree<Token<K, V>> outer = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false); + final ITree<Token<K, V>> outer = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, + false); return new Tree<>(mark, inner, operand, outer); } @Override public int nextBinding() { - if (nonassoc) { - return leftBinding() - 1; - } + if (nonassoc) return leftBinding() - 1; return leftBinding(); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java index d75c8ce..a706ea8 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java @@ -10,41 +10,41 @@ import bjc.utils.parserutils.ParserException; /** * An initial command that transforms the result of another command. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class TransformingInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private InitialCommand<K, V, C> internl; + private final InitialCommand<K, V, C> internl; - private UnaryOperator<ITree<Token<K, V>>> transfrm; + private final UnaryOperator<ITree<Token<K, V>>> transfrm; /** * Create a new transforming initial command. - * + * * @param internal * The initial command to delegate to. - * + * * @param transform * The transform to apply to the returned tree. */ - public TransformingInitialCommand(InitialCommand<K, V, C> internal, - UnaryOperator<ITree<Token<K, V>>> transform) { + public TransformingInitialCommand(final InitialCommand<K, V, C> internal, + final UnaryOperator<ITree<Token<K, V>>> transform) { super(); internl = internal; transfrm = transform; } @Override - protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) throws ParserException { return transfrm.apply(internl.denote(operator, ctx)); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java index 0689210..d36dc24 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java @@ -8,15 +8,15 @@ import bjc.utils.parserutils.ParserException; /** * A unary operator. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ @@ -25,22 +25,20 @@ public class UnaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { /** * Create a new unary command. - * + * * @param precedence * The precedence of this operator. */ - public UnaryCommand(int precedence) { - if(precedence < 0) { - throw new IllegalArgumentException("Precedence must be non-negative"); - } - + public UnaryCommand(final int precedence) { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); + nullPwer = precedence; } @Override - protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); + final ITree<Token<K, V>> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); return new Tree<>(operator, opr); } diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java index 9f97e33..6d3c1a5 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -4,24 +4,24 @@ import bjc.pratt.Token; /** * Simple token implementation for strings. - * + * * @author EVE * */ public class StringToken implements Token<String, String> { - private String key; - private String val; + private final String key; + private final String val; /** * Create a new string token. - * + * * @param ky * The key for the token. - * + * * @param vl * The value for the token. */ - public StringToken(String ky, String vl) { + public StringToken(final String ky, final String vl) { key = ky; val = vl; } @@ -41,19 +41,19 @@ public class StringToken implements Token<String, String> { final int prime = 31; int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - result = prime * result + ((val == null) ? 0 : val.hashCode()); + result = prime * result + (key == null ? 0 : key.hashCode()); + result = prime * result + (val == null ? 0 : val.hashCode()); return result; } @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof StringToken)) return false; - StringToken other = (StringToken) obj; + final StringToken other = (StringToken) obj; if (key == null) { if (other.key != null) return false; @@ -73,13 +73,13 @@ public class StringToken implements Token<String, String> { /** * Create a new literal token (has same key/value). - * + * * @param val * The value for the literal token. - * + * * @return A literal token with that key. */ - public static StringToken litToken(String val) { + public static StringToken litToken(final String val) { return new StringToken(val, val); } } diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java index 7f8215a..b33dae8 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java @@ -1,34 +1,34 @@ package bjc.pratt.tokens; -import bjc.pratt.Token; -import bjc.pratt.TokenStream; +import static bjc.pratt.tokens.StringToken.litToken; import java.util.Iterator; -import static bjc.pratt.tokens.StringToken.litToken; +import bjc.pratt.Token; +import bjc.pratt.TokenStream; /** * Simple implementation of token stream for strings. - * + * * The terminal token here is represented by a token with type and value * '(end)'. - * + * * @author EVE * */ public class StringTokenStream extends TokenStream<String, String> { - private Iterator<Token<String, String>> iter; + private final Iterator<Token<String, String>> iter; private Token<String, String> curr; /** * Create a new token stream from a iterator. - * + * * @param itr * The iterator to use. - * + * */ - public StringTokenStream(Iterator<Token<String, String>> itr) { + public StringTokenStream(final Iterator<Token<String, String>> itr) { iter = itr; } |
