From 7cd05bfaeea0fc1d269ace114d3225bffbd82940 Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Fri, 1 Jul 2016 14:57:39 -0300 Subject: Adding function 'calcline' to relabel and updating documentation/examples --- examples/listIdRe2.lua | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'examples/listIdRe2.lua') diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index ccb7ce5..fe30535 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua @@ -1,9 +1,9 @@ local re = require 'relabel' local errinfo = { - {"errUndef", "undefined"}, - {"errId", "expecting an identifier"}, - {"errComma", "expecting ','"}, + {"errUndef", "undefined"}, + {"errId", "expecting an identifier"}, + {"errComma", "expecting ','"}, } local errmsgs = {} @@ -16,30 +16,22 @@ end re.setlabels(labels) -local function calcline (s, i) - if i == 1 then return 1, 1 end - local rest, line = s:sub(1,i):gsub("[^\n]*\n", "") - local col = #rest - return 1 + line, col ~= 0 and col or 1 -end - - local g = re.compile[[ S <- Id List - List <- !. / (',' / %{errComma}) (Id / %{errId}) List + List <- !. / (',' / %{errComma}) (Id / %{errId}) List Id <- Sp [a-z]+ Comma <- Sp ',' Sp <- %s* ]] function mymatch (g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. "): " - return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" - end - return r + local r, e, sfail = g:match(s) + if not r then + local line, col = re.calcline(s, #s - #sfail) + local msg = "Error at line " .. line .. " (col " .. col .. "): " + return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" + end + return r end print(mymatch(g, "one,two")) -- cgit v1.2.3-55-g6feb