[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg as a part of lua (was: An introduction to Lua)
- From: Sean Conner <sean@...>
- Date: Sun, 22 Oct 2017 21:01:01 -0400
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.
-spc
[1] LPeg comes with the re module, which is one example of this.
[2] LPeg code I wrote to parse the string passed to os.date() (and
ultimately, the C function strftime()) to generate an LPeg
expression to parse a date of said format:
https://github.com/spc476/LPeg-talk/blob/master/strftime.lua