1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
local re = require 'relabel'
local errUndef, errId, errComma = 0, 1, 2
local terror = {
[errUndef] = "Error",
[errId] = "Error: expecting an identifier",
[errComma] = "Error: expecting ','",
}
local tlabels = { ["errUndef"] = errUndef,
["errId"] = errId,
["errComma"] = errComma }
re.setlabels(tlabels)
local g = re.compile[[
S <- Id List
List <- !. / (',' / %{errComma}) Id List
Id <- [a-z] / %{errId}
]]
function mymatch (g, s)
local r, e, sfail = g:match(s)
if not r then
return r, terror[e] .. " before '" .. sfail .. "'"
end
return r
end
print(mymatch(g, "a,b"))
print(mymatch(g, "a b"))
print(mymatch(g, ", b"))
|