From 3a5f5683bf6d676ea4118c3a8d0fc25de317d91f Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Wed, 23 Nov 2016 15:22:46 -0300 Subject: Updating examples to use the recovery operator --- examples/listId2Rec2.lua | 4 +- examples/listId2Rec2Cap.lua | 4 +- examples/recoveryFabio.lua | 119 ------------------------------------------- examples/recoveryRecCap.lua | 121 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 123 deletions(-) delete mode 100644 examples/recoveryFabio.lua create mode 100644 examples/recoveryRecCap.lua diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua index a347a5b..ab8f1dd 100644 --- a/examples/listId2Rec2.lua +++ b/examples/listId2Rec2.lua @@ -41,8 +41,8 @@ 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) + ",") + ErrComma = record(errComma) * sync(id), + ErrId = record(errId) * sync(m.P",") } diff --git a/examples/listId2Rec2Cap.lua b/examples/listId2Rec2Cap.lua index f9fc2bd..1c22c88 100644 --- a/examples/listId2Rec2Cap.lua +++ b/examples/listId2Rec2Cap.lua @@ -45,8 +45,8 @@ 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() + ErrComma = record(errComma) * sync(id), + ErrId = record(errId) * sync(m.P",") * defaultValue(), } diff --git a/examples/recoveryFabio.lua b/examples/recoveryFabio.lua deleted file mode 100644 index f16c9e7..0000000 --- a/examples/recoveryFabio.lua +++ /dev/null @@ -1,119 +0,0 @@ -local lpeg = require"lpeglabelrec" - -local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V -local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt -local T, Rec = lpeg.T, lpeg.Rec - -local labels = { - {"NoExp", "no expression found"}, - {"Extra", "extra characters found after the expression"}, - {"ExpTerm", "expected a term after the operator"}, - {"ExpExp", "expected an expression after the parenthesis"}, - {"MisClose", "missing a closing ')' after the expression"}, -} - -local function labelindex(labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return i - end - end - error("could not find label: " .. labname) -end - -local errors = {} - -local function expect(patt, labname, recpatt) - local i = labelindex(labname) - function recorderror(input, pos) - table.insert(errors, {i, pos}) - return true - end - return patt + T(i) -end - -local num = R("09")^1 / tonumber -local op = S("+-") - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = P { - "Exp", - Exp = Ct(V"Operand" * (C(op) * V"Operand")^0) / compute, - Operand = expect(V"Term", "ExpTerm"), - Term = num + V"Group", - Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); - InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(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 (labname) - return (m.Cp() * m.Cc(labelindex(labname))) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue () - return m.Cc"NONE" -end - -local recg = P { - "S", - S = Rec(m.V"A", Cc(0), labelindex("ExpTerm")), -- default value is 0 - A = Rec(m.V"B", Cc(0), labelindex("ExpExp")), - B = Rec(m.V"Sg", Cc(0), labelindex("InnerExp")), - Sg = Rec(g, Cc(0), labelindex("MisClose")), - ErrExpTerm = record(labelindex("ExpTerm")) * sync() * defaultValue() -} - - -local function eval(input) - local result, label, suffix = recg:match(input) - if #errors == 0 then - return result - else - local out = {} - for i, err in ipairs(errors) do - local pos = err[2] - local msg = labels[err[1]][2] - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - errors = {} - return nil, table.concat(out, "\n") - end -end - -print(eval "90-70*5") ---> 20 - -print(eval "2+") ---> 2 + 0 - -print(eval "-2") ---> 0 - 2 - - -print(eval "1+3+-9") ---> 1 + 3 + 0 - 9 - diff --git a/examples/recoveryRecCap.lua b/examples/recoveryRecCap.lua new file mode 100644 index 0000000..c1b3a53 --- /dev/null +++ b/examples/recoveryRecCap.lua @@ -0,0 +1,121 @@ +local m = require"lpeglabelrec" +local re = require"relabelrec" + +local R, S, P, V = m.R, m.S, m.P, m.V +local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt +local T, Rec = m.T, m.Rec + +local labels = { + {"ExpTermFirst", "expected an expression"}, + {"ExpTermOp", "expected a term after the operator"}, + {"MisClose", "missing a closing ')' after the expression"}, +} + +local function labelindex(labname) + for i, elem in ipairs(labels) do + if elem[1] == labname then + return i + end + end + error("could not find label: " .. labname) +end + +local errors, subject + +local function expect(patt, labname, recpatt) + local i = labelindex(labname) + return patt + T(i) +end + + +local num = R("09")^1 / tonumber +local op = S("+-") + +local function compute(tokens) + local result = tokens[1] + for i = 2, #tokens, 2 do + if tokens[i] == '+' then + result = result + tokens[i+1] + elseif tokens[i] == '-' then + result = result - tokens[i+1] + else + error('unknown operation: ' .. tokens[i]) + end + end + return result +end + +local g = P { + "Exp", + Exp = Ct(V"OperandFirst" * (C(op) * V"Operand")^0) / compute, + OperandFirst = expect(V"Term", "ExpTermFirst"), + Operand = expect(V"Term", "ExpTermOp"), + Term = num + V"Group", + Group = "(" * V"Exp" * expect(")", "MisClose"), +} + +function recorderror(pos, lab) + local line, col = re.calcline(subject, pos) + table.insert(errors, { line = line, col = col, msg = labels[lab][2] }) +end + +function record (labname) + return (m.Cp() * m.Cc(labelindex(labname))) / recorderror +end + +function sync (p) + return (-p * m.P(1))^0 +end + +function defaultValue (p) + return p or m.Cc(0) +end + +local recg = P { + "S", + S = Rec(V"A", V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 + A = Rec(V"Sg", V"ErrExpTermOp", labelindex("ExpTermOp")), + Sg = Rec(g, V"ErrMisClose", labelindex("MisClose")), + ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), + ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), + ErrMisClose = record("MisClose") * sync(P")") * defaultValue(m.P""), +} + + +local function eval(input) + errors = {} + subject = input + local result, label, suffix = recg:match(input) + if #errors > 0 then + local out = {} + for i, err in ipairs(errors) do + local pos = err.col + local msg = err.msg + table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") + end + print(table.concat(out, "\n")) + end + return result +end + +print(eval "90-70*5") +--> 20 + +print(eval "2+") +--> 2 + 0 + +print(eval "-2") +--> 0 - 2 + +print(eval "1+3+-9") +--> 1 + 3 + [0] - 9 + +print(eval "1+()3+") +--> 1 + ([0]) [+] 3 + [0] + +print(eval "8-(2+)-5") +--> 8 - (2 + [0]) - 5 + +print(eval "()") + +print(eval "") -- cgit v1.2.3-55-g6feb