Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lua C++ Parser Error Messages

Started by Morbular Feb 1, 2009 at 2:14 PM 1 replies 5.8k views
Original Post
Morbular
Morbular
Hello, I was looking through some of the Lua source files, and I've come across various parser error messages built in, including bad arguments, expected symbols, unfinished strings, unexpected symbols, and more to display errors when there are problems whilst parsing a file. I downloaded the source from the Lua website and have compiled a static library, implemented it into my project, and there are no error messages which display. Is there anything that I have to define or do in order to have these essential parser error messages appear in my console application?
Drew_Benton
Drew_Benton
Quote:
Original post by Morbular
Is there anything that I have to define or do in order to have these essential parser error messages appear in my console application?


For simple error handling, when you use functions like luaL_dofile and luaL_dostring, when a Lua error is generated, or when you call lua_error from C/C++, it will be a single string at the top of the stack. You can simply print it out using: printf("%s\n", lua_tostring(L, -1)); where L is the lua_State pointer. You will also need to call lua_pop(L, 1); to remove the error string from the Lua stack.

Here's a simple function I use that adds some ascii art and coloring to make errors stand out when you have a lot of console text scrolling. You can extend the method yourself to output in any format you need.
void ReportErrors(lua_State * L){	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	SetConsoleTextAttribute(hConsole, FOREGROUND_RED | BACKGROUND_INTENSITY);	printf("*******************************************************************************\n");	SetConsoleTextAttribute(hConsole, 7);	printf("%s\n", lua_tostring(L, -1));	SetConsoleTextAttribute(hConsole, FOREGROUND_RED | BACKGROUND_INTENSITY);	printf("*******************************************************************************\n\n");	SetConsoleTextAttribute(hConsole, 7);	lua_pop(L, 1);}


Here is a simple Lua script executing program that would show how to use it:
extern "C"{	#include "lua.h"	#include "lualib.h"	#include "lauxlib.h"}#include <windows.h>int main(int argc, char** argv){	lua_State * L = lua_open();	if(L == 0) return -1;	luaL_openlibs(L);	// Script that concatenates a nil value        // t.lua:   print(""..nil);	if(luaL_dofile(L, "t.lua"))		ReportErrors(L);	// Missing ( after print	if(luaL_dostring(L, "print\"Hello World!\");"))		ReportErrors(L);	lua_close(L);	return 0;}


And the output would be:
*******************************************************************************t.lua:1: attempt to concatenate a nil value**************************************************************************************************************************************************************[string "print"Hello World!");"]:1: unexpected symbol near ')'*******************************************************************************Press any key to continue . . .
Morbular
Morbular
Many thanks there mate! I'll give it a try right now. I just have one question: is every lua error present pushed onto the stack, or would I have to detect all of them somehow?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.