From f394306a4b65a3328551f9f6b8d4abff8bfd5b27 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 12 Apr 2017 10:46:51 -0400 Subject: Package reorganization --- .../bjc/pratt/examples/lang/AssignCommand.java | 4 +- .../java/bjc/pratt/examples/lang/BlockEnter.java | 2 +- .../bjc/pratt/examples/lang/PrattParserTest.java | 30 ++-- .../bjc/pratt/examples/lang/SwitchCommand.java | 4 +- .../java/bjc/pratt/examples/lang/TestContext.java | 2 +- .../java/bjc/pratt/examples/lang/Tokenizer.java | 2 +- .../java/bjc/pratt/examples/lang/VarCommand.java | 2 +- JPratt/src/main/java/bjc/pratt/InitialCommand.java | 38 ----- .../main/java/bjc/pratt/MetaInitialCommand.java | 11 -- .../main/java/bjc/pratt/MetaNonInitialCommand.java | 19 --- .../src/main/java/bjc/pratt/NonInitialCommand.java | 63 -------- JPratt/src/main/java/bjc/pratt/ParseBlock.java | 37 ----- JPratt/src/main/java/bjc/pratt/ParserContext.java | 2 + JPratt/src/main/java/bjc/pratt/PrattParser.java | 10 +- JPratt/src/main/java/bjc/pratt/Token.java | 30 ---- JPratt/src/main/java/bjc/pratt/TokenStream.java | 104 ------------- .../java/bjc/pratt/blocks/ChainParseBlock.java | 3 +- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 5 +- .../src/main/java/bjc/pratt/blocks/ParseBlock.java | 39 +++++ .../main/java/bjc/pratt/blocks/ParseBlocks.java | 3 +- .../java/bjc/pratt/blocks/RepeatingParseBlock.java | 3 +- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 3 +- .../java/bjc/pratt/blocks/TriggeredParseBlock.java | 3 +- .../bjc/pratt/commands/AbstractInitialCommand.java | 3 +- .../java/bjc/pratt/commands/BinaryCommand.java | 2 +- .../java/bjc/pratt/commands/BinaryPostCommand.java | 2 - .../bjc/pratt/commands/BlockInitialCommand.java | 41 ----- .../main/java/bjc/pratt/commands/ChainCommand.java | 74 --------- .../java/bjc/pratt/commands/ConstantCommand.java | 41 ----- .../bjc/pratt/commands/DefaultInitialCommand.java | 29 ---- .../pratt/commands/DefaultNonInitialCommand.java | 33 ---- .../java/bjc/pratt/commands/DenestingCommand.java | 45 ------ .../java/bjc/pratt/commands/GroupingCommand.java | 51 ------ .../java/bjc/pratt/commands/InitialCommand.java | 40 +++++ .../java/bjc/pratt/commands/InitialCommands.java | 171 --------------------- .../main/java/bjc/pratt/commands/LeafCommand.java | 30 ---- .../java/bjc/pratt/commands/LeftBinaryCommand.java | 32 ---- .../bjc/pratt/commands/MetaInitialCommand.java | 13 ++ .../bjc/pratt/commands/MetaNonInitialCommand.java | 21 +++ .../java/bjc/pratt/commands/NonBinaryCommand.java | 37 ----- .../java/bjc/pratt/commands/NonInitialCommand.java | 65 ++++++++ .../bjc/pratt/commands/NonInitialCommands.java | 141 ----------------- .../bjc/pratt/commands/PostCircumfixCommand.java | 58 ------- .../java/bjc/pratt/commands/PostfixCommand.java | 39 ----- .../java/bjc/pratt/commands/PreTernaryCommand.java | 75 --------- .../bjc/pratt/commands/RightBinaryCommand.java | 30 ---- .../java/bjc/pratt/commands/TernaryCommand.java | 77 ---------- .../pratt/commands/TransformingInitialCommand.java | 52 ------- .../main/java/bjc/pratt/commands/UnaryCommand.java | 45 ------ .../pratt/commands/impls/BlockInitialCommand.java | 42 +++++ .../bjc/pratt/commands/impls/ChainCommand.java | 75 +++++++++ .../bjc/pratt/commands/impls/ConstantCommand.java | 41 +++++ .../commands/impls/DefaultInitialCommand.java | 29 ++++ .../commands/impls/DefaultNonInitialCommand.java | 33 ++++ .../bjc/pratt/commands/impls/DenestingCommand.java | 46 ++++++ .../bjc/pratt/commands/impls/GroupingCommand.java | 52 +++++++ .../bjc/pratt/commands/impls/InitialCommands.java | 171 +++++++++++++++++++++ .../java/bjc/pratt/commands/impls/LeafCommand.java | 30 ++++ .../pratt/commands/impls/LeftBinaryCommand.java | 34 ++++ .../bjc/pratt/commands/impls/NonBinaryCommand.java | 39 +++++ .../pratt/commands/impls/NonInitialCommands.java | 141 +++++++++++++++++ .../pratt/commands/impls/PostCircumfixCommand.java | 59 +++++++ .../bjc/pratt/commands/impls/PostfixCommand.java | 40 +++++ .../pratt/commands/impls/PreTernaryCommand.java | 76 +++++++++ .../pratt/commands/impls/RightBinaryCommand.java | 32 ++++ .../bjc/pratt/commands/impls/TernaryCommand.java | 78 ++++++++++ .../commands/impls/TransformingInitialCommand.java | 53 +++++++ .../bjc/pratt/commands/impls/UnaryCommand.java | 46 ++++++ .../main/java/bjc/pratt/tokens/StringToken.java | 2 - .../java/bjc/pratt/tokens/StringTokenStream.java | 3 - JPratt/src/main/java/bjc/pratt/tokens/Token.java | 30 ++++ .../main/java/bjc/pratt/tokens/TokenStream.java | 104 +++++++++++++ 72 files changed, 1471 insertions(+), 1451 deletions(-) delete mode 100644 JPratt/src/main/java/bjc/pratt/InitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/MetaInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/MetaNonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/NonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/ParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/Token.java delete mode 100644 JPratt/src/main/java/bjc/pratt/TokenStream.java create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/BlockInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/tokens/Token.java create mode 100644 JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java (limited to 'JPratt/src') diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java index 5053f67..8b96850 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/AssignCommand.java @@ -1,9 +1,9 @@ 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; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockEnter.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockEnter.java index 41e0303..dedf010 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockEnter.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/BlockEnter.java @@ -2,7 +2,7 @@ package bjc.pratt.examples.lang; import java.util.function.UnaryOperator; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.esodata.Directory; import bjc.utils.esodata.Stack; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java index 3686a84..7af16a0 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java @@ -1,17 +1,17 @@ package bjc.pratt.examples.lang; -import static bjc.pratt.commands.InitialCommands.delimited; -import static bjc.pratt.commands.InitialCommands.grouping; -import static bjc.pratt.commands.InitialCommands.leaf; -import static bjc.pratt.commands.InitialCommands.preTernary; -import static bjc.pratt.commands.InitialCommands.unary; -import static bjc.pratt.commands.NonInitialCommands.chain; -import static bjc.pratt.commands.NonInitialCommands.infixLeft; -import static bjc.pratt.commands.NonInitialCommands.infixNon; -import static bjc.pratt.commands.NonInitialCommands.infixRight; -import static bjc.pratt.commands.NonInitialCommands.postCircumfix; -import static bjc.pratt.commands.NonInitialCommands.postfix; -import static bjc.pratt.commands.NonInitialCommands.ternary; +import static bjc.pratt.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; @@ -25,12 +25,12 @@ import java.util.Scanner; import java.util.Set; import java.util.function.UnaryOperator; -import bjc.pratt.InitialCommand; -import bjc.pratt.NonInitialCommand; import bjc.pratt.PrattParser; -import bjc.pratt.Token; +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; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java index f5fc9b1..e824375 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/SwitchCommand.java @@ -1,9 +1,9 @@ package bjc.pratt.examples.lang; -import bjc.pratt.InitialCommand; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +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; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/TestContext.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/TestContext.java index 659ba36..340c83d 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/TestContext.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/TestContext.java @@ -1,6 +1,6 @@ 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; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java index 800a4ec..7a84d93 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/Tokenizer.java @@ -5,8 +5,8 @@ import static bjc.pratt.tokens.StringToken.litToken; import java.util.Set; import java.util.function.Function; -import bjc.pratt.Token; import bjc.pratt.tokens.StringToken; +import bjc.pratt.tokens.Token; final class Tokenizer implements Function> { private final Set ops; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/VarCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/VarCommand.java index a2f428b..48fc49c 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/VarCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/VarCommand.java @@ -1,9 +1,9 @@ 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; diff --git a/JPratt/src/main/java/bjc/pratt/InitialCommand.java b/JPratt/src/main/java/bjc/pratt/InitialCommand.java deleted file mode 100644 index 403cf24..0000000 --- a/JPratt/src/main/java/bjc/pratt/InitialCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -package bjc.pratt; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * Represents an initial command in parsing. - * - * @author EVE - * - * @param - * The key type for the tokens. - * - * @param - * The value type for the tokens. - * - * @param - * The state type of the parser. - * - * - */ -@FunctionalInterface -public interface InitialCommand { - /** - * 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. - */ - ITree> denote(Token operator, ParserContext ctx) throws ParserException; -} diff --git a/JPratt/src/main/java/bjc/pratt/MetaInitialCommand.java b/JPratt/src/main/java/bjc/pratt/MetaInitialCommand.java deleted file mode 100644 index 067d084..0000000 --- a/JPratt/src/main/java/bjc/pratt/MetaInitialCommand.java +++ /dev/null @@ -1,11 +0,0 @@ -package bjc.pratt; - -/** - * A 'meta-command' that yields the actual command to use. - * - * @author bjculkin - * - */ -public interface MetaInitialCommand { - InitialCommand getCommand(ParserContext ctx); -} diff --git a/JPratt/src/main/java/bjc/pratt/MetaNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/MetaNonInitialCommand.java deleted file mode 100644 index 444c0e4..0000000 --- a/JPratt/src/main/java/bjc/pratt/MetaNonInitialCommand.java +++ /dev/null @@ -1,19 +0,0 @@ -package bjc.pratt; - -/** - * A 'meta-command' for non-initial commands. - * - * @author bjculkin - * - * @param - * The token key type. - * - * @param - * The token value type. - * - * @param - * The parser state type. - */ -public interface MetaNonInitialCommand { - NonInitialCommand getCommand(ParserContext ctx); -} diff --git a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java deleted file mode 100644 index bb1aa5b..0000000 --- a/JPratt/src/main/java/bjc/pratt/NonInitialCommand.java +++ /dev/null @@ -1,63 +0,0 @@ -package bjc.pratt; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a non-initial command in parsing. - * - * @author EVE - * - * @param - * The key type for the tokens. - * - * @param - * The value type for the tokens. - * - * @param - * The state type of the parser. - * - */ -public abstract class NonInitialCommand { - /** - * 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. - */ - public abstract ITree> denote(ITree> operand, Token operator, - ParserContext ctx) throws ParserException; - - /** - * 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() { - return leftBinding(); - } -} diff --git a/JPratt/src/main/java/bjc/pratt/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/ParseBlock.java deleted file mode 100644 index 28caf43..0000000 --- a/JPratt/src/main/java/bjc/pratt/ParseBlock.java +++ /dev/null @@ -1,37 +0,0 @@ -package bjc.pratt; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a embedded block in an expression. - * - * @author bjculkin - * - * @param - * The key type of the token. - * - * @param - * The value type of the token. - * - * @param - * The state type of the parser. - */ -@FunctionalInterface -public interface ParseBlock { - - /** - * 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. - */ - ITree> parse(ParserContext ctx) throws ParserException; - -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/ParserContext.java b/JPratt/src/main/java/bjc/pratt/ParserContext.java index 9007f79..1e0b054 100644 --- a/JPratt/src/main/java/bjc/pratt/ParserContext.java +++ b/JPratt/src/main/java/bjc/pratt/ParserContext.java @@ -1,5 +1,7 @@ package bjc.pratt; +import bjc.pratt.tokens.TokenStream; + /** * Represents the contextual state passed to a command. * diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index e2654f7..7783736 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -3,8 +3,14 @@ package bjc.pratt; import java.util.HashMap; import java.util.Map; -import bjc.pratt.commands.DefaultInitialCommand; -import bjc.pratt.commands.DefaultNonInitialCommand; +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; diff --git a/JPratt/src/main/java/bjc/pratt/Token.java b/JPratt/src/main/java/bjc/pratt/Token.java deleted file mode 100644 index d49dee5..0000000 --- a/JPratt/src/main/java/bjc/pratt/Token.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.pratt; - -/** - * Represents a simple parsing token. - * - * @author EVE - * - * @param - * The key type of this token. Represents the type of the token. - * - * @param - * The value type of this token. Represents any additional data - * for the token. - * - */ -public interface Token { - /** - * Get the key for this token. - * - * @return The key for this token - */ - K getKey(); - - /** - * Get the value for this token. - * - * @return The value for this token. - */ - V getValue(); -} diff --git a/JPratt/src/main/java/bjc/pratt/TokenStream.java b/JPratt/src/main/java/bjc/pratt/TokenStream.java deleted file mode 100644 index 7390e5f..0000000 --- a/JPratt/src/main/java/bjc/pratt/TokenStream.java +++ /dev/null @@ -1,104 +0,0 @@ -package bjc.pratt; - -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 - * The key type of the token. - * - * @param - * The value type of the token. - */ -public abstract class TokenStream implements Iterator> { - /** - * The exception thrown when an expectation fails. - * - * @author EVE - * - */ - public static class ExpectationException extends ParserException { - private static final long serialVersionUID = 4299299480127680805L; - - /** - * Create a new exception with the specified message. - * - * @param msg - * The message of the exception. - */ - public ExpectationException(final String msg) { - super(msg); - } - } - - /** - * Get the current token. - * - * @return The current token. - */ - public abstract Token current(); - - @Override - public abstract Token next(); - - @Override - public abstract boolean hasNext(); - - /** - * 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(final Set expectedKeys) throws ExpectationException { - final K curKey = current().getKey(); - - if (!expectedKeys.contains(curKey)) { - final String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false); - - throw new ExpectationException("One of '" + expectedList + "' was expected, not " + curKey); - } - - 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(final K... expectedKeys) throws ExpectationException { - expect(new HashSet<>(Arrays.asList(expectedKeys))); - } - - /** - * Check whether the head token is a certain type. - * - * @param val - * The type to check for. - * - * @return Whether or not the head token is of that type. - */ - public boolean headIs(final K val) { - return current().getKey().equals(val); - } -} diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java index 1758c17..5c728d9 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.Set; -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; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index b714940..6bf5582 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -2,11 +2,10 @@ package bjc.pratt.blocks; import java.util.function.Function; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; import bjc.pratt.PrattParser; -import bjc.pratt.Token; -import bjc.pratt.TokenStream; +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; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java new file mode 100644 index 0000000..2fddac0 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java @@ -0,0 +1,39 @@ +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 + * The key type of the token. + * + * @param + * The value type of the token. + * + * @param + * The state type of the parser. + */ +@FunctionalInterface +public interface ParseBlock { + + /** + * 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. + */ + ITree> parse(ParserContext ctx) throws ParserException; + +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java index 44288d2..21fa7e1 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -3,8 +3,7 @@ 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; /** diff --git a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java index 7791ea9..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; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index 0fb5097..1ff561c 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -2,9 +2,8 @@ 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; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index bb3d6f7..844a4f8 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.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.parserutils.ParserException; diff --git a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java index 9fc4fbc..c1f9f6b 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java @@ -1,8 +1,7 @@ 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; diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java index ef81d3f..06b26aa 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java @@ -1,7 +1,7 @@ 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; diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java index 316f0fe..943554c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java @@ -1,7 +1,5 @@ package bjc.pratt.commands; -import bjc.pratt.NonInitialCommand; - /** * A operator with fixed precedence. * diff --git a/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java deleted file mode 100644 index ca2d31a..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java +++ /dev/null @@ -1,41 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParseBlock; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The token key type. - * - * @param - * The token value type. - * - * @param - * The parser state type. - * - */ -public class BlockInitialCommand extends AbstractInitialCommand { - private final ParseBlock blck; - - /** - * Create a new block initial command. - * - * @param block - * The block to delegate to. - */ - public BlockInitialCommand(final ParseBlock block) { - blck = block; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext 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/ChainCommand.java deleted file mode 100644 index 4e471d0..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/ChainCommand.java +++ /dev/null @@ -1,74 +0,0 @@ -package bjc.pratt.commands; - -import java.util.Set; - -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * Create a new chained operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class ChainCommand extends BinaryPostCommand { - private final Set chainWith; - - private final Token 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(final int precedence, final Set chainSet, final Token chainMarker) { - super(precedence); - - chainWith = chainSet; - chain = chainMarker; - } - - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) throws ParserException { - final ITree> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, - false); - - final ITree> res = new Tree<>(operator, operand, tree); - - if (chainWith.contains(ctx.tokens.current().getKey())) { - final Token tok = ctx.tokens.current(); - ctx.tokens.next(); - - final ITree> other = denote(tree, tok, - new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); - - return new Tree<>(chain, res, other); - } - - return res; - } - - @Override - public int nextBinding() { - return leftBinding() - 1; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java b/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java deleted file mode 100644 index 9b12cda..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/ConstantCommand.java +++ /dev/null @@ -1,41 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * A command that represents a specific tree. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class ConstantCommand implements InitialCommand { - private final ITree> val; - - /** - * Create a new constant. - * - * @param con - * The tree this constant represents. - */ - public ConstantCommand(final ITree> con) { - val = con; - } - - @Override - public ITree> denote(final Token operator, final ParserContext ctx) - throws ParserException { - return val; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java deleted file mode 100644 index ff40ef2..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultInitialCommand.java +++ /dev/null @@ -1,29 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * Default implementation of an initial command. - * - * @author EVE - * - * @param - * The key type of the token. - * - * @param - * The value type of the token. - * - * @param - * The state type of the parser. - */ -public class DefaultInitialCommand implements InitialCommand { - @Override - public ITree> denote(final Token operator, final ParserContext ctx) - throws ParserException { - throw new ParserException("Unexpected token " + operator); - } -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java deleted file mode 100644 index 6f590c9..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/DefaultNonInitialCommand.java +++ /dev/null @@ -1,33 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.NonInitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; - -/** - * Default implementation of a non-initial command. - * - * @author EVE - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class DefaultNonInitialCommand extends NonInitialCommand { - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) { - throw new UnsupportedOperationException("Default command has no left denotation"); - } - - @Override - public int leftBinding() { - return -1; - } -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java deleted file mode 100644 index e3b1d3c..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/DenestingCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - * - */ -public class DenestingCommand extends AbstractInitialCommand { - private final InitialCommand wrapped; - - /** - * Create a new transforming initial command. - * - * @param internal - * The initial command to delegate to. - */ - public DenestingCommand(final InitialCommand internal) { - wrapped = internal; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) - throws ParserException { - return wrapped.denote(operator, ctx).getChild(0); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java deleted file mode 100644 index f4288d0..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/GroupingCommand.java +++ /dev/null @@ -1,51 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParseBlock; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * A grouping operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class GroupingCommand extends AbstractInitialCommand { - private final ParseBlock innerBlock; - - private final Token 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(final ParseBlock inner, final Token marker) { - innerBlock = inner; - - mark = marker; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) - throws ParserException { - final ITree> opr = innerBlock.parse(ctx); - - return new Tree<>(mark, opr); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java new file mode 100644 index 0000000..57dc2d1 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java @@ -0,0 +1,40 @@ +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 + * The key type for the tokens. + * + * @param + * The value type for the tokens. + * + * @param + * The state type of the parser. + * + * + */ +@FunctionalInterface +public interface InitialCommand { + /** + * 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. + */ + ITree> denote(Token operator, ParserContext ctx) throws ParserException; +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java deleted file mode 100644 index b373a7c..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java +++ /dev/null @@ -1,171 +0,0 @@ -package bjc.pratt.commands; - -import static bjc.pratt.blocks.ParseBlocks.repeating; -import static bjc.pratt.blocks.ParseBlocks.simple; -import static bjc.pratt.blocks.ParseBlocks.trigger; - -import java.util.function.UnaryOperator; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; -import bjc.utils.data.ITree; - -/** - * * 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 InitialCommand 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 InitialCommand grouping(final int precedence, final K term, - final Token mark) { - final ParseBlock innerBlock = simple(precedence, term, null); - - return new GroupingCommand<>(innerBlock, mark); - } - - /** - * Create a new leaf operator. - * - * @return A command implementing the operator. - */ - public static InitialCommand leaf() { - return new LeafCommand<>(); - } - - /** - * 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 InitialCommand preTernary(final int cond1, final int block1, final int block2, - final K mark1, final K mark2, final Token term) { - final ParseBlock condBlock = simple(cond1, mark1, null); - final ParseBlock opblock1 = simple(block1, mark2, null); - final ParseBlock 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 InitialCommand constant(final ITree> 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 InitialCommand delimited(final int inner, final K delim, final K mark, - final Token term, final UnaryOperator onEnter, final UnaryOperator onDelim, - final UnaryOperator onExit, final boolean statement) { - final ParseBlock innerBlock = simple(inner, null, null); - final ParseBlock delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); - final ParseBlock scopedBlock = trigger(delimsBlock, onEnter, onExit); - - final GroupingCommand command = new GroupingCommand<>(scopedBlock, term); - - /* - * Remove the wrapper layer from grouping-command on top of - * RepeatingParseBlock. - */ - return denest(command); - } - - /** - * 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 InitialCommand denest(final InitialCommand 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/LeafCommand.java deleted file mode 100644 index 9b4480d..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/LeafCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class LeafCommand implements InitialCommand { - @Override - public ITree> denote(final Token operator, final ParserContext ctx) - throws ParserException { - return new Tree<>(operator); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java deleted file mode 100644 index adf98a1..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/LeftBinaryCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package bjc.pratt.commands; - -/** - * A left-associative operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class LeftBinaryCommand extends BinaryCommand { - /** - * Create a new left-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public LeftBinaryCommand(final int precedence) { - super(precedence); - } - - @Override - protected int rightBinding() { - return 1 + leftBinding(); - } -} \ No newline at end of file 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 { + InitialCommand getCommand(ParserContext 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 + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + */ +public interface MetaNonInitialCommand { + NonInitialCommand getCommand(ParserContext ctx); +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java deleted file mode 100644 index 54ab98b..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/NonBinaryCommand.java +++ /dev/null @@ -1,37 +0,0 @@ -package bjc.pratt.commands; - -/** - * A non-associative operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class NonBinaryCommand extends BinaryCommand { - /** - * Create a new non-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public NonBinaryCommand(final int precedence) { - super(precedence); - } - - @Override - protected int rightBinding() { - return 1 + leftBinding(); - } - - @Override - public int nextBinding() { - return leftBinding() - 1; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java new file mode 100644 index 0000000..d06e336 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java @@ -0,0 +1,65 @@ +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 + * The key type for the tokens. + * + * @param + * The value type for the tokens. + * + * @param + * The state type of the parser. + * + */ +public abstract class NonInitialCommand { + /** + * 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. + */ + public abstract ITree> denote(ITree> operand, Token operator, + ParserContext ctx) throws ParserException; + + /** + * 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() { + return leftBinding(); + } +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java deleted file mode 100644 index 39baf1f..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java +++ /dev/null @@ -1,141 +0,0 @@ -package bjc.pratt.commands; - -import java.util.Set; - -import bjc.pratt.NonInitialCommand; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; -import bjc.pratt.blocks.SimpleParseBlock; - -/** - * 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 NonInitialCommand 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 NonInitialCommand 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 NonInitialCommand 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 NonInitialCommand chain(final int precedence, final Set chainSet, - final Token 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 NonInitialCommand 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 NonInitialCommand postCircumfix(final int precedence, - final int insidePrecedence, final K closer, final Token marker) { - final ParseBlock 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 NonInitialCommand ternary(final int precedence, final int insidePrecedence, - final K closer, final Token marker, final boolean nonassoc) { - final ParseBlock 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/PostCircumfixCommand.java deleted file mode 100644 index e7d2eea..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/PostCircumfixCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParseBlock; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class PostCircumfixCommand extends BinaryPostCommand { - private final ParseBlock innerBlock; - - private final Token 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(final int precedence, final ParseBlock inner, final Token marker) { - super(precedence); - - if (inner == null) throw new NullPointerException("Inner block must not be null"); - - innerBlock = inner; - - mark = marker; - } - - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) throws ParserException { - final ITree> inside = innerBlock.parse(ctx); - - return new Tree<>(mark, operand, inside); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java deleted file mode 100644 index 3276d09..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/PostfixCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * A postfix operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class PostfixCommand extends BinaryPostCommand { - /** - * Create a new postfix operator. - * - * @param precedence - * The precedence of the operator. - */ - public PostfixCommand(final int precedence) { - super(precedence); - } - - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) throws ParserException { - return new Tree<>(operator, operand); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java deleted file mode 100644 index d8304e2..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java +++ /dev/null @@ -1,75 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParseBlock; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class PreTernaryCommand extends AbstractInitialCommand { - private final Token trm; - - private final ParseBlock condBlock; - - private final ParseBlock opblock1; - private final ParseBlock 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(final ParseBlock cond, final ParseBlock op1, - final ParseBlock op2, final Token term) { - super(); - - if (cond == null) - throw new NullPointerException("Cond block must not be null"); - else if (op1 == null) - throw new NullPointerException("Op block #1 must not be null"); - else if (op2 == null) throw new NullPointerException("Op block #2 must not be null"); - - condBlock = cond; - opblock1 = op1; - opblock2 = op2; - - trm = term; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) - throws ParserException { - final ITree> cond = condBlock.parse(ctx); - - final ITree> op1 = opblock1.parse(ctx); - - final ITree> op2 = opblock2.parse(ctx); - - 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/RightBinaryCommand.java deleted file mode 100644 index 35c828f..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/RightBinaryCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.pratt.commands; - -/** - * A right-associative binary operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * @param - * The value type of the tokens. - * @param - * The state type of the parser. - */ -public class RightBinaryCommand extends BinaryCommand { - /** - * Create a new right-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public RightBinaryCommand(final int precedence) { - super(precedence); - } - - @Override - protected int rightBinding() { - return leftBinding(); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java deleted file mode 100644 index 2e12050..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/TernaryCommand.java +++ /dev/null @@ -1,77 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParseBlock; -import bjc.pratt.ParserContext; -import bjc.pratt.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 - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class TernaryCommand extends BinaryPostCommand { - private final ParseBlock innerBlck; - - private final Token mark; - - 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(final int precedence, final ParseBlock innerBlock, final Token marker, - final boolean isNonassoc) { - super(precedence); - - if (innerBlock == null) - throw new NullPointerException("Inner block must not be null"); - else if (marker == null) throw new NullPointerException("Marker must not be null"); - - innerBlck = innerBlock; - mark = marker; - nonassoc = isNonassoc; - } - - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) throws ParserException { - final ITree> inner = innerBlck.parse(ctx); - - final ITree> 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; - - 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/TransformingInitialCommand.java deleted file mode 100644 index a706ea8..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/TransformingInitialCommand.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.pratt.commands; - -import java.util.function.UnaryOperator; - -import bjc.pratt.InitialCommand; -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; - -/** - * An initial command that transforms the result of another command. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class TransformingInitialCommand extends AbstractInitialCommand { - private final InitialCommand internl; - - private final UnaryOperator>> 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(final InitialCommand internal, - final UnaryOperator>> transform) { - super(); - internl = internal; - transfrm = transform; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) - throws ParserException { - return transfrm.apply(internl.denote(operator, ctx)); - } - -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java deleted file mode 100644 index d36dc24..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/UnaryCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.Token; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * A unary operator. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public class UnaryCommand extends AbstractInitialCommand { - private final int nullPwer; - - /** - * Create a new unary command. - * - * @param precedence - * The precedence of this operator. - */ - public UnaryCommand(final int precedence) { - if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); - - nullPwer = precedence; - } - - @Override - protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) - throws ParserException { - final ITree> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); - - return new Tree<>(operator, opr); - } -} \ No newline at end of file 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 + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + * + */ +public class BlockInitialCommand extends AbstractInitialCommand { + private final ParseBlock blck; + + /** + * Create a new block initial command. + * + * @param block + * The block to delegate to. + */ + public BlockInitialCommand(final ParseBlock block) { + blck = block; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + return blck.parse(ctx); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java new file mode 100644 index 0000000..2a9bf35 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java @@ -0,0 +1,75 @@ +package bjc.pratt.commands.impls; + +import java.util.Set; + +import bjc.pratt.ParserContext; +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; + +/** + * Create a new chained operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class ChainCommand extends BinaryPostCommand { + private final Set chainWith; + + private final Token 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(final int precedence, final Set chainSet, final Token chainMarker) { + super(precedence); + + chainWith = chainSet; + chain = chainMarker; + } + + @Override + public ITree> denote(final ITree> operand, final Token operator, + final ParserContext ctx) throws ParserException { + final ITree> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, + false); + + final ITree> res = new Tree<>(operator, operand, tree); + + if (chainWith.contains(ctx.tokens.current().getKey())) { + final Token tok = ctx.tokens.current(); + ctx.tokens.next(); + + final ITree> other = denote(tree, tok, + new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); + + return new Tree<>(chain, res, other); + } + + return res; + } + + @Override + public int nextBinding() { + return leftBinding() - 1; + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java new file mode 100644 index 0000000..16af5aa --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java @@ -0,0 +1,41 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class ConstantCommand implements InitialCommand { + private final ITree> val; + + /** + * Create a new constant. + * + * @param con + * The tree this constant represents. + */ + public ConstantCommand(final ITree> con) { + val = con; + } + + @Override + public ITree> denote(final Token operator, final ParserContext ctx) + throws ParserException { + return val; + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java new file mode 100644 index 0000000..16d2e59 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java @@ -0,0 +1,29 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the token. + * + * @param + * The value type of the token. + * + * @param + * The state type of the parser. + */ +public class DefaultInitialCommand implements InitialCommand { + @Override + public ITree> denote(final Token operator, final ParserContext ctx) + throws ParserException { + throw new ParserException("Unexpected token " + operator); + } +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java new file mode 100644 index 0000000..48aab29 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java @@ -0,0 +1,33 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class DefaultNonInitialCommand extends NonInitialCommand { + @Override + public ITree> denote(final ITree> operand, final Token operator, + final ParserContext ctx) { + throw new UnsupportedOperationException("Default command has no left denotation"); + } + + @Override + public int leftBinding() { + return -1; + } +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java new file mode 100644 index 0000000..220e4cc --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java @@ -0,0 +1,46 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + * + */ +public class DenestingCommand extends AbstractInitialCommand { + private final InitialCommand wrapped; + + /** + * Create a new transforming initial command. + * + * @param internal + * The initial command to delegate to. + */ + public DenestingCommand(final InitialCommand internal) { + wrapped = internal; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + return wrapped.denote(operator, ctx).getChild(0); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java new file mode 100644 index 0000000..28f1299 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java @@ -0,0 +1,52 @@ +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.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A grouping operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class GroupingCommand extends AbstractInitialCommand { + private final ParseBlock innerBlock; + + private final Token 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(final ParseBlock inner, final Token marker) { + innerBlock = inner; + + mark = marker; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + final ITree> opr = innerBlock.parse(ctx); + + return new Tree<>(mark, opr); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java new file mode 100644 index 0000000..8da758a --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java @@ -0,0 +1,171 @@ +package bjc.pratt.commands.impls; + +import static bjc.pratt.blocks.ParseBlocks.repeating; +import static bjc.pratt.blocks.ParseBlocks.simple; +import static bjc.pratt.blocks.ParseBlocks.trigger; + +import java.util.function.UnaryOperator; + +import bjc.pratt.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 InitialCommand 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 InitialCommand grouping(final int precedence, final K term, + final Token mark) { + final ParseBlock innerBlock = simple(precedence, term, null); + + return new GroupingCommand<>(innerBlock, mark); + } + + /** + * Create a new leaf operator. + * + * @return A command implementing the operator. + */ + public static InitialCommand leaf() { + return new LeafCommand<>(); + } + + /** + * 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 InitialCommand preTernary(final int cond1, final int block1, final int block2, + final K mark1, final K mark2, final Token term) { + final ParseBlock condBlock = simple(cond1, mark1, null); + final ParseBlock opblock1 = simple(block1, mark2, null); + final ParseBlock 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 InitialCommand constant(final ITree> 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 InitialCommand delimited(final int inner, final K delim, final K mark, + final Token term, final UnaryOperator onEnter, final UnaryOperator onDelim, + final UnaryOperator onExit, final boolean statement) { + final ParseBlock innerBlock = simple(inner, null, null); + final ParseBlock delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); + final ParseBlock scopedBlock = trigger(delimsBlock, onEnter, onExit); + + final GroupingCommand command = new GroupingCommand<>(scopedBlock, term); + + /* + * Remove the wrapper layer from grouping-command on top of + * RepeatingParseBlock. + */ + return denest(command); + } + + /** + * 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 InitialCommand denest(final InitialCommand comm) { + return new DenestingCommand<>(comm); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java new file mode 100644 index 0000000..bb999f6 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java @@ -0,0 +1,30 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class LeafCommand implements InitialCommand { + @Override + public ITree> denote(final Token operator, final ParserContext ctx) + throws ParserException { + return new Tree<>(operator); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java new file mode 100644 index 0000000..9727dcb --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java @@ -0,0 +1,34 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; + +/** + * A left-associative operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class LeftBinaryCommand extends BinaryCommand { + /** + * Create a new left-associative operator. + * + * @param precedence + * The precedence of the operator. + */ + public LeftBinaryCommand(final int precedence) { + super(precedence); + } + + @Override + protected int rightBinding() { + return 1 + leftBinding(); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java new file mode 100644 index 0000000..2d92780 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java @@ -0,0 +1,39 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; + +/** + * A non-associative operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class NonBinaryCommand extends BinaryCommand { + /** + * Create a new non-associative operator. + * + * @param precedence + * The precedence of the operator. + */ + public NonBinaryCommand(final int precedence) { + super(precedence); + } + + @Override + protected int rightBinding() { + return 1 + leftBinding(); + } + + @Override + public int nextBinding() { + return leftBinding() - 1; + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java new file mode 100644 index 0000000..a2c158c --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java @@ -0,0 +1,141 @@ +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 NonInitialCommand 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 NonInitialCommand 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 NonInitialCommand 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 NonInitialCommand chain(final int precedence, final Set chainSet, + final Token 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 NonInitialCommand 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 NonInitialCommand postCircumfix(final int precedence, + final int insidePrecedence, final K closer, final Token marker) { + final ParseBlock 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 NonInitialCommand ternary(final int precedence, final int insidePrecedence, + final K closer, final Token marker, final boolean nonassoc) { + final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); + + return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); + } +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java new file mode 100644 index 0000000..c1d70b8 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java @@ -0,0 +1,59 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class PostCircumfixCommand extends BinaryPostCommand { + private final ParseBlock innerBlock; + + private final Token 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(final int precedence, final ParseBlock inner, final Token marker) { + super(precedence); + + if (inner == null) throw new NullPointerException("Inner block must not be null"); + + innerBlock = inner; + + mark = marker; + } + + @Override + public ITree> denote(final ITree> operand, final Token operator, + final ParserContext ctx) throws ParserException { + final ITree> inside = innerBlock.parse(ctx); + + return new Tree<>(mark, operand, inside); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java new file mode 100644 index 0000000..00c7ad2 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java @@ -0,0 +1,40 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class PostfixCommand extends BinaryPostCommand { + /** + * Create a new postfix operator. + * + * @param precedence + * The precedence of the operator. + */ + public PostfixCommand(final int precedence) { + super(precedence); + } + + @Override + public ITree> denote(final ITree> operand, final Token operator, + final ParserContext ctx) throws ParserException { + return new Tree<>(operator, operand); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java new file mode 100644 index 0000000..fa63c9c --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java @@ -0,0 +1,76 @@ +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.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A prefix ternary operator, like an if/then/else group. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class PreTernaryCommand extends AbstractInitialCommand { + private final Token trm; + + private final ParseBlock condBlock; + + private final ParseBlock opblock1; + private final ParseBlock 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(final ParseBlock cond, final ParseBlock op1, + final ParseBlock op2, final Token term) { + super(); + + if (cond == null) + throw new NullPointerException("Cond block must not be null"); + else if (op1 == null) + throw new NullPointerException("Op block #1 must not be null"); + else if (op2 == null) throw new NullPointerException("Op block #2 must not be null"); + + condBlock = cond; + opblock1 = op1; + opblock2 = op2; + + trm = term; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + final ITree> cond = condBlock.parse(ctx); + + final ITree> op1 = opblock1.parse(ctx); + + final ITree> op2 = opblock2.parse(ctx); + + return new Tree<>(trm, cond, op1, op2); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java new file mode 100644 index 0000000..c3887ee --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java @@ -0,0 +1,32 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.commands.BinaryCommand; + +/** + * A right-associative binary operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * @param + * The value type of the tokens. + * @param + * The state type of the parser. + */ +public class RightBinaryCommand extends BinaryCommand { + /** + * Create a new right-associative operator. + * + * @param precedence + * The precedence of the operator. + */ + public RightBinaryCommand(final int precedence) { + super(precedence); + } + + @Override + protected int rightBinding() { + return leftBinding(); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java new file mode 100644 index 0000000..92355c0 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java @@ -0,0 +1,78 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class TernaryCommand extends BinaryPostCommand { + private final ParseBlock innerBlck; + + private final Token mark; + + 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(final int precedence, final ParseBlock innerBlock, final Token marker, + final boolean isNonassoc) { + super(precedence); + + if (innerBlock == null) + throw new NullPointerException("Inner block must not be null"); + else if (marker == null) throw new NullPointerException("Marker must not be null"); + + innerBlck = innerBlock; + mark = marker; + nonassoc = isNonassoc; + } + + @Override + public ITree> denote(final ITree> operand, final Token operator, + final ParserContext ctx) throws ParserException { + final ITree> inner = innerBlck.parse(ctx); + + final ITree> 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; + + return leftBinding(); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java new file mode 100644 index 0000000..e6c509c --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java @@ -0,0 +1,53 @@ +package bjc.pratt.commands.impls; + +import java.util.function.UnaryOperator; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class TransformingInitialCommand extends AbstractInitialCommand { + private final InitialCommand internl; + + private final UnaryOperator>> 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(final InitialCommand internal, + final UnaryOperator>> transform) { + super(); + internl = internal; + transfrm = transform; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + return transfrm.apply(internl.denote(operator, ctx)); + } + +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java new file mode 100644 index 0000000..156dee0 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java @@ -0,0 +1,46 @@ +package bjc.pratt.commands.impls; + +import bjc.pratt.ParserContext; +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 + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class UnaryCommand extends AbstractInitialCommand { + private final int nullPwer; + + /** + * Create a new unary command. + * + * @param precedence + * The precedence of this operator. + */ + public UnaryCommand(final int precedence) { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); + + nullPwer = precedence; + } + + @Override + protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + final ITree> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); + + return new Tree<>(operator, opr); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java index 6d3c1a5..ff47667 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -1,7 +1,5 @@ package bjc.pratt.tokens; -import bjc.pratt.Token; - /** * Simple token implementation for strings. * diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java index b33dae8..07e1827 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java @@ -4,9 +4,6 @@ import static bjc.pratt.tokens.StringToken.litToken; import java.util.Iterator; -import bjc.pratt.Token; -import bjc.pratt.TokenStream; - /** * Simple implementation of token stream for strings. * diff --git a/JPratt/src/main/java/bjc/pratt/tokens/Token.java b/JPratt/src/main/java/bjc/pratt/tokens/Token.java new file mode 100644 index 0000000..b07d2e1 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/tokens/Token.java @@ -0,0 +1,30 @@ +package bjc.pratt.tokens; + +/** + * Represents a simple parsing token. + * + * @author EVE + * + * @param + * The key type of this token. Represents the type of the token. + * + * @param + * The value type of this token. Represents any additional data + * for the token. + * + */ +public interface Token { + /** + * 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/tokens/TokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java new file mode 100644 index 0000000..a5febcc --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java @@ -0,0 +1,104 @@ +package bjc.pratt.tokens; + +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 + * The key type of the token. + * + * @param + * The value type of the token. + */ +public abstract class TokenStream implements Iterator> { + /** + * The exception thrown when an expectation fails. + * + * @author EVE + * + */ + public static class ExpectationException extends ParserException { + private static final long serialVersionUID = 4299299480127680805L; + + /** + * Create a new exception with the specified message. + * + * @param msg + * The message of the exception. + */ + public ExpectationException(final String msg) { + super(msg); + } + } + + /** + * Get the current token. + * + * @return The current token. + */ + public abstract Token current(); + + @Override + public abstract Token next(); + + @Override + public abstract boolean hasNext(); + + /** + * 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(final Set expectedKeys) throws ExpectationException { + final K curKey = current().getKey(); + + if (!expectedKeys.contains(curKey)) { + final String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false); + + throw new ExpectationException("One of '" + expectedList + "' was expected, not " + curKey); + } + + 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(final K... expectedKeys) throws ExpectationException { + expect(new HashSet<>(Arrays.asList(expectedKeys))); + } + + /** + * Check whether the head token is a certain type. + * + * @param val + * The type to check for. + * + * @return Whether or not the head token is of that type. + */ + public boolean headIs(final K val) { + return current().getKey().equals(val); + } +} -- cgit v1.2.3