Request for LPeg feature

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Request for LPeg feature

Sean Conner

  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.

Reply | Threaded
Open this post in threaded view
|

Re: Request for LPeg feature

Albert Chan
On Apr 19, 2018, at 11:10 PM, Sean Conner <[hidden email]> wrote:

>
>  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])
>

~> for folding capture is probably better

http://lua-users.org/lists/lua-l/2015-08/msg00029.html


Reply | Threaded
Open this post in threaded view
|

Re: Request for LPeg feature

Sean Conner
It was thus said that the Great Albert Chan once stated:

> On Apr 19, 2018, at 11:10 PM, Sean Conner <[hidden email]> wrote:
>
> >
> >  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])
> >
>
> ~> for folding capture is probably better
>
> http://lua-users.org/lists/lua-l/2015-08/msg00029.html

  Heh ... I forgot about that.  

  I looked at re.lua last night and realized my own proposal could be done
in one line of code, much like Alex's proposal is also one line of code.
I'm not wedded to my syntax [1] but I would like the ability to do a folding
capture added to re, that way I don't have to rely upon a custom version
just to have nice looking code.

  Please, Roberto?  Please?

  -spc

[1] I based mine off the named group capture; Alex based his off the
        match-time capture.

Reply | Threaded
Open this post in threaded view
|

Re: Request for LPeg feature

Dirk Laurie-2
2018-04-21 2:30 GMT+02:00 Sean Conner <[hidden email]>:

> It was thus said that the Great Albert Chan once stated:
>> On Apr 19, 2018, at 11:10 PM, Sean Conner <[hidden email]> wrote:
>>
>> >
>> >  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])
>> >
>>
>> ~> for folding capture is probably better
>>
>> http://lua-users.org/lists/lua-l/2015-08/msg00029.html
>
>   Heh ... I forgot about that.
>
>   I looked at re.lua last night and realized my own proposal could be done
> in one line of code, much like Alex's proposal is also one line of code.
> I'm not wedded to my syntax [1] but I would like the ability to do a folding
> capture added to re, that way I don't have to rely upon a custom version
> just to have nice looking code.
>
>   Please, Roberto?  Please?

Don't distract Roberto from lua-5.4.0-work2 just now :-)