aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-08 10:11:50 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-08 10:11:50 -0300
commit5ffef3da93ad53069d2510a75b11ecbb1b6e8aa7 (patch)
treee98c59a9e0aae891638eca25a25558d933fdf44f /examples
parent030df9b4a4f4dc3a2cc5775e032f83e92d3c0097 (diff)
downloadlpeglabel-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.lua25
-rw-r--r--examples/listId1.lua8
-rw-r--r--examples/listId2.lua6
-rw-r--r--examples/listIdRe1.lua8
-rw-r--r--examples/tiny.lua4
-rw-r--r--examples/typedlua/tlparser.lua4
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 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2
3function matchPrint(p, s) 3function 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)
6end
7
8function getSub (s, i)
9 if i then return s:sub(i) else return nil end
10end
11
12function matchPrint2(p, s)
13 local r, lab, ifail = p:match(s)
14 print("r: ", r, "lab: ", lab, "sfail: ", getSub(s, ifail))
6end 15end
7 16
8local p = m.P"a"^0 * m.P"b" + m.P"c" 17local p = m.P"a"^0 * m.P"b" + m.P"c"
9matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil 18matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil
10matchPrint(p, "c") --> r: 2 lab: nil sfail: nil 19matchPrint(p, "c") --> r: 2 lab: nil errpos: nil
11matchPrint(p, "aac") --> r: nil lab: 0 sfail: c 20matchPrint(p, "aac") --> r: nil lab: 0 errpos: 3
12matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc 21matchPrint(p, "xxc") --> r: nil lab: 0 errpos: 1
13 22
14 23
24matchPrint2(p, "abc") --> r: 3 lab: nil sfail: nil
25matchPrint2(p, "c") --> r: 2 lab: nil sfail: nil
26matchPrint2(p, "aac") --> r: nil lab: 0 sfail: c
27matchPrint2(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
15function mymatch (g, s) 15function 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
27function mymatch (g, s) 27function 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
35end 35end
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
11function mymatch (g, s) 11function 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
92local function mymatch(g, s) 92local 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
226function tlparser.parse (subject, filename, strict, integer) 226function 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