From e7e17699870f0bd6ba43b4e946297fb581d28b48 Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Tue, 29 Nov 2016 18:06:10 -0300 Subject: Updating examples --- examples/expressionRecovery.lua | 127 ++++++++++++++++++++++++++++++++++++++++ examples/recoveryOpFail.lua | 108 ---------------------------------- examples/recoveryOpLab.lua | 107 --------------------------------- examples/recoveryRecCap.lua | 121 -------------------------------------- examples/typedlua/test.lua | 9 ++- examples/typedlua/tllexer.lua | 11 ++-- examples/typedlua/tlparser.lua | 2 +- testlabel.lua | 9 +++ 8 files changed, 148 insertions(+), 346 deletions(-) create mode 100644 examples/expressionRecovery.lua delete mode 100644 examples/recoveryOpFail.lua delete mode 100644 examples/recoveryOpLab.lua delete mode 100644 examples/recoveryRecCap.lua diff --git a/examples/expressionRecovery.lua b/examples/expressionRecovery.lua new file mode 100644 index 0000000..a4a3288 --- /dev/null +++ b/examples/expressionRecovery.lua @@ -0,0 +1,127 @@ +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(1000) +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 + print("sub", subject) + 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 "") + +print(eval "1+()+") + +print(eval "3)") + diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua deleted file mode 100644 index bd1beb7..0000000 --- a/examples/recoveryOpFail.lua +++ /dev/null @@ -1,108 +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, Lc, Rec = lpeg.T, lpeg.Lc, 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 - if not recpatt then recpatt = P"" end - return Rec(patt, Cmt("", recorderror) * recpatt) -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] - elseif 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"Term" * (C(op) * V"OpRecov")^0) / compute; - OpRecov = V"Operand"; - Operand = expect(V"Term", "ExpTerm", Cc(0)); - Term = num + V"Group"; - Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); - InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); -} - -g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") - -local function eval(input) - local result, label, suffix = g: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 "98-76*(54/32)") ---> 37.125 - -print(eval "(1+1-1*2/2") ---> syntax error: missing a closing ')' after the expression (at index 11) - -print(eval "(1+)-1*(2/2)") ---> syntax error: expected a term after the operator (at index 4) - -print(eval "(1+1)-1*(/2)") ---> syntax error: expected an expression after the parenthesis (at index 10) - -print(eval "1+(1-(1*2))/2x") ---> syntax error: extra chracters found after the expression (at index 14) - -print(eval "-1+(1-(1*2))/2") ---> syntax error: no expression found (at index 1) - -print(eval "(1+1-1*(2/2+)-():") ---> syntax error: expected a term after the operator (at index 13) ---> syntax error: expected an expression after the parenthesis (at index 16) ---> syntax error: missing a closing ')' after the expression (at index 17) ---> syntax error: extra characters found after the expression (at index 17) diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua deleted file mode 100644 index 6697f8b..0000000 --- a/examples/recoveryOpLab.lua +++ /dev/null @@ -1,107 +0,0 @@ -local lpeg = require"lpeglabel" - -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, Lc, Rec = lpeg.T, lpeg.Lc, 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) - local i = labelindex(labname) - function recorderror(input, pos) - table.insert(errors, {i, pos}) - return true - end - return patt + Cmt("", recorderror) * 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] - elseif 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"Term" * (C(op) * V"OpRecov")^0) / compute; - OpRecov = Rec(V"Operand", Cc(0), labelindex("ExpTerm")); - Operand = expect(V"Term", "ExpTerm"); - Term = num + Rec(V"Group", P"", labelindex("MisClose")); - Group = "(" * Rec(V"InnerExp", (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")) * expect(")", "MisClose"); - InnerExp = expect(V"Exp", "ExpExp"); -} - -g = expect(g, "NoExp") * expect(-P(1), "Extra") - -local function eval(input) - local result, label, suffix = g: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 "98-76*(54/32)") ---> 37.125 - -print(eval "(1+1-1*2/2") ---> syntax error: missing a closing ')' after the expression (at index 11) - -print(eval "(1+)-1*(2/2)") ---> syntax error: expected a term after the operator (at index 4) - -print(eval "(1+1)-1*(/2)") ---> syntax error: expected an expression after the parenthesis (at index 10) - -print(eval "1+(1-(1*2))/2x") ---> syntax error: extra chracters found after the expression (at index 14) - -print(eval "-1+(1-(1*2))/2") ---> syntax error: no expression found (at index 1) - -print(eval "(1+1-1*(2/2+)-():") ---> syntax error: expected a term after the operator (at index 13) ---> syntax error: expected an expression after the parenthesis (at index 16) ---> syntax error: missing a closing ')' after the expression (at index 17) ---> syntax error: extra characters found after the expression (at index 17) diff --git a/examples/recoveryRecCap.lua b/examples/recoveryRecCap.lua deleted file mode 100644 index c1b3a53..0000000 --- a/examples/recoveryRecCap.lua +++ /dev/null @@ -1,121 +0,0 @@ -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 "") diff --git a/examples/typedlua/test.lua b/examples/typedlua/test.lua index ed4e7a1..95474ba 100755 --- a/examples/typedlua/test.lua +++ b/examples/typedlua/test.lua @@ -401,15 +401,18 @@ assert(m == e) -- unfinished comments s = [=[ ---[[ testing +--[[ + +testing unfinished + comment -]=] + ]=] --[=[ test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.' ]=] e = [=[ -test.lua:1:1: unfinished long comment +test.lua:1:2: unfinished long comment ]=] r, m = parse(s) diff --git a/examples/typedlua/tllexer.lua b/examples/typedlua/tllexer.lua index 6517ba5..d6033ec 100644 --- a/examples/typedlua/tllexer.lua +++ b/examples/typedlua/tllexer.lua @@ -1,6 +1,6 @@ local tllexer = {} -local lpeg = require "lpeglabel" +local lpeg = require "lpeglabelrec" lpeg.locale(lpeg) local tlerror = require "tlerror" @@ -9,10 +9,6 @@ function tllexer.try (pat, label) return pat + lpeg.T(tlerror.labels[label]) end -function tllexer.catch (pat, label) - return lpeg.Lc(pat, lpeg.P(false), tlerror.labels[label]) -end - local function setffp (s, i, t, n) if not t.ffp or i > t.ffp then t.ffp = i @@ -45,7 +41,10 @@ local CloseEQ = lpeg.Cmt(Close * lpeg.Cb("init"), local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") / function (s, o) return s end -local Comment = lpeg.Lc(lpeg.P("--") * LongString / function () return end, +local LongStringCm1 = Open * (lpeg.P(1) - CloseEQ)^0 * Close / + function (s, o) return s end + +local Comment = lpeg.Rec(lpeg.P"--" * #Open * (LongStringCm1 / function() return end + lpeg.T(tlerror.labels["LongString"])), lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) + lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 diff --git a/examples/typedlua/tlparser.lua b/examples/typedlua/tlparser.lua index a301fa6..fe4fd5e 100644 --- a/examples/typedlua/tlparser.lua +++ b/examples/typedlua/tlparser.lua @@ -1,6 +1,6 @@ local tlparser = {} -local lpeg = require "lpeglabel" +local lpeg = require "lpeglabelrec" lpeg.locale(lpeg) local tllexer = require "tllexer" diff --git a/testlabel.lua b/testlabel.lua index cb92657..d9bac64 100644 --- a/testlabel.lua +++ b/testlabel.lua @@ -173,6 +173,15 @@ p = m.Rec(#m.T(22), m.P"a", 22) r, l, serror = p:match("bbc") assert(r == nil and l == 0 and serror == "bbc") +p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) +r, l, serror = p:match("abc") +assert(r == nil and l == 15 and serror == "abc") + +p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) +r, l, serror = p:match("abc") +assert(r == nil and l == 15 and serror == "bc") + + -- tests related to repetition p = m.T(1)^0 -- cgit v1.2.3-55-g6feb