lua-users home
lua-l archive

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


Hello ,

As per my earlier email, I now successfully replicated the "strange" behaviour with recursive patterns :

the first part of below code (recursuve pattern) will print :

input  key  peer
input  key2  peer2
input  key2  peer2
insert key
insert key2

whereas the second part (repeat operator ^1) will print :
input  key  peer
input  key2  peer2
insert key
insert key2

Which is the expected behaviour, but doesn't give fine grained control over white space handling.

I don't actually know if this is problematic because (as witnessed by the two 'insert' lines) the values are only inserted once in the table.  But :
1. I did read something about limits to the amount of Cmt's (and my file has around 100,000 captures)
2. on a higher level in my PATTERN this same type of pattern produces no captures with the recursive pattern (but does with the ^1 capture)


Lpeg = require('lpeg')

local C,Cc,Cf,Cmt,Cp,Ct,P,R,S,V = Lpeg.C,Lpeg.Cc,Lpeg.Cf,Lpeg.Cmt,Lpeg.Cp,Lpeg.Ct,Lpeg.P,Lpeg.R,Lpeg.S,Lpeg.V
local PATTERN = {
      'Inputs';
      _SPC = S(' \t\n') ^ 1,                          -- white space
      _SPCOPT = V('_SPC') ^ 0,                        -- optional space
      _STR = R('az','AZ','09') + P('-') + P('_'),     -- string
      _Str = C(V('_STR') ^ 1),                        -- string capture

      Input = V('Input_kp'),
      Input_kp = Cmt(V('_Str') * P('::') * V('_Str'),function(s,i,key,peer) print('input',key,peer) return i,key end),
      Inputs = Cmt(P('(') * Cf(Ct('') * V('Inputs_'),function(t,v) print ('insert') table.insert(t,v) return t end) * P(')'),function(s,i,t) return i end),
      Inputs_ = V('Input') * V('_SPC') * V('Inputs_') + V('Input')
--      Inputs_ = V('Input') * V('_SPCOPT')
    }

Lpeg.match(PATTERN,'(key::peer key2::peer2)')

print('pattern without recursion')

local PATTERN = {
      'Inputs';
      _SPC = S(' \t\n') ^ 1,                          -- white space
      _SPCOPT = V('_SPC') ^ 0,                        -- optional space
      _STR = R('az','AZ','09') + P('-') + P('_'),     -- string
      _Str = C(V('_STR') ^ 1),                        -- string capture

      Input = V('Input_kp'),
      Input_kp = Cmt(V('_Str') * P('::') * V('_Str'),function(s,i,key,peer) print('input',key,peer) return i,key end),
      Inputs = Cmt(P('(') * Cf(Ct('') * V('Inputs_')    ^1    ,function(t,v) print ('insert') table.insert(t,v) return t end) * P(')'),function(s,i,t) return i end),
--      Inputs_ = V('Input') * V('_SPC') * V('Inputs_') + V('Input')
      Inputs_ = V('Input') * V('_SPCOPT')
    }

Lpeg.match(PATTERN,'(key::peer key2::peer2)')

Cheers ,

Wim