]> git.lizzy.rs Git - metalua.git/blob - src/lib/errnode.lua
Adding function to handle AST generation failures and nicely parse error strings
[metalua.git] / src / lib / errnode.lua
1 require 'metalua.compiler'
2 --
3 -- Ecapsulates funcion mlc.luastring_to_ast in order to protect call and parse
4 -- error string when an error occurs.
5 --
6 -- @param src string containg Lua code to evaluate
7 -- @return AST of table type, as returned by mlc.luastring_to_ast. Contains an
8 --      error when AST generation fails
9 --
10 function getast(src)
11    local status, result = pcall(mlc.luastring_to_ast, src)
12    if status then return result else
13       local line, column, offset = result:match '%(l.(%d+), c.(%d+), k.(%d+)%)'
14       local filename = result :match '^([^:]+)'
15       local msg = result :match 'line %d+, char %d+: (.-)\n'
16       local li = {line, column, offset, filename}
17       return {tag='Error', lineinfo={first=li, last=li}, msg}
18    end
19 end