From f6c19bb14bf5be44c2d7bf1fef014d170f1c4343 Mon Sep 17 00:00:00 2001 From: student Date: Mon, 27 Mar 2017 11:46:59 -0400 Subject: Update FDS --- .../src/main/java/bjc/utils/cli/fds/FDS.java | 82 +++++++++++++++------- .../src/main/java/bjc/utils/cli/fds/FDSState.java | 9 +++ .../main/java/bjc/utils/ioutils/BlockReaders.java | 33 ++++++++- 3 files changed, 94 insertions(+), 30 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils') 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 runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSMode mode, - FDSState state) throws FDSException { - //printer.print("Enter a command (m for help): "); - + public static S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSState 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 void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSMode mode, FDSState state) throws FDSException { + PrintStream printer, FDSState 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 void handleCommand(char charAt, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSMode mode, FDSState state) { - printer.printf("Recieved command '%s'\n", charAt); + private static void chordCommand(Block comBlock, FDSState 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 void handleCommand(char com, BlockReader blockSource, BlockReader datain, PrintStream printer, + FDSState 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 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; /** @@ -56,6 +58,11 @@ public class FDSState { */ public InputMode mode; + /** + * The modes being used. + */ + public Stack> modes; + /** * Function to add a command block to be processed. */ @@ -79,6 +86,8 @@ public class FDSState { 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); + } } -- cgit v1.2.3