From 2749ed721a02e608315698d81841ae186e3266b5 Mon Sep 17 00:00:00 2001 From: Kevin KIN-FOO Date: Thu, 26 May 2011 16:40:07 +0200 Subject: [PATCH] Adding function to handle AST generation failures and nicely parse error strings --- src/lib/errnode.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/lib/errnode.lua diff --git a/src/lib/errnode.lua b/src/lib/errnode.lua new file mode 100644 index 0000000..3a3ab34 --- /dev/null +++ b/src/lib/errnode.lua @@ -0,0 +1,19 @@ +require 'metalua.compiler' +-- +-- Ecapsulates funcion mlc.luastring_to_ast in order to protect call and parse +-- error string when an error occurs. +-- +-- @param src string containg Lua code to evaluate +-- @return AST of table type, as returned by mlc.luastring_to_ast. Contains an +-- error when AST generation fails +-- +function getast(src) + local status, result = pcall(mlc.luastring_to_ast, src) + if status then return result else + local line, column, offset = result:match '%(l.(%d+), c.(%d+), k.(%d+)%)' + local filename = result :match '^([^:]+)' + local msg = result :match 'line %d+, char %d+: (.-)\n' + local li = {line, column, offset, filename} + return {tag='Error', lineinfo={first=li, last=li}, msg} + end +end -- 2.44.0