aboutsummaryrefslogtreecommitdiff
path: root/examples/listId2.lua
blob: 49433688985551c14b1e16a3c8024ef60d4743c8 (plain)
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
local m = require'lpeglabel'

local errUndef = 0
local errId = 1
local errComma = 2

local terror = {
	[errUndef] = "Error",
	[errId] = "Error: expecting an identifier",
	[errComma] = "Error: expecting ','",
}

local g = m.P{
  "S",
  S = m.V"Id" * m.V"List",
  List = -m.P(1) + ("," + m.T(errComma)) * m.V"Id" * m.V"List",
  Id = m.R'az'^1 + m.T(errId),
}

function mymatch (g, s)
	local r, e = g:match(s)
	if not r then
		return r, terror[e]
	end
	return r
end
	
print(mymatch(g, "a,b"))
print(mymatch(g, "a b"))
print(mymatch(g, ", b"))