diff options
| author | student <student@192.168.1.186> | 2017-03-27 11:46:59 -0400 |
|---|---|---|
| committer | student <student@192.168.1.186> | 2017-03-27 11:46:59 -0400 |
| commit | f6c19bb14bf5be44c2d7bf1fef014d170f1c4343 (patch) | |
| tree | 4e0f7164005a35a2029f691e30cf4a25580f61cb /BJC-Utils2/src/main/java | |
| parent | 572551b78e7f36b65185cb258bea31114d9992f6 (diff) | |
Update FDS
Diffstat (limited to 'BJC-Utils2/src/main/java')
3 files changed, 94 insertions, 30 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java index 91934be..effc200 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java @@ -2,6 +2,7 @@ package bjc.utils.cli.fds; import java.io.PrintStream; +import bjc.utils.cli.fds.FDSState.InputMode; import bjc.utils.ioutils.Block; import bjc.utils.ioutils.BlockReader; @@ -21,65 +22,92 @@ public class FDS { * Run a provided FDS mode until it is exited or there is no more input. * * @param blockSource - * The command input source for the FDS mode. + * The command input source for the FDS mode. * * @param datain - * The data input source for the FDS mode. + * The data input source for the FDS mode. * * @param printer - * The output source for the FDS mode. + * The output source for the FDS mode. * * @param mode - * The mode to start in. + * The mode to start in. * * @param state - * The initial state for the mode. + * The initial state for the mode. * * @return The final state of the mode. * * @throws FDSException - * If something went wrong during mode execution. + * If something went wrong during mode execution. */ - public static <S> S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSMode<S> mode, - FDSState<S> state) throws FDSException { - //printer.print("Enter a command (m for help): "); - + public static <S> S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSState<S> state) + throws FDSException { while (blockSource.hasNext()) { Block comBlock = blockSource.next(); - handleCommandString(comBlock, blockSource, datain, printer, mode, state); - - //printer.print("Enter a command (m for help): "); + handleCommandString(comBlock, blockSource, datain, printer, state); } return state.state; } private static <S> void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSMode<S> mode, FDSState<S> state) throws FDSException { + PrintStream printer, FDSState<S> state) throws FDSException { String comString = comBlock.contents.trim(); switch (state.mode) { case CHORD: - if (comString.length() > 1) { - for (char c : comString.substring(1).toCharArray()) { - Block newCom = new Block(comBlock.blockNo + 1, Character.toString(c), - comBlock.startLine, comBlock.startLine); - - state.enqueCommand.accept(newCom); - } - } + chordCommand(comBlock, state, comString); case NORMAL: - handleCommand(comString.charAt(0), blockSource, datain, printer, mode, state); + handleCommand(comString.charAt(0), blockSource, datain, printer, state); break; - default: throw new FDSException(String.format("Unknown input mode '%s'", state.mode)); } } - private static <S> void handleCommand(char charAt, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSMode<S> mode, FDSState<S> state) { - printer.printf("Recieved command '%s'\n", charAt); + private static <S> void chordCommand(Block comBlock, FDSState<S> state, String comString) { + for (int i = 1; i < comString.length(); i++) { + char c = comString.charAt(i); + + Block newCom = new Block(comBlock.blockNo + 1, Character.toString(c), comBlock.startLine, + comBlock.startLine); + + state.enqueCommand.accept(newCom); + } + } + + private static <S> void handleCommand(char com, BlockReader blockSource, BlockReader datain, PrintStream printer, + FDSState<S> state) throws FDSException { + /* + * Handle built-in commands over user commands. + */ + switch (com) { + case 'x': + if (state.mode == InputMode.CHORD) { + state.mode = InputMode.NORMAL; + } else if (state.mode == InputMode.NORMAL) { + state.mode = InputMode.CHORD; + } else { + printer.println("? CNV\n"); + } + break; + case 'X': + /* + * TODO implement loading scripts from file. + */ + break; + default: + FDSMode<S> curMode = state.modes.top(); + + if (curMode.hasSubmode(com)) { + curMode.getCommand(com).run(state.state, datain); + } else if (curMode.hasCommand(com)) { + state.modes.push(curMode.getSubmode(com)); + } else { + printer.printf("? UBC '%s'", com); + } + } } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java index a0ad5e6..4a6e1c6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java @@ -2,6 +2,8 @@ package bjc.utils.cli.fds; import java.util.function.Consumer; +import bjc.utils.esodata.SimpleStack; +import bjc.utils.esodata.Stack; import bjc.utils.ioutils.Block; /** @@ -57,6 +59,11 @@ public class FDSState<S> { public InputMode mode; /** + * The modes being used. + */ + public Stack<FDSMode<S>> modes; + + /** * Function to add a command block to be processed. */ public Consumer<Block> enqueCommand; @@ -79,6 +86,8 @@ public class FDSState<S> { state = stat; mode = inputMode; + modes = new SimpleStack<>(); + enqueCommand = comQueue; enqueData = dataQueue; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReaders.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReaders.java index ec8dcfb..8a359b8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReaders.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReaders.java @@ -13,14 +13,41 @@ public class BlockReaders { * Create a new simple block reader that works off a regex.
*
* @param blockDelim
- * The regex that seperates blocks.
+ * The regex that seperates blocks.
*
* @param source
- * The reader to get blocks from.
+ * The reader to get blocks from.
*
* @return A configured simple reader.
*/
- public static BlockReader simple(String blockDelim, Reader source) {
+ public static SimpleBlockReader simple(String blockDelim, Reader source) {
return new SimpleBlockReader(blockDelim, source);
}
+
+ /**
+ * Create a new pushback block reader.
+ *
+ * @param src
+ * The block reader to read blocks from.
+ *
+ * @return A configured pushback reader.
+ */
+ public static PushbackBlockReader pushback(BlockReader src) {
+ return new PushbackBlockReader(src);
+ }
+
+ /**
+ * Create a new triggered block reader.
+ *
+ * @param source
+ * The block reader to read blocks from.
+ *
+ * @param action
+ * The action to execute before reading a block.
+ *
+ * @return A configured triggered block reader.
+ */
+ public static BlockReader trigger(BlockReader source, Runnable action) {
+ return new TriggeredBlockReader(source, action);
+ }
}
|
