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
|
local m = require'lpeglabel'
local g = m.P{
"S",
S = m.V"Id" * m.V"List",
List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List",
Id = m.R'az'^1 + m.T(1),
}
function mymatch (g, s)
local r, e, sfail = g:match(s)
if not r then
if e == 1 then
return r, "Error: expecting an identifier before '" .. sfail .. "'"
elseif e == 2 then
return r, "Error: expecting ',' before '" .. sfail .. "'"
else
return r, "Error"
end
end
return r
end
print(mymatch(g, "a,b"))
print(mymatch(g, "a b"))
print(mymatch(g, ", b"))
|