lpeg.B(-n) patch for uncondtional backtrack

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

lpeg.B(-n) patch for uncondtional backtrack

albertmcchan
Finally bypassed the infinite recursion lpeg error checking.
(i treated %b as normal character, so be careful of infinite loops)

Now I can do this:
pat = re.compile "{ .* (g <- &'and' / %b g)} .^3 {.*}"

= pat:match "this and that and whatever"
this and that
whatever

---- Follwing is the patch if anyone interested (4 files) ----

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

>diff lpeg\lptree.c lpeg-1.0.1\lptree.c
430d429
<       tree->u.n = n;                        // possibly lpeg.B(-n)
689,690c688
<   // lpeg.B(-n) = behind n
<   tree->u.n = (sib1(tree)->tag == TNot) ? sib1(tree)->u.n : n;
---
>   tree->u.n = n;
1008,1009d1005
<       case TBehind:  /* look-ahead cannot have calls */
<          if (sib1(tree)->tag != TNot) return 1;
1013a1010
>       case TBehind:  /* look-ahead cannot have calls */

>diff lpeg\lpcode.c lpeg-1.0.1\lpcode.c
204,206c204
<     case TBehind:                        // lpeg.B(-n) not nullable
<       if (sib1(tree)->tag == TNot) return 0;
<     case TNot:                             // fall thru for lpeg.B(n)
---
>     case TNot: case TBehind:     /* can match empty, but can fail */
664d661
<   if (sib1(tree)->tag != TNot)       // lpeg.B(-n) = behind n

>diff lpeg\lpprint.c lpeg-1.0.1\lpprint.c
185d184
<       if (sib1(tree)->tag != TNot)   // lpeg.B(-n) = behind n