diff options
| author | Student <student@Administrators-iMac-2.local> | 2017-04-12 11:05:57 -0400 |
|---|---|---|
| committer | Student <student@Administrators-iMac-2.local> | 2017-04-12 11:05:57 -0400 |
| commit | 22c356cd411cf0fcc18d548291af26bc7588a3aa (patch) | |
| tree | 4f24fdda182b358ca96aed2249bb4e8a19994747 | |
| parent | 2dc1b5dd145ab0e2b3e3df67f967a9c07ed6d303 (diff) | |
| parent | f394306a4b65a3328551f9f6b8d4abff8bfd5b27 (diff) | |
Merge branch 'master' of https://github.com/bculkin2442/JPratt.git
54 files changed, 1437 insertions, 1057 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/InputState.java b/JPratt/src/examples/java/bjc/pratt/examples/InputState.java deleted file mode 100644 index 7fe74fb..0000000 --- a/JPratt/src/examples/java/bjc/pratt/examples/InputState.java +++ /dev/null @@ -1,150 +0,0 @@ -package bjc.pratt.examples; - -import java.util.Arrays; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -import bjc.pratt.Token; -import bjc.utils.data.TransformIterator; -import bjc.utils.parserutils.splitterv2.ChainTokenSplitter; -import bjc.utils.parserutils.splitterv2.ConfigurableTokenSplitter; -import bjc.utils.parserutils.splitterv2.ExcludingTokenSplitter; -import bjc.utils.parserutils.splitterv2.TokenSplitter; - -/** - * Packaged input state for the parser. - * - * @author student - * - */ -public class InputState { - public Set<String> ops; - public Set<String> reserved; - - public TokenSplitter splitter; - - public TestContext ctx; - - public String ln; - - private InputState(Set<String> opps, TokenSplitter splt, String lin, Set<String> rserved, TestContext ctxt) { - ops = opps; - reserved = rserved; - - splitter = splt; - - ctx = ctxt; - - ln = lin; - } - - public static InputState createState() { - /* - * Use a linked hash set to preserve insertion order. - */ - Set<String> ops = new LinkedHashSet<>(); - - ops.add("!!!"); - - ops.addAll(Arrays.asList("->", "=>")); - ops.add(":="); - 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("^", "!")); - ops.addAll(Arrays.asList("(", ")")); - ops.addAll(Arrays.asList("[", "]")); - ops.addAll(Arrays.asList("{", "}")); - - /* - * Reserved words that represent themselves, not literals. - */ - Set<String> reserved = new LinkedHashSet<>(); - - reserved.addAll(Arrays.asList("if", "then", "else")); - reserved.addAll(Arrays.asList("and", "or")); - reserved.addAll(Arrays.asList("begin", "end")); - reserved.addAll(Arrays.asList("switch", "case")); - reserved.addAll(Arrays.asList("sqrt", "cbrt", "root")); - - reserved.add("var"); - - ChainTokenSplitter nsplit = new ChainTokenSplitter(); - - ConfigurableTokenSplitter hi = new ConfigurableTokenSplitter(true); - ConfigurableTokenSplitter lo = new ConfigurableTokenSplitter(true); - - hi.addSimpleDelimiters("->"); - hi.addSimpleDelimiters(":="); - hi.addSimpleDelimiters("||", "&&"); - hi.addSimpleDelimiters("<=", ">="); - - lo.addSimpleDelimiters("±"); - lo.addSimpleDelimiters(".", ",", ";", ":"); - lo.addSimpleDelimiters("=", "<", ">"); - lo.addSimpleDelimiters("+", "-", "*", "/"); - lo.addSimpleDelimiters("^"); - - lo.addMultiDelimiters("!"); - lo.addMultiDelimiters("(", ")"); - lo.addMultiDelimiters("[", "]"); - lo.addMultiDelimiters("{", "}"); - - hi.compile(); - lo.compile(); - - nsplit.appendSplitters(hi, lo); - - ExcludingTokenSplitter excluder = new ExcludingTokenSplitter(nsplit); - - excluder.addLiteralExclusions(reserved.toArray(new String[0])); - - TestContext ctx = new TestContext(); - - InputState state = new InputState(ops, excluder, null, reserved, ctx); - - return state; - } - - public Iterator<Token<String, String>> preprocessInput() { - String[] rawTokens = ln.split("\\s+"); - - List<String> splitTokens = new LinkedList<>(); - - for (String raw : rawTokens) { - boolean doSplit = false; - - for (String op : ops) { - if (raw.contains(op)) { - doSplit = true; - break; - } - } - - if (doSplit) { - String[] strangs = splitter.split(raw).toArray(new String[0]); - - splitTokens.addAll(Arrays.asList(strangs)); - } else { - splitTokens.add(raw); - } - } - - System.out.println("\nSplit string: " + splitTokens); - - Iterator<String> source = splitTokens.iterator(); - - Tokenizer tokenzer = new Tokenizer(ops, reserved, ctx); - - Iterator<Token<String, String>> tokens = new TransformIterator<>(source, tokenzer); - - return tokens; - } -}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java deleted file mode 100644 index 7b9795e..0000000 --- a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java +++ /dev/null @@ -1,184 +0,0 @@ -package bjc.pratt.examples; - -import bjc.pratt.InitialCommand; -import bjc.pratt.NonInitialCommand; -import bjc.pratt.PrattParser; -import bjc.pratt.Token; -import bjc.pratt.tokens.StringToken; -import bjc.pratt.tokens.StringTokenStream; -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Scanner; -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) { - InputState state = InputState.createState(); - - PrattParser<String, String, TestContext> parser = createParser(); - - Scanner scn = new Scanner(System.in); - - System.out.print("Enter a command (blank line to exit): "); - String ln = scn.nextLine(); - - while (!ln.trim().equals("")) { - state.ln = ln; - - Iterator<Token<String, String>> tokens = state.preprocessInput(); - - try { - StringTokenStream tokenStream = new StringTokenStream(tokens); - - /* - * Prime stream. - */ - tokenStream.next(); - - ITree<Token<String, String>> tree = parser.parseExpression(0, tokenStream, state.ctx, true); - - if (!tokenStream.headIs("(end)")) { - System.out.println("\nMultiple expressions on line"); - } - - System.out.println("\nParsed expression:\n" + tree); - } catch (ParserException pex) { - pex.printStackTrace(); - } - - System.out.print("\nEnter a command (blank line to exit): "); - ln = scn.nextLine(); - } - - System.out.println(); - System.out.println("\nContext is: " + state.ctx); - - scn.close(); - } - - private static PrattParser<String, String, TestContext> createParser() { - /* - * Set of which relational operators chain with each other. - */ - HashSet<String> relChain = new HashSet<>(); - relChain.addAll(Arrays.asList("=", "<", ">", "<=", ">=")); - - /* - * Token for marking chains. - */ - StringToken chainToken = litToken("and"); - - /* - * ID function. - */ - UnaryOperator<TestContext> idfun = id(); - - 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); - parser.addNonInitialCommand("if", ifElse); - - parser.addNonInitialCommand(":=", new AssignCommand(10)); - - parser.addNonInitialCommand("->", infixRight(11)); - - NonInitialCommand<String, String, TestContext> nonSSRelJoin = infixLeft(13); - parser.addNonInitialCommand("and", nonSSRelJoin); - parser.addNonInitialCommand("or", nonSSRelJoin); - - 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); - parser.addNonInitialCommand("&&", ssRelJoin); - parser.addNonInitialCommand("||", ssRelJoin); - - NonInitialCommand<String, String, TestContext> addSub = infixLeft(20); - parser.addNonInitialCommand("+", addSub); - parser.addNonInitialCommand("-", addSub); - parser.addNonInitialCommand("±", addSub); - - NonInitialCommand<String, String, TestContext> mulDiv = infixLeft(30); - parser.addNonInitialCommand("*", mulDiv); - parser.addNonInitialCommand("/", mulDiv); - - parser.addNonInitialCommand("!", postfix(40)); - - NonInitialCommand<String, String, TestContext> expon = infixRight(50); - parser.addNonInitialCommand("^", expon); - parser.addNonInitialCommand("root", expon); - - 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")); - parser.addNonInitialCommand("[", arrayIdx); - - 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")); - parser.addInitialCommand("(", parens); - - 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); - parser.addInitialCommand("[", arrayLiteral); - - InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), idfun, idfun, - idfun, false); - parser.addInitialCommand("{", jsonLiteral); - - parser.addInitialCommand("case", unary(5)); - - parser.addInitialCommand("-", unary(30)); - - InitialCommand<String, String, TestContext> root = unary(50); - parser.addInitialCommand("sqrt", root); - parser.addInitialCommand("cbrt", root); - - InitialCommand<String, String, TestContext> leaf = leaf(); - parser.addInitialCommand("(literal)", leaf); - - parser.addInitialCommand("var", new VarCommand()); - - parser.addInitialCommand("switch", new SwitchCommand()); - - return parser; - } -}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java deleted file mode 100644 index 4a5bd22..0000000 --- a/JPratt/src/examples/java/bjc/pratt/examples/SwitchCommand.java +++ /dev/null @@ -1,21 +0,0 @@ -package bjc.pratt.examples; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.pratt.tokens.StringToken; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -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); - - 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/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java deleted file mode 100644 index cfaf3e2..0000000 --- a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java +++ /dev/null @@ -1,38 +0,0 @@ -package bjc.pratt.examples; - -import bjc.pratt.Token; -import bjc.pratt.tokens.StringToken; - -import java.util.Set; -import java.util.function.Function; - -import static bjc.pratt.tokens.StringToken.litToken; - -final class Tokenizer implements Function<String, Token<String, String>> { - private Set<String> ops; - private Set<String> reserved; - - @SuppressWarnings("unused") - private TestContext ctx; - - public Tokenizer(Set<String> operators, Set<String> reservedWords, TestContext context) { - ops = operators; - reserved = reservedWords; - ctx = context; - } - - @Override - public Token<String, String> apply(String strang) { - if (ops.contains(strang) || reserved.contains(strang)) { - return litToken(strang); - } 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); - } - } -} diff --git a/JPratt/src/examples/java/bjc/pratt/examples/TreeEvaluator.java b/JPratt/src/examples/java/bjc/pratt/examples/TreeEvaluator.java deleted file mode 100644 index e4325b2..0000000 --- a/JPratt/src/examples/java/bjc/pratt/examples/TreeEvaluator.java +++ /dev/null @@ -1,49 +0,0 @@ -package bjc.pratt.examples; - -import bjc.utils.data.ITree; -import bjc.utils.data.TopDownTransformResult; - -/** - * Evaluate a tree to a result. - * - * @author student - * - */ -public class TreeEvaluator { - /** - * The result of evaluating a tree. - * - * @author student - * - */ - public static final class EvaluationResult { - public static EvaluationResult fromToken(EvaluationToken tok) { - return null; - } - } - - /** - * The token type for trees to evaluate. - * - * @author student - * - */ - public static final class EvaluationToken { - - } - - public static EvaluationResult evaluateTree(ITree<EvaluationToken> tree, TestContext ctx) { - ITree<EvaluationToken> result = tree.topDownTransform(TreeEvaluator::pickNodeEvaluation, - TreeEvaluator::evaluateNode); - - return EvaluationResult.fromToken(result.getHead()); - } - - private static TopDownTransformResult pickNodeEvaluation(EvaluationToken node) { - return TopDownTransformResult.PUSHDOWN; - } - - private static ITree<EvaluationToken> evaluateNode(ITree<EvaluationToken> tree) { - return tree; - } -} diff --git a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java index 45aac1a..8b96850 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java @@ -1,22 +1,23 @@ -package bjc.pratt.examples; +package bjc.pratt.examples.lang; import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.pratt.commands.NonBinaryCommand; +import bjc.pratt.commands.impls.NonBinaryCommand; import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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/lang/BlockEnter.java index 67e560c..dedf010 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockEnter.java @@ -1,19 +1,19 @@ -package bjc.pratt.examples; +package bjc.pratt.examples.lang; -import bjc.pratt.Token; +import java.util.function.UnaryOperator; + +import bjc.pratt.tokens.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/lang/BlockExit.java index 1cca6d5..15b2a5e 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/BlockExit.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockExit.java @@ -1,10 +1,10 @@ -package bjc.pratt.examples; +package bjc.pratt.examples.lang; 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/lang/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java new file mode 100644 index 0000000..7af16a0 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java @@ -0,0 +1,317 @@ +package bjc.pratt.examples.lang; + +import static bjc.pratt.commands.impls.InitialCommands.delimited; +import static bjc.pratt.commands.impls.InitialCommands.grouping; +import static bjc.pratt.commands.impls.InitialCommands.leaf; +import static bjc.pratt.commands.impls.InitialCommands.preTernary; +import static bjc.pratt.commands.impls.InitialCommands.unary; +import static bjc.pratt.commands.impls.NonInitialCommands.chain; +import static bjc.pratt.commands.impls.NonInitialCommands.infixLeft; +import static bjc.pratt.commands.impls.NonInitialCommands.infixNon; +import static bjc.pratt.commands.impls.NonInitialCommands.infixRight; +import static bjc.pratt.commands.impls.NonInitialCommands.postCircumfix; +import static bjc.pratt.commands.impls.NonInitialCommands.postfix; +import static bjc.pratt.commands.impls.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.PrattParser; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.commands.NonInitialCommand; +import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.StringTokenStream; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.TransformIterator; +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.ParserException; +import bjc.utils.parserutils.splitter.ChainTokenSplitter; +import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; +import bjc.utils.parserutils.splitter.ExcludingTokenSplitter; +import bjc.utils.parserutils.splitter.FilteredTokenSplitter; +import bjc.utils.parserutils.splitter.TokenSplitter; + +/** + * Simple test for Pratt parser. + * + * @author EVE + * + */ +public class PrattParserTest { + /** + * Main method. + * + * @param args + * Unused CLI arguments. + */ + public static void main(final String[] args) { + /* + * Use a linked hash set to preserve insertion order. + */ + final Set<String> ops = new LinkedHashSet<>(); + + ops.add("!!!"); + + ops.addAll(Arrays.asList("->", "=>")); + ops.add(":="); + ops.addAll(Arrays.asList("||", "&&")); + ops.addAll(Arrays.asList("<=", ">=")); + + ops.addAll(Arrays.asList("\u00B1")); // Unicode plus/minus + 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("{", "}")); + + /* + * Reserved words that represent themselves, not literals. + */ + 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")); + reserved.addAll(Arrays.asList("switch", "case")); + reserved.addAll(Arrays.asList("sqrt", "cbrt", "root")); + reserved.addAll(Arrays.asList("try", "catch", "finally")); + reserved.add("var"); + + final ChainTokenSplitter nsplit = new ChainTokenSplitter(); + + final ConfigurableTokenSplitter hi = new ConfigurableTokenSplitter(true); + final ConfigurableTokenSplitter lo = new ConfigurableTokenSplitter(true); + + hi.addSimpleDelimiters("->"); + hi.addSimpleDelimiters(":="); + hi.addSimpleDelimiters("||", "&&"); + hi.addSimpleDelimiters("<=", ">="); + + lo.addSimpleDelimiters("\u00B1"); // Unicode plus/minus + lo.addSimpleDelimiters(".", ",", ";", ":"); + lo.addSimpleDelimiters("=", "<", ">"); + lo.addSimpleDelimiters("+", "-", "*", "/"); + lo.addSimpleDelimiters("^"); + + lo.addMultiDelimiters("!"); + lo.addMultiDelimiters("(", ")"); + lo.addMultiDelimiters("[", "]"); + lo.addMultiDelimiters("{", "}"); + + hi.compile(); + lo.compile(); + + nsplit.appendSplitters(hi, lo); + + final ExcludingTokenSplitter excluder = new ExcludingTokenSplitter(nsplit); + + excluder.addLiteralExclusions(reserved.toArray(new String[0])); + + final FilteredTokenSplitter filtered = new FilteredTokenSplitter(excluder, (tok) -> !tok.equals("")); + + final PrattParser<String, String, TestContext> parser = createParser(); + + final TestContext ctx = new TestContext(); + + 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("")) { + final Iterator<Token<String, String>> tokens = preprocessInput(ops, filtered, ln, reserved, + ctx); + + try { + final StringTokenStream tokenStream = new StringTokenStream(tokens); + + /* + * Prime stream. + */ + tokenStream.next(); + + 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 (final ParserException pex) { + pex.printStackTrace(); + } + + System.out.print("\nEnter a command (blank line to exit): "); + ln = scn.nextLine(); + } + + System.out.println(); + System.out.println("\nContext is: " + ctx); + + scn.close(); + } + + 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+"); + + final List<String> splitTokens = new LinkedList<>(); + + for (final String raw : rawTokens) { + if (raw.equals("")) continue; + + boolean doSplit = false; + + for (final String op : ops) { + if (raw.contains(op)) { + doSplit = true; + break; + } + } + + if (doSplit) { + IList<String> splitStrangs = split.split(raw); + splitStrangs.removeMatching(""); + + splitStrangs.forEach(splitTokens::add); + } else { + splitTokens.add(raw); + } + } + + System.out.println("\nSplit string: " + splitTokens); + + final Iterator<String> source = splitTokens.iterator(); + + final Tokenizer tokenzer = new Tokenizer(ops, reserved, ctx); + + final Iterator<Token<String, String>> tokens = new TransformIterator<>(source, tokenzer); + + return tokens; + } + + private static PrattParser<String, String, TestContext> createParser() { + /* + * Set of which relational operators chain with each other. + */ + final HashSet<String> relChain = new HashSet<>(); + relChain.addAll(Arrays.asList("=", "<", ">", "<=", ">=")); + + /* + * Token for marking chains. + */ + final StringToken chainToken = litToken("and"); + + /* + * ID function. + */ + final UnaryOperator<TestContext> idfun = id(); + + final PrattParser<String, String, TestContext> parser = new PrattParser<>(); + + parser.addNonInitialCommand("!!!", postfix(0)); + + parser.addNonInitialCommand(":", infixNon(3)); + + parser.addNonInitialCommand("finally", infixLeft(4)); + + parser.addNonInitialCommand("catch", infixLeft(5)); + + final NonInitialCommand<String, String, TestContext> ifElse = ternary(6, 0, "else", litToken("cond"), + false); + parser.addNonInitialCommand("if", ifElse); + + parser.addNonInitialCommand(":=", new AssignCommand(10)); + + parser.addNonInitialCommand("->", infixRight(11)); + + final NonInitialCommand<String, String, TestContext> nonSSRelJoin = infixLeft(13); + parser.addNonInitialCommand("and", nonSSRelJoin); + parser.addNonInitialCommand("or", nonSSRelJoin); + + 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); + + final NonInitialCommand<String, String, TestContext> ssRelJoin = infixRight(17); + parser.addNonInitialCommand("&&", ssRelJoin); + parser.addNonInitialCommand("||", ssRelJoin); + + final NonInitialCommand<String, String, TestContext> addSub = infixLeft(20); + parser.addNonInitialCommand("+", addSub); + parser.addNonInitialCommand("-", addSub); + parser.addNonInitialCommand("\u00B1", addSub); // Unicode plus/minus + + final NonInitialCommand<String, String, TestContext> mulDiv = infixLeft(30); + parser.addNonInitialCommand("*", mulDiv); + parser.addNonInitialCommand("/", mulDiv); + + parser.addNonInitialCommand("!", postfix(40)); + + final NonInitialCommand<String, String, TestContext> expon = infixRight(50); + parser.addNonInitialCommand("^", expon); + parser.addNonInitialCommand("root", expon); + + final NonInitialCommand<String, String, TestContext> superexpon = postfix(50); + parser.addNonInitialCommand("(superexp)", superexpon); + + parser.addNonInitialCommand(".", infixLeft(60)); + + final NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", + litToken("idx")); + parser.addNonInitialCommand("[", arrayIdx); + + final InitialCommand<String, String, TestContext> ifThenElse = preTernary(0, 0, 0, "then", "else", + litToken("ifelse")); + parser.addInitialCommand("if", ifThenElse); + + final InitialCommand<String, String, TestContext> parens = grouping(0, ")", litToken("parens")); + parser.addInitialCommand("(", parens); + + final InitialCommand<String, String, TestContext> scoper = delimited(0, ";", "end", litToken("block"), + new BlockEnter(), idfun, new BlockExit(), true); + parser.addInitialCommand("begin", scoper); + + final InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", + litToken("array"), idfun, idfun, idfun, false); + parser.addInitialCommand("[", arrayLiteral); + + final InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), + idfun, idfun, idfun, false); + parser.addInitialCommand("{", jsonLiteral); + + parser.addInitialCommand("try", unary(3)); + + parser.addInitialCommand("case", unary(5)); + + parser.addInitialCommand("-", unary(30)); + + final InitialCommand<String, String, TestContext> root = unary(50); + parser.addInitialCommand("sqrt", root); + parser.addInitialCommand("cbrt", root); + + final InitialCommand<String, String, TestContext> leaf = leaf(); + parser.addInitialCommand("(literal)", leaf); + + parser.addInitialCommand("var", new VarCommand()); + + parser.addInitialCommand("switch", new SwitchCommand()); + + return parser; + } +}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java new file mode 100644 index 0000000..e824375 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java @@ -0,0 +1,21 @@ +package bjc.pratt.examples.lang; + +import bjc.pratt.ParserContext; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.ParserException; + +class SwitchCommand implements InitialCommand<String, String, TestContext> { + @Override + 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); + + 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/lang/TestContext.java index b5e8448..340c83d 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/TestContext.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/TestContext.java @@ -1,6 +1,6 @@ -package bjc.pratt.examples; +package bjc.pratt.examples.lang; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.esodata.Directory; import bjc.utils.esodata.SimpleDirectory; @@ -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/lang/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java new file mode 100644 index 0000000..7a84d93 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java @@ -0,0 +1,38 @@ +package bjc.pratt.examples.lang; + +import static bjc.pratt.tokens.StringToken.litToken; + +import java.util.Set; +import java.util.function.Function; + +import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.Token; + +final class Tokenizer implements Function<String, Token<String, String>> { + private final Set<String> ops; + private final Set<String> reserved; + + @SuppressWarnings("unused") + private final TestContext ctx; + + 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(final String strang) { + if (ops.contains(strang) || reserved.contains(strang)) + return litToken(strang); + else if (strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) { + /* + * This regular expression matches series of unicode + * super - scripts 1 - 9. + */ + String subscript = strang; + + return new StringToken("(superexp)", subscript); + } 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/lang/VarCommand.java index dd04a59..48fc49c 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/VarCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/VarCommand.java @@ -1,9 +1,9 @@ -package bjc.pratt.examples; +package bjc.pratt.examples.lang; import bjc.pratt.ParserContext; -import bjc.pratt.Token; import bjc.pratt.commands.AbstractInitialCommand; import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; @@ -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/examples/java/bjc/pratt/examples/regex/Destringer.java b/JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java new file mode 100644 index 0000000..7ba2d5b --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java @@ -0,0 +1,31 @@ +package bjc.pratt.examples.regex; + +import java.util.Iterator; +import java.util.Map; +import java.util.function.UnaryOperator; + +import bjc.utils.data.GeneratingIterator; + +final class Destringer implements UnaryOperator<String> { + private final Iterator<Integer> numbers; + public final Map<String, String> stringLiterals; + + public Destringer(final Map<String, String> literals) { + numbers = new GeneratingIterator<>(0, (num) -> num + 1, (val) -> true); + stringLiterals = literals; + } + + @Override + public String apply(final String token) { + if (token.startsWith("\"") && token.endsWith("\"")) { + final String symName = "stringLiteral" + Integer.toString(numbers.next()); + + final String dequotedString = token.substring(1, token.length() - 1); + stringLiterals.put(symName, dequotedString); + + return symName; + } + + return token; + } +}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java b/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java new file mode 100644 index 0000000..5bb606e --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java @@ -0,0 +1,73 @@ +package bjc.pratt.examples.regex; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Scanner; + +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.TokenUtils.StringTokenSplitter; +import bjc.utils.parserutils.splitter.ChainTokenSplitter; +import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; +import bjc.utils.parserutils.splitter.TokenSplitter; +import bjc.utils.parserutils.splitter.TransformTokenSplitter; + +/** + * Grammar test for regular expressions. + * + * @author bjculkin + * + */ +public class RegexGrammar { + /** + * Main method. + * + * @param args + * Unused CLI arguments. + */ + public static void main(final String[] args) { + final Scanner scn = new Scanner(System.in); + + System.out.print("Enter text to parse (blank line to exit): "); + String ln = scn.nextLine().trim(); + + final Map<String, String> stringLiterals = new HashMap<>(); + + /* + * Build the token splitter + */ + final ChainTokenSplitter splitter = buildSplitter(stringLiterals); + + while (!ln.equals("")) { + final IList<String> quotelessTokens = splitter.split(ln); + + System.out.println("\nSplit tokens: " + quotelessTokens); + + System.out.print("\nEnter text to parse (blank line to exit): "); + ln = scn.nextLine().trim(); + } + + System.out.println("\nString table: "); + for (final Entry<String, String> entry : stringLiterals.entrySet()) { + System.out.printf("\t%s\t'%s'\n", entry.getKey(), entry.getValue()); + } + + scn.close(); + } + + private static ChainTokenSplitter buildSplitter(final Map<String, String> stringLiterals) { + final Destringer destringer = new Destringer(stringLiterals); + + final TokenSplitter dquoteSplitter = new StringTokenSplitter(); + final TokenSplitter dquoteRemover = new TransformTokenSplitter(dquoteSplitter, destringer); + + final ConfigurableTokenSplitter regexSplitter = new ConfigurableTokenSplitter(true); + regexSplitter.addSimpleDelimiters("+", "|"); + regexSplitter.compile(); + + final ChainTokenSplitter splitter = new ChainTokenSplitter(); + splitter.appendSplitters(dquoteRemover, regexSplitter); + + return splitter; + } +} diff --git a/JPratt/src/main/java/bjc/pratt/ParserContext.java b/JPratt/src/main/java/bjc/pratt/ParserContext.java index f5e22ba..1e0b054 100644 --- a/JPratt/src/main/java/bjc/pratt/ParserContext.java +++ b/JPratt/src/main/java/bjc/pratt/ParserContext.java @@ -1,15 +1,17 @@ package bjc.pratt; +import bjc.pratt.tokens.TokenStream; + /** * 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. */ @@ -17,31 +19,38 @@ public class ParserContext<K, V, C> { /** * The source of tokens. */ - public TokenStream<K, V> tokens; + public TokenStream<K, V> tokens; + /** * The parser for sub-expressions. */ - public PrattParser<K, V, C> parse; + public PrattParser<K, V, C> parse; + /** * The state of the parser. */ - public C state; + public C state; + + /** + * The initial command for the current expression. + */ + public K initial; /** * Create a new parser context. - * - * @param tokens + * + * @param tokns * The source of tokens. - * - * @param parse + * + * @param prse * The parser to call for sub expressions. - * - * @param state + * + * @param stte * Any state needing to be kept during parsing. */ - public ParserContext(TokenStream<K, V> tokens, PrattParser<K, V, C> parse, C state) { - this.tokens = tokens; - this.parse = parse; - this.state = state; + public ParserContext(final TokenStream<K, V> tokns, final PrattParser<K, V, C> prse, final C stte) { + tokens = tokns; + parse = prse; + state = stte; } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index e08a67c..7783736 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -1,28 +1,34 @@ package bjc.pratt; -import bjc.pratt.commands.DefaultInitialCommand; -import bjc.pratt.commands.DefaultNonInitialCommand; +import java.util.HashMap; +import java.util.Map; + +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.commands.MetaInitialCommand; +import bjc.pratt.commands.MetaNonInitialCommand; +import bjc.pratt.commands.NonInitialCommand; +import bjc.pratt.commands.impls.DefaultInitialCommand; +import bjc.pratt.commands.impls.DefaultNonInitialCommand; +import bjc.pratt.tokens.Token; +import bjc.pratt.tokens.TokenStream; 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,94 +41,97 @@ 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; + private final Map<K, Map<K, MetaNonInitialCommand<K, V, C>>> dependantMetaLeftCommands; /* * The left commands. */ - private Map<K, NonInitialCommand<K, V, C>> leftCommands; + private final Map<K, NonInitialCommand<K, V, C>> leftCommands; + private final Map<K, MetaNonInitialCommand<K, V, C>> metaLeftCommands; + /* * The initial commands. */ - private Map<K, InitialCommand<K, V, C>> nullCommands; + private final Map<K, InitialCommand<K, V, C>> nullCommands; + private final Map<K, MetaInitialCommand<K, V, C>> metaNullCommands; + /* * Initial commands only checked for statements. */ - private Map<K, InitialCommand<K, V, C>> statementCommands; + private final Map<K, InitialCommand<K, V, C>> statementCommands; + private final Map<K, MetaInitialCommand<K, V, C>> metaStatementCommands; /** * Create a new Pratt parser. - * + * */ public PrattParser() { dependantLeftCommands = new HashMap<>(); + dependantMetaLeftCommands = new HashMap<>(); leftCommands = new HashMap<>(); + metaLeftCommands = new HashMap<>(); + nullCommands = new HashMap<>(); + metaNullCommands = new HashMap<>(); + statementCommands = new HashMap<>(); + metaStatementCommands = new HashMap<>(); } /** * 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) { + 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(); + ParserContext<K, V, C> parserContext = new ParserContext<>(tokens, this, state); + + final Token<K, V> initToken = tokens.current(); tokens.next(); - K initKey = initToken.getKey(); + final K initKey = initToken.getKey(); - ITree<Token<K, V>> ast; + InitialCommand<K, V, C> nullCommand = getInitialCommand(isStatement, initKey, parserContext); + ITree<Token<K, V>> ast = nullCommand.denote(initToken, parserContext); - if (isStatement && statementCommands.containsKey(initKey)) { - ast = statementCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken, - new ParserContext<>(tokens, this, state)); - } else { - ast = nullCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken, - new ParserContext<>(tokens, this, state)); - } + parserContext.initial = initKey; 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); - - if (dependantLeftCommands.containsKey(initKey)) { - command = dependantLeftCommands.get(initKey).getOrDefault(key, command); - } + NonInitialCommand<K, V, C> leftCommand = getNonInitialCommand(key, parserContext); - int leftBind = command.leftBinding(); + final int leftBind = leftCommand.leftBinding(); if (NumberUtils.between(precedence, rightPrec, leftBind)) { tokens.next(); - ast = command.denote(ast, tok, new ParserContext<>(tokens, this, state)); - rightPrec = command.nextBinding(); + ast = leftCommand.denote(ast, tok, parserContext); + rightPrec = leftCommand.nextBinding(); } else { break; } @@ -133,58 +142,125 @@ 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 dependant non-initial command to this parser. + * 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) { - if (dependantLeftCommands.containsKey(dependant)) { - dependantLeftCommands.get(dependant).put(marker, comm); - } else { - Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>(); + public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand<K, V, C> comm) { + Map<K, NonInitialCommand<K, V, C>> dependantMap = dependantLeftCommands.getOrDefault(dependant, + new HashMap<>()); - comms.put(marker, comm); + dependantMap.put(marker, comm); + } - dependantLeftCommands.put(dependant, comms); + /** + * Lookup an initial command. + * + * @param isStatement + * Whether to look for statement commands or not. + * + * @param key + * The key of the command. + * + * @param ctx + * The context for meta-commands. + * + * @return A command attached to that key, or a default implementation. + */ + public InitialCommand<K, V, C> getInitialCommand(boolean isStatement, K key, ParserContext<K, V, C> ctx) { + if (isStatement) { + if (metaStatementCommands.containsKey(key)) + return metaStatementCommands.get(key).getCommand(ctx); + else if (statementCommands.containsKey(key)) + return statementCommands.get(key); } + + if (metaNullCommands.containsKey(key)) + return metaNullCommands.get(key).getCommand(ctx); + else + return nullCommands.getOrDefault(key, DEFAULT_NULL_COMMAND); + } + + /** + * Lookup a non-initial command. + * + * @param key + * The key of the command. + * + * @param ctx + * The context for meta-commands. + * + * @return A command attached to that key, or a default implementation. + */ + public NonInitialCommand<K, V, C> getNonInitialCommand(K key, ParserContext<K, V, C> ctx) { + if (dependantMetaLeftCommands.containsKey(ctx.initial)) { + Map<K, MetaNonInitialCommand<K, V, C>> dependantCommands = dependantMetaLeftCommands + .get(ctx.initial); + + if (dependantCommands.containsKey(key)) { + return dependantCommands.get(key).getCommand(ctx); + } + } + + if (dependantLeftCommands.containsKey(ctx.initial)) { + Map<K, NonInitialCommand<K, V, C>> dependantCommands = dependantLeftCommands.get(ctx.initial); + + if (dependantCommands.containsKey(key)) { + return dependantCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); + } + } + + if (metaLeftCommands.containsKey(key)) { + return metaLeftCommands.get(key).getCommand(ctx); + } else + return leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..5c728d9 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,78 @@ +package bjc.pratt.blocks; + +import java.util.Set; + +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A {@link ParseBlock} for a series of parse blocks, linked by a set of tokens. + * + * Roughly analogous to Perl 6s list associative operators. + * + * @author bjculkin + * + * @param <K> + * The token key type. + * + * @param <V> + * The token value type. + * + * @param <C> + * The parser state type. + * + */ +public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private ParseBlock<K, V, C> iner; + + private Set<K> indicators; + + private Token<K, V> trm; + + /** + * Create a new chain parser block. + * + * @param inner + * The block for the chains interior. + * + * @param chainIndicators + * The set of markers that indicate continuing the chain + * + * @param term + * The node in the AST for the expression. + */ + public ChainParseBlock(ParseBlock<K, V, C> inner, Set<K> chainIndicators, Token<K, V> term) { + iner = inner; + indicators = chainIndicators; + trm = term; + } + + @Override + public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { + ITree<Token<K, V>> expression = iner.parse(ctx); + + Token<K, V> currentToken = ctx.tokens.current(); + if (indicators.contains(currentToken.getKey())) { + ITree<Token<K, V>> res = new Tree<>(trm); + res.addChild(expression); + + while (indicators.contains(currentToken.getKey())) { + res.addChild(new Tree<>(currentToken)); + ctx.tokens.next(); + + ITree<Token<K, V>> innerExpression = iner.parse(ctx); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return res; + } + + return expression; + } + +} diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java new file mode 100644 index 0000000..6bf5582 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -0,0 +1,81 @@ +package bjc.pratt.blocks; + +import java.util.function.Function; + +import bjc.pratt.ParserContext; +import bjc.pratt.PrattParser; +import bjc.pratt.tokens.Token; +import bjc.pratt.tokens.TokenStream; +import bjc.utils.data.ITree; +import bjc.utils.funcutils.Isomorphism; +import bjc.utils.parserutils.ParserException; + +/** + * A {@link ParseBlock} that parses an expression from a 'inner' grammar. + * + * @author bjculkin + * + * @param <K> + * The key type of the outer tokens. + * + * @param <V> + * The value type of the outer tokens. + * + * @param <C> + * The state type of the outer parser. + * + * @param <K2> + * The key type of the inner tokens. + * + * @param <V2> + * The value type of the inner tokens. + * + * @param <C2> + * The state type of the outer parser. + */ +public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, C> { + private final PrattParser<K2, V2, C2> innr; + + private final int prcedence; + private final boolean isStatemnt; + + private final Function<TokenStream<K, V>, TokenStream<K2, V2>> tkenTransform; + private final Isomorphism<C, C2> stteTransform; + private final Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> xpressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner + * @param precedence + * @param isStatement + * @param tokenTransform + * @param stateTransform + * @param expressionTransform + */ + public GrammarParseBlock(final PrattParser<K2, V2, C2> inner, final int precedence, final boolean isStatement, + final Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform, + final Isomorphism<C, C2> stateTransform, + final Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; + } + + @Override + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); + + final TokenStream<K2, V2> newTokens = tkenTransform.apply(ctx.tokens); + + final ITree<Token<K2, V2>> expression = innr.parseExpression(prcedence, newTokens, newState, + isStatemnt); + + ctx.state = stteTransform.from(newState); + + return xpressionTransform.apply(expression); + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java index cf707ed..2fddac0 100644 --- a/JPratt/src/main/java/bjc/pratt/ParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java @@ -1,19 +1,21 @@ -package bjc.pratt; +package bjc.pratt.blocks; +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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 +24,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/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java index e0dea48..21fa7e1 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -3,82 +3,85 @@ package bjc.pratt.blocks; import java.util.function.Predicate; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; /** * Utility class for creating common implementations of {@link ParseBlock} - * + * * @author bjculkin * */ public class ParseBlocks { + /* + * Grammar parse blocks are complex enough to not get a builder method. + */ + /** * Create a new repeating parse block. - * + * * @param inner * The parse block to repeat. - * + * * @param delim - * The token type that seperates repetitions. - * + * The token type that separates repetitions. + * * @param term - * The token type that terminates repititions. - * + * The token type that terminates repetitions. + * * @param mark * The token to use as the node in the AST. - * + * * @param action * The action to perform on the state after every - * repitition. - * + * repetition. + * * @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) { - return new SimpleParseBlock<>(precedence, terminator, 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, validator, terminator); } }
\ 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..4146648 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; @@ -12,49 +11,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 +73,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..1ff561c 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -2,47 +2,45 @@ package bjc.pratt.blocks; import java.util.function.Predicate; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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 validator + * The predicate to apply to blocks. * @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 Predicate<ITree<Token<K, V>>> validator, + final K terminator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -51,16 +49,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 +68,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; @@ -93,9 +89,4 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { return true; } - - @Override - public String toString() { - return String.format("ParseBlock [pow=%s, term='%s']", pow, term); - } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index 5e561fc..844a4f8 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -2,15 +2,14 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.parserutils.ParserException; /** * A parse block that can adjust the state before handling its context. - * + * * @author bjculkin * * @param <K> @@ -21,39 +20,39 @@ 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> onEnter; - private UnaryOperator<C> onExit; + private final UnaryOperator<C> onEntr; + private final UnaryOperator<C> onExt; - private ParseBlock<K, V, C> source; + 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) { - super(); - this.onEnter = onEnter; - this.onExit = onExit; - this.source = 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 = onEnter.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 = source.parse(newCtx); + final ITree<Token<K, V>> res = sourc.parse(newCtx); - ctx.state = onExit.apply(newState); + ctx.state = onExt.apply(newState); return res; } diff --git a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java index b5a789c..c1f9f6b 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java @@ -1,28 +1,28 @@ package bjc.pratt.commands; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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..06b26aa 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java @@ -1,42 +1,42 @@ package bjc.pratt.commands; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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..943554c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java @@ -1,18 +1,16 @@ package bjc.pratt.commands; -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 +19,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/InitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java index 470ef3b..57dc2d1 100644 --- a/JPratt/src/main/java/bjc/pratt/InitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java @@ -1,36 +1,38 @@ -package bjc.pratt; +package bjc.pratt.commands; +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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/commands/MetaInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java new file mode 100644 index 0000000..9f14f36 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java @@ -0,0 +1,13 @@ +package bjc.pratt.commands;
+
+import bjc.pratt.ParserContext;
+
+/**
+ * A 'meta-command' that yields the actual command to use.
+ *
+ * @author bjculkin
+ *
+ */
+public interface MetaInitialCommand<K, V, C> {
+ InitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx);
+}
diff --git a/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java new file mode 100644 index 0000000..3a5cba4 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java @@ -0,0 +1,21 @@ +package bjc.pratt.commands;
+
+import bjc.pratt.ParserContext;
+
+/**
+ * A 'meta-command' for non-initial commands.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ */
+public interface MetaNonInitialCommand<K, V, C> {
+ NonInitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx);
+}
diff --git a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java index 3f6512d..d06e336 100644 --- a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java @@ -1,19 +1,21 @@ -package bjc.pratt; +package bjc.pratt.commands; +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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 +23,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 +42,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/commands/impls/BlockInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/BlockInitialCommand.java new file mode 100644 index 0000000..3e8c8c2 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/BlockInitialCommand.java @@ -0,0 +1,42 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.parserutils.ParserException; + +/** + * An initial command that delegates all the work to a {@link ParseBlock} + * + * @author bjculkin + * @param <K> + * The token key type. + * + * @param <V> + * The token value type. + * + * @param <C> + * The parser state type. + * + */ +public class BlockInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { + private final ParseBlock<K, V, C> blck; + + /** + * Create a new block initial command. + * + * @param block + * The block to delegate to. + */ + public BlockInitialCommand(final ParseBlock<K, V, C> block) { + blck = block; + } + + @Override + protected ITree<Token<K, V>> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) + throws ParserException { + return blck.parse(ctx); + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java index c7a10b1..2a9bf35 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java @@ -1,45 +1,46 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; + +import java.util.Set; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.BinaryPostCommand; +import bjc.pratt.tokens.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,23 +48,24 @@ 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); - } else { - return res; } + + return res; } @Override diff --git a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java index 86a172f..16af5aa 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java @@ -1,40 +1,41 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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/impls/DefaultInitialCommand.java index 717444a..16d2e59 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java @@ -1,28 +1,29 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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/impls/DefaultNonInitialCommand.java index aeb337d..48aab29 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java @@ -1,27 +1,28 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.NonInitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.NonInitialCommand; +import bjc.pratt.tokens.Token; 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/impls/DenestingCommand.java index 9b4518f..220e4cc 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java @@ -1,44 +1,45 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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/impls/GroupingCommand.java index 1a8d3c8..28f1299 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java @@ -1,50 +1,51 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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/impls/InitialCommands.java index 3ece14c..8da758a 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java @@ -1,57 +1,60 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.InitialCommand; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; -import bjc.utils.data.ITree; +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 static bjc.pratt.blocks.ParseBlocks.*; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; /** * * 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); } -} +}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java index 798b9ce..bb999f6 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java @@ -1,29 +1,30 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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/impls/LeftBinaryCommand.java index 4a481d7..9727dcb 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java @@ -1,27 +1,29 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; /** * 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/impls/NonBinaryCommand.java index d1208bd..2d92780 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java @@ -1,27 +1,29 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; /** * 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/impls/NonInitialCommands.java index b7eda95..a2c158c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java @@ -1,139 +1,140 @@ -package bjc.pratt.commands; - -import bjc.pratt.NonInitialCommand; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; -import bjc.pratt.blocks.SimpleParseBlock; +package bjc.pratt.commands.impls; import java.util.Set; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.blocks.SimpleParseBlock; +import bjc.pratt.commands.NonInitialCommand; +import bjc.pratt.tokens.Token; + /** * 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, null, closer); 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, null, closer); 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/impls/PostCircumfixCommand.java index 6bed0ff..c1d70b8 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java @@ -1,49 +1,48 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.BinaryPostCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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 +50,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/impls/PostfixCommand.java index 5e2ce28..00c7ad2 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java @@ -1,39 +1,40 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.BinaryPostCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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/impls/PreTernaryCommand.java index efa7872..fa63c9c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java @@ -1,51 +1,52 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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> term; + 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) @@ -54,22 +55,22 @@ public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> throw new NullPointerException("Op block #1 must not be null"); else if (op2 == null) throw new NullPointerException("Op block #2 must not be null"); - this.condBlock = cond; - this.opblock1 = op1; - this.opblock2 = op2; + condBlock = cond; + opblock1 = op1; + opblock2 = op2; - this.term = term; + trm = term; } @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<>(term, cond, op1, op2); + return new Tree<>(trm, cond, op1, op2); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java index 8ddab06..c3887ee 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java @@ -1,8 +1,10 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; /** * A right-associative binary operator. - * + * * @author bjculkin * * @param <K> @@ -15,11 +17,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/impls/TernaryCommand.java index bac12cd..92355c0 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java @@ -1,50 +1,52 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.blocks.ParseBlock; +import bjc.pratt.commands.BinaryPostCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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,21 +59,20 @@ 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; - } else { - return leftBinding(); - } + if (nonassoc) return leftBinding() - 1; + + return leftBinding(); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java index 9ec3631..e6c509c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java @@ -1,52 +1,53 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; import java.util.function.UnaryOperator; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; 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> internal; + private final InitialCommand<K, V, C> internl; - private UnaryOperator<ITree<Token<K, V>>> transform; + 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(); - this.internal = internal; - this.transform = transform; + 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 transform.apply(internal.denote(operator, ctx)); + 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/impls/UnaryCommand.java index 0689210..156dee0 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java @@ -1,22 +1,23 @@ -package bjc.pratt.commands; +package bjc.pratt.commands.impls; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.commands.AbstractInitialCommand; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; 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 +26,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 2e75702..ff47667 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -1,27 +1,25 @@ package bjc.pratt.tokens; -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,34 +39,27 @@ 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) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof StringToken)) - return false; + 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; - } else if (!key.equals(other.key)) - return false; + if (other.key != null) return false; + } else if (!key.equals(other.key)) return false; if (val == null) { - if (other.val != null) - return false; - } else if (!val.equals(other.val)) - return false; + if (other.val != null) return false; + } else if (!val.equals(other.val)) return false; return true; } @@ -77,8 +68,16 @@ public class StringToken implements Token<String, String> { public String toString() { return String.format("StringToken [key='%s', val='%s']", key, val); } - - public static StringToken litToken(String val) { + + /** + * 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(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..07e1827 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java @@ -1,34 +1,31 @@ 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; - /** * 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; } diff --git a/JPratt/src/main/java/bjc/pratt/Token.java b/JPratt/src/main/java/bjc/pratt/tokens/Token.java index 1f2616b..b07d2e1 100644 --- a/JPratt/src/main/java/bjc/pratt/Token.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/Token.java @@ -1,13 +1,13 @@ -package bjc.pratt; +package bjc.pratt.tokens; /** * 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/tokens/TokenStream.java index d3472a0..a5febcc 100644 --- a/JPratt/src/main/java/bjc/pratt/TokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java @@ -1,27 +1,28 @@ -package bjc.pratt; +package bjc.pratt.tokens; -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,41 +56,49 @@ 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); - } else { - next(); } + + next(); } /** * 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))); } - - public boolean headIs(K val) { + + /** + * 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(final K val) { return current().getKey().equals(val); } } |
