aboutsummaryrefslogtreecommitdiff
path: root/examples/listId2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/listId2.lua')
-rw-r--r--examples/listId2.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/listId2.lua b/examples/listId2.lua
new file mode 100644
index 0000000..4943368
--- /dev/null
+++ b/examples/listId2.lua
@@ -0,0 +1,32 @@
1local m = require'lpeglabel'
2
3local errUndef = 0
4local errId = 1
5local errComma = 2
6
7local terror = {
8 [errUndef] = "Error",
9 [errId] = "Error: expecting an identifier",
10 [errComma] = "Error: expecting ','",
11}
12
13local g = m.P{
14 "S",
15 S = m.V"Id" * m.V"List",
16 List = -m.P(1) + ("," + m.T(errComma)) * m.V"Id" * m.V"List",
17 Id = m.R'az'^1 + m.T(errId),
18}
19
20function mymatch (g, s)
21 local r, e = g:match(s)
22 if not r then
23 return r, terror[e]
24 end
25 return r
26end
27
28print(mymatch(g, "a,b"))
29print(mymatch(g, "a b"))
30print(mymatch(g, ", b"))
31
32