lua-users home
lua-l archive

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

On Wed, 30 Aug 2023 at 10:18, Roberto Ierusalimschy <> wrote:
> I myself tend to use named captures nested in table captures only for
> very simple tasks. For anything more complex I prefer to use unamed
> captures nested in function captures: The function then creates the
> table with the proper fields.

More specifically: I tend to use named captures nested in table captures
when there is already a table capture for a list of unamed captures
and I want to add one or two extra named fields in the table (e.g., a
tag). When the table being created is a record, I prefer to use a function.

 Hi Roberto,

can you show some examples? I'm still struggling to understand unnamed
group captures...

By the way, I found a solution to the problem in the beginning of this
thread... it is in the last line here:

  require "lpeg"
  B,C,P,R,S,V = lpeg.B,lpeg.C,lpeg.P,lpeg.R,lpeg.S,lpeg.V
  Cb,Cc,Cf,Cg = lpeg.Cb,lpeg.Cc,lpeg.Cf,lpeg.Cg
  Cp,Cs,Ct    = lpeg.Cp,lpeg.Cs,lpeg.Ct
  Carg,Cmt    = lpeg.Carg,lpeg.Cmt = function (pat, str) PP(pat:match(str or "")) end
  lpeg.Cfromthere = function (pat)
      return pat:Cmt(function(subj,pos,there)
          return pos,subj:sub(there,pos-1)

  c0 = Cp():Cg"c"                -- stores this position in c
  ab = C(1):Cg"a" * C(1):Cg"b"   -- stores things in a and b
  c1 = Cb"c":Cfromthere():Cg"c"  -- replaces c by everything from there to here

  ab:C():Cg"c"  :Ct():pm "ABCD"  --> bad:  {"c"="AB"}
  (c0 * ab * c1):Ct():pm "ABCD"  --> good: {"a"="A", "b"="B", "c"="AB"}

Thanks in advance,
  Eduardo Ochs