lua-users home
lua-l archive

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


On Tue, Jun 21, 2011 at 10:04 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Tue, Jun 21, 2011 at 10:46 AM, David Given <dg@cowlark.com> wrote:
>> Well, for a start it would allow more complex parsers to be made easily
>> from simple Lua patterns, which are much easier to write than lpeg
>> parsers, such as...
>
> Ah, I see - the results of lpeg.L are now available in other lpeg
> contexts, etc. So alternation becomes '+' and so forth.
>
> It would be nice to be able to say lpeg.L "(foo|bar)", however. '|'
> becomes magic of course.
>
> Certainly it would help those (such as myself) who are fluent in Lua
> patterns but still struggle to express things in LPeg.
>
> steve d.

There is one slightly cheeky way to embed Lua patterns in LPeg:

local function adjust(_, x, ...)
  if x then
    return x + 1, ...
  end
end
local function patternToLPeg(p)
  p = "^".. p:gsub("$$", "$%%$")
  return require"lpeg".Cmt(0, function(s, i) return
adjust(string.find(s, p, i)) end)
end