diff options
| author | Sérgio Medeiros <sqmedeiros@gmail.com> | 2016-09-01 15:19:28 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-09-01 15:19:28 -0300 |
| commit | 63e712a2fe81d917389463b7d4b069b0d18a2ffa (patch) | |
| tree | 4ba8b110d8a783329c707ea25f15c6326a96c097 /examples | |
| parent | 727ee89b490f91a59cd4147c717aa35939940a57 (diff) | |
| parent | d0fbb9d070d5675fba11a7f8c7d0ce95b55dcb3e (diff) | |
| download | lpeglabel-63e712a2fe81d917389463b7d4b069b0d18a2ffa.tar.gz lpeglabel-63e712a2fe81d917389463b7d4b069b0d18a2ffa.tar.bz2 lpeglabel-63e712a2fe81d917389463b7d4b069b0d18a2ffa.zip | |
Merge pull request #14 from undecidabot/master
Adding documentation for the recovery operator
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/recoveryOpFail.lua | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua index d65b9e0..6ddc6a2 100644 --- a/examples/recoveryOpFail.lua +++ b/examples/recoveryOpFail.lua | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | local lpeg = require"lpeglabel" | 1 | local lpeg = require"lpeglabel" |
| 2 | 2 | ||
| 3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | 3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V |
| 4 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | 4 | local C, Cc, Ct, Cmt, Carg = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt, lpeg.Carg |
| 5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec | 5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec |
| 6 | 6 | ||
| 7 | local labels = { | 7 | local labels = { |
| @@ -21,16 +21,14 @@ local function labelindex(labname) | |||
| 21 | error("could not find label: " .. labname) | 21 | error("could not find label: " .. labname) |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | local errors = {} | ||
| 25 | |||
| 26 | local function expect(patt, labname, recpatt) | 24 | local function expect(patt, labname, recpatt) |
| 27 | local i = labelindex(labname) | 25 | local i = labelindex(labname) |
| 28 | function recorderror(input, pos) | 26 | local function recorderror(input, pos, errors) |
| 29 | table.insert(errors, {i, pos}) | 27 | table.insert(errors, {i, pos}) |
| 30 | return true | 28 | return true |
| 31 | end | 29 | end |
| 32 | if not recpatt then recpatt = P"" end | 30 | if not recpatt then recpatt = P"" end |
| 33 | return Rec(patt, Cmt("", recorderror) * recpatt) | 31 | return Rec(patt, Cmt(Carg(1), recorderror) * recpatt) |
| 34 | end | 32 | end |
| 35 | 33 | ||
| 36 | local num = R("09")^1 / tonumber | 34 | local num = R("09")^1 / tonumber |
| @@ -57,18 +55,18 @@ end | |||
| 57 | 55 | ||
| 58 | local g = P { | 56 | local g = P { |
| 59 | "Exp", | 57 | "Exp", |
| 60 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | 58 | Exp = Ct(V"Term" * (C(op) * V"Operand")^0) / compute; |
| 61 | OpRecov = V"Operand"; | ||
| 62 | Operand = expect(V"Term", "ExpTerm", Cc(0)); | 59 | Operand = expect(V"Term", "ExpTerm", Cc(0)); |
| 63 | Term = num + V"Group"; | 60 | Term = num + V"Group"; |
| 64 | Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); | 61 | Group = "(" * V"InnerExp" * expect(")", "MisClose"); |
| 65 | InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); | 62 | InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); |
| 66 | } | 63 | } |
| 67 | 64 | ||
| 68 | g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") | 65 | g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") |
| 69 | 66 | ||
| 70 | local function eval(input) | 67 | local function eval(input) |
| 71 | local result, label, suffix = g:match(input) | 68 | local errors = {} |
| 69 | local result, label, suffix = g:match(input, 1, errors) | ||
| 72 | if #errors == 0 then | 70 | if #errors == 0 then |
| 73 | return result | 71 | return result |
| 74 | else | 72 | else |
| @@ -78,7 +76,6 @@ local function eval(input) | |||
| 78 | local msg = labels[err[1]][2] | 76 | local msg = labels[err[1]][2] |
| 79 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | 77 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") |
| 80 | end | 78 | end |
| 81 | errors = {} | ||
| 82 | return nil, table.concat(out, "\n") | 79 | return nil, table.concat(out, "\n") |
| 83 | end | 80 | end |
| 84 | end | 81 | end |
