diff options
| author | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-03-27 10:51:28 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-03-27 10:51:28 -0400 |
| commit | 572551b78e7f36b65185cb258bea31114d9992f6 (patch) | |
| tree | 3b5491e001e2a0b667f92eda78476146f41cfd9b /BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands | |
| parent | c9bce5d0ad88667eebf0f646fcc2323505ebc4ac (diff) | |
Simplifications
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/DenestingCommand.java | 31 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/InitialCommands.java | 36 |
2 files changed, 56 insertions, 11 deletions
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<C, V, K> extends AbstractInitialCommand<K, V, C> {
+ private InitialCommand<K, V, C> 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<K, V, C> internal) {
+ super();
+ this.internal = internal;
+ }
+
+ @Override
+ protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> 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 <K, V, C> InitialCommand<K, V, C> grouping(int precedence, K term, Token<K, V> mark) { - ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(precedence, term, null); + ParseBlock<K, V, C> innerBlock = simple(precedence, term, null); return new GroupingCommand<>(innerBlock, mark); } @@ -84,9 +83,9 @@ public class InitialCommands { */ public static <K, V, C> InitialCommand<K, V, C> preTernary(int cond1, int block1, int block2, K mark1, K mark2, Token<K, V> term) { - ParseBlock<K, V, C> condBlock = new SimpleParseBlock<>(cond1, mark1, null); - ParseBlock<K, V, C> opblock1 = new SimpleParseBlock<>(block1, mark2, null); - ParseBlock<K, V, C> opblock2 = new SimpleParseBlock<>(block2, null, null); + ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null); + ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null); + ParseBlock<K, V, C> opblock2 = simple(block2, null, null); return new PreTernaryCommand<>(condBlock, opblock1, opblock2, term); } @@ -139,9 +138,9 @@ public class InitialCommands { public static <K, V, C> InitialCommand<K, V, C> delimited(int inner, K delim, K mark, Token<K, V> term, UnaryOperator<C> onEnter, UnaryOperator<C> onDelim, UnaryOperator<C> onExit, boolean statement) { - ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(inner, null, null); - ParseBlock<K, V, C> delimsBlock = new RepeatingParseBlock<>(innerBlock, delim, mark, term, onDelim); - ParseBlock<K, V, C> scopedBlock = new TriggeredParseBlock<>(onEnter, onExit, delimsBlock); + ParseBlock<K, V, C> innerBlock = simple(inner, null, null); + ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim); + ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit); GroupingCommand<K, V, C> 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 <K, V, C> InitialCommand<K, V, C> denest(InitialCommand<K, V, C> comm) { + return new DenestingCommand<>(comm); } } |
