diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/console/commandline/CommandLineParser.ts | 38 | ||||
-rw-r--r-- | src/console/commandline/CommandParser.ts | 52 | ||||
-rw-r--r-- | src/shared/Command.ts | 15 |
3 files changed, 105 insertions, 0 deletions
diff --git a/src/console/commandline/CommandLineParser.ts b/src/console/commandline/CommandLineParser.ts new file mode 100644 index 0000000..a166f49 --- /dev/null +++ b/src/console/commandline/CommandLineParser.ts @@ -0,0 +1,38 @@ +import CommandParser from "./CommandParser"; +import { Command } from "../../shared/Command"; + +export type CommandLine = { + readonly command: Command, + readonly args: string +} + +export enum InputPhase { + OnCommand, + OnArgs, +} + +export default class CommandLineParser { + private commandParser: CommandParser = new CommandParser(); + + inputPhase(line: string): InputPhase { + line = line.trimLeft(); + if (line.length == 0) { + return InputPhase.OnCommand + } + const command = line.split(/\s+/, 1)[0]; + if (line.length == command.length) { + return InputPhase.OnCommand + } + return InputPhase.OnArgs; + } + + parse(line: string): CommandLine { + const trimLeft = line.trimLeft(); + const command = trimLeft.split(/\s+/, 1)[0]; + const args = trimLeft.slice(command.length).trimLeft(); + return { + command: this.commandParser.parse(command), + args: args, + } + } +} diff --git a/src/console/commandline/CommandParser.ts b/src/console/commandline/CommandParser.ts new file mode 100644 index 0000000..5228c77 --- /dev/null +++ b/src/console/commandline/CommandParser.ts @@ -0,0 +1,52 @@ +import { Command } from "../../shared/Command"; + +export class UnknownCommandError extends Error { + constructor(value: string) { + super(`unknown command '${value}'`); + } +} + +export default class CommandParser { + parse(value: string): Command { + switch (value) { + case 'o': + case 'open': + return Command.Open; + case 't': + case 'tabopen': + return Command.TabOpen; + case 'w': + case 'winopen': + return Command.WindowOpen; + case 'b': + case 'buffer': + return Command.Buffer; + case 'bd': + case 'bdel': + case 'bdelete': + return Command.BufferDelete; + case 'bd!': + case 'bdel!': + case 'bdelete!': + return Command.BufferDeleteForce; + case 'bdeletes': + return Command.BuffersDelete; + case 'bdeletes!': + return Command.BuffersDeleteForce; + case 'addbookmark': + return Command.AddBookmark; + case 'q': + case 'quit': + return Command.Quit; + case 'qa': + case 'quitall': + return Command.QuitAll; + case 'set': + return Command.Set; + case 'h': + case 'help': + return Command.Help; + } + throw new UnknownCommandError(value); + } +} diff --git a/src/shared/Command.ts b/src/shared/Command.ts new file mode 100644 index 0000000..e492f4a --- /dev/null +++ b/src/shared/Command.ts @@ -0,0 +1,15 @@ +export enum Command { + Open = "open", + TabOpen = "tabopen", + WindowOpen = "winopen", + Buffer = "buffer", + BufferDelete = "bdelete", + BufferDeleteForce = "bdelete!", + BuffersDelete = "bdeletes", + BuffersDeleteForce = "bdeletes!", + AddBookmark = "addbookmark", + Quit = "quit", + QuitAll = "quitall", + Set = "set", + Help = "help", +} |