lua-users home
lua-l archive

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


On 13 April 2016 at 04:27, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> On 30 May 2015 at 11:31, Daurnimator <quae@daurnimator.com> wrote:
>> > Trying to use Peter Odding's lxsh
>> > Issue has been filed for a while over there:
>> > https://github.com/xolox/lua-lxsh/issues/5
>> > But this should probably be fixed in LPEG itself?
>> >
>> >
>> > The below works fine with lpeg 0.10:
>> >
>> >> lpeg = require"lpeg"
>> >> D = lpeg.R'09'
>> >> BB = lpeg.B(-D, 1)
>> > stdin:1: bad argument #1 to 'B' (pattern may not have fixed length)
>>
>> Today I finally got around to looking at this again.
>> I was working on updating peter odding's lxsh module for lua 5.3
>> https://github.com/daurnimator/lua-lxsh
>> And found that in lpeg 1.0 some of the tests fail (they passed in lpeg 0.10).
>>
>> To get things working, I had to force a check for the length via a lookahead.
>> See https://github.com/daurnimator/lua-lxsh/commit/b620081cb8644e577f7f642d482087e3a0d366ba
>
> I am not sure what this message is about. Is it trying to say that
> the above piece of code does not work with LPeg 1.0?
>
> -- Roberto
>

Consider this piece of code:

```
local lpeg = require 'lpeg'

local D = lpeg.R'09'
local I = lpeg.R('AZ', 'az', '\127\255') + '_'
local SOS = lpeg.P(function(s, i) return i == 1 end) -- start of string
local EOS = -lpeg.P(1) -- end of string
local B = -(I + D) -- word boundary

-- Transform a string with keywords into an LPeg pattern that matches a keyword
-- followed by a word boundary.

local patt = lpeg.P("foo")+lpeg.P("bar")+lpeg.P("qux")
local BB = lpeg.B(B) + SOS -- starting boundary
local AB = B + EOS -- ending boundary

patt = BB * patt * AB

patt = lpeg.P{ patt + 1 * lpeg.V(1) }

print(patt:match(" foo"))
```

It works in lpeg 0.10. but not in lpeg 1.0
To get it to work in lpeg 1.0 I applied this change:

-local B = -(I + D) -- word boundary
+local B = #lpeg.P(1) + -(I + D) -- word boundary