diff options
| author | Undecidable Robot <undecidabot@gmail.com> | 2016-07-07 23:45:11 +0800 |
|---|---|---|
| committer | Undecidable Robot <undecidabot@gmail.com> | 2016-07-07 23:45:11 +0800 |
| commit | 8544119e5bd87b2c57bb7ed1f997519ad8e83f01 (patch) | |
| tree | 5ad9fe49aadf704e5c85d6694d0c2c0b5d911600 /examples | |
| parent | c1ac9cad179a8308dadb6f48cef9a40b124c1da1 (diff) | |
| download | lpeglabel-8544119e5bd87b2c57bb7ed1f997519ad8e83f01.tar.gz lpeglabel-8544119e5bd87b2c57bb7ed1f997519ad8e83f01.tar.bz2 lpeglabel-8544119e5bd87b2c57bb7ed1f997519ad8e83f01.zip | |
Adding relabel example with error recovery
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/expectWithRecovRe.lua | 114 |
1 files changed, 114 insertions, 0 deletions
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 @@ | |||
| 1 | local lpeg = require"lpeglabel" | ||
| 2 | local re = require"relabel" | ||
| 3 | |||
| 4 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
| 5 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
| 6 | local T, Lc = lpeg.T, lpeg.Lc | ||
| 7 | |||
| 8 | local errinfo = { | ||
| 9 | {"NoExp", "no expression found"}, | ||
| 10 | {"Extra", "extra characters found after the expression"}, | ||
| 11 | {"ExpTerm", "expected a term after the operator"}, | ||
| 12 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
| 13 | {"MisClose", "missing a closing ')' after the expression"}, | ||
| 14 | } | ||
| 15 | |||
| 16 | local labels = {} | ||
| 17 | local errmsgs = {} | ||
| 18 | |||
| 19 | for i, err in ipairs(errinfo) do | ||
| 20 | labels[err[1]] = i | ||
| 21 | errmsgs[err[1]] = err[2] | ||
| 22 | end | ||
| 23 | |||
| 24 | re.setlabels(labels) | ||
| 25 | |||
| 26 | local errors = {} | ||
| 27 | |||
| 28 | function recordError(input, pos, label) | ||
| 29 | table.insert(errors, {label, pos}) | ||
| 30 | return true | ||
| 31 | end | ||
| 32 | |||
| 33 | local function compute(tokens) | ||
| 34 | local result = tokens[1] | ||
| 35 | for i = 2, #tokens, 2 do | ||
| 36 | if tokens[i] == '+' then | ||
| 37 | result = result + tokens[i+1] | ||
| 38 | elseif tokens[i] == '-' then | ||
| 39 | result = result - tokens[i+1] | ||
| 40 | elseif tokens[i] == '*' then | ||
| 41 | result = result * tokens[i+1] | ||
| 42 | elseif tokens[i] == '/' then | ||
| 43 | result = result / tokens[i+1] | ||
| 44 | else | ||
| 45 | error('unknown operation: ' .. tokens[i]) | ||
| 46 | end | ||
| 47 | end | ||
| 48 | return result | ||
| 49 | end | ||
| 50 | |||
| 51 | local g = re.compile([[ | ||
| 52 | S <- (Exp / ErrNoExp) (!. / ErrExtra) | ||
| 53 | Exp <- {| Term (op Operand)* |} -> compute | ||
| 54 | Operand <- Term / ErrExpTerm /{ExpTerm} dummy | ||
| 55 | Term <- num / Group | ||
| 56 | Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") | ||
| 57 | InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy | ||
| 58 | |||
| 59 | op <- {[-+*/]} | ||
| 60 | num <- [0-9]+ -> tonumber | ||
| 61 | |||
| 62 | ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} | ||
| 63 | ErrExtra <- ("" -> "Extra" => recordError) %{Extra} | ||
| 64 | ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} | ||
| 65 | ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} | ||
| 66 | ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} | ||
| 67 | |||
| 68 | dummy <- "" -> "0" -> tonumber | ||
| 69 | ]], { | ||
| 70 | compute = compute; | ||
| 71 | recordError = recordError; | ||
| 72 | tonumber = tonumber; | ||
| 73 | }) | ||
| 74 | |||
| 75 | local function eval(input) | ||
| 76 | local result, label, suffix = g:match(input) | ||
| 77 | if #errors == 0 then | ||
| 78 | return result | ||
| 79 | else | ||
| 80 | local out = "" | ||
| 81 | for i, err in ipairs(errors) do | ||
| 82 | local pos = err[2] | ||
| 83 | local msg = errmsgs[err[1]] | ||
| 84 | local line, col = re.calcline(input, pos) | ||
| 85 | out = out .. "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")\n" | ||
| 86 | end | ||
| 87 | errors = {} | ||
| 88 | return nil, out | ||
| 89 | end | ||
| 90 | end | ||
| 91 | |||
| 92 | print(eval "98-76*(54/32)") | ||
| 93 | --> 37.125 | ||
| 94 | |||
| 95 | print(eval "(1+1-1*2/2") | ||
| 96 | --> syntax error: missing a closing ')' after the expression (line 1, col 10) | ||
| 97 | |||
| 98 | print(eval "(1+)-1*(2/2)") | ||
| 99 | --> syntax error: expected a term after the operator (line 1, col 4) | ||
| 100 | |||
| 101 | print(eval "(1+1)-1*(/2)") | ||
| 102 | --> syntax error: expected an expression after the parenthesis (line 1, col 10) | ||
| 103 | |||
| 104 | print(eval "1+(1-(1*2))/2x") | ||
| 105 | --> syntax error: extra characters found after the expression (line 1, col 14) | ||
| 106 | |||
| 107 | print(eval "-1+(1-(1*2))/2") | ||
| 108 | --> syntax error: no expression found (line 1, col 1) | ||
| 109 | |||
| 110 | print(eval "(1+1-1*(2/2+)-():") | ||
| 111 | --> syntax error: expected a term after the operator (line 1, col 13) | ||
| 112 | --> syntax error: expected an expression after the parenthesis (line 1, col 16) | ||
| 113 | --> syntax error: missing a closing ')' after the expression (line 1, col 17) | ||
| 114 | --> syntax error: extra characters found after the expression (line 1, col 17) | ||
