Make LuaStack::Load return special codes for 'slash command', 'white space', and 'truncated lua'
This commit is contained in:
@@ -205,25 +205,21 @@ FString UlxLuaCallLibrary::AllFunctionsWithPrefix(const TCHAR *Prefix)
|
|||||||
void UlxLuaCallLibrary::ValidateLuaExpr(
|
void UlxLuaCallLibrary::ValidateLuaExpr(
|
||||||
ElxLuaSyntaxCheck &Status, FString &ErrorMessage, UObject *context, const FString &Code)
|
ElxLuaSyntaxCheck &Status, FString &ErrorMessage, UObject *context, const FString &Code)
|
||||||
{
|
{
|
||||||
if (Code.StartsWith(TEXT("/")))
|
|
||||||
{
|
|
||||||
ErrorMessage = "SlashCommand";
|
|
||||||
Status = ElxLuaSyntaxCheck::SlashCommand;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Code.TrimStart().IsEmpty())
|
|
||||||
{
|
|
||||||
ErrorMessage = "";
|
|
||||||
Status = ElxLuaSyntaxCheck::Whitespace;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
|
||||||
ErrorMessage = mode->ValidateLuaExpr(Code);
|
ErrorMessage = mode->ValidateLuaExpr(Code);
|
||||||
if (ErrorMessage.IsEmpty())
|
if (ErrorMessage.IsEmpty())
|
||||||
{
|
{
|
||||||
Status = ElxLuaSyntaxCheck::ValidLua;
|
Status = ElxLuaSyntaxCheck::ValidLua;
|
||||||
}
|
}
|
||||||
else if (ErrorMessage.Contains(TEXT("<eof>")))
|
else if (ErrorMessage == TEXT("slash command"))
|
||||||
|
{
|
||||||
|
Status = ElxLuaSyntaxCheck::SlashCommand;
|
||||||
|
}
|
||||||
|
else if (ErrorMessage == TEXT("white space"))
|
||||||
|
{
|
||||||
|
Status = ElxLuaSyntaxCheck::Whitespace;
|
||||||
|
}
|
||||||
|
else if (ErrorMessage == TEXT("truncated lua"))
|
||||||
{
|
{
|
||||||
Status = ElxLuaSyntaxCheck::TruncatedLua;
|
Status = ElxLuaSyntaxCheck::TruncatedLua;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -333,6 +333,17 @@ eng::string LuaCoreStack::load(LuaSlot result, std::string_view code, std::strin
|
|||||||
code = expanded;
|
code = expanded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sv::has_prefix(code, "/"))
|
||||||
|
{
|
||||||
|
set(result, "slash command");
|
||||||
|
return "slash command";
|
||||||
|
}
|
||||||
|
if (sv::is_whitespace(code))
|
||||||
|
{
|
||||||
|
set(result, "white space");
|
||||||
|
return "white space";
|
||||||
|
}
|
||||||
|
|
||||||
eng::string fullcontext = eng::string("=") + eng::string(context);
|
eng::string fullcontext = eng::string("=") + eng::string(context);
|
||||||
luaL_loadbuffer(L_, code.data(), code.size(), fullcontext.c_str());
|
luaL_loadbuffer(L_, code.data(), code.size(), fullcontext.c_str());
|
||||||
int type = lua_type(L_, -1);
|
int type = lua_type(L_, -1);
|
||||||
@@ -346,6 +357,10 @@ eng::string LuaCoreStack::load(LuaSlot result, std::string_view code, std::strin
|
|||||||
const char *str = lua_tolstring(L_, -1, &len);
|
const char *str = lua_tolstring(L_, -1, &len);
|
||||||
eng::string message(str, len);
|
eng::string message(str, len);
|
||||||
lua_pop(L_, 1);
|
lua_pop(L_, 1);
|
||||||
|
if (sv::has_suffix(message, "near <eof>"))
|
||||||
|
{
|
||||||
|
message = "truncated lua";
|
||||||
|
}
|
||||||
if (message.empty()) message = "unknown compiler error";
|
if (message.empty()) message = "unknown compiler error";
|
||||||
set(result, message);
|
set(result, message);
|
||||||
return message;
|
return message;
|
||||||
|
|||||||
@@ -199,6 +199,15 @@ bool is_lua_comment(string_view s) {
|
|||||||
return s.substr(start, 2) == "--";
|
return s.substr(start, 2) == "--";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_whitespace(string_view s) {
|
||||||
|
for (int i = 0; i < int(s.size()); i++) {
|
||||||
|
if (!ascii_isspace(s[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
string_view read_to_sep(string_view &source, char sep) {
|
string_view read_to_sep(string_view &source, char sep) {
|
||||||
size_t pos = source.find(sep);
|
size_t pos = source.find(sep);
|
||||||
string_view result;
|
string_view result;
|
||||||
|
|||||||
@@ -110,6 +110,9 @@ bool is_lua_classname(string_view s);
|
|||||||
// Return true if the line of code is a lua comment.
|
// Return true if the line of code is a lua comment.
|
||||||
bool is_lua_comment(string_view s);
|
bool is_lua_comment(string_view s);
|
||||||
|
|
||||||
|
// Return true if the line is entirely whitespace.
|
||||||
|
bool is_whitespace(string_view s);
|
||||||
|
|
||||||
// Return the first character, but if the view is empty,
|
// Return the first character, but if the view is empty,
|
||||||
// return zero.
|
// return zero.
|
||||||
inline char zfront(string_view &s) {
|
inline char zfront(string_view &s) {
|
||||||
|
|||||||
Reference in New Issue
Block a user