aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdRe1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/listIdRe1.lua')
-rw-r--r--examples/listIdRe1.lua35
1 files changed, 14 insertions, 21 deletions
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua
index 361e53f..d092566 100644
--- a/examples/listIdRe1.lua
+++ b/examples/listIdRe1.lua
@@ -1,34 +1,27 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local function calcline (s, i)
4 if i == 1 then return 1, 1 end
5 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
6 local col = #rest
7 return 1 + line, col ~= 0 and col or 1
8end
9
10local g = re.compile[[ 3local g = re.compile[[
11 S <- Id List 4 S <- Id List
12 List <- !. / (',' / %{2}) (Id / %{1}) List 5 List <- !. / (',' / %{2}) (Id / %{1}) List
13 Id <- Sp [a-z]+ 6 Id <- Sp [a-z]+
14 Comma <- Sp ',' 7 Comma <- Sp ','
15 Sp <- %s* 8 Sp <- %s*
16]] 9]]
17 10
18function mymatch (g, s) 11function mymatch (g, s)
19 local r, e, sfail = g:match(s) 12 local r, e, sfail = g:match(s)
20 if not r then 13 if not r then
21 local line, col = calcline(s, #s - #sfail) 14 local line, col = re.calcline(s, #s - #sfail)
22 local msg = "Error at line " .. line .. " (col " .. col .. ")" 15 local msg = "Error at line " .. line .. " (col " .. col .. ")"
23 if e == 1 then 16 if e == 1 then
24 return r, msg .. ": expecting an identifier before '" .. sfail .. "'" 17 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
25 elseif e == 2 then 18 elseif e == 2 then
26 return r, msg .. ": expecting ',' before '" .. sfail .. "'" 19 return r, msg .. ": expecting ',' before '" .. sfail .. "'"
27 else 20 else
28 return r, msg 21 return r, msg
29 end 22 end
30 end 23 end
31 return r 24 return r
32end 25end
33 26
34print(mymatch(g, "one,two")) 27print(mymatch(g, "one,two"))