Files
integration/luprex/cpp/luaconsole.hpp

79 lines
2.0 KiB
C++

//////////////////////////////////////////////////////
//
// LuaConsole:
//
// Used to parse commands that are being interactively typed
// in by a user.
//
// The command syntax is always a single character command,
// followed by arguments. Only two commands are hardwired:
//
// l - evaluate lua expression
// r - evaluate lua expression with 'return' prepended
//
// 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.
//
//////////////////////////////////////////////////////
#ifndef LUACONSOLE_HPP
#define LUACONSOLE_HPP
#include <string>
#include "luastack.hpp"
class LuaConsole {
public:
enum {
DO_NOTHING, // We need more input text.
DO_COMMAND, // We have a valid non-lua 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_;
void split_words();
public:
LuaConsole();
~LuaConsole();
// Get the recommended action.
int action() const { return action_; }
// 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();
};
#endif // LUACONSOLE_HPP