lua-users home
lua-l archive

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


> One of the changes from lpeg 0.9 to 0.10.2 is the new behaviour of the
> and predicate (#) which now no longer keeps its captures. Unfortunately
> this broke a big (>150 loc) parser in my application, which uses this 
> feature quite a lot.  
> 
> Is there a trick or workaround to make LPeg show similar behaviour as it
> did in 0.9 ?

You may try run-time captures. (They would have to save the captures
somewhere.) But I think it would be easier to change the parser.

More often than not a positive look-ahead will be matched again outside
the look-ahead (otherwise the match does not go on). In those cases
you only have to change the capture from the look-ahead to the non
look-ahead pattern.  (This new form is also slightly more efficient, as
it avoids some work when the look-ahead fails.)


> Below an example snippet showing the different output from 0.9 to
> 0.10.2. I'd expect { 'foo', 'bar' } as on 0.9, but now returns { 'bar',
> nil } on 0.10.2
> [...]
>
>   local _, Ct, Cg, P, R = LPeg.V, lpeg.Ct, lpeg.Cg, lpeg.P, lpeg.R
>   
>   local pat = P {
>      "pair",
>      pair = Ct(#_'name' * _'value'),
>      name = Cg(_'token'),
>      value = _'token' * _'HCOLON' * Cg(_'token'),
>      HCOLON = P":",
>      token = R("az","AZ") ^ 0,
>   }

For instance, in this snippet all you have to do is this:

-   name = Cg(_'token'),
-   value = _'token' * _'HCOLON' * Cg(_'token'),
+   name = _'token',
+   value = Cg(_'token') * _'HCOLON' * Cg(_'token'),


(You also need this for that snippet to work:

-local _, Ct, Cg, P, R = LPeg.V, lpeg.Ct, lpeg.Cg, lpeg.P, lpeg.R
+local _, Ct, Cg, P, R = lpeg.V, lpeg.Ct, lpeg.Cg, lpeg.P, lpeg.R

:)

-- Roberto