diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-07-01 14:57:39 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-07-01 14:57:39 -0300 |
commit | 7cd05bfaeea0fc1d269ace114d3225bffbd82940 (patch) | |
tree | 955ed449edeed25506a3b47977840d642dad5026 /examples/listIdRe2.lua | |
parent | 6a3c4bfddcfaa0e1348aa2c3d9fb7135a4f330c8 (diff) | |
download | lpeglabel-7cd05bfaeea0fc1d269ace114d3225bffbd82940.tar.gz lpeglabel-7cd05bfaeea0fc1d269ace114d3225bffbd82940.tar.bz2 lpeglabel-7cd05bfaeea0fc1d269ace114d3225bffbd82940.zip |
Adding function 'calcline' to relabel and updating documentation/examples
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r-- | examples/listIdRe2.lua | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index ccb7ce5..fe30535 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua | |||
@@ -1,9 +1,9 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
2 | 2 | ||
3 | local errinfo = { | 3 | local errinfo = { |
4 | {"errUndef", "undefined"}, | 4 | {"errUndef", "undefined"}, |
5 | {"errId", "expecting an identifier"}, | 5 | {"errId", "expecting an identifier"}, |
6 | {"errComma", "expecting ','"}, | 6 | {"errComma", "expecting ','"}, |
7 | } | 7 | } |
8 | 8 | ||
9 | local errmsgs = {} | 9 | local errmsgs = {} |
@@ -16,30 +16,22 @@ end | |||
16 | 16 | ||
17 | re.setlabels(labels) | 17 | re.setlabels(labels) |
18 | 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 | ||
25 | |||
26 | |||
27 | local g = re.compile[[ | 19 | local g = re.compile[[ |
28 | S <- Id List | 20 | S <- Id List |
29 | List <- !. / (',' / %{errComma}) (Id / %{errId}) List | 21 | List <- !. / (',' / %{errComma}) (Id / %{errId}) List |
30 | Id <- Sp [a-z]+ | 22 | Id <- Sp [a-z]+ |
31 | Comma <- Sp ',' | 23 | Comma <- Sp ',' |
32 | Sp <- %s* | 24 | Sp <- %s* |
33 | ]] | 25 | ]] |
34 | 26 | ||
35 | function mymatch (g, s) | 27 | function mymatch (g, s) |
36 | local r, e, sfail = g:match(s) | 28 | local r, e, sfail = g:match(s) |
37 | if not r then | 29 | if not r then |
38 | local line, col = calcline(s, #s - #sfail) | 30 | local line, col = re.calcline(s, #s - #sfail) |
39 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | 31 | local msg = "Error at line " .. line .. " (col " .. col .. "): " |
40 | return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" | 32 | return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" |
41 | end | 33 | end |
42 | return r | 34 | return r |
43 | end | 35 | end |
44 | 36 | ||
45 | print(mymatch(g, "one,two")) | 37 | print(mymatch(g, "one,two")) |