From 20cb6e1443f3b79a4db3fedf25bc2eff91ef7d70 Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Tue, 13 Dec 2016 13:58:50 -0300 Subject: Fixing README.md after the merge --- README.md | 114 -------------------------------------------------------------- 1 file changed, 114 deletions(-) diff --git a/README.md b/README.md index 9484b3d..9bc162d 100644 --- a/README.md +++ b/README.md @@ -74,15 +74,6 @@ Otherwise, the result of the matching of `p1` is the pattern's result. -#### lpeglabel.Rec(p1, p2 [, l1, ..., ln]) - -The *recovery operator* is similar to labeled order choice except -that the matching of `p2` is tried from the failure position of `p1`. - -If no label is provided, the regular PEG failure is caught -i.e. `lpeg.Rec(p1, p2)` is equivalent to `lpeg.Rec(p1, p2, 0)`. - - #### %{l} Syntax of *relabelrec* module. Equivalent to `lpeg.T(l)`. @@ -698,108 +689,3 @@ print(eval "3)") -- syntax error: extra characters found after the expression (at index 2) -- Result = 3 ``` - -#### Error Recovery - -By using labeled ordered choice or the recovery operator, when a label -is thrown, the parser may record the error and still continue parsing -to find more errors. We can even record the error right away without -actually throwing a label (relying on the regular PEG failure instead). -Below we rewrite the arithmetic expression example and modify -the `expect` function to use the recovery operator for error recovery: - -```lua -local lpeg = require"lpeglabel" - -local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V -local C, Cc, Ct, Cmt, Carg = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt, lpeg.Carg -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 function expect(patt, labname, recpatt) - local i = labelindex(labname) - local function recorderror(input, pos, errors) - table.insert(errors, {i, pos}) - return true - end - if not recpatt then recpatt = P"" end - return Rec(patt, Cmt(Carg(1), 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"Operand")^0) / compute; - 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 errors = {} - local result, label, suffix = g:match(input, 1, errors) - 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 - 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: 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