Skip to content

Commit

Permalink
fix: zencode error reporting cleanup
Browse files Browse the repository at this point in the history
also using error inside main ZEN:parse function to block execution
  • Loading branch information
jaromil committed Oct 12, 2023
1 parent a8398de commit baf8936
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 92 deletions.
94 changes: 47 additions & 47 deletions src/lua/zencode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -414,57 +414,57 @@ end

function ZEN:parse(text)
self:crumb()
if #text < 9 then -- strlen("and debug") == 9
warn("Zencode text too short to parse")
return false
end
local branching = false
local looping = false
local prefixes = {}
local parse_prefix <const> = parse_prefix -- optimization
self.linenum = 0
if #text < 9 then -- strlen("and debug") == 9
error("Zencode text too short to parse")
return false
end
local branching = false
local looping = false
local prefixes = {}
local parse_prefix <const> = parse_prefix -- optimization
self.linenum = 0
for line in zencode_newline_iter(text) do
self.linenum = self.linenum + 1
local tline = trim(line) -- saves trims in isempty / iscomment
xxx(tline,3)

if not zencode_isempty(tline) and not zencode_iscomment(tline) then
-- xxx('Line: '.. text, 3)
-- max length for single zencode line is #define MAX_LINE
-- hard-coded inside zenroom.h
local prefix = parse_prefix(line) -- trim is included
assert(prefix, "Invalid Zencode line "..self.linenum..": "..line)
self.OK = true
exitcode(0)
if not branching and prefix == 'if' then
branching = true
table.insert(prefixes, 1, 'if')
elseif not looping and prefix == 'foreach' then
looping = true
table.insert(prefixes, 1, 'foreach')
elseif prefix == 'endif' then
branching = false
table.remove(prefixes, 1)
elseif prefix == 'endforeach' then
looping = false
table.remove(prefixes, 1)
end
if prefix == 'if' or prefix == 'foreach' then
prefix = table.concat(prefixes,'')
elseif prefix == 'when' or prefix == 'then'
or prefix == 'endif' or prefix == 'endforeach' then
prefix = prefix .. table.concat(prefixes,'')
end
self.linenum = self.linenum + 1
local tline = trim(line) -- saves trims in isempty / iscomment
xxx(tline,3)

if not zencode_isempty(tline) and not zencode_iscomment(tline) then
-- xxx('Line: '.. text, 3)
-- max length for single zencode line is #define MAX_LINE
-- hard-coded inside zenroom.h
local prefix = parse_prefix(line) -- trim is included
assert(prefix, "Invalid Zencode line "..self.linenum..": "..line)
self.OK = true
exitcode(0)
if not branching and prefix == 'if' then
branching = true
table.insert(prefixes, 1, 'if')
elseif not looping and prefix == 'foreach' then
looping = true
table.insert(prefixes, 1, 'foreach')
elseif prefix == 'endif' then
branching = false
table.remove(prefixes, 1)
elseif prefix == 'endforeach' then
looping = false
table.remove(prefixes, 1)
end
if prefix == 'if' or prefix == 'foreach' then
prefix = table.concat(prefixes,'')
elseif prefix == 'when' or prefix == 'then'
or prefix == 'endif' or prefix == 'endforeach' then
prefix = prefix .. table.concat(prefixes,'')
end

-- try to enter the machine state named in prefix
-- xxx("Zencode machine enter_"..prefix..": "..text, 3)
local fm <const> = self.machine["enter_"..prefix]
assert(fm, "Invalid Zencode prefix "..self.linenum..": '"..line.."'")
assert(fm(self.machine, { msg = tline, Z = self }),
-- try to enter the machine state named in prefix
-- xxx("Zencode machine enter_"..prefix..": "..text, 3)
local fm <const> = self.machine["enter_"..prefix]
assert(fm, "Invalid Zencode prefix "..self.linenum..": '"..line.."'")
assert(fm(self.machine, { msg = tline, Z = self }),
line.."\n "..
"Invalid transition from: "..self.machine.current)
end
-- continue
end
-- continue
end
collectgarbage'collect'
return true
Expand Down
85 changes: 40 additions & 45 deletions src/zenroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,39 +350,35 @@ void zen_teardown(zenroom_t *ZZ) {
int zen_exec_zencode(zenroom_t *ZZ, const char *script) {
SAFE_EXEC;
int ret;
lua_State* L = (lua_State*)ZZ->lua;
// introspection on code being executed
zen_setenv(L,"CODE",(char*)script);
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:begin() end)\n"
"if not _res then exitcode(4) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode init error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:parse(CONF.code.encoding.fun(CODE)) end)\n"
"if not _res then exitcode(3) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode parser error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:run() end)\n"
"if not _res then exitcode(2) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode runtime error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
func(L, "Zencode successfully executed");
ZZ->exitcode = SUCCESS;
return SUCCESS;
lua_State* L = (lua_State*)ZZ->lua;
// introspection on code being executed
zen_setenv(L,"CODE",(char*)script);
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:begin() end)\n"
"if not _res then exitcode(4) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode init error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:parse(CONF.code.encoding.fun(CODE)) end)\n"
"if not _res then exitcode(3) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode parser error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:run() end)\n"
"if not _res then exitcode(2) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode runtime error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
if(ZZ->exitcode == SUCCESS) func(L, "Zencode successfully executed");
return ZZ->exitcode;
}

int zen_exec_script(zenroom_t *ZZ, const char *script) {
Expand Down Expand Up @@ -449,10 +445,9 @@ int _check_zenroom_init(zenroom_t *zz) {
return SUCCESS;
}

int _check_zenroom_result(zenroom_t *zz, int res) {
int exitcode = res;
zz->exitcode = res;
if(res != SUCCESS) {
int _check_zenroom_result(zenroom_t *zz) {
int exitcode = zz->exitcode;
if(exitcode != SUCCESS) {
zerror(zz->lua, "Execution aborted with errors.");
} else {
act(zz->lua, "Zenroom execution completed.");
Expand Down Expand Up @@ -480,8 +475,8 @@ int zencode_exec(const char *script, const char *conf, const char *keys, const c
zenroom_t *Z = zen_init(c, k, d);

if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;

return( _check_zenroom_result(Z, zen_exec_zencode(Z, script) ));
zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z) );
}

int zenroom_exec(const char *script, const char *conf, const char *keys, const char *data) {
Expand All @@ -495,8 +490,8 @@ int zenroom_exec(const char *script, const char *conf, const char *keys, const c

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;

return( _check_zenroom_result(Z, zen_exec_script(Z, script) ));
zen_exec_script(Z, script);
return( _check_zenroom_result(Z));
}

int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, const char *data,
Expand All @@ -518,8 +513,8 @@ int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, c
Z->stdout_len = stdout_len;
Z->stderr_buf = stderr_buf;
Z->stderr_len = stderr_len;

return( _check_zenroom_result(Z, zen_exec_zencode(Z, script) ));
zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z));
}


Expand All @@ -542,7 +537,7 @@ int zenroom_exec_tobuf(const char *script, const char *conf, const char *keys, c
Z->stdout_len = stdout_len;
Z->stderr_buf = stderr_buf;
Z->stderr_len = stderr_len;

return( _check_zenroom_result(Z, zen_exec_script(Z, script) ));
zen_exec_script(Z, script);
return( _check_zenroom_result(Z));
}

0 comments on commit baf8936

Please sign in to comment.