aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdCatch.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/listIdCatch.lua')
-rw-r--r--examples/listIdCatch.lua48
1 files changed, 34 insertions, 14 deletions
diff --git a/examples/listIdCatch.lua b/examples/listIdCatch.lua
index 38ad2e5..3cbc834 100644
--- a/examples/listIdCatch.lua
+++ b/examples/listIdCatch.lua
@@ -1,25 +1,45 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2
3local errUndef, errId, errComma = 0, 1, 2 3local terror = {}
4 4
5local terror = { 5local function newError(s)
6 [errUndef] = "Error", 6 table.insert(terror, s)
7 [errId] = "Error: expecting an identifier", 7 return #terror
8 [errComma] = "Error: expecting ','", 8end
9} 9
10local errUndef = newError("undefined")
11local errId = newError("expecting an identifier")
12local errComma = newError("expecting ','")
13
14local function calcline (s, i)
15 if i == 1 then return 1, 1 end
16 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
17 local col = #rest
18 return 1 + line, col ~= 0 and col or 1
19end
10 20
11g = m.P{ 21local g = m.P{
12 "S", 22 "S",
13 S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId), 23 S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId),
14 m.V"ErrComma", errComma), 24 m.V"ErrComma", errComma),
15 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", 25 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List",
16 Id = m.R'az'^1 + m.T(errId), 26 Id = m.V"Sp" * m.R'az'^1,
17 Comma = "," + m.T(errComma), 27 Comma = m.V"Sp" * ",",
28 Sp = m.S" \n\t"^0,
18 ErrId = m.Cc(errId) / terror, 29 ErrId = m.Cc(errId) / terror,
19 ErrComma = m.Cc(errComma) / terror 30 ErrComma = m.Cc(errComma) / terror
20} 31}
21 32
22print(g:match("a,b")) 33function mymatch (g, s)
23print(g:match("a b")) 34 local r, e, sfail = g:match(s)
24print(g:match(",b")) 35 if not r then
36 local line, col = calcline(s, #s - #sfail)
37 local msg = "Error at line " .. line .. " (col " .. col .. "): "
38 return r, msg .. terror[e] .. " before '" .. sfail .. "'"
39 end
40 return r
41end
25 42
43print(mymatch(g, "one,two"))
44print(mymatch(g, "one two"))
45print(mymatch(g, "one,\n two,\nthree,"))