lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Does lpeg.B(-n) has any real meaning ?
I did a patch that use B(-n) for the behind n instruction
NOTE: %b == lpge.B(-1) == behind 1

Example: lua patten "(.*)and(.*)"

pat = re.compile "{(g <- 'and' / . [^a]* g)+ %b%b%b} ... {.*}"
= pat:match 'this and that and whatever'
this and that
whatever

Example: capture chars in groups of 3's

pat = re.compile "({...} %b%b)+"
= pat:match "123456789"
123     234     345     456     567     678     789

----
Follwing is the patches if anyone interested (ONLY 4 lines !)

>diff lpeg\lptree.c lpeg-1.0.1\lptree.c
430d429
<       tree->u.n = n;                        // TNot, possibly UNDO
689d687
<   if (sib1(tree)->tag == TNot) n = sib1(tree)->u.n;

>diff lpeg\lpcode.c lpeg-1.0.1\lpcode.c
661d661
<   if (sib1(tree)->tag != TNot)        // TNot == UNDO

>diff lpeg\re.lua lpeg-1.0.1\re.lua
23c27,28
< local Predef = { nl = m.P"\n", b = m.B(-1) }
---
> local Predef = { nl = m.P"\n" }