From 96284f8b4a6a25efd3c0c5ce9c7595604ba3143f Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Thu, 17 Nov 2016 15:55:24 -0300 Subject: Updating examples using the recovery operator --- examples/listId2Rec2Cap.lua | 79 +++++++++++++++++++++++++++++++++++++++++++++ examples/listIdRe1.lua | 8 ++--- examples/listIdRe2.lua | 50 ++++++++++++++++++++-------- 3 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 examples/listId2Rec2Cap.lua diff --git a/examples/listId2Rec2Cap.lua b/examples/listId2Rec2Cap.lua new file mode 100644 index 0000000..f9fc2bd --- /dev/null +++ b/examples/listId2Rec2Cap.lua @@ -0,0 +1,79 @@ +local m = require'lpeglabelrec' +local re = require'relabelrec' + +local terror = {} + +local function newError(s) + table.insert(terror, s) + return #terror +end + +local errUndef = newError("undefined") +local errId = newError("expecting an identifier") +local errComma = newError("expecting ','") + +local id = m.R'az'^1 + +local g = m.P{ + "S", + S = m.V"Id" * m.V"List", + List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", + Id = m.V"Sp" * m.C(id) + m.T(errId), + Comma = m.V"Sp" * "," + m.T(errComma), + Sp = m.S" \n\t"^0, +} + +local subject, errors + +function recorderror(pos, lab) + local line, col = re.calcline(subject, pos) + table.insert(errors, { line = line, col = col, msg = terror[lab] }) +end + +function record (lab) + return (m.Cp() * m.Cc(lab)) / recorderror +end + +function sync (p) + return (-p * m.P(1))^0 +end + +function defaultValue () + return m.Cc"NONE" +end + +local grec = m.P{ + "S", + S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), + ErrComma = record(errComma) * sync(-m.P(1) + id), + ErrId = record(errId) * sync(-m.P(1) + ",") * defaultValue() +} + + +function mymatch (g, s) + errors = {} + subject = s + io.write("Input: ", s, "\n") + local r = { g:match(s) } + io.write("Captures (separated by ';'): ") + for k, v in pairs(r) do + io.write(v .. "; ") + end + io.write("\nSyntactic errors found: " .. #errors) + if #errors > 0 then + io.write("\n") + local out = {} + for i, err in ipairs(errors) do + local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg + table.insert(out, msg) + end + io.write(table.concat(out, "\n")) + end + print("\n") + return r +end + +mymatch(grec, "one,two") +mymatch(grec, "one two three") +mymatch(grec, "1,\n two, \n3,") +mymatch(grec, "one\n two123, \nthree,") diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua index d092566..3988a3b 100644 --- a/examples/listIdRe1.lua +++ b/examples/listIdRe1.lua @@ -1,10 +1,10 @@ -local re = require 'relabel' +local re = require 'relabelrec' local g = re.compile[[ S <- Id List - List <- !. / (',' / %{2}) (Id / %{1}) List - Id <- Sp [a-z]+ - Comma <- Sp ',' + List <- !. / Comma Id List + Id <- Sp [a-z]+ / %{2} + Comma <- Sp ',' / %{3} Sp <- %s* ]] diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index fe30535..070bcdb 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua @@ -1,4 +1,4 @@ -local re = require 'relabel' +local re = require 'relabelrec' local errinfo = { {"errUndef", "undefined"}, @@ -18,23 +18,45 @@ re.setlabels(labels) local g = re.compile[[ S <- Id List - List <- !. / (',' / %{errComma}) (Id / %{errId}) List - Id <- Sp [a-z]+ - Comma <- Sp ',' + List <- !. / Comma Id List + Id <- Sp [a-z]+ / %{errId} + Comma <- Sp ',' / %{errComma} Sp <- %s* ]] +local errors + +function recorderror (subject, pos, label) + local line, col = re.calcline(subject, pos) + table.insert(errors, { line = line, col = col, msg = errmsgs[labels[label]] }) + return true +end + +function sync (p) + return '( !(' .. p .. ') .)*' +end + +local grec = re.compile( + "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. + "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('!. / [a-z]+') .. "\n" .. + "ErrId <- ('' -> 'errId' => recorderror) (!(!. / ',') .)*" + , {g = g, recorderror = recorderror}) + function mymatch (g, s) - 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 .. "'" + errors = {} + local r, e, sfail = g:match(s) + if #errors > 0 then + local out = {} + for i, err in ipairs(errors) do + local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg + table.insert(out, msg) + end + return nil, table.concat(out, "\n") end return r end - -print(mymatch(g, "one,two")) -print(mymatch(g, "one two")) -print(mymatch(g, "one,\n two,\nthree,")) - + +print(mymatch(grec, "one,two")) +print(mymatch(grec, "one two three")) +print(mymatch(grec, "1,\n two, \n3,")) +print(mymatch(grec, "one\n two123, \nthree,")) -- cgit v1.2.3-55-g6feb