diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-08 10:11:50 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-08 10:11:50 -0300 |
commit | 5ffef3da93ad53069d2510a75b11ecbb1b6e8aa7 (patch) | |
tree | e98c59a9e0aae891638eca25a25558d933fdf44f /examples | |
parent | 030df9b4a4f4dc3a2cc5775e032f83e92d3c0097 (diff) | |
download | lpeglabel-5ffef3da93ad53069d2510a75b11ecbb1b6e8aa7.tar.gz lpeglabel-5ffef3da93ad53069d2510a75b11ecbb1b6e8aa7.tar.bz2 lpeglabel-5ffef3da93ad53069d2510a75b11ecbb1b6e8aa7.zip |
Updating the examples since lpeglabel now returns an error position instead of a string and p^lab is syntatic sugar
Diffstat (limited to 'examples')
-rw-r--r-- | examples/farthest.lua | 25 | ||||
-rw-r--r-- | examples/listId1.lua | 8 | ||||
-rw-r--r-- | examples/listId2.lua | 6 | ||||
-rw-r--r-- | examples/listIdRe1.lua | 8 | ||||
-rw-r--r-- | examples/tiny.lua | 4 | ||||
-rw-r--r-- | examples/typedlua/tlparser.lua | 4 |
6 files changed, 34 insertions, 21 deletions
diff --git a/examples/farthest.lua b/examples/farthest.lua index 692390f..9cbe03d 100644 --- a/examples/farthest.lua +++ b/examples/farthest.lua | |||
@@ -1,14 +1,27 @@ | |||
1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
2 | 2 | ||
3 | function matchPrint(p, s) | 3 | function matchPrint(p, s) |
4 | local r, lab, sfail = p:match(s) | 4 | local r, lab, errpos = p:match(s) |
5 | print("r: ", r, "lab: ", lab, "sfail: ", sfail) | 5 | print("r: ", r, "lab: ", lab, "errpos: ", errpos) |
6 | end | ||
7 | |||
8 | function getSub (s, i) | ||
9 | if i then return s:sub(i) else return nil end | ||
10 | end | ||
11 | |||
12 | function matchPrint2(p, s) | ||
13 | local r, lab, ifail = p:match(s) | ||
14 | print("r: ", r, "lab: ", lab, "sfail: ", getSub(s, ifail)) | ||
6 | end | 15 | end |
7 | 16 | ||
8 | local p = m.P"a"^0 * m.P"b" + m.P"c" | 17 | local p = m.P"a"^0 * m.P"b" + m.P"c" |
9 | matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil | 18 | matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil |
10 | matchPrint(p, "c") --> r: 2 lab: nil sfail: nil | 19 | matchPrint(p, "c") --> r: 2 lab: nil errpos: nil |
11 | matchPrint(p, "aac") --> r: nil lab: 0 sfail: c | 20 | matchPrint(p, "aac") --> r: nil lab: 0 errpos: 3 |
12 | matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc | 21 | matchPrint(p, "xxc") --> r: nil lab: 0 errpos: 1 |
13 | 22 | ||
14 | 23 | ||
24 | matchPrint2(p, "abc") --> r: 3 lab: nil sfail: nil | ||
25 | matchPrint2(p, "c") --> r: 2 lab: nil sfail: nil | ||
26 | matchPrint2(p, "aac") --> r: nil lab: 0 sfail: c | ||
27 | matchPrint2(p, "xxc") --> r: nil lab: 0 sfail: xxc | ||
diff --git a/examples/listId1.lua b/examples/listId1.lua index b7943c1..9bba783 100644 --- a/examples/listId1.lua +++ b/examples/listId1.lua | |||
@@ -13,14 +13,14 @@ local g = m.P{ | |||
13 | } | 13 | } |
14 | 14 | ||
15 | function mymatch (g, s) | 15 | function mymatch (g, s) |
16 | local r, e, sfail = g:match(s) | 16 | local r, e, pos = g:match(s) |
17 | if not r then | 17 | if not r then |
18 | local line, col = re.calcline(s, #s - #sfail) | 18 | local line, col = re.calcline(s, pos) |
19 | local msg = "Error at line " .. line .. " (col " .. col .. ")" | 19 | local msg = "Error at line " .. line .. " (col " .. col .. ")" |
20 | if e == 1 then | 20 | if e == 1 then |
21 | return r, msg .. ": expecting an identifier before '" .. sfail .. "'" | 21 | return r, msg .. ": expecting an identifier before '" .. s:sub(pos) .. "'" |
22 | elseif e == 2 then | 22 | elseif e == 2 then |
23 | return r, msg .. ": expecting ',' before '" .. sfail .. "'" | 23 | return r, msg .. ": expecting ',' before '" .. s:sub(pos) .. "'" |
24 | else | 24 | else |
25 | return r, msg | 25 | return r, msg |
26 | end | 26 | end |
diff --git a/examples/listId2.lua b/examples/listId2.lua index 3ee89da..592dae3 100644 --- a/examples/listId2.lua +++ b/examples/listId2.lua | |||
@@ -25,11 +25,11 @@ local g = m.P{ | |||
25 | 25 | ||
26 | 26 | ||
27 | function mymatch (g, s) | 27 | function mymatch (g, s) |
28 | local r, e, sfail = g:match(s) | 28 | local r, e, pos = g:match(s) |
29 | if not r then | 29 | if not r then |
30 | local line, col = re.calcline(s, #s - #sfail) | 30 | local line, col = re.calcline(s, pos) |
31 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | 31 | local msg = "Error at line " .. line .. " (col " .. col .. "): " |
32 | return r, msg .. terror[e] .. " before '" .. sfail .. "'" | 32 | return r, msg .. terror[e] .. " before '" .. s:sub(pos) .. "'" |
33 | end | 33 | end |
34 | return r | 34 | return r |
35 | end | 35 | end |
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua index 3098c66..d60706a 100644 --- a/examples/listIdRe1.lua +++ b/examples/listIdRe1.lua | |||
@@ -9,14 +9,14 @@ local g = re.compile[[ | |||
9 | ]] | 9 | ]] |
10 | 10 | ||
11 | function mymatch (g, s) | 11 | function mymatch (g, s) |
12 | local r, e, sfail = g:match(s) | 12 | local r, e, pos = g:match(s) |
13 | if not r then | 13 | if not r then |
14 | local line, col = re.calcline(s, #s - #sfail) | 14 | local line, col = re.calcline(s, pos) |
15 | local msg = "Error at line " .. line .. " (col " .. col .. ")" | 15 | local msg = "Error at line " .. line .. " (col " .. col .. ")" |
16 | if e == 1 then | 16 | if e == 1 then |
17 | return r, msg .. ": expecting an identifier before '" .. sfail .. "'" | 17 | return r, msg .. ": expecting an identifier before '" .. s:sub(pos) .. "'" |
18 | elseif e == 2 then | 18 | elseif e == 2 then |
19 | return r, msg .. ": expecting ',' before '" .. sfail .. "'" | 19 | return r, msg .. ": expecting ',' before '" .. s:sub(pos) .. "'" |
20 | else | 20 | else |
21 | return r, msg | 21 | return r, msg |
22 | end | 22 | end |
diff --git a/examples/tiny.lua b/examples/tiny.lua index 734536c..cc718e7 100644 --- a/examples/tiny.lua +++ b/examples/tiny.lua | |||
@@ -90,9 +90,9 @@ local g = re.compile[[ | |||
90 | 90 | ||
91 | 91 | ||
92 | local function mymatch(g, s) | 92 | local function mymatch(g, s) |
93 | local r, e, sfail = g:match(s) | 93 | local r, e, pos = g:match(s) |
94 | if not r then | 94 | if not r then |
95 | local line, col = re.calcline(s, #s - #sfail) | 95 | local line, col = re.calcline(s, pos) |
96 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | 96 | local msg = "Error at line " .. line .. " (col " .. col .. "): " |
97 | return r, msg .. terror[e].msg | 97 | return r, msg .. terror[e].msg |
98 | end | 98 | end |
diff --git a/examples/typedlua/tlparser.lua b/examples/typedlua/tlparser.lua index a301fa6..2ede2eb 100644 --- a/examples/typedlua/tlparser.lua +++ b/examples/typedlua/tlparser.lua | |||
@@ -226,9 +226,9 @@ end | |||
226 | function tlparser.parse (subject, filename, strict, integer) | 226 | function tlparser.parse (subject, filename, strict, integer) |
227 | local errorinfo = {} | 227 | local errorinfo = {} |
228 | lpeg.setmaxstack(1000) | 228 | lpeg.setmaxstack(1000) |
229 | local ast, label, suffix = lpeg.match(G, subject, nil, errorinfo, strict, integer) | 229 | local ast, label, pos = lpeg.match(G, subject, nil, errorinfo, strict, integer) |
230 | if not ast then | 230 | if not ast then |
231 | local line, col = lineno(subject, string.len(subject) - string.len(suffix)) | 231 | local line, col = lineno(subject, pos) |
232 | local error_msg = string.format("%s:%d:%d: ", filename, line, col) | 232 | local error_msg = string.format("%s:%d:%d: ", filename, line, col) |
233 | if label ~= 0 then | 233 | if label ~= 0 then |
234 | error_msg = error_msg .. tlerror.errors[label].msg | 234 | error_msg = error_msg .. tlerror.errors[label].msg |