More work on command parser
This commit is contained in:
@@ -12,12 +12,32 @@ LuaConsole::~LuaConsole() {
|
||||
}
|
||||
|
||||
void LuaConsole::clear() {
|
||||
expression_ = "";
|
||||
raw_input_ = "";
|
||||
lines_ = 0;
|
||||
lua_expression_ = "";
|
||||
words_.clear();
|
||||
syntax_ = "";
|
||||
action_ = DO_NOTHING;
|
||||
}
|
||||
|
||||
|
||||
void LuaConsole::split_words() {
|
||||
words_.clear();
|
||||
std::string acc;
|
||||
for (char c : raw_input_) {
|
||||
if ((c == ' ')||(c == '\n')) {
|
||||
if (!acc.empty()) {
|
||||
words_.push_back(acc);
|
||||
acc = "";
|
||||
}
|
||||
} else {
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
if (!acc.empty()) {
|
||||
words_.push_back(acc);
|
||||
}
|
||||
}
|
||||
|
||||
void LuaConsole::add(std::string line) {
|
||||
if (action_ != DO_NOTHING) {
|
||||
clear();
|
||||
@@ -25,30 +45,42 @@ void LuaConsole::add(std::string line) {
|
||||
for (int i = 0; i < int(line.size()); i++) {
|
||||
if (line[i] == '\n') line[i] = ' ';
|
||||
}
|
||||
expression_ += line;
|
||||
expression_ += '\n';
|
||||
raw_input_ += line;
|
||||
raw_input_ += '\n';
|
||||
lines_ += 1;
|
||||
char cmd = raw_input_[0];
|
||||
|
||||
// Convert leading = to 'return'
|
||||
if (expression_[0] == '=') {
|
||||
expression_ = std::string("return ") + expression_.substr(1);
|
||||
// All commands are a single character.
|
||||
if ((raw_input_[1] != ' ')&&(raw_input_[1] != '\n')) {
|
||||
action_ = DO_SYNTAX;
|
||||
syntax_ = "Commands are a single character followed by arguments";
|
||||
return;
|
||||
}
|
||||
|
||||
// Analyze slash-commands.
|
||||
if (expression_[0] == '/') {
|
||||
// Analyze non-lua commands.
|
||||
if ((cmd != 'r') && (cmd != 'l')) {
|
||||
lua_expression_ = "";
|
||||
if (lines_ == 1) {
|
||||
action_ = DO_SLASH;
|
||||
syntax_ = "";
|
||||
action_ = DO_COMMAND;
|
||||
split_words();
|
||||
} else {
|
||||
action_ = DO_SYNTAX;
|
||||
syntax_ = "slash command has more than one line";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Strip the leading punctuation from lua commands.
|
||||
std::string partial;
|
||||
if (cmd == 'r') {
|
||||
partial = std::string("return ") + raw_input_.substr(2);
|
||||
} else {
|
||||
partial = raw_input_.substr(2);
|
||||
}
|
||||
|
||||
// Analyze lua expressions.
|
||||
int top = lua_gettop(lua_state_);
|
||||
int status = luaL_loadbuffer(lua_state_, expression_.c_str(), expression_.size(), "=stdin");
|
||||
int status = luaL_loadbuffer(lua_state_, partial.c_str(), partial.size(), "=stdin");
|
||||
if (status == LUA_ERRSYNTAX)
|
||||
{
|
||||
const char *eof = "'<eof>'";
|
||||
@@ -56,15 +88,14 @@ void LuaConsole::add(std::string line) {
|
||||
const char *msg = lua_tolstring(lua_state_, -1, &lmsg);
|
||||
const char *tp = msg + lmsg - (sizeof(eof) - 1);
|
||||
if (strstr(msg, eof) == tp) {
|
||||
syntax_ = "";
|
||||
action_ = DO_NOTHING;
|
||||
} else {
|
||||
syntax_ = msg;
|
||||
action_ = DO_SYNTAX;
|
||||
syntax_ = msg;
|
||||
}
|
||||
} else {
|
||||
syntax_ = "";
|
||||
action_ = DO_COMMAND;
|
||||
action_ = DO_LUA;
|
||||
lua_expression_ = partial;
|
||||
}
|
||||
lua_settop(lua_state_, top);
|
||||
}
|
||||
@@ -75,7 +106,7 @@ void LuaConsole::add_stdin() {
|
||||
}
|
||||
const int MAXINPUT = 1000;
|
||||
char buf[MAXINPUT];
|
||||
fputs(expression_.empty() ? "> " : ">> ", stdout);
|
||||
fputs(raw_input_.empty() ? "> " : ">> ", stdout);
|
||||
fflush(stdout);
|
||||
if (fgets(buf, MAXINPUT, stdin)) {
|
||||
size_t len = strlen(buf);
|
||||
|
||||
Reference in New Issue
Block a user