lua-users home
lua-l archive

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


have run into the issue of how can a Lua Function-based pattern perform captures...

I ran into that as well recently, and the answer seems to be that it doesn't... You need the explicit match / capture distinction. That is, when you need to capture something different than the matching substring.

Maybe this would be a nice addition for LPeg 0.8? That is, allow patterns created with lpeg.P(function() ... end) to return zero or more captures after the new index? This might seem redundant at first because

  lpeg.P(function() ... end) / function() ... end

does the same thing, however any context available in the function that matches the input has to be recreated in the function that manipulates the capture(s). Here's the case I ran into, matching Lua long comments and capturing just the body:

  # lpeg.P'--['
  * function(input, index)
      local i, j = input:find('^%-%-%[(=*)%[.-%]%1%]', index)
      return j and j + 1
    end
  / function(comment)
      local level, body = comment:match('^%-%-%[(=*)%[(.-)%]%1%]$')
      return comment, body
    end

Assuming my proposal is possible this would become:

  # lpeg.P'--['
  * function(input, index)
      local i, j, body = input:find('^%-%-%[(=*)%[(.-)%]%1%]', index)
      if j then return j + 1, body end
    end

 - Peter Odding

Thomas Harning wrote:
On Fri, 29 Feb 2008 18:53:41 -0500
Thomas Harning <harningt@gmail.com> wrote:

Does anybody have any suggestions on how to use LPeg 0.6 for TLV
parsing, especially BER?
I've come up with something that mostly works using Lua Function-based
patterns, and have run into the issue of how can a Lua Function-based
pattern perform captures...

The hacky idea I came up w/ is to use something like:

  lpeg.C(function) / getFunctionCaptures

Any better ideas?