diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
commit | 6fd747b3061728245d4f7a0b5fc5baab18dac895 (patch) | |
tree | 1764d7817edd3740be17c9a7a96a871ccd082f6e /examples/listIdRe2.lua | |
parent | 2f18ef50aeb1acb15ec1bbed3ad03e758e2756c9 (diff) | |
download | lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.gz lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.bz2 lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.zip |
Updating the documentation and the examples
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r-- | examples/listIdRe2.lua | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index 4cd140b..ccb7ce5 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua | |||
@@ -1,35 +1,48 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
2 | 2 | ||
3 | local errUndef, errId, errComma = 0, 1, 2 | 3 | local errinfo = { |
4 | 4 | {"errUndef", "undefined"}, | |
5 | local terror = { | 5 | {"errId", "expecting an identifier"}, |
6 | [errUndef] = "Error", | 6 | {"errComma", "expecting ','"}, |
7 | [errId] = "Error: expecting an identifier", | ||
8 | [errComma] = "Error: expecting ','", | ||
9 | } | 7 | } |
10 | 8 | ||
11 | local tlabels = { ["errUndef"] = errUndef, | 9 | local errmsgs = {} |
12 | ["errId"] = errId, | 10 | local labels = {} |
13 | ["errComma"] = errComma } | 11 | |
12 | for i, err in ipairs(errinfo) do | ||
13 | errmsgs[i] = err[2] | ||
14 | labels[err[1]] = i | ||
15 | end | ||
16 | |||
17 | re.setlabels(labels) | ||
18 | |||
19 | local function calcline (s, i) | ||
20 | if i == 1 then return 1, 1 end | ||
21 | local rest, line = s:sub(1,i):gsub("[^\n]*\n", "") | ||
22 | local col = #rest | ||
23 | return 1 + line, col ~= 0 and col or 1 | ||
24 | end | ||
14 | 25 | ||
15 | re.setlabels(tlabels) | ||
16 | 26 | ||
17 | local g = re.compile[[ | 27 | local g = re.compile[[ |
18 | S <- Id List | 28 | S <- Id List |
19 | List <- !. / (',' / %{errComma}) Id List | 29 | List <- !. / (',' / %{errComma}) (Id / %{errId}) List |
20 | Id <- [a-z] / %{errId} | 30 | Id <- Sp [a-z]+ |
31 | Comma <- Sp ',' | ||
32 | Sp <- %s* | ||
21 | ]] | 33 | ]] |
22 | 34 | ||
23 | function mymatch (g, s) | 35 | function mymatch (g, s) |
24 | local r, e, sfail = g:match(s) | 36 | local r, e, sfail = g:match(s) |
25 | if not r then | 37 | if not r then |
26 | return r, terror[e] .. " before '" .. sfail .. "'" | 38 | local line, col = calcline(s, #s - #sfail) |
39 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | ||
40 | return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" | ||
27 | end | 41 | end |
28 | return r | 42 | return r |
29 | end | 43 | end |
30 | |||
31 | print(mymatch(g, "a,b")) | ||
32 | print(mymatch(g, "a b")) | ||
33 | print(mymatch(g, ", b")) | ||
34 | 44 | ||
45 | print(mymatch(g, "one,two")) | ||
46 | print(mymatch(g, "one two")) | ||
47 | print(mymatch(g, "one,\n two,\nthree,")) | ||
35 | 48 | ||