Files
integration/luprex/cpp/luaconsole.hpp

79 lines
2.0 KiB
C++
Raw Normal View History

2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
//
// LuaConsole:
//
2021-02-02 16:29:07 -05:00
// Used to parse commands that are being interactively typed
2021-01-23 20:12:21 -05:00
// in by a user.
//
2021-02-02 16:29:07 -05:00
// The command syntax is always a single character command,
// followed by arguments. Only two commands are hardwired:
2021-01-23 20:12:21 -05:00
//
2021-02-02 16:29:07 -05:00
// l - evaluate lua expression
// r - evaluate lua expression with 'return' prepended
2021-01-23 20:12:21 -05:00
//
2021-02-02 16:29:07 -05:00
// 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.
2021-01-23 20:12:21 -05:00
//
2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
#ifndef LUACONSOLE_HPP
#define LUACONSOLE_HPP
#include <string>
#include "luastack.hpp"
class LuaConsole {
public:
enum {
2021-01-23 20:12:21 -05:00
DO_NOTHING, // We need more input text.
2021-02-07 15:35:31 -05:00
DO_COMMAND, // We have a valid slash command.
2021-02-02 16:29:07 -05:00
DO_LUA, // We have a valid lua expression.
2021-01-23 20:12:21 -05:00
DO_SYNTAX, // We have a syntax error.
2021-01-23 16:10:29 -05:00
};
2021-02-02 16:29:07 -05:00
using StringVec = std::vector<std::string>;
2021-01-23 16:10:29 -05:00
private:
lua_State *lua_state_;
2021-01-23 20:12:21 -05:00
int action_;
2021-02-02 16:29:07 -05:00
std::string raw_input_;
2021-01-23 16:10:29 -05:00
int lines_;
2021-02-02 16:29:07 -05:00
std::string lua_expression_;
StringVec words_;
2021-01-23 16:10:29 -05:00
std::string syntax_;
2021-02-02 16:29:07 -05:00
void split_words();
2021-01-23 16:10:29 -05:00
public:
LuaConsole();
~LuaConsole();
2021-02-02 16:29:07 -05:00
// Get the recommended action.
2021-01-23 20:12:21 -05:00
int action() const { return action_; }
2021-02-02 16:29:07 -05:00
// 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.
2021-01-23 16:10:29 -05:00
const std::string &syntax() const { return syntax_; }
2021-02-02 16:29:07 -05:00
// Add a line of text that was just read from the console.
2021-01-23 16:10:29 -05:00
void add(std::string line);
2021-02-02 16:29:07 -05:00
// Read a line of text from stdin and add it.
2021-01-23 16:10:29 -05:00
void add_stdin();
2021-02-02 16:29:07 -05:00
// Clear the state.
2021-01-23 16:10:29 -05:00
void clear();
};
2021-01-23 20:12:21 -05:00
#endif // LUACONSOLE_HPP