More work on command parser

This commit is contained in:
2021-02-02 16:29:07 -05:00
parent df3ec8ab99
commit 0721d29c72
10 changed files with 239 additions and 106 deletions

View File

@@ -2,20 +2,23 @@
//
// LuaConsole:
//
// Used to parse lines of text that are being interactively typed
// Used to parse commands that are being interactively typed
// in by a user.
//
// Whenever the user types in a line, you should add the line to
// the LuaConsole object. After adding a line, the console will
// recommend one of several actions:
// The command syntax is always a single character command,
// followed by arguments. Only two commands are hardwired:
//
// 1. DO_NOTHING: The input is not complete, so you need to wait.
// 2. DO_COMMAND: Execute a lua command.
// 3. DO_SYNTAX: Print a syntax error message.
// 4. DO_SLASH: Execute a slash-command that bypasses lua.
// l - evaluate lua expression
// r - evaluate lua expression with 'return' prepended
//
// Also: if the first character of the lua form is '=', then
// replace the '=' with 'return '.
// The console expects you to add lines of text one at a time.
// After adding a line, the console will recommend one of these
// actions:
//
// 1. DO_NOTHING: Add more lines of text.
// 2. DO_LUA: Evaluate a lua expression.
// 3. DO_OTHER: Execute a non-lua command.
// 4. DO_SYNTAX: Print a lua syntax error.
//
//////////////////////////////////////////////////////
@@ -29,28 +32,46 @@ class LuaConsole {
public:
enum {
DO_NOTHING, // We need more input text.
DO_COMMAND, // We have a valid lua expression.
DO_COMMAND, // We have a valid non-lua command.
DO_LUA, // We have a valid lua expression.
DO_SYNTAX, // We have a syntax error.
DO_SLASH, // We have a slash-command.
};
using StringVec = std::vector<std::string>;
private:
lua_State *lua_state_;
int action_;
std::string raw_input_;
int lines_;
std::string expression_;
std::string lua_expression_;
StringVec words_;
std::string syntax_;
void split_words();
public:
LuaConsole();
~LuaConsole();
// Get the recommended action.
int action() const { return action_; }
int lines() const { return lines_; }
const std::string &expression() const { return expression_; }
// When action is DO_COMMAND, get the command words.
const StringVec &words() const { return words_; }
// When action is DO_LUA, get the valid lua expression.
std::string lua_expression() const { return lua_expression_; }
// When action is DO_SYNTAX, get the syntax error.
const std::string &syntax() const { return syntax_; }
// Add a line of text that was just read from the console.
void add(std::string line);
// Read a line of text from stdin and add it.
void add_stdin();
// Clear the state.
void clear();
};