From c1ac9cad179a8308dadb6f48cef9a40b124c1da1 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Thu, 7 Jul 2016 22:46:32 +0800 Subject: Adding example with error recovery --- examples/expectWithRecov.lua | 106 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 examples/expectWithRecov.lua diff --git a/examples/expectWithRecov.lua b/examples/expectWithRecov.lua new file mode 100644 index 0000000..8d894c1 --- /dev/null +++ b/examples/expectWithRecov.lua @@ -0,0 +1,106 @@ +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 = lpeg.T, lpeg.Lc + +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 = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); + Operand = expect(V"Term", "ExpTerm"); + Term = num + V"Group"; + Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); + InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("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] + out = out .. "syntax error: " .. msg .. " (at index " .. pos .. ")\n" + end + errors = {} + return nil, out + 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) -- cgit v1.2.3-55-g6feb From 8544119e5bd87b2c57bb7ed1f997519ad8e83f01 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Thu, 7 Jul 2016 23:45:11 +0800 Subject: Adding relabel example with error recovery --- examples/expectWithRecovRe.lua | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 examples/expectWithRecovRe.lua diff --git a/examples/expectWithRecovRe.lua b/examples/expectWithRecovRe.lua new file mode 100644 index 0000000..c424160 --- /dev/null +++ b/examples/expectWithRecovRe.lua @@ -0,0 +1,114 @@ +local lpeg = require"lpeglabel" +local re = require"relabel" + +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 = lpeg.T, lpeg.Lc + +local errinfo = { + {"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 labels = {} +local errmsgs = {} + +for i, err in ipairs(errinfo) do + labels[err[1]] = i + errmsgs[err[1]] = err[2] +end + +re.setlabels(labels) + +local errors = {} + +function recordError(input, pos, label) + table.insert(errors, {label, pos}) + return true +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] + 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 = re.compile([[ + S <- (Exp / ErrNoExp) (!. / ErrExtra) + Exp <- {| Term (op Operand)* |} -> compute + Operand <- Term / ErrExpTerm /{ExpTerm} dummy + Term <- num / Group + Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") + InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy + + op <- {[-+*/]} + num <- [0-9]+ -> tonumber + + ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} + ErrExtra <- ("" -> "Extra" => recordError) %{Extra} + ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} + ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} + ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} + + dummy <- "" -> "0" -> tonumber +]], { + compute = compute; + recordError = recordError; + tonumber = tonumber; +}) + +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 = errmsgs[err[1]] + local line, col = re.calcline(input, pos) + out = out .. "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")\n" + end + errors = {} + return nil, out + 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 (line 1, col 10) + +print(eval "(1+)-1*(2/2)") +--> syntax error: expected a term after the operator (line 1, col 4) + +print(eval "(1+1)-1*(/2)") +--> syntax error: expected an expression after the parenthesis (line 1, col 10) + +print(eval "1+(1-(1*2))/2x") +--> syntax error: extra characters found after the expression (line 1, col 14) + +print(eval "-1+(1-(1*2))/2") +--> syntax error: no expression found (line 1, col 1) + +print(eval "(1+1-1*(2/2+)-():") +--> syntax error: expected a term after the operator (line 1, col 13) +--> syntax error: expected an expression after the parenthesis (line 1, col 16) +--> syntax error: missing a closing ')' after the expression (line 1, col 17) +--> syntax error: extra characters found after the expression (line 1, col 17) -- cgit v1.2.3-55-g6feb From 5b7aa7aeba2be2b0e07bbae2b83433e68f3c0443 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Thu, 7 Jul 2016 23:54:31 +0800 Subject: Renaming the recovery examples --- examples/expectWithRecov.lua | 106 -------------------------------------- examples/expectWithRecovRe.lua | 114 ----------------------------------------- examples/recovery.lua | 106 ++++++++++++++++++++++++++++++++++++++ examples/recoveryRe.lua | 114 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 220 deletions(-) delete mode 100644 examples/expectWithRecov.lua delete mode 100644 examples/expectWithRecovRe.lua create mode 100644 examples/recovery.lua create mode 100644 examples/recoveryRe.lua diff --git a/examples/expectWithRecov.lua b/examples/expectWithRecov.lua deleted file mode 100644 index 8d894c1..0000000 --- a/examples/expectWithRecov.lua +++ /dev/null @@ -1,106 +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 = lpeg.T, lpeg.Lc - -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 = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); - Operand = expect(V"Term", "ExpTerm"); - Term = num + V"Group"; - Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); - InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("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] - out = out .. "syntax error: " .. msg .. " (at index " .. pos .. ")\n" - end - errors = {} - return nil, out - 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/expectWithRecovRe.lua b/examples/expectWithRecovRe.lua deleted file mode 100644 index c424160..0000000 --- a/examples/expectWithRecovRe.lua +++ /dev/null @@ -1,114 +0,0 @@ -local lpeg = require"lpeglabel" -local re = require"relabel" - -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 = lpeg.T, lpeg.Lc - -local errinfo = { - {"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 labels = {} -local errmsgs = {} - -for i, err in ipairs(errinfo) do - labels[err[1]] = i - errmsgs[err[1]] = err[2] -end - -re.setlabels(labels) - -local errors = {} - -function recordError(input, pos, label) - table.insert(errors, {label, pos}) - return true -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] - 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 = re.compile([[ - S <- (Exp / ErrNoExp) (!. / ErrExtra) - Exp <- {| Term (op Operand)* |} -> compute - Operand <- Term / ErrExpTerm /{ExpTerm} dummy - Term <- num / Group - Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") - InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy - - op <- {[-+*/]} - num <- [0-9]+ -> tonumber - - ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} - ErrExtra <- ("" -> "Extra" => recordError) %{Extra} - ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} - ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} - ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} - - dummy <- "" -> "0" -> tonumber -]], { - compute = compute; - recordError = recordError; - tonumber = tonumber; -}) - -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 = errmsgs[err[1]] - local line, col = re.calcline(input, pos) - out = out .. "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")\n" - end - errors = {} - return nil, out - 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 (line 1, col 10) - -print(eval "(1+)-1*(2/2)") ---> syntax error: expected a term after the operator (line 1, col 4) - -print(eval "(1+1)-1*(/2)") ---> syntax error: expected an expression after the parenthesis (line 1, col 10) - -print(eval "1+(1-(1*2))/2x") ---> syntax error: extra characters found after the expression (line 1, col 14) - -print(eval "-1+(1-(1*2))/2") ---> syntax error: no expression found (line 1, col 1) - -print(eval "(1+1-1*(2/2+)-():") ---> syntax error: expected a term after the operator (line 1, col 13) ---> syntax error: expected an expression after the parenthesis (line 1, col 16) ---> syntax error: missing a closing ')' after the expression (line 1, col 17) ---> syntax error: extra characters found after the expression (line 1, col 17) diff --git a/examples/recovery.lua b/examples/recovery.lua new file mode 100644 index 0000000..8d894c1 --- /dev/null +++ b/examples/recovery.lua @@ -0,0 +1,106 @@ +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 = lpeg.T, lpeg.Lc + +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 = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); + Operand = expect(V"Term", "ExpTerm"); + Term = num + V"Group"; + Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); + InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("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] + out = out .. "syntax error: " .. msg .. " (at index " .. pos .. ")\n" + end + errors = {} + return nil, out + 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/recoveryRe.lua b/examples/recoveryRe.lua new file mode 100644 index 0000000..c424160 --- /dev/null +++ b/examples/recoveryRe.lua @@ -0,0 +1,114 @@ +local lpeg = require"lpeglabel" +local re = require"relabel" + +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 = lpeg.T, lpeg.Lc + +local errinfo = { + {"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 labels = {} +local errmsgs = {} + +for i, err in ipairs(errinfo) do + labels[err[1]] = i + errmsgs[err[1]] = err[2] +end + +re.setlabels(labels) + +local errors = {} + +function recordError(input, pos, label) + table.insert(errors, {label, pos}) + return true +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] + 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 = re.compile([[ + S <- (Exp / ErrNoExp) (!. / ErrExtra) + Exp <- {| Term (op Operand)* |} -> compute + Operand <- Term / ErrExpTerm /{ExpTerm} dummy + Term <- num / Group + Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") + InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy + + op <- {[-+*/]} + num <- [0-9]+ -> tonumber + + ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} + ErrExtra <- ("" -> "Extra" => recordError) %{Extra} + ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} + ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} + ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} + + dummy <- "" -> "0" -> tonumber +]], { + compute = compute; + recordError = recordError; + tonumber = tonumber; +}) + +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 = errmsgs[err[1]] + local line, col = re.calcline(input, pos) + out = out .. "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")\n" + end + errors = {} + return nil, out + 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 (line 1, col 10) + +print(eval "(1+)-1*(2/2)") +--> syntax error: expected a term after the operator (line 1, col 4) + +print(eval "(1+1)-1*(/2)") +--> syntax error: expected an expression after the parenthesis (line 1, col 10) + +print(eval "1+(1-(1*2))/2x") +--> syntax error: extra characters found after the expression (line 1, col 14) + +print(eval "-1+(1-(1*2))/2") +--> syntax error: no expression found (line 1, col 1) + +print(eval "(1+1-1*(2/2+)-():") +--> syntax error: expected a term after the operator (line 1, col 13) +--> syntax error: expected an expression after the parenthesis (line 1, col 16) +--> syntax error: missing a closing ')' after the expression (line 1, col 17) +--> syntax error: extra characters found after the expression (line 1, col 17) -- cgit v1.2.3-55-g6feb From 7f6e794999e330c752b9c08442ca0a2a993c8eb9 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Sun, 10 Jul 2016 17:27:39 +0800 Subject: Switching to use the more idiomatic table.concat --- examples/recovery.lua | 6 +++--- examples/recoveryRe.lua | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/recovery.lua b/examples/recovery.lua index 8d894c1..7e039c7 100644 --- a/examples/recovery.lua +++ b/examples/recovery.lua @@ -70,14 +70,14 @@ local function eval(input) if #errors == 0 then return result else - local out = "" + local out = {} for i, err in ipairs(errors) do local pos = err[2] local msg = labels[err[1]][2] - out = out .. "syntax error: " .. msg .. " (at index " .. pos .. ")\n" + table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") end errors = {} - return nil, out + return nil, table.concat(out, "\n") end end diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua index c424160..05acf84 100644 --- a/examples/recoveryRe.lua +++ b/examples/recoveryRe.lua @@ -77,15 +77,15 @@ local function eval(input) if #errors == 0 then return result else - local out = "" + local out = {} for i, err in ipairs(errors) do local pos = err[2] local msg = errmsgs[err[1]] local line, col = re.calcline(input, pos) - out = out .. "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")\n" + table.insert(out, "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")") end errors = {} - return nil, out + return nil, table.concat(out, "\n") end end -- cgit v1.2.3-55-g6feb From e2e4403aa84ed863ab1ab930c43d7abfd8fb5500 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Mon, 11 Jul 2016 09:16:55 +0800 Subject: Removing unused imports in the re recovery example --- examples/recoveryRe.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua index 05acf84..50ec53a 100644 --- a/examples/recoveryRe.lua +++ b/examples/recoveryRe.lua @@ -1,10 +1,5 @@ -local lpeg = require"lpeglabel" local re = require"relabel" -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 = lpeg.T, lpeg.Lc - local errinfo = { {"NoExp", "no expression found"}, {"Extra", "extra characters found after the expression"}, -- cgit v1.2.3-55-g6feb From 882cc2e5a99585250e03c7454aa2fcbc33b46a82 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Mon, 11 Jul 2016 09:56:22 +0800 Subject: Adding comments to the original expect example --- examples/expect.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/expect.lua b/examples/expect.lua index 2b7e904..cb68d38 100644 --- a/examples/expect.lua +++ b/examples/expect.lua @@ -2,6 +2,9 @@ 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"}, @@ -10,6 +13,11 @@ local labels = { {"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 @@ -23,6 +31,9 @@ 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 @@ -41,6 +52,9 @@ local function compute(tokens) 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; @@ -50,6 +64,10 @@ local g = P { 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 -- cgit v1.2.3-55-g6feb From 6dcf7eb5bd2bf6aa35cb3f3d8b0394bf2f625093 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Mon, 11 Jul 2016 11:44:31 +0800 Subject: Adding comments to the recovery examples --- examples/recovery.lua | 28 ++++++++++++++++++++++++++++ examples/recoveryRe.lua | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/examples/recovery.lua b/examples/recovery.lua index 7e039c7..7af8455 100644 --- a/examples/recovery.lua +++ b/examples/recovery.lua @@ -4,6 +4,9 @@ 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 = lpeg.T, lpeg.Lc +-- 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"}, @@ -12,6 +15,9 @@ local labels = { {"MisClose", "missing a closing ')' after the expression"}, } +-- The `labelIndex` function gives us the index of a label in the +-- `labels` table, which serves as the integer representation of the label. +-- We need this because LPegLabel requires us to use integers for the labels. local function labelIndex(labname) for i, elem in ipairs(labels) do if elem[1] == labname then @@ -21,8 +27,13 @@ local function labelIndex(labname) error("could not find label: " .. labname) end +-- The `errors` table will hold the list of errors recorded during parsing local errors = {} +-- The `expect` function takes a pattern and a label and returns a pattern +-- that throws the specified label if the original pattern fails to match. +-- Before throwing the label, it records the label to be thrown along with +-- the position of the failure (index in input string) into the `errors` table. local function expect(patt, labname) local i = labelIndex(labname) function recordError(input, pos) @@ -35,6 +46,9 @@ 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 @@ -53,18 +67,32 @@ local function compute(tokens) 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. We have incorporated some error recovery startegies +-- to our grammar so that it may resume parsing even after encountering +-- an error, which allows us to report more errors. local g = P { "Exp", Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; + -- `OpRecov` handles missing terms/operands by returning a dummy (zero). OpRecov = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); Operand = expect(V"Term", "ExpTerm"); Term = num + V"Group"; + -- `Group` handles missing closing parenthesis by simply ignoring it. + -- Like all the others, the error is still recorded of course. Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); + -- `InnerExp` handles missing expressions by skipping to the next closing + -- parenthesis. A dummy (zero) is returned in place of the expression. InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("ExpExp")); } 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 messages and +-- positions of all the failures encountered. local function eval(input) local result, label, suffix = g:match(input) if #errors == 0 then diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua index 50ec53a..795e01d 100644 --- a/examples/recoveryRe.lua +++ b/examples/recoveryRe.lua @@ -1,5 +1,8 @@ local re = require"relabel" +-- The `errinfo` 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 errinfo = { {"NoExp", "no expression found"}, {"Extra", "extra characters found after the expression"}, @@ -8,6 +11,10 @@ local errinfo = { {"MisClose", "missing a closing ')' after the expression"}, } +-- We split the errinfo table into two tables: `labels` which is a +-- mapping from the label names to its integer representation, and +-- `errmsgs` which is a mapping from the label names to its +-- corresponding error message. local labels = {} local errmsgs = {} @@ -16,15 +23,26 @@ for i, err in ipairs(errinfo) do errmsgs[err[1]] = err[2] end +-- The `labels` table is especially useful for making our re grammar more +-- readable through the use of the `setlabels` function which allows us +-- to use the label names directly in the re grammar instead of the integers. re.setlabels(labels) +-- The `errors` table will hold the list of errors recorded during parsing local errors = {} +-- The `recordError` function simply records the label and position of +-- the failure (index in input string) into the `errors` table. +-- Note: The unused `input` parameter is necessary, as this will be called +-- by LPeg's match-time capture. function recordError(input, pos, label) table.insert(errors, {label, pos}) return true end +-- 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 @@ -43,17 +61,27 @@ local function compute(tokens) 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. We have incorporated some error recovery startegies +-- to our grammar so that it may resume parsing even after encountering +-- an error, which allows us to report more errors. local g = re.compile([[ S <- (Exp / ErrNoExp) (!. / ErrExtra) Exp <- {| Term (op Operand)* |} -> compute + -- If we encounter a missing term/operand, we return a dummy instead. Operand <- Term / ErrExpTerm /{ExpTerm} dummy Term <- num / Group + -- If we encounter a missing closing parenthesis, we ignore it. Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") + -- If we encounter a missing inner expression, we skip to the next + -- closing parenthesis, and return a dummy in its place. InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy - op <- {[-+*/]} + op <- {[-+*/]} num <- [0-9]+ -> tonumber + -- Before throwing an error, we make sure to record it first. ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} ErrExtra <- ("" -> "Extra" => recordError) %{Extra} ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} @@ -67,6 +95,10 @@ local g = re.compile([[ tonumber = tonumber; }) +-- 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 messages and +-- positions of all the failures encountered. local function eval(input) local result, label, suffix = g:match(input) if #errors == 0 then -- cgit v1.2.3-55-g6feb From 71e4d12c25093bf43284d39bc58db99689591b27 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Mon, 11 Jul 2016 11:50:59 +0800 Subject: Switching to use lowercase function names for consistency --- examples/recovery.lua | 16 ++++++++-------- examples/recoveryRe.lua | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/recovery.lua b/examples/recovery.lua index 7af8455..3272ae7 100644 --- a/examples/recovery.lua +++ b/examples/recovery.lua @@ -15,10 +15,10 @@ local labels = { {"MisClose", "missing a closing ')' after the expression"}, } --- The `labelIndex` function gives us the index of a label in the +-- The `labelindex` function gives us the index of a label in the -- `labels` table, which serves as the integer representation of the label. -- We need this because LPegLabel requires us to use integers for the labels. -local function labelIndex(labname) +local function labelindex(labname) for i, elem in ipairs(labels) do if elem[1] == labname then return i @@ -35,12 +35,12 @@ local errors = {} -- Before throwing the label, it records the label to be thrown along with -- the position of the failure (index in input string) into the `errors` table. local function expect(patt, labname) - local i = labelIndex(labname) - function recordError(input, pos) + local i = labelindex(labname) + function recorderror(input, pos) table.insert(errors, {i, pos}) return true end - return patt + Cmt("", recordError) * T(i) + return patt + Cmt("", recorderror) * T(i) end local num = R("09")^1 / tonumber @@ -76,15 +76,15 @@ local g = P { "Exp", Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; -- `OpRecov` handles missing terms/operands by returning a dummy (zero). - OpRecov = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); + OpRecov = Lc(V"Operand", Cc(0), labelindex("ExpTerm")); Operand = expect(V"Term", "ExpTerm"); Term = num + V"Group"; -- `Group` handles missing closing parenthesis by simply ignoring it. -- Like all the others, the error is still recorded of course. - Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); + Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelindex("MisClose")); -- `InnerExp` handles missing expressions by skipping to the next closing -- parenthesis. A dummy (zero) is returned in place of the expression. - InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("ExpExp")); + InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")); } g = expect(g, "NoExp") * expect(-P(1), "Extra") diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua index 795e01d..3b83d88 100644 --- a/examples/recoveryRe.lua +++ b/examples/recoveryRe.lua @@ -31,11 +31,11 @@ re.setlabels(labels) -- The `errors` table will hold the list of errors recorded during parsing local errors = {} --- The `recordError` function simply records the label and position of +-- The `recorderror` function simply records the label and position of -- the failure (index in input string) into the `errors` table. -- Note: The unused `input` parameter is necessary, as this will be called -- by LPeg's match-time capture. -function recordError(input, pos, label) +local function recorderror(input, pos, label) table.insert(errors, {label, pos}) return true end @@ -82,16 +82,16 @@ local g = re.compile([[ num <- [0-9]+ -> tonumber -- Before throwing an error, we make sure to record it first. - ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} - ErrExtra <- ("" -> "Extra" => recordError) %{Extra} - ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} - ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} - ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} + ErrNoExp <- ("" -> "NoExp" => recorderror) %{NoExp} + ErrExtra <- ("" -> "Extra" => recorderror) %{Extra} + ErrExpTerm <- ("" -> "ExpTerm" => recorderror) %{ExpTerm} + ErrExpExp <- ("" -> "ExpExp" => recorderror) %{ExpExp} + ErrMisClose <- ("" -> "MisClose" => recorderror) %{MisClose} dummy <- "" -> "0" -> tonumber ]], { compute = compute; - recordError = recordError; + recorderror = recorderror; tonumber = tonumber; }) -- cgit v1.2.3-55-g6feb