From 2ea15094a1d4a9f422c983480aab7231951ee20e Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Wed, 7 Dec 2016 08:50:03 -0300 Subject: Renaming files and removing example based on another version --- examples/expRec.lua | 128 ++++++++++++++++++++++++++++++++++++++ examples/expRecAut.lua | 132 ++++++++++++++++++++++++++++++++++++++++ examples/expect.lua | 98 ----------------------------- examples/expressionRecAut.lua | 132 ---------------------------------------- examples/expressionRecovery.lua | 128 -------------------------------------- 5 files changed, 260 insertions(+), 358 deletions(-) create mode 100644 examples/expRec.lua create mode 100644 examples/expRecAut.lua delete mode 100644 examples/expect.lua delete mode 100644 examples/expressionRecAut.lua delete mode 100644 examples/expressionRecovery.lua diff --git a/examples/expRec.lua b/examples/expRec.lua new file mode 100644 index 0000000..c5cbcca --- /dev/null +++ b/examples/expRec.lua @@ -0,0 +1,128 @@ +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 + 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 "1+(") + +print(eval "3)") + diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua new file mode 100644 index 0000000..e098078 --- /dev/null +++ b/examples/expRecAut.lua @@ -0,0 +1,132 @@ +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 num = R("09")^1 / tonumber +local op = S("+-") + +local labels = {} +local nlabels = 0 + +local function newError(lab, msg, psync, pcap) + nlabels = nlabels + 1 + psync = psync or m.P(-1) + pcap = pcap or m.P"" + labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } +end + +newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) +newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000)) +newError("MisClose", "missing a closing ')' after the expression", m.P")") +newError("Extra", "extra characters found after the expression") + +local errors, subject + +local function expect(patt, labname) + local i = labels[labname].id + return patt + T(i) +end + +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].msg }) +end + +function record (labname) + return (m.Cp() * m.Cc(labname)) / recorderror +end + +function sync (p) + return (-p * m.P(1))^0 +end + +function defaultValue (p) + return p or m.Cc(1000) +end + +local recg2 = g +for k, v in pairs(labels) do + recg2 = Rec(recg2, record(k) * sync(v.psync) * v.pcap, v.id) +end + +local recg = P { + "S", + S = Rec(V"A", V"ErrExpTermFirst", labels["ExpTermFirst"].id), -- default value is 0 + A = Rec(V"Sg", V"ErrExpTermOp", labels["ExpTermOp"].id), + Sg = Rec(g, V"ErrMisClose", labels["MisClose"].id), + 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 = recg2: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 "") + +print(eval "1+()+") + +print(eval "1+(") + +print(eval "3)") + diff --git a/examples/expect.lua b/examples/expect.lua deleted file mode 100644 index cb68d38..0000000 --- a/examples/expect.lua +++ /dev/null @@ -1,98 +0,0 @@ -local lpeg = require"lpeglabel" - -local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T - --- The `labels` table contains the list of labels that we will be using --- as well as the corresponding error message for each label, which will --- be used in our error reporting later on. -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"}, -} - --- The `expect` function takes a pattern and a label defined in --- the `labels` table and returns a pattern that throws the specified --- label if the original pattern fails to match. --- Note: LPegLabel requires us to use integers for the labels, so we --- use the index of the label in the `labels` table to represent it. -local function expect(patt, labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return patt + T(i) - end - end - - error("could not find label: " .. labname) -end - -local num = R("09")^1 / tonumber -local op = S("+-*/") - --- The `compute` function takes an alternating list of numbers and --- operators and computes the result of applying the operations --- to the numbers in a left to right order (no operator precedence). -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 - --- Our grammar is a simple arithmetic expression of integers that --- does not take operator precedence into account but allows grouping --- via parenthesis. -local g = P { - "Exp", - Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute; - Term = num + V"Group"; - Group = "(" * expect(V"Exp", "ExpExp") * expect(")", "MisClose"); -} - -g = expect(g, "NoExp") * expect(-P(1), "Extra") - --- The `eval` function takes an input string to match against the grammar --- we've just defined. If the input string matches, then the result of the --- computation is returned, otherwise we return the error message and --- position of the first failure encountered. -local function eval(input) - local result, label, suffix = g:match(input) - if result ~= nil then - return result - else - local pos = input:len() - suffix:len() + 1 - local msg = labels[label][2] - return nil, "syntax error: " .. msg .. " (at index " .. pos .. ")" - 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) diff --git a/examples/expressionRecAut.lua b/examples/expressionRecAut.lua deleted file mode 100644 index e098078..0000000 --- a/examples/expressionRecAut.lua +++ /dev/null @@ -1,132 +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 num = R("09")^1 / tonumber -local op = S("+-") - -local labels = {} -local nlabels = 0 - -local function newError(lab, msg, psync, pcap) - nlabels = nlabels + 1 - psync = psync or m.P(-1) - pcap = pcap or m.P"" - labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } -end - -newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) -newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000)) -newError("MisClose", "missing a closing ')' after the expression", m.P")") -newError("Extra", "extra characters found after the expression") - -local errors, subject - -local function expect(patt, labname) - local i = labels[labname].id - return patt + T(i) -end - -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].msg }) -end - -function record (labname) - return (m.Cp() * m.Cc(labname)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue (p) - return p or m.Cc(1000) -end - -local recg2 = g -for k, v in pairs(labels) do - recg2 = Rec(recg2, record(k) * sync(v.psync) * v.pcap, v.id) -end - -local recg = P { - "S", - S = Rec(V"A", V"ErrExpTermFirst", labels["ExpTermFirst"].id), -- default value is 0 - A = Rec(V"Sg", V"ErrExpTermOp", labels["ExpTermOp"].id), - Sg = Rec(g, V"ErrMisClose", labels["MisClose"].id), - 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 = recg2: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 "") - -print(eval "1+()+") - -print(eval "1+(") - -print(eval "3)") - diff --git a/examples/expressionRecovery.lua b/examples/expressionRecovery.lua deleted file mode 100644 index c5cbcca..0000000 --- a/examples/expressionRecovery.lua +++ /dev/null @@ -1,128 +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(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 - 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 "1+(") - -print(eval "3)") - -- cgit v1.2.3-55-g6feb