aboutsummaryrefslogtreecommitdiff
path: root/examples/listId3.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/listId3.lua')
-rw-r--r--examples/listId3.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/listId3.lua b/examples/listId3.lua
new file mode 100644
index 0000000..03da97d
--- /dev/null
+++ b/examples/listId3.lua
@@ -0,0 +1,35 @@
1local m = require'lpeglabel'
2local re = require'relabel'
3
4local terror = {
5 ErrId = "expecting an identifier",
6 ErrComma = "expecting ','",
7 fail = "undefined"
8}
9
10local id = m.R'az'^1
11
12local g = m.P{
13 'S',
14 S = m.V'List',
15 List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0,
16 Id = m.V'Sp' * id,
17 Comma = m.V'Sp' * ',' + m.T'ErrComma',
18 Sp = m.S' \n\t'^0,
19}
20
21
22function mymatch (g, s)
23 local r, e, pos = g:match(s)
24 if not r then
25 local line, col = re.calcline(s, pos)
26 local msg = "Error at line " .. line .. " (col " .. col .. "): "
27 return r, msg .. terror[e] .. " before '" .. s:sub(pos) .. "'"
28 end
29 return r
30end
31
32print(mymatch(g, "one,two"))
33print(mymatch(g, "one two"))
34print(mymatch(g, "one,\n two,\nthree,4"))
35print(mymatch(g, " 1,2"))