Skip to content

Commit

Permalink
lua-parser/parser.lua: add feature for returning the detailed error
Browse files Browse the repository at this point in the history
  • Loading branch information
E-Fir committed Nov 9, 2022
1 parent cf573f0 commit 20e9078
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lua-parser/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ local G = { V"Lua",
PowOp = sym("^") / "pow";
}

local parser = {}
local parser = { detailed_errors = false }

local validator = require("lua-parser.validator")
local validate = validator.validate
Expand All @@ -467,8 +467,20 @@ function parser.parse (subject, filename)
lpeg.setmaxstack(1000)
local ast, label, errorpos = lpeg.match(G, subject, nil, errorinfo)
if not ast then
local errmsg = labels[label][2]
return ast, syntaxerror(errorinfo, errorpos, errmsg)
if parser.detailed_errors then
local re = require "relabel"
local line, col = re.calcline(subject, errorpos)
return ast, {
line = line;
column = col;
id = labels[label][1];
message = labels[label][2];
position = errorpos;
}
else
local errmsg = labels[label][2]
return ast, syntaxerror(errorinfo, errorpos, errmsg)
end
end
return validate(ast, errorinfo)
end
Expand Down

0 comments on commit 20e9078

Please sign in to comment.