lua-users home
lua-l archive

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

It was thus said that the Great Dirk Laurie once stated:
> Put another way: the issue is not what can you do with LPeg, it is
> what can you do that you can't do with Lua patterns, and whether
> that extra is sufficiently common to justify adding LPeg to Lua.

  For me, it's mostly the composibility of LPeg expressions.  I can have an
LPeg expression for parsing a SIP URL:

	sip_url = ... -- some complex LPeg expression

and another one for TEL URLs:

	tel_url = ... -- some other complex LPeg expression

and I can combine them into a single expression to parse both types (I use
this at work actually):

	url = sip_url + tel_url

And if I need to support, say, a mailto: URL, then ...

	mailto_url = ... -- LPeg expression for this
	url = url + mailto_url

Unlike Lua patterns, which can return only individual captures, LPeg can
transform the data, for example, turning month names into values:

	months = lpeg.P"January"  * lpeg.Cc(1)
               + lpeg.P"February" * lpeg.Cc(2)
	       + lpeg.P"March"    * lpeg.Cc(3) ...

as well as collect the captures into a Lua table (with named fields even). 
Heck, you can use LPeg expressions to build other LPeg expressions [1][2].

  I use LPeg extensively at work.


[1]	LPeg comes with the re module, which is one example of this.

[2]	LPeg code I wrote to parse the string passed to (and
	ultimately, the C function strftime()) to generate an LPeg
	expression to parse a date of said format: