aboutsummaryrefslogtreecommitdiff
path: root/examples/listId1.lua
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2016-06-28 16:00:31 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2016-06-28 16:00:31 -0300
commit6fd747b3061728245d4f7a0b5fc5baab18dac895 (patch)
tree1764d7817edd3740be17c9a7a96a871ccd082f6e /examples/listId1.lua
parent2f18ef50aeb1acb15ec1bbed3ad03e758e2756c9 (diff)
downloadlpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.gz
lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.bz2
lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.zip
Updating the documentation and the examples
Diffstat (limited to 'examples/listId1.lua')
-rw-r--r--examples/listId1.lua45
1 files changed, 28 insertions, 17 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua
index 0bf26a1..12c0678 100644
--- a/examples/listId1.lua
+++ b/examples/listId1.lua
@@ -1,27 +1,38 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2
3local function calcline (s, i)
4 if i == 1 then return 1, 1 end
5 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
6 local col = #rest
7 return 1 + line, col ~= 0 and col or 1
8end
9
3local g = m.P{ 10local g = m.P{
4 "S", 11 "S",
5 S = m.V"Id" * m.V"List", 12 S = m.V"Id" * m.V"List",
6 List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List", 13 List = -m.P(1) + (m.V"Comma" + m.T(2)) * (m.V"Id" + m.T(1)) * m.V"List",
7 Id = m.R'az'^1 + m.T(1), 14 Id = m.V"Sp" * m.R'az'^1,
15 Comma = m.V"Sp" * ",",
16 Sp = m.S" \n\t"^0,
8} 17}
9 18
10function mymatch (g, s) 19function mymatch (g, s)
11 local r, e, sfail = g:match(s) 20 local r, e, sfail = g:match(s)
12 if not r then 21 if not r then
13 if e == 1 then 22 local line, col = calcline(s, #s - #sfail)
14 return r, "Error: expecting an identifier before '" .. sfail .. "'" 23 local msg = "Error at line " .. line .. " (col " .. col .. ")"
15 elseif e == 2 then 24 if e == 1 then
16 return r, "Error: expecting ',' before '" .. sfail .. "'" 25 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
17 else 26 elseif e == 2 then
18 return r, "Error" 27 return r, msg .. ": expecting ',' before '" .. sfail .. "'"
19 end 28 else
20 end 29 return r, msg
21 return r 30 end
31 end
32 return r
22end 33end
23 34
24print(mymatch(g, "a,b")) 35print(mymatch(g, "one,two"))
25print(mymatch(g, "a b")) 36print(mymatch(g, "one two"))
26print(mymatch(g, ", b")) 37print(mymatch(g, "one,\n two,\nthree,"))
27 38