From 448762908fd822fbc101a4fe66fac9cd8aa913b5 Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Mon, 14 Nov 2016 17:15:27 -0300 Subject: Changing documentation and examples with recovery --- examples/listId2Rec2.lua | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/listId2Rec2.lua (limited to 'examples/listId2Rec2.lua') diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua new file mode 100644 index 0000000..a347a5b --- /dev/null +++ b/examples/listId2Rec2.lua @@ -0,0 +1,67 @@ +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" * 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 + +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) + ",") +} + + +function mymatch (g, s) + errors = {} + subject = s + 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(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