lua-users home
lua-l archive

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


  More specifically, a production in the 're' module of LPeg, for folding
captures.  I even have a proposed syntax:

	{^name^ p ^}	folding capture equivalent to lpeg.Cf(p,defs[name])

  And here's a concrete example---this hypothetical example:

local parse = re.compile([[
	parse   <- {^collect^ {| |} {: {id} EQ {value} %nl :}* ^}
	id      <- [A-Za-z_] [A-Za-z0-9_]*
	value   <- [!~]*
	EQ      <- %s* '=' %s*
	
	]],

	{
	  collect = function(a,i,v) a[i] = v return a end
	}
)

is the same as this LPeg code:

local EQ    = P" "^0 * P"=" * P" "^0
local id    = R("AZ","az","__") * R("AZ","az","09","__")^0
local value = R"!~"^0
local parse = Cf(
                   Ct"" * Cg(C(id) * EQ * C(value) * P"\n")^0,
                   function(a,i,v)
                     a[i] = v
                     return a
                   end
                 )

  I ask because I'm working on parsing certain types of URIs [1] and while I
can write this in LPeg, it just looks nicer using re (more like the RFCs and
less like line noise).  

  -spc

[1]	tel: and sip: URIs; in particular, the optional named parameters
	defined in a bazillion RFCs.