From c82452e59b1547392c3e89d08d9173cc6dc79e23 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 5 Apr 2017 15:35:13 -0400 Subject: Reorganize --- .../pratt/commands/AbstractInitialCommand.java | 32 ---- .../parserutils/pratt/commands/BinaryCommand.java | 43 ------ .../pratt/commands/BinaryPostCommand.java | 40 ----- .../parserutils/pratt/commands/ChainCommand.java | 73 --------- .../pratt/commands/ConstantCommand.java | 40 ----- .../pratt/commands/DefaultInitialCommand.java | 28 ---- .../pratt/commands/DefaultNonInitialCommand.java | 32 ---- .../pratt/commands/DenestingCommand.java | 45 ------ .../pratt/commands/GroupingCommand.java | 51 ------- .../pratt/commands/InitialCommands.java | 168 --------------------- .../parserutils/pratt/commands/LeafCommand.java | 29 ---- .../pratt/commands/LeftBinaryCommand.java | 32 ---- .../pratt/commands/NonBinaryCommand.java | 37 ----- .../pratt/commands/NonInitialCommands.java | 140 ----------------- .../pratt/commands/PostCircumfixCommand.java | 60 -------- .../parserutils/pratt/commands/PostfixCommand.java | 39 ----- .../pratt/commands/PreTernaryCommand.java | 75 --------- .../pratt/commands/RightBinaryCommand.java | 30 ---- .../parserutils/pratt/commands/TernaryCommand.java | 77 ---------- .../pratt/commands/TransformingInitialCommand.java | 52 ------- .../parserutils/pratt/commands/UnaryCommand.java | 47 ------ 21 files changed, 1170 deletions(-) delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/AbstractInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryPostCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ChainCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ConstantCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultNonInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/GroupingCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeafCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeftBinaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonBinaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonInitialCommands.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostfixCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/RightBinaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TernaryCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TransformingInitialCommand.java delete mode 100644 JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/UnaryCommand.java (limited to 'JPratt/src/main/java/bjc/utils/parserutils/pratt/commands') diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/AbstractInitialCommand.java deleted file mode 100644 index 3c3a89b..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/AbstractInitialCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * Abstract base for initial commands. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public abstract class AbstractInitialCommand implements InitialCommand { - @Override - public ITree> denote(Token operator, ParserContext ctx) throws ParserException { - return intNullDenotation(operator, ctx); - } - - protected abstract ITree> intNullDenotation(Token operator, ParserContext ctx) - throws ParserException; - -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryCommand.java deleted file mode 100644 index 781309c..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryCommand.java +++ /dev/null @@ -1,43 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * A 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 abstract class BinaryCommand extends BinaryPostCommand { - /** - * Create a new binary operator with the specified precedence. - * - * @param precedence - * The precedence of the operator. - */ - public BinaryCommand(int precedence) { - super(precedence); - } - - protected abstract int rightBinding(); - - @Override - public ITree> denote(ITree> operand, Token operator, ParserContext ctx) - throws ParserException { - ITree> opr = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false); - - return new Tree<>(operator, operand, opr); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryPostCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryPostCommand.java deleted file mode 100644 index 806f2f3..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/BinaryPostCommand.java +++ /dev/null @@ -1,40 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.parserutils.pratt.NonInitialCommand; - -/** - * A operator with fixed precedence. - * - * @author bjculkin - * - * @param - * The key type of the tokens. - * - * @param - * The value type of the tokens. - * - * @param - * The state type of the parser. - */ -public abstract class BinaryPostCommand extends NonInitialCommand { - private final int leftPower; - - /** - * Create a new operator with fixed precedence. - * - * @param precedence - * The precedence of the operator. - */ - public BinaryPostCommand(int precedence) { - if (precedence < 0) { - throw new IllegalArgumentException("Precedence must be non-negative"); - } - - leftPower = precedence; - } - - @Override - public int leftBinding() { - return leftPower; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ChainCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ChainCommand.java deleted file mode 100644 index 1324586..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ChainCommand.java +++ /dev/null @@ -1,73 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -import java.util.Set; - -/** - * 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 Set chainWith; - - private 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(int precedence, Set chainSet, Token chainMarker) { - super(precedence); - - chainWith = chainSet; - chain = chainMarker; - } - - @Override - public ITree> denote(ITree> operand, Token operator, ParserContext ctx) - throws ParserException { - ITree> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false); - - ITree> res = new Tree<>(operator, operand, tree); - - if (chainWith.contains(ctx.tokens.current().getKey())) { - Token tok = ctx.tokens.current(); - ctx.tokens.next(); - - ITree> other = denote(tree, tok, - new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); - - return new Tree<>(chain, res, other); - } else { - return res; - } - } - - @Override - public int nextBinding() { - return leftBinding() - 1; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ConstantCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ConstantCommand.java deleted file mode 100644 index 10ff184..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/ConstantCommand.java +++ /dev/null @@ -1,40 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 ITree> val; - - /** - * Create a new constant. - * - * @param con - * The tree this constant represents. - */ - public ConstantCommand(ITree> con) { - val = con; - } - - @Override - public ITree> denote(Token operator, ParserContext ctx) throws ParserException { - return val; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultInitialCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultInitialCommand.java deleted file mode 100644 index 7409755..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultInitialCommand.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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(Token operator, ParserContext ctx) throws ParserException { - throw new ParserException("Unexpected token " + operator); - } -} diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultNonInitialCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultNonInitialCommand.java deleted file mode 100644 index 887dd25..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DefaultNonInitialCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.pratt.NonInitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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(ITree> operand, Token operator, 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/utils/parserutils/pratt/commands/DenestingCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java deleted file mode 100644 index 567e608..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 InitialCommand wrapped; - - /** - * Create a new transforming initial command. - * - * @param internal - * The initial command to delegate to. - */ - public DenestingCommand(InitialCommand internal) { - wrapped = internal; - } - - @Override - protected ITree> intNullDenotation(Token operator, 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/utils/parserutils/pratt/commands/GroupingCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/GroupingCommand.java deleted file mode 100644 index 37cc6ee..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/GroupingCommand.java +++ /dev/null @@ -1,51 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 ParseBlock innerBlock; - - private 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(ParseBlock inner, Token marker) { - innerBlock = inner; - - mark = marker; - } - - @Override - protected ITree> intNullDenotation(Token operator, ParserContext ctx) - throws ParserException { - ITree> opr = innerBlock.parse(ctx); - - return new Tree<>(mark, opr); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java deleted file mode 100644 index eac357a..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java +++ /dev/null @@ -1,168 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.Token; - -import java.util.function.UnaryOperator; - -import static bjc.utils.parserutils.pratt.blocks.ParseBlocks.*; - -/** - * * Contains factory methods for producing common implementations of - * {@link InitialCommand} - * - * @author EVE - * - */ -public class InitialCommands { - /** - * Create a new unary operator. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static InitialCommand unary(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(int precedence, K term, Token mark) { - 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(int cond1, int block1, int block2, K mark1, K mark2, - Token term) { - ParseBlock condBlock = simple(cond1, mark1, null); - ParseBlock opblock1 = simple(block1, mark2, null); - 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(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(int inner, K delim, K mark, Token term, - UnaryOperator onEnter, UnaryOperator onDelim, UnaryOperator onExit, - boolean statement) { - ParseBlock innerBlock = simple(inner, null, null); - ParseBlock delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); - ParseBlock scopedBlock = trigger(delimsBlock, onEnter, onExit); - - 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(InitialCommand comm) { - return new DenestingCommand<>(comm); - } -} diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeafCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeafCommand.java deleted file mode 100644 index 3937f5f..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeafCommand.java +++ /dev/null @@ -1,29 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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(Token operator, ParserContext ctx) throws ParserException { - return new Tree<>(operator); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeftBinaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeftBinaryCommand.java deleted file mode 100644 index 58d7261..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/LeftBinaryCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package bjc.utils.parserutils.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(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/utils/parserutils/pratt/commands/NonBinaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonBinaryCommand.java deleted file mode 100644 index d32a1a7..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonBinaryCommand.java +++ /dev/null @@ -1,37 +0,0 @@ -package bjc.utils.parserutils.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(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/utils/parserutils/pratt/commands/NonInitialCommands.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonInitialCommands.java deleted file mode 100644 index 45bdc51..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/NonInitialCommands.java +++ /dev/null @@ -1,140 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.parserutils.pratt.NonInitialCommand; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.Token; -import bjc.utils.parserutils.pratt.blocks.SimpleParseBlock; - -import java.util.Set; - -/** - * Contains factory methods for producing common implementations of - * {@link NonInitialCommand} - * - * @author EVE - * - */ -public class NonInitialCommands { - /** - * Create a left-associative infix operator. - * - * @param precedence - * The precedence of the operator. - * - * @return A command implementing that operator. - */ - public static NonInitialCommand infixLeft(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(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(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(int precedence, Set chainSet, 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(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(int precedence, int insidePrecedence, K closer, - Token marker) { - ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); - - return new PostCircumfixCommand<>(precedence, innerBlock, marker); - } - - /** - * Create a ternary operator. - * - * This is like C's ?: operator. - * - * @param precedence - * The precedence of the operator. - * - * @param insidePrecedence - * The precedence of the inner section of the operator. - * - * @param closer - * The token that marks the end of the inner section. - * - * @param marker - * The token to use as the AST node for the operator. - * - * @param nonassoc - * True if the command is non-associative, false - * otherwise. - * - * @return A command implementing this operator. - */ - public static NonInitialCommand ternary(int precedence, int insidePrecedence, K closer, - Token marker, boolean nonassoc) { - ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); - - return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); - } -} diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java deleted file mode 100644 index 90fca00..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java +++ /dev/null @@ -1,60 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 ParseBlock innerBlock; - - private 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(int precedence, ParseBlock inner, Token marker) { - super(precedence); - - if (inner == null) { - throw new NullPointerException("Inner block must not be null"); - } - - innerBlock = inner; - - mark = marker; - } - - @Override - public ITree> denote(ITree> operand, Token operator, ParserContext ctx) - throws ParserException { - 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/utils/parserutils/pratt/commands/PostfixCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostfixCommand.java deleted file mode 100644 index bab3de4..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PostfixCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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(int precedence) { - super(precedence); - } - - @Override - public ITree> denote(ITree> operand, Token operator, ParserContext ctx) - throws ParserException { - return new Tree<>(operator, operand); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java deleted file mode 100644 index 42d1a6e..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java +++ /dev/null @@ -1,75 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 Token term; - - private ParseBlock condBlock; - - private ParseBlock opblock1; - private 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(ParseBlock cond, ParseBlock op1, ParseBlock op2, - 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"); - - this.condBlock = cond; - this.opblock1 = op1; - this.opblock2 = op2; - - this.term = term; - } - - @Override - protected ITree> intNullDenotation(Token operator, ParserContext ctx) - throws ParserException { - ITree> cond = condBlock.parse(ctx); - - ITree> op1 = opblock1.parse(ctx); - - ITree> op2 = opblock2.parse(ctx); - - return new Tree<>(term, cond, op1, op2); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/RightBinaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/RightBinaryCommand.java deleted file mode 100644 index 5f3d9f2..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/RightBinaryCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.utils.parserutils.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(int precedence) { - super(precedence); - } - - @Override - protected int rightBinding() { - return leftBinding(); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TernaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TernaryCommand.java deleted file mode 100644 index 8f04368..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TernaryCommand.java +++ /dev/null @@ -1,77 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 ParseBlock innerBlck; - - private Token mark; - - private boolean nonassoc; - - /** - * Create a new ternary command. - * - * @param precedence - * The precedence of this operator. - * - * @param innerBlock - * The representation of the inner block of the - * expression. - * - * @param marker - * The token to use as the root of the AST node. - * - * @param isNonassoc - * Whether or not the conditional is associative. - */ - public TernaryCommand(int precedence, ParseBlock innerBlock, Token marker, 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(ITree> operand, Token operator, ParserContext ctx) - throws ParserException { - ITree> inner = innerBlck.parse(ctx); - - 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; - } else { - return leftBinding(); - } - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TransformingInitialCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TransformingInitialCommand.java deleted file mode 100644 index 88803df..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/TransformingInitialCommand.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import java.util.function.UnaryOperator; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.InitialCommand; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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 InitialCommand internal; - - private UnaryOperator>> transform; - - /** - * Create a new transforming initial command. - * - * @param internal - * The initial command to delegate to. - * - * @param transform - * The transform to apply to the returned tree. - */ - public TransformingInitialCommand(InitialCommand internal, - UnaryOperator>> transform) { - super(); - this.internal = internal; - this.transform = transform; - } - - @Override - protected ITree> intNullDenotation(Token operator, ParserContext ctx) - throws ParserException { - return transform.apply(internal.denote(operator, ctx)); - } - -} diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/UnaryCommand.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/UnaryCommand.java deleted file mode 100644 index c608362..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/commands/UnaryCommand.java +++ /dev/null @@ -1,47 +0,0 @@ -package bjc.utils.parserutils.pratt.commands; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.parserutils.ParserException; -import bjc.utils.parserutils.pratt.ParserContext; -import bjc.utils.parserutils.pratt.Token; - -/** - * 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(int precedence) { - if(precedence < 0) { - throw new IllegalArgumentException("Precedence must be non-negative"); - } - - nullPwer = precedence; - } - - @Override - protected ITree> intNullDenotation(Token operator, ParserContext ctx) - throws ParserException { - ITree> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); - - return new Tree<>(operator, opr); - } -} \ No newline at end of file -- cgit v1.2.3