diff options
| author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-07-15 15:45:14 -0300 |
|---|---|---|
| committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-07-15 15:45:14 -0300 |
| commit | 28a44f01cb7a75068b75a99c0badb3abdaf7047a (patch) | |
| tree | c00b466179116dba369c09811510b31146d30f71 /examples | |
| parent | 5dd46ae31c3455a7cd6df2a721fee723a648e695 (diff) | |
| download | lpeglabel-28a44f01cb7a75068b75a99c0badb3abdaf7047a.tar.gz lpeglabel-28a44f01cb7a75068b75a99c0badb3abdaf7047a.tar.bz2 lpeglabel-28a44f01cb7a75068b75a99c0badb3abdaf7047a.zip | |
Adding examples using the new recovery operator
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/recoveryOpFail.lua | 108 | ||||
| -rw-r--r-- | examples/recoveryOpLab.lua | 107 |
2 files changed, 215 insertions, 0 deletions
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua new file mode 100644 index 0000000..d65b9e0 --- /dev/null +++ b/examples/recoveryOpFail.lua | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | local lpeg = require"lpeglabel" | ||
| 2 | |||
| 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 | ||
| 5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec | ||
| 6 | |||
| 7 | local labels = { | ||
| 8 | {"NoExp", "no expression found"}, | ||
| 9 | {"Extra", "extra characters found after the expression"}, | ||
| 10 | {"ExpTerm", "expected a term after the operator"}, | ||
| 11 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
| 12 | {"MisClose", "missing a closing ')' after the expression"}, | ||
| 13 | } | ||
| 14 | |||
| 15 | local function labelindex(labname) | ||
| 16 | for i, elem in ipairs(labels) do | ||
| 17 | if elem[1] == labname then | ||
| 18 | return i | ||
| 19 | end | ||
| 20 | end | ||
| 21 | error("could not find label: " .. labname) | ||
| 22 | end | ||
| 23 | |||
| 24 | local errors = {} | ||
| 25 | |||
| 26 | local function expect(patt, labname, recpatt) | ||
| 27 | local i = labelindex(labname) | ||
| 28 | function recorderror(input, pos) | ||
| 29 | table.insert(errors, {i, pos}) | ||
| 30 | return true | ||
| 31 | end | ||
| 32 | if not recpatt then recpatt = P"" end | ||
| 33 | return Rec(patt, Cmt("", recorderror) * recpatt) | ||
| 34 | end | ||
| 35 | |||
| 36 | local num = R("09")^1 / tonumber | ||
| 37 | local op = S("+-*/") | ||
| 38 | |||
| 39 | local function compute(tokens) | ||
| 40 | local result = tokens[1] | ||
| 41 | for i = 2, #tokens, 2 do | ||
| 42 | if tokens[i] == '+' then | ||
| 43 | result = result + tokens[i+1] | ||
| 44 | elseif tokens[i] == '-' then | ||
| 45 | result = result - tokens[i+1] | ||
| 46 | elseif tokens[i] == '*' then | ||
| 47 | result = result * tokens[i+1] | ||
| 48 | elseif tokens[i] == '/' then | ||
| 49 | result = result / tokens[i+1] | ||
| 50 | else | ||
| 51 | error('unknown operation: ' .. tokens[i]) | ||
| 52 | end | ||
| 53 | end | ||
| 54 | return result | ||
| 55 | end | ||
| 56 | |||
| 57 | |||
| 58 | local g = P { | ||
| 59 | "Exp", | ||
| 60 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
| 61 | OpRecov = V"Operand"; | ||
| 62 | Operand = expect(V"Term", "ExpTerm", Cc(0)); | ||
| 63 | Term = num + V"Group"; | ||
| 64 | Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); | ||
| 65 | InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); | ||
| 66 | } | ||
| 67 | |||
| 68 | g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") | ||
| 69 | |||
| 70 | local function eval(input) | ||
| 71 | local result, label, suffix = g:match(input) | ||
| 72 | if #errors == 0 then | ||
| 73 | return result | ||
| 74 | else | ||
| 75 | local out = {} | ||
| 76 | for i, err in ipairs(errors) do | ||
| 77 | local pos = err[2] | ||
| 78 | local msg = labels[err[1]][2] | ||
| 79 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
| 80 | end | ||
| 81 | errors = {} | ||
| 82 | return nil, table.concat(out, "\n") | ||
| 83 | end | ||
| 84 | end | ||
| 85 | |||
| 86 | print(eval "98-76*(54/32)") | ||
| 87 | --> 37.125 | ||
| 88 | |||
| 89 | print(eval "(1+1-1*2/2") | ||
| 90 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
| 91 | |||
| 92 | print(eval "(1+)-1*(2/2)") | ||
| 93 | --> syntax error: expected a term after the operator (at index 4) | ||
| 94 | |||
| 95 | print(eval "(1+1)-1*(/2)") | ||
| 96 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
| 97 | |||
| 98 | print(eval "1+(1-(1*2))/2x") | ||
| 99 | --> syntax error: extra chracters found after the expression (at index 14) | ||
| 100 | |||
| 101 | print(eval "-1+(1-(1*2))/2") | ||
| 102 | --> syntax error: no expression found (at index 1) | ||
| 103 | |||
| 104 | print(eval "(1+1-1*(2/2+)-():") | ||
| 105 | --> syntax error: expected a term after the operator (at index 13) | ||
| 106 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
| 107 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
| 108 | --> syntax error: extra characters found after the expression (at index 17) | ||
diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua new file mode 100644 index 0000000..6697f8b --- /dev/null +++ b/examples/recoveryOpLab.lua | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | local lpeg = require"lpeglabel" | ||
| 2 | |||
| 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 | ||
| 5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec | ||
| 6 | |||
| 7 | local labels = { | ||
| 8 | {"NoExp", "no expression found"}, | ||
| 9 | {"Extra", "extra characters found after the expression"}, | ||
| 10 | {"ExpTerm", "expected a term after the operator"}, | ||
| 11 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
| 12 | {"MisClose", "missing a closing ')' after the expression"}, | ||
| 13 | } | ||
| 14 | |||
| 15 | local function labelindex(labname) | ||
| 16 | for i, elem in ipairs(labels) do | ||
| 17 | if elem[1] == labname then | ||
| 18 | return i | ||
| 19 | end | ||
| 20 | end | ||
| 21 | error("could not find label: " .. labname) | ||
| 22 | end | ||
| 23 | |||
| 24 | local errors = {} | ||
| 25 | |||
| 26 | local function expect(patt, labname) | ||
| 27 | local i = labelindex(labname) | ||
| 28 | function recorderror(input, pos) | ||
| 29 | table.insert(errors, {i, pos}) | ||
| 30 | return true | ||
| 31 | end | ||
| 32 | return patt + Cmt("", recorderror) * T(i) | ||
| 33 | end | ||
| 34 | |||
| 35 | local num = R("09")^1 / tonumber | ||
| 36 | local op = S("+-*/") | ||
| 37 | |||
| 38 | local function compute(tokens) | ||
| 39 | local result = tokens[1] | ||
| 40 | for i = 2, #tokens, 2 do | ||
| 41 | if tokens[i] == '+' then | ||
| 42 | result = result + tokens[i+1] | ||
| 43 | elseif tokens[i] == '-' then | ||
| 44 | result = result - tokens[i+1] | ||
| 45 | elseif tokens[i] == '*' then | ||
| 46 | result = result * tokens[i+1] | ||
| 47 | elseif tokens[i] == '/' then | ||
| 48 | result = result / tokens[i+1] | ||
| 49 | else | ||
| 50 | error('unknown operation: ' .. tokens[i]) | ||
| 51 | end | ||
| 52 | end | ||
| 53 | return result | ||
| 54 | end | ||
| 55 | |||
| 56 | |||
| 57 | local g = P { | ||
| 58 | "Exp", | ||
| 59 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
| 60 | OpRecov = Rec(V"Operand", Cc(0), labelindex("ExpTerm")); | ||
| 61 | Operand = expect(V"Term", "ExpTerm"); | ||
| 62 | Term = num + Rec(V"Group", P"", labelindex("MisClose")); | ||
| 63 | Group = "(" * Rec(V"InnerExp", (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")) * expect(")", "MisClose"); | ||
| 64 | InnerExp = expect(V"Exp", "ExpExp"); | ||
| 65 | } | ||
| 66 | |||
| 67 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
| 68 | |||
| 69 | local function eval(input) | ||
| 70 | local result, label, suffix = g:match(input) | ||
| 71 | if #errors == 0 then | ||
| 72 | return result | ||
| 73 | else | ||
| 74 | local out = {} | ||
| 75 | for i, err in ipairs(errors) do | ||
| 76 | local pos = err[2] | ||
| 77 | local msg = labels[err[1]][2] | ||
| 78 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
| 79 | end | ||
| 80 | errors = {} | ||
| 81 | return nil, table.concat(out, "\n") | ||
| 82 | end | ||
| 83 | end | ||
| 84 | |||
| 85 | print(eval "98-76*(54/32)") | ||
| 86 | --> 37.125 | ||
| 87 | |||
| 88 | print(eval "(1+1-1*2/2") | ||
| 89 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
| 90 | |||
| 91 | print(eval "(1+)-1*(2/2)") | ||
| 92 | --> syntax error: expected a term after the operator (at index 4) | ||
| 93 | |||
| 94 | print(eval "(1+1)-1*(/2)") | ||
| 95 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
| 96 | |||
| 97 | print(eval "1+(1-(1*2))/2x") | ||
| 98 | --> syntax error: extra chracters found after the expression (at index 14) | ||
| 99 | |||
| 100 | print(eval "-1+(1-(1*2))/2") | ||
| 101 | --> syntax error: no expression found (at index 1) | ||
| 102 | |||
| 103 | print(eval "(1+1-1*(2/2+)-():") | ||
| 104 | --> syntax error: expected a term after the operator (at index 13) | ||
| 105 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
| 106 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
| 107 | --> syntax error: extra characters found after the expression (at index 17) | ||
