diff options
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/commands')
29 files changed, 0 insertions, 1740 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java deleted file mode 100644 index 3e4f3a9..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/AbstractInitialCommand.java +++ /dev/null @@ -1,44 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * Abstract base for initial commands. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public abstract class AbstractInitialCommand<K, V, C> implements InitialCommand<K, V, C> { - @Override - public CommandResult<K, V> denote(final Token<K, V> operator, - final ParserContext<K, V, C> ctx) throws ParserException { - return intNullDenotation(operator, ctx); - } - - /** - * Internal null denotation method. - * - * @param operator - * The operator that was parsed. - * @param ctx - * The parser context at this point. - * - * @return The tree that this method parsed. - * - * @throws ParserException - * If something went wrong while parsing. - */ - protected abstract CommandResult<K, V> intNullDenotation(Token<K, V> operator, - ParserContext<K, V, C> ctx) throws ParserException; - -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java deleted file mode 100644 index 7a65052..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryCommand.java +++ /dev/null @@ -1,53 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A binary operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public abstract class BinaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - /** - * Create a new binary operator with the specified precedence. - * - * @param precedence - * The precedence of the operator. - */ - public BinaryCommand(final int precedence) { - super(precedence); - } - - /** - * The right-binding power (right-precedence) of this command. - * - * @return The right binding power of this command. - */ - protected abstract int rightBinding(); - - @Override - public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, - final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - final CommandResult<K,V> opr - = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false); - - if (opr.status != Status.SUCCESS) return opr; - - return CommandResult.success(new SimpleTree<>(operator, operand, opr.success())); - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java deleted file mode 100644 index 1fa1ba3..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/BinaryPostCommand.java +++ /dev/null @@ -1,36 +0,0 @@ -package bjc.pratt.commands; - -/** - * A operator with fixed precedence. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public abstract class BinaryPostCommand<K, V, C> extends NonInitialCommand<K, V, C> { - private final int leftPower; - - /** - * Create a new operator with fixed precedence. - * - * @param precedence - * The precedence of the operator. - */ - public BinaryPostCommand(final int precedence) { - if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); - - leftPower = precedence; - } - - @Override - public int leftBinding() { - return leftPower; - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java deleted file mode 100644 index 3f3093e..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -package bjc.pratt.commands; - -import java.util.Map; - -import bjc.pratt.ParserContext; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a initial command that has a number of 'sub-commands' in the way that Go/Git CLI does. - * - * @author bjcul - * - * @param <K> Token key type - * @param <V> Token value type - * @param <C> Parser context type - */ -public class BranchInitialCommand<K, V, C> implements InitialCommand<K, V, C> { - private Map<K, InitialCommand<K, V, C>> comMap; - - /** - * Create a new branch initial command - * - * @param mep The map containing the commands - */ - public BranchInitialCommand(Map<K, InitialCommand<K, V, C>> mep) { - this.comMap = mep; - } - - @Override - public CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { - Token<K, V> curToken = ctx.tokens.current(); - ctx.tokens.expect(comMap.keySet()); - - return comMap.get(curToken.getKey()).denote(curToken, ctx); - } - -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java b/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java deleted file mode 100644 index 38a55ae..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java +++ /dev/null @@ -1,112 +0,0 @@ -package bjc.pratt.commands; - -import bjc.data.Tree; -import bjc.pratt.tokens.Token; - -/** - * Represents the result of executing a command. - * - * @author bjcul - * - * @param <K> The key type of the tokens - * @param <V> The value type of the tokens - */ -public class CommandResult<K, V> { - /** - * Represents the status of a command execution - * - * @author bjcul - * - */ - public static enum Status { - /** - * The command successfully parsed. - */ - SUCCESS, - /** - * The command failed, in a non-recoverable way - */ - FAIL, - /** - * The command failed. Attempt recovery via backtracking - */ - BACKTRACK - } - - /** - * The status of this command. - */ - public final Status status; - - private Tree<Token<K, V>> success; - - private CommandResult(Status status) { - this.status = status; - } - - /** - * Get the success value of this command, or null if it failed. - * - * @return The success value of the command - */ - public Tree<Token<K, V>> success() { - return success; - } - - /** - * Create a success result - * - * @param <K2> The key type of the token - * @param <V2> The value type of the token - * - * @param succ The tree produced by the command - * - * @return A command result representing a success - */ - public static <K2, V2> CommandResult<K2, V2> success(Tree<Token<K2, V2>> succ) { - CommandResult<K2, V2> result = new CommandResult<>(Status.SUCCESS); - result.success = succ; - return result; - } - - /** - * Create a non-backtracking failure result. - * - * @param <K2> The key type of the token - * @param <V2> The value type of the token - * - * @return A command result representing a non-backtracking fail - */ - public static <K2, V2> CommandResult<K2, V2> fail() { - CommandResult<K2, V2> result = new CommandResult<>(Status.FAIL); - return result; - } - - /** - * Create a backtracking failure result. - * - * @param <K2> The key type of the token - * @param <V2> The value type of the token - * - * @return A command result representing a backtracking fail - */ - public static <K2, V2> CommandResult<K2, V2> backtrack() { - CommandResult<K2, V2> result = new CommandResult<>(Status.BACKTRACK); - return result; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("CommandResult [status="); - builder.append(status); - if (status == Status.SUCCESS) { - builder.append(", success="); - builder.append(success); - } - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java deleted file mode 100644 index 0cfa290..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * Represents an initial command in parsing. - * - * @author EVE - * - * @param <K> - * The key type for the tokens. - * - * @param <V> - * The value type for the tokens. - * - * @param <C> - * The state type of the parser. - * - * - */ -@FunctionalInterface -public interface InitialCommand<K, V, C> { - /** - * Construct the null denotation of this command. - * - * @param operator - * The operator for this command. - * @param ctx - * The context for the command. - * - * @return The result of executing the command. - * - * @throws ParserException - * If something goes wrong during parsing. - */ - CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException; -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java deleted file mode 100644 index d209177..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -package bjc.pratt.commands;
-
-import bjc.pratt.ParserContext;
-
-/**
- * A 'meta-command' that yields the actual command to use.
- *
- * @author bjculkin
- *
- * @param <K>
- * The key type of the context.
- * @param <V>
- * The value type of the context.
- * @param <C>
- * The storage type of the context.
- *
- */
-public interface MetaInitialCommand<K, V, C> {
- /**
- * Get the command to use.
- *
- * @param ctx
- * The current parser context.
- * @return The command to use.
- */
- InitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx);
-}
diff --git a/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java deleted file mode 100644 index 956dcec..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; - -/** - * A 'meta-command' for non-initial commands. - * - * @author bjculkin - * - * @param <K> - * The token key type. - * - * @param <V> - * The token value type. - * - * @param <C> - * The parser state type. - */ -public interface MetaNonInitialCommand<K, V, C> { - /** - * Get the command to use. - * - * @param ctx - * The context to use. - * @return The command to use. - */ - NonInitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx); -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java deleted file mode 100644 index 02826a9..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -package bjc.pratt.commands; - -import bjc.pratt.ParserContext; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a non-initial command in parsing. - * - * @author EVE - * - * @param <K> - * The key type for the tokens. - * - * @param <V> - * The value type for the tokens. - * - * @param <C> - * The state type of the parser. - * - */ -public abstract class NonInitialCommand<K, V, C> { - /** - * Construct the left denotation of this command. - * - * @param operand - * The left-hand operand of this command. - * @param operator - * The operator for this command. - * - * @param ctx - * The state needed for commands. - * - * @return The result of executing the command. - * - * @throws ParserException - * If something went wrong during parsing. - */ - public abstract CommandResult<K, V> denote(Tree<Token<K, V>> operand, Token<K, V> operator, - ParserContext<K, V, C> 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/impls/BlockInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/BlockInitialCommand.java deleted file mode 100644 index 07d9a76..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/BlockInitialCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * An initial command that delegates all the work to a {@link ParseBlock} - * - * @author bjculkin - * @param <K> - * The token key type. - * - * @param <V> - * The token value type. - * - * @param <C> - * The parser state type. - * - */ -public class BlockInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private final ParseBlock<K, V, C> blck; - - /** - * Create a new block initial command. - * - * @param block - * The block to delegate to. - */ - public BlockInitialCommand(final ParseBlock<K, V, C> block) { - blck = block; - } - - @Override - protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - return blck.parse(ctx); - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java deleted file mode 100644 index 7311eb9..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java +++ /dev/null @@ -1,82 +0,0 @@ -package bjc.pratt.commands.impls; - -import java.util.Set; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.BinaryPostCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * Create a new chained operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private final Set<K> chainWith; - - private final Token<K, V> chain; - - /** - * Create a new chained operator. - * - * @param precedence - * The precedence of this operator. - * - * @param chainSet - * The operators to chain with. - * - * @param chainMarker - * The token to use as the node in the AST. - */ - public ChainCommand(final int precedence, final Set<K> chainSet, final Token<K, V> chainMarker) { - super(precedence); - - chainWith = chainSet; - chain = chainMarker; - } - - @Override - public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator, - final ParserContext<K, V, C> ctx) throws ParserException { - CommandResult<K, V> resOuter = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, - false); - if (resOuter.status != Status.SUCCESS) return resOuter; - final Tree<Token<K, V>> tree = resOuter.success(); - - final Tree<Token<K, V>> res = new SimpleTree<>(operator, operand, tree); - - if(chainWith.contains(ctx.tokens.current().getKey())) { - final Token<K, V> tok = ctx.tokens.current(); - ctx.tokens.next(); - - CommandResult<K, V> resOther = denote(tree, tok, - new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); - if (resOther.status != Status.SUCCESS) return resOther; - - final Tree<Token<K, V>> other = resOther.success(); - - return CommandResult.success(new SimpleTree<>(chain, res, other)); - } - - return CommandResult.success(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 deleted file mode 100644 index 657743c..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/ConstantCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * A command that represents a specific tree. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class ConstantCommand<K, V, C> implements InitialCommand<K, V, C> { - private final Tree<Token<K, V>> val; - - /** - * Create a new constant. - * - * @param con - * The tree this constant represents. - */ - public ConstantCommand(final Tree<Token<K, V>> con) { - val = con; - } - - @Override - public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - return CommandResult.success(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 deleted file mode 100644 index 8d28b57..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultInitialCommand.java +++ /dev/null @@ -1,29 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * Default implementation of an initial command. - * - * @author EVE - * - * @param <K> - * The key type of the token. - * - * @param <V> - * The value type of the token. - * - * @param <C> - * The state type of the parser. - */ -public class DefaultInitialCommand<K, V, C> implements InitialCommand<K, V, C> { - @Override - public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - throw new ParserException("Unexpected token " + operator); - } -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java deleted file mode 100644 index 2ae9fb7..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/DefaultNonInitialCommand.java +++ /dev/null @@ -1,34 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.NonInitialCommand; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; - -/** - * Default implementation of a non-initial command. - * - * @author EVE - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class DefaultNonInitialCommand<K, V, C> extends NonInitialCommand<K, V, C> { - @Override - public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator, - final ParserContext<K, V, C> 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 deleted file mode 100644 index de39e0b..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/DenestingCommand.java +++ /dev/null @@ -1,49 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * A command that denests a input tree. - * - * Useful for processing the result of passing a complex parse group to a - * command. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - * - */ -public class DenestingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private final InitialCommand<K, V, C> wrapped; - - /** - * Create a new transforming initial command. - * - * @param internal - * The initial command to delegate to. - */ - public DenestingCommand(final InitialCommand<K, V, C> internal) { - wrapped = internal; - } - - @Override - protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - CommandResult<K, V> res = wrapped.denote(operator, ctx); - if (res.status != Status.SUCCESS) return res; - return CommandResult.success(res.success().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 deleted file mode 100644 index 44aa2c1..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/GroupingCommand.java +++ /dev/null @@ -1,53 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A grouping operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class GroupingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private final ParseBlock<K, V, C> innerBlock; - - private final Token<K, V> mark; - - /** - * Create a new grouping command. - * - * @param inner - * The inner block. - * - * @param marker - * The token to use as the node in the AST. - */ - public GroupingCommand(final ParseBlock<K, V, C> inner, final Token<K, V> marker) { - innerBlock = inner; - - mark = marker; - } - - @Override - protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - final CommandResult<K,V> resOpr = innerBlock.parse(ctx); - Tree<Token<K, V>> opr = resOpr.success(); - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index 6af6954..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java +++ /dev/null @@ -1,241 +0,0 @@ -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.*; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.functypes.MapBuilder; - -/** - * * Contains factory methods for producing common implementations of - * {@link InitialCommand} - * - * @author EVE - * - */ -public class InitialCommands { - /** - * Create a new unary operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> InitialCommand<K, V, C> unary(final int precedence) { - return new UnaryCommand<>(precedence); - } - - /** - * Create a new grouping operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the expression in the operator. - * - * @param term - * The type that closes the group. - * - * @param mark - * The token for the AST node of the group. - * - * @return A command implementing the operator. - */ - public static <K, V, C> InitialCommand<K, V, C> grouping(final int precedence, final K term, - final Token<K, V> mark) { - final ParseBlock<K, V, C> innerBlock = simple(precedence, term, null); - - return new GroupingCommand<>(innerBlock, mark); - } - - /** - * Create a new leaf operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @return A command implementing the operator. - */ - public static <K, V, C> InitialCommand<K, V, C> leaf() { - return new LeafCommand<>(); - } - - /** - * Create a new pre-ternary operator, like an if-then-else statement. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param cond1 - * The priority of the first block. - * - * @param block1 - * The priority of the second block. - * - * @param block2 - * The priority of the third block. - * - * @param mark1 - * The marker that ends the first block. - * - * @param mark2 - * The marker that ends the second block. - * - * @param term - * The token for the AST node of the group. - * - * @return A command implementing the operator. - */ - public static <K, V, C> InitialCommand<K, V, C> preTernary(final int cond1, final int block1, final int block2, - final K mark1, final K mark2, final Token<K, V> term) { - final ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null); - final ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null); - final ParseBlock<K, V, C> opblock2 = simple(block2, null, null); - - return new PreTernaryCommand<>(condBlock, opblock1, opblock2, term); - } - - /** - * Create a new named constant. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param val - * The value of the constant. - * - * @return A command implementing the constant. - */ - public static <K, V, C> InitialCommand<K, V, C> constant(final Tree<Token<K, V>> val) { - return new ConstantCommand<>(val); - } - - /** - * Create a new delimited command. This is for block-like constructs. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param inner - * The precedence of the inner blocks. - * - * @param delim - * The marker between sub-blocks. - * - * @param mark - * The block terminator. - * - * @param term - * The token for the AST node of the group. - * - * @param onEnter - * The function to apply to the state on entering the block. - * - * @param onDelim - * The function to apply to the state on finishing a sub-block. - * - * @param onExit - * The function to apply to the state on exiting the block. - * - * @param statement - * Whether or not the sub-blocks are statements or expressions. - * - * @return A command implementing the operator. - */ - public static <K, V, C> InitialCommand<K, V, C> delimited(final int inner, final K delim, final K mark, - final Token<K, V> term, final UnaryOperator<C> onEnter, final UnaryOperator<C> onDelim, - final UnaryOperator<C> onExit, final boolean statement) { - final ParseBlock<K, V, C> innerBlock = simple(inner, null, null); - final ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); - final ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit); - - final GroupingCommand<K, V, C> 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 <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param comm - * The command to denest. - * - * @return A command that denests the result of the provided command. - */ - public static <K, V, C> InitialCommand<K, V, C> denest(final InitialCommand<K, V, C> comm) { - return new DenestingCommand<>(comm); - } - - /** - * Create a new 'panfix' command. - * - * Form is <term> <expr> <term> <expr> <term> - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence The precedence for this operator - * @param term The indicator for the operator - * @param marker The token to mark this tree - * - * @return A command that implements a panfix operator - */ - public static <K, V, C> InitialCommand<K, V, C> panfix(final int precedence, final K term, final Token<K, V> marker) { - return new PanfixCommand<>(marker, term, precedence); - } - - /** - * Create a command that unconditionally returns a failure result. - * - * @param <K> Token key type - * @param <V> Token value type - * @param <C> Context type - * - * @return A command that unconditionally fails - */ - public static <K, V, C> InitialCommand<K, V, C> fail() { - return (operator, ctx) -> CommandResult.fail(); - } - - /** - * Create a new builder for branching/sub-command style commands. - * - * @param <K> Token key type - * @param <V> Value key type - * @param <C> Context type - * - * @return A builder for branching/sub-command style commands - */ - public static <K, V, C> MapBuilder<K, InitialCommand<K, V, C>, InitialCommand<K, V, C>> branch() { - return MapBuilder.from(BranchInitialCommand::new); - } -}
\ 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 deleted file mode 100644 index 20fb3d4..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/LeafCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.tokens.Token; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A operator that stands for itself. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class LeafCommand<K, V, C> implements InitialCommand<K, V, C> { - @Override - public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index 9a72859..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/LeftBinaryCommand.java +++ /dev/null @@ -1,34 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.commands.BinaryCommand; - -/** - * A left-associative operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class LeftBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { - /** - * Create a new left-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public LeftBinaryCommand(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 deleted file mode 100644 index d303663..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/NonBinaryCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.commands.BinaryCommand; - -/** - * A non-associative operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class NonBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { - /** - * Create a new non-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public NonBinaryCommand(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 deleted file mode 100644 index 6019ffe..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/NonInitialCommands.java +++ /dev/null @@ -1,167 +0,0 @@ -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 <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> infixLeft(final int precedence) { - return new LeftBinaryCommand<>(precedence); - } - - /** - * Create a right-associative infix operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> infixRight(final int precedence) { - return new RightBinaryCommand<>(precedence); - } - - /** - * Create a non-associative infix operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> infixNon(final int precedence) { - return new NonBinaryCommand<>(precedence); - } - - /** - * Create a chained operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @param chainSet - * The operators it forms a chain with. - * - * @param marker - * The token to use as the AST node for the chained operators. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> chain(final int precedence, final Set<K> chainSet, - final Token<K, V> marker) { - return new ChainCommand<>(precedence, chainSet, marker); - } - - /** - * Create a postfix operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> postfix(final int precedence) { - return new PostfixCommand<>(precedence); - } - - /** - * Create a post-circumfix operator. - * - * This is an operator in form similar to array indexing. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of this operator - * - * @param insidePrecedence - * The precedence of the expression inside the operator - * - * @param closer - * The token that closes the circumfix. - * - * @param marker - * The token to use as the AST node for the operator. - * - * @return A command implementing that operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> postCircumfix(final int precedence, - final int insidePrecedence, final K closer, final Token<K, V> marker) { - final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); - - return new PostCircumfixCommand<>(precedence, innerBlock, marker); - } - - /** - * Create a ternary operator. - * - * This is like C's ?: operator. - * - * @param <K> The key type for the tokens. - * @param <V> The value type for the tokens. - * @param <C> The context type for the tokens. - * - * @param precedence - * The precedence of the operator. - * - * @param insidePrecedence - * The precedence of the inner section of the operator. - * - * @param closer - * The token that marks the end of the inner section. - * - * @param marker - * The token to use as the AST node for the operator. - * - * @param nonassoc - * True if the command is non-associative, false otherwise. - * - * @return A command implementing this operator. - */ - public static <K, V, C> NonInitialCommand<K, V, C> ternary(final int precedence, final int insidePrecedence, - final K closer, final Token<K, V> marker, final boolean nonassoc) { - final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); - - return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); - } -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/PanfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/PanfixCommand.java deleted file mode 100644 index 04ad370..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/PanfixCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.data.SimpleTree; -import bjc.data.Tree; -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a 'panfix' command, one where the operator is repeated prefix, infix and postfix. - * @author bjcul - * - * @param <K> The key type of the token - * @param <V> The value type of the token - * @param <C> The context type of the parser - */ -public final class PanfixCommand<K, V, C> implements InitialCommand<K, V, C> { - private final Token<K, V> marker; - private final K term; - private final int precedence; - - /** - * Create a new panfix command. - * - * @param marker The marker token. - * @param term The value to use as the root of the result-tree - * @param precedence The precedence for this command - */ - public PanfixCommand(Token<K, V> marker, K term, int precedence) { - this.marker = marker; - this.term = term; - this.precedence = precedence; - } - - @Override - public CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { - CommandResult<K,V> resLeftSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); - if (resLeftSide.status != Status.SUCCESS) return resLeftSide; - Tree<Token<K, V>> leftSide = resLeftSide.success(); - ctx.tokens.expect(term); - ctx.tokens.next(); - - CommandResult<K, V> resRightSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); - if (resLeftSide.status != Status.SUCCESS) return resRightSide; - Tree<Token<K,V>> rightSide = resRightSide.success(); - ctx.tokens.expect(term); - ctx.tokens.next(); - - return CommandResult.success(new SimpleTree<>(marker, leftSide, rightSide)); - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java deleted file mode 100644 index ec2c8fb..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/PostCircumfixCommand.java +++ /dev/null @@ -1,62 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.BinaryPostCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A post-circumfix operator, like array indexing. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class PostCircumfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private final ParseBlock<K, V, C> innerBlock; - - private final Token<K, V> mark; - - /** - * Create a new post-circumfix operator. - * - * @param precedence - * The precedence of the operator. - * - * @param inner - * The block inside the expression. - * - * @param marker - * The token to use as the node for the AST. - */ - public PostCircumfixCommand(final int precedence, final ParseBlock<K, V, C> inner, final Token<K, V> marker) { - super(precedence); - - if(inner == null) throw new NullPointerException("Inner block must not be null"); - - innerBlock = inner; - - mark = marker; - } - - @Override - public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator, - final ParserContext<K, V, C> ctx) throws ParserException { - final CommandResult<K,V> insideRes = innerBlock.parse(ctx); - if (insideRes.status != Status.SUCCESS) return insideRes; - Tree<Token<K, V>> inside = insideRes.success(); - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index ff370d0..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/PostfixCommand.java +++ /dev/null @@ -1,41 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.BinaryPostCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A postfix operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class PostfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - /** - * Create a new postfix operator. - * - * @param precedence - * The precedence of the operator. - */ - public PostfixCommand(final int precedence) { - super(precedence); - } - - @Override - public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator, - final ParserContext<K, V, C> ctx) throws ParserException { - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index 5d5cbe1..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/PreTernaryCommand.java +++ /dev/null @@ -1,83 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A prefix ternary operator, like an if/then/else group. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private final Token<K, V> trm; - - private final ParseBlock<K, V, C> condBlock; - - private final ParseBlock<K, V, C> opblock1; - private final ParseBlock<K, V, C> opblock2; - - /** - * Create a new ternary statement. - * - * @param cond - * The block for handling the condition. - * - * @param op1 - * The block for handling the first operator. - * - * @param op2 - * The block for handling the second operator. - * - * @param term - * The token to use as the node for the AST. - */ - public PreTernaryCommand(final ParseBlock<K, V, C> cond, final ParseBlock<K, V, C> op1, - final ParseBlock<K, V, C> op2, final Token<K, V> term) { - super(); - - if(cond == null) - 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 CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - final CommandResult<K,V> condRes = condBlock.parse(ctx); - if (condRes.status != Status.SUCCESS) return condRes; - Tree<Token<K, V>> cond = condRes.success(); - - final CommandResult<K,V> op1Res = opblock1.parse(ctx); - if (op1Res.status != Status.SUCCESS) return op1Res; - Tree<Token<K, V>> op1 = op1Res.success(); - - final CommandResult<K,V> op2Res = opblock2.parse(ctx); - if (op2Res.status != Status.SUCCESS) return op2Res; - Tree<Token<K, V>> op2 = op2Res.success(); - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index 4439930..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/RightBinaryCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.commands.BinaryCommand; - -/** - * A right-associative binary operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * @param <V> - * The value type of the tokens. - * @param <C> - * The state type of the parser. - */ -public class RightBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> { - /** - * Create a new right-associative operator. - * - * @param precedence - * The precedence of the operator. - */ - public RightBinaryCommand(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 deleted file mode 100644 index 786dfec..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/TernaryCommand.java +++ /dev/null @@ -1,76 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.BinaryPostCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A ternary command, like C's ?: - * - * @author bjculkin - * - * @param <K> The key type of the tokens. - * - * @param <V> The value type of the tokens. - * - * @param <C> The state type of the parser. - */ -public class TernaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> { - private final ParseBlock<K, V, C> innerBlck; - - private final Token<K, V> 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<K, V, C> innerBlock, final Token<K, V> 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 CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator, - final ParserContext<K, V, C> ctx) throws ParserException { - final CommandResult<K, V> innerRes = innerBlck.parse(ctx); - if (innerRes.status != Status.SUCCESS) return innerRes; - Tree<Token<K, V>> inner = innerRes.success(); - - final CommandResult<K,V> outerRes = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false); - if (outerRes.status != Status.SUCCESS) return outerRes; - Tree<Token<K, V>> outer = outerRes.success(); - return CommandResult.success(new SimpleTree<>(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 deleted file mode 100644 index 36f881d..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/TransformingInitialCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -package bjc.pratt.commands.impls; - -import java.util.function.UnaryOperator; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.commands.InitialCommand; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * An initial command that transforms the result of another command. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class TransformingInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private final InitialCommand<K, V, C> internl; - - private final UnaryOperator<Tree<Token<K, V>>> transfrm; - - /** - * Create a new transforming initial command. - * - * @param internal - * The initial command to delegate to. - * - * @param transform - * The transform to apply to the returned tree. - */ - public TransformingInitialCommand(final InitialCommand<K, V, C> internal, - final UnaryOperator<Tree<Token<K, V>>> transform) { - super(); - internl = internal; - transfrm = transform; - } - - @Override - protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - CommandResult<K,V> result = internl.denote(operator, ctx); - if (result.status != Status.SUCCESS) return result; - - return CommandResult.success(transfrm.apply(result.success())); - } - -} diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java deleted file mode 100644 index 657c004..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/UnaryCommand.java +++ /dev/null @@ -1,50 +0,0 @@ -package bjc.pratt.commands.impls; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.AbstractInitialCommand; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A unary operator. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class UnaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - 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 CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx) - throws ParserException { - CommandResult<K,V> result = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); - if (result.status != Status.SUCCESS) return result; - final Tree<Token<K, V>> opr = result.success(); - - return CommandResult.success(new SimpleTree<>(operator, opr)); - } -}
\ No newline at end of file |
