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

• Subject: Lpeg recursive patterens
• From: Wim Langers <wim.langers@...>
• Date: Sun, 20 Feb 2011 12:39:48 +0100

Hello ,

While debugging a recursive pattern I noticed that Lpeg matches occur twice :

-- this PATTERN triggers V('Input') twice
local Cf,Cmt,Ct,P,V = Lpeg.Cf,Lpeg.Cmt,Lpeg.Ct,Lpeg.P,Lpeg.V
local PATTERN = {
Inputs = Cmt(P('ins') * (V('_PARBEG') * Cf(Ct('') * V('Inputs_'),function(t,v) print ('insert') table.insert(t,v) return t end) * V('_PAREND') + V('_Err')),function(s,i,t) return i,new(nil,t) end),
Inputs_ = V('Input') * V('_SPC') * V('Inputs_') + V('Input')
}

-- this one doesn't
local Cf,Cmt,Ct,P,V = Lpeg.Cf,Lpeg.Cmt,Lpeg.Ct,Lpeg.P,Lpeg.V
local PATTERN = {
Inputs = Cmt(P('ins') * (V('_PARBEG') * Cf(Ct('') * V('Inputs_')  ^1  ,function(t,v) print ('insert') table.insert(t,v) return t end) * V('_PAREND') + V('_Err')),function(s,i,t) return i,new(nil,t) end),
Inputs_ = V('Input') * V('_SPCOPT')
}

What I'm trying to accomplish is :
- the entire pattern (divided over different classes) is a tree like structure
- white space is used as a field separator
- I want to be able to match e.g.: ([Input]    [Input]), but not ([Input][Input]) which might match at the Lpeg level but would result in erroneous data
With :
- Input : PATTERN pertaining to a lower level class
- PARBEG and PAREND : begin and end parentheses with optional white space
- _Err : error function (record error position)
- _SPCOTP : optional white space

I noticed the problem when inserting print statements in the Cmt of the Input PATTERN.

I tried to replicate this behaviour with a small test case but that worked as expected.  So it must be something in the Input-PATTERN ?  But then why does the pattern work with the Lpeg repeat factor (^1) and not as a recursive pattern ?

Any feedback appreciated

Wim