From 572551b78e7f36b65185cb258bea31114d9992f6 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 27 Mar 2017 10:51:28 -0400 Subject: Simplifications --- .../pratt/commands/DenestingCommand.java | 31 +++++++++++++++++++ .../pratt/commands/InitialCommands.java | 36 +++++++++++++++------- 2 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands') diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java new file mode 100644 index 0000000..d2d7829 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java @@ -0,0 +1,31 @@ +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; + +public class DenestingCommand extends AbstractInitialCommand { + private InitialCommand internal; + + /** + * 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 DenestingCommand(InitialCommand internal) { + super(); + this.internal = internal; + } + + @Override + protected ITree> intNullDenotation(Token operator, ParserContext ctx) + throws ParserException { + return internal.denote(operator, ctx).getChild(0); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java index bad5964..eac357a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java @@ -4,12 +4,11 @@ import bjc.utils.data.ITree; import bjc.utils.parserutils.pratt.InitialCommand; import bjc.utils.parserutils.pratt.ParseBlock; import bjc.utils.parserutils.pratt.Token; -import bjc.utils.parserutils.pratt.blocks.RepeatingParseBlock; -import bjc.utils.parserutils.pratt.blocks.SimpleParseBlock; -import bjc.utils.parserutils.pratt.blocks.TriggeredParseBlock; import java.util.function.UnaryOperator; +import static bjc.utils.parserutils.pratt.blocks.ParseBlocks.*; + /** * * Contains factory methods for producing common implementations of * {@link InitialCommand} @@ -45,7 +44,7 @@ public class InitialCommands { * @return A command implementing the operator. */ public static InitialCommand grouping(int precedence, K term, Token mark) { - ParseBlock innerBlock = new SimpleParseBlock<>(precedence, term, null); + ParseBlock innerBlock = simple(precedence, term, null); return new GroupingCommand<>(innerBlock, mark); } @@ -84,9 +83,9 @@ public class InitialCommands { */ public static InitialCommand preTernary(int cond1, int block1, int block2, K mark1, K mark2, Token term) { - ParseBlock condBlock = new SimpleParseBlock<>(cond1, mark1, null); - ParseBlock opblock1 = new SimpleParseBlock<>(block1, mark2, null); - ParseBlock opblock2 = new SimpleParseBlock<>(block2, null, null); + 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); } @@ -139,9 +138,9 @@ public class InitialCommands { public static InitialCommand delimited(int inner, K delim, K mark, Token term, UnaryOperator onEnter, UnaryOperator onDelim, UnaryOperator onExit, boolean statement) { - ParseBlock innerBlock = new SimpleParseBlock<>(inner, null, null); - ParseBlock delimsBlock = new RepeatingParseBlock<>(innerBlock, delim, mark, term, onDelim); - ParseBlock scopedBlock = new TriggeredParseBlock<>(onEnter, onExit, delimsBlock); + 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); @@ -149,6 +148,21 @@ public class InitialCommands { * Remove the wrapper layer from grouping-command on top of * RepeatingParseBlock. */ - return new TransformingInitialCommand<>(command, (tree) -> tree.getChild(0)); + 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); } } -- cgit v1.2.3