Refactor luaconsole to make it easier to use
This commit is contained in:
@@ -3,22 +3,26 @@
|
||||
// LuaConsole:
|
||||
//
|
||||
// Used to parse commands that are being interactively typed
|
||||
// in by a user.
|
||||
// in by a user. The common usage pattern is:
|
||||
//
|
||||
// The command syntax is always a single character command,
|
||||
// followed by arguments. Only two commands are hardwired:
|
||||
// 1. Print the prompt suggested by 'get_prompt'.
|
||||
// 2. Read a line of text from stdin.
|
||||
// 3. Add the line to the LuaConsole using 'add'.
|
||||
// 4. Get the command words using 'words'.
|
||||
// 5. If the words are empty, do nothing.
|
||||
// 6. If the words contain something, execute and call 'clear'.
|
||||
//
|
||||
// l - evaluate lua expression
|
||||
// r - evaluate lua expression with 'return' prepended
|
||||
// The 'words' returned by the luaconsole can be empty, meaning
|
||||
// that there's no command to execute. If the words are nonempty,
|
||||
// then the first word is the command. There are two hardwired
|
||||
// commands:
|
||||
//
|
||||
// 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:
|
||||
// lua <expr> - execute a lua expression
|
||||
// syntax <message> - print a syntax error message
|
||||
//
|
||||
// 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.
|
||||
// If the first word is anything else, it means the user typed
|
||||
// that word on the command line. It is up to you to interpret
|
||||
// such commands.
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
@@ -30,46 +34,34 @@
|
||||
|
||||
class LuaConsole {
|
||||
public:
|
||||
enum {
|
||||
DO_NOTHING, // We need more input text.
|
||||
DO_COMMAND, // We have a valid slash command.
|
||||
DO_LUA, // We have a valid lua expression.
|
||||
DO_SYNTAX, // We have a syntax error.
|
||||
};
|
||||
using StringVec = std::vector<std::string>;
|
||||
|
||||
private:
|
||||
lua_State *lua_state_;
|
||||
int action_;
|
||||
std::string raw_input_;
|
||||
int lines_;
|
||||
std::string lua_expression_;
|
||||
StringVec words_;
|
||||
std::string syntax_;
|
||||
std::string prompt_;
|
||||
|
||||
void split_words();
|
||||
void clear_command();
|
||||
|
||||
public:
|
||||
LuaConsole();
|
||||
~LuaConsole();
|
||||
|
||||
// Get the recommended action.
|
||||
int action() const { return action_; }
|
||||
|
||||
// Fetch the stored prompt. Also clears the stored prompt. You should fetch
|
||||
// and print the prompt after 'add' or 'clear'.
|
||||
std::string get_prompt();
|
||||
|
||||
// When action is DO_COMMAND, get the command words.
|
||||
// Get the command words.
|
||||
//
|
||||
// Returns the empty vector if there is no command.
|
||||
// If there is a command, the first word is the command word.
|
||||
// See the file comment for certain built-in 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.
|
||||
// If more input is needed, stores the ">> " prompt.
|
||||
void add(std::string line);
|
||||
|
||||
Reference in New Issue
Block a user