From 572551b78e7f36b65185cb258bea31114d9992f6 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 27 Mar 2017 10:51:28 -0400 Subject: Simplifications --- .../pratt/commands/InitialCommands.java | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java') 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