lua-users home
lua-l archive

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


Hello ,

I managed to replicate the (original) problem of the recursive pattern not matching to below code :
Apparently the problem lies in my pattern trying to match "))", where I would like to allow for optional white space eg. ") )" or ")\n)" or...
With or without white space in the input, the match fails ?
The first print statement will give "nil", the other two give the desired values (but without properly handling white space).

Any help appreciated.

Cheers ,

Wim


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 = {
      'Shapes';
      _Str = C(R('az') ^ 1),

      Shape = V('_Str') * P('::') * V('_Str') * P('()') * P(' ') ^ 0 / function(key,id) return 'shape'..key..id end,

      Shapes = P('shapes') * (P('(') * Ct(V('Shapes_')) * P(')')) / function(t) return t end,
      Shapes_ = V('Shape') * P(' ')^1 * V('Shapes_') + V('Shape')
}
print('pattern that covers my needs ?',Lpeg.match(PATTERN,'shapes(key::id() kez::ie())'))

PATTERN.Shape = V('_Str') * P('::') * V('_Str') * P('()') / function(key,id) return 'shape'..key..id end
print('pattern without (non existing in input) white space pattern :',unpack(Lpeg.match(PATTERN,'shapes(key::id() kez::ie())')))

PATTERN.Shape = V('_Str') * P('::') * V('_Str') * P('()') * P(' ') ^ 0 / function(key,id) return 'shape'..key..id end
PATTERN.Shapes = P('shapes') * (P('(') * Ct(V('Shapes_')^1) * P(')')) / function(t) return t end
print('pattern without recursion :',unpack(Lpeg.match(PATTERN,'shapes(key::id() kez::ie())')))