lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I was wondering what the easiest way was to check the syntax of a block of
code (from a buffer), without executing was? I currently am doing the
following (taken from chunks of the lua_dobuffer() code):

bool VLCompiler::CheckSyntax(string sBuffer)
 lua_State *cState;
 struct lua_longjmp cLJ;
 Proto *cClosure;
 int nStatus = 1;

 cState = lua_open(100);
  return false;

 luaZ_mopen(&cIO, sBuffer.c_str(), sBuffer.length(), "SyntaxCheck");

 cLJ.status = 0;
 cState->errorJmp = &cLJ;

 if(setjmp(cLJ.b) == 0)
  cClosure = luaY_parser(cState, &cIO);
  luaV_Lclosure(cState, cClosure, 0);

 nStatus = cLJ.status;
 if(nStatus >= 1)
  return false;
  return true;

This works, but it requires me to call some of the Lua internals, and I am
not sure if this is exactly "safe".

Also, what is the best way to extract the error messages when a compile
error occurs? Should I create a C tag method for _ERRORMESSAGE? And finally,
one last question. Is there any way to get the line number that the
compile/parse error occured on?

Thanks in advance,

Matt Holmes
Lead Developer
The Visual Lua Project