aboutsummaryrefslogtreecommitdiff
path: root/examples/farthest.lua
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/farthest.lua
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/farthest.lua')
-rw-r--r--examples/farthest.lua25
1 files changed, 19 insertions, 6 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