diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
commit | 6fd747b3061728245d4f7a0b5fc5baab18dac895 (patch) | |
tree | 1764d7817edd3740be17c9a7a96a871ccd082f6e /examples/listIdRe1.lua | |
parent | 2f18ef50aeb1acb15ec1bbed3ad03e758e2756c9 (diff) | |
download | lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.gz lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.bz2 lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.zip |
Updating the documentation and the examples
Diffstat (limited to 'examples/listIdRe1.lua')
-rw-r--r-- | examples/listIdRe1.lua | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua index fc213bc..361e53f 100644 --- a/examples/listIdRe1.lua +++ b/examples/listIdRe1.lua | |||
@@ -1,27 +1,37 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
2 | 2 | ||
3 | local 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 | ||
8 | end | ||
9 | |||
3 | local g = re.compile[[ | 10 | local g = re.compile[[ |
4 | S <- Id List | 11 | S <- Id List |
5 | List <- !. / (',' / %{2}) Id List | 12 | List <- !. / (',' / %{2}) (Id / %{1}) List |
6 | Id <- [a-z] / %{1} | 13 | Id <- Sp [a-z]+ |
14 | Comma <- Sp ',' | ||
15 | Sp <- %s* | ||
7 | ]] | 16 | ]] |
8 | 17 | ||
9 | function mymatch (g, s) | 18 | function mymatch (g, s) |
10 | local r, e, sfail = g:match(s) | 19 | local r, e, sfail = g:match(s) |
11 | if not r then | 20 | if not r then |
21 | local line, col = calcline(s, #s - #sfail) | ||
22 | local msg = "Error at line " .. line .. " (col " .. col .. ")" | ||
12 | if e == 1 then | 23 | if e == 1 then |
13 | return r, "Error: expecting an identifier before '" .. sfail .. "'" | 24 | return r, msg .. ": expecting an identifier before '" .. sfail .. "'" |
14 | elseif e == 2 then | 25 | elseif e == 2 then |
15 | return r, "Error: expecting ',' before '" .. sfail .. "'" | 26 | return r, msg .. ": expecting ',' before '" .. sfail .. "'" |
16 | else | 27 | else |
17 | return r, "Error" | 28 | return r, msg |
18 | end | 29 | end |
19 | end | 30 | end |
20 | return r | 31 | return r |
21 | end | 32 | end |
22 | |||
23 | print(mymatch(g, "a,b")) | ||
24 | print(mymatch(g, "a b")) | ||
25 | print(mymatch(g, ", b")) | ||
26 | 33 | ||
34 | print(mymatch(g, "one,two")) | ||
35 | print(mymatch(g, "one two")) | ||
36 | print(mymatch(g, "one,\n two,\nthree,")) | ||
27 | 37 | ||