Lua 语法错误的描述性错误消息
Descriptive Error Messages for Lua Syntax Errors
我有一个Lua解释器,每当我在代码中犯语法错误时,返回的错误消息只是attempted to call a string value
,而不是有意义的错误消息。例如,如果我运行此 lua 代码:
for a= 1,10
print(a)
end
它不会返回有意义的'do' expected near 'print'
和行号,而是返回错误attempted to call a string value
。
我的C++代码如下:
void LuaInterpreter::run(std::string script) {
luaL_openlibs(m_mainState);
// Adds all functions for calling in lua code
addFunctions(m_mainState);
// Loading the script string into lua
luaL_loadstring(m_mainState, script.c_str());
// Calls the script
int error =lua_pcall(m_mainState, 0, 0, 0);
if (error) {
std::cout << lua_tostring(m_mainState, -1) << std::endl;
lua_pop(m_mainState, 1);
}
}
提前感谢!
您的问题是luaL_loadstring
无法加载字符串,因为它不是有效的 Lua 代码。但是你从不费心检查它的返回值来找出这一点。因此,您最终会尝试执行它推送到堆栈上的编译错误,就好像它是一个有效的 Lua 函数一样。
使用此功能的正确方法如下:
auto error = luaL_loadstring(m_mainState, script.c_str());
if(error)
{
std::cout << lua_tostring(m_mainState, -1) << std::endl;
lua_pop(m_mainState, 1);
return; //Perhaps throw or something to signal an error?
}
我能够通过替换来解决问题
luaL_loadstring(m_mainState, script.c_str());
// Calls the script
int error =lua_pcall(m_mainState, 0, 0, 0);
用代码
int error = luaL_dostring(m_mainState, script.c_str());
相关文章:
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- GCC 警告和 gcc 错误消息之间的区别
- 为 c++ cin 上的输入数无效生成错误消息
- 来自带有mbedtls TLS的Mongoose Web服务器的错误消息
- 如何使用realsense摄像头调试ROS错误消息
- C++ 概念 - 需要括号中的概念会导致 2 条冲突的错误消息
- 编译器收到错误消息(textc.exe已停止)
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用 c++/WinRT Direct3D UWP Game DR 模板 Direct X11 的错误消息
- max_element() 给出非常奇怪的错误消息
- 不断收到错误消息,并非所有控制路径都返回值