aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdRe2.lua
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2016-07-01 14:57:39 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2016-07-01 14:57:39 -0300
commit7cd05bfaeea0fc1d269ace114d3225bffbd82940 (patch)
tree955ed449edeed25506a3b47977840d642dad5026 /examples/listIdRe2.lua
parent6a3c4bfddcfaa0e1348aa2c3d9fb7135a4f330c8 (diff)
downloadlpeglabel-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.lua30
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 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local errinfo = { 3local 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
9local errmsgs = {} 9local errmsgs = {}
@@ -16,30 +16,22 @@ end
16 16
17re.setlabels(labels) 17re.setlabels(labels)
18 18
19local 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
24end
25
26
27local g = re.compile[[ 19local 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
35function mymatch (g, s) 27function 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
43end 35end
44 36
45print(mymatch(g, "one,two")) 37print(mymatch(g, "one,two"))