Roberto Ierusalimschy | 1 Jun 11:03 2008
Picon
Picon

Re: lpeg brain melt

> I don't know if other people will try escape sequences in the long
> string compilation method, but it may be worth mentioning on this
> page:
> http://www.inf.puc-rio.br/~roberto/lpeg/re.html
> 
> That is the page I've been using as a reference/docs. Is that the best resource?

Yes. Now there is a paper about LPEG, but it is more about its design and
implementation, not a reference:

  http://www.inf.puc-rio.br/~roberto/docs/peg.pdf

> Also, on that page the CSV parser uses '\n' inside of [[...]] ... I
> haven't tested that CSV parser example, but does that mean it would
> fail?

That example should be corrected...

-- Roberto

Eike Decker | 1 Jun 11:52 2008
Picon

Using luaL_newmetatable / luaL_checkudata

Hi

Is there some "lua way" how to use the luaL_newmetatable functionality in
combination with userdatas that need an individual metatable?

In my case, I'd like to store a table together with the userdata to keep some
lua variables that can be modified by the user. I thought about storing this
table in the userdata's metatable. However, that model requires individual
metatables for each userdata. However, this couldn't be used in combination
with these functions. I could of course also use a table in the registry (or
the metatable), using the userdata as keys. 

What would you suggest?

Eike

Peter Cawley | 1 Jun 12:34 2008

Re: Using luaL_newmetatable / luaL_checkudata

I would go with one of two options:
1) Assuming you just need to store Lua data with a userdata, consider
setting a new table as the environment for the userdata and using that
to store the data
2) If the userdatum need to have different metatables (e.g. they have
differing metamethods), then you could add a "__isTypeX" field to each
metatable, and use that to validate that the userdata are of the
correct type.

2008/6/1 Eike Decker <eike <at> cube3d.de>:
> Hi
>
> Is there some "lua way" how to use the luaL_newmetatable functionality in
> combination with userdatas that need an individual metatable?
>
> In my case, I'd like to store a table together with the userdata to keep some
> lua variables that can be modified by the user. I thought about storing this
> table in the userdata's metatable. However, that model requires individual
> metatables for each userdata. However, this couldn't be used in combination
> with these functions. I could of course also use a table in the registry (or
> the metatable), using the userdata as keys.
>
> What would you suggest?
>
> Eike
>

Eike Decker | 1 Jun 12:56 2008
Picon

Re: Using luaL_newmetatable / luaL_checkudata

OMG, seems like I use now lua for 4 years without realizing, that userdatas can
have function environments. How useful is that!
Thanks a lot, that helps much :)

Eike

> I would go with one of two options:
> 1) Assuming you just need to store Lua data with a userdata, consider
> setting a new table as the environment for the userdata and using that
> to store the data
> 2) If the userdatum need to have different metatables (e.g. they have
> differing metamethods), then you could add a "__isTypeX" field to each
> metatable, and use that to validate that the userdata are of the
> correct type.
> 
> 2008/6/1 Eike Decker <eike <at> cube3d.de>:
> > Hi
> >
> > Is there some "lua way" how to use the luaL_newmetatable functionality in
> > combination with userdatas that need an individual metatable?
> >
> > In my case, I'd like to store a table together with the userdata to keep
> some
> > lua variables that can be modified by the user. I thought about storing
> this
> > table in the userdata's metatable. However, that model requires individual
> > metatables for each userdata. However, this couldn't be used in
> combination
> > with these functions. I could of course also use a table in the registry
> (or
(Continue reading)

Nick Gammon | 2 Jun 00:30 2008
Picon

lpeg pattern repetition

Hi,

I have been trying out lpeg, and am wondering if there is an easy way  
of representing the notion of something like this:

   I see 4567 cattle

... where the number is a 4 to 6-digit number.

In PCRE I would write it like this:

   I see [0-9]{4,6} cattle

The best I could come up with in lpeg is:

   require "lpeg"

   digits = lpeg.R "09"
   patt = lpeg.P "I see " * digits * digits * digits * digits *  
digits^-2 * " cattle"

   print (lpeg.match (patt, "I see 4567 cattle"))  --> 18

This requires the explicit repetition of the digits four times,  
followed by no more than 2 extra ones.

Is there an easier or less wordy way of doing this? I know I can use a  
"for" loop to concatenate "digits", but apart from that?

- Nick Gammon
(Continue reading)

Roberto Ierusalimschy | 2 Jun 02:25 2008
Picon
Picon

Re: lpeg pattern repetition

> In PCRE I would write it like this:
>
>   I see [0-9]{4,6} cattle

If you do not want to write a repetition function, you can use something
like this:

  num = -digit^7 * digit^4

Not terrificly efficient (reads the number twice), but does the job.

-- Roberto

Nick Gammon | 2 Jun 02:35 2008
Picon

Re: lpeg pattern repetition

Another thing I can't get to work is something simple like this:

I want to match on "Nick says hello" where "Nick" can vary. That is,  
in regular expression syntax:

.* says hello

I tried to do this in LPeg like so:

print (lpeg.match (lpeg.P (1)^0 * " says hello",
       "Nick says hello"))  --> nil

I am guessing that the lpeg.P (1) is so greedy it is consuming the "  
says hello" part as well. But if that is the case, how can that sort  
of thing be easily expressed? Another example would be:

.* says .*

That would be useful for general decoding of conversations. <someone>  
says <something>

The only way I can get my first example to work is by specifying the  
character set for the name, like this:

print (lpeg.match (lpeg.R ("AZ", "az")^0 * " says hello",
        "Nick says hello")) --> 16

That works, but only because I have excluded a space.

If I wanted to match on "Nick the programmer says hello",  then I need  
(Continue reading)

Mark Hamburg | 2 Jun 03:00 2008

GoDaddy accounts

Is anyone subscribed to this list from a GoDaddy account? My efforts  
to move my subscription over to an account on that ISP all result in  
no messages getting through.

Thanks.
Mark

Roberto Ierusalimschy | 2 Jun 05:25 2008
Picon
Picon

Re: lpeg pattern repetition

> I want to match on "Nick says hello" where "Nick" can vary.

option 1: 
  p =lpeg.P{" says hello" + 1 * lpeg.V(1)}

option 2:
  line = lpeg.P" says hello"
  p = (1 - line)^0 * line

You should have a look at the lpeg paper:

  A Text Pattern-Matching Tool based on Parsing Expression Grammars
  http://www.inf.puc-rio.br/~roberto/docs/peg.pdf

-- Roberto

Nick Gammon | 2 Jun 05:59 2008
Picon

Re: lpeg pattern repetition


On 02/06/2008, at 1:25 PM, Roberto Ierusalimschy wrote:

>>
> You should have a look at the lpeg paper:
>
>  A Text Pattern-Matching Tool based on Parsing Expression Grammars
>  http://www.inf.puc-rio.br/~roberto/docs/peg.pdf

Thanks for the solution. I was indeed looking at the pdf file, which  
gave me the idea for:

   function make_wildcard (s)
     return lpeg.C((#-lpeg.P(s) * 1)^1) * (s)
   end -- make_wildcard

   print ( lpeg.match (make_wildcard (" says "), "Nick says  
hello")  )  --> Nick

However your proposal looks more elegant, especially the 2nd one,  
which can be modified to return the capture fairly easily:

  line = lpeg.P" says hello"
  p = lpeg.C((1 - line)^0) * line

  print (lpeg.match (p, "Nick says hello"))  --> Nick

Your solution can be further modified to solve the problem of  
<someone> says <something>

(Continue reading)


Gmane