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 (limited to 'examples') 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