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 joy mondal once stated:
> Hi Everybody and Sean !

  Hi.

> I noticed something odd ( I am following the design decisions I have
> previously learnt from Sean Conner).
> 
> When using lpeg.Cc with lpeg.Cmt, for whatever reason if you create a
> object with lpeg.Cc expect the object to be
> 
> deleted or garbage collected by lpeg internals.
> 
> Here is a concrete simplified example:
> 
> local fin = { }
> local add = function(all, pos, constant)
>   table.insert(fin, constant)
>   print(fin)
>   return pos
> end
> local test_constant = { 'hello',  'world'}
> 
> local gram = Cmt (((Cc(test_constant)) * P('a')), add)
> 
> local patt = gram ^ 1
> 
> return patt:match('aa')
> 
> ------<| CONSOLE OUTPUT START |>--------
> 
> {
>   {"hello", "world"}
> }
> {
>   {"hello", "world"}, nil -- nil should be {"hello", "world"}, why is the
> constant being garbage collected.
> }
> 
> ------<| CONSOLE OUTPUT END |>--------

  It's hard to say what you were expecting, so I ran the code using Lua
5.3.4 and LPEG 1.0.2.  I modifed the code to include LPEG and to dump the
fin table instead of just returning the results of patt:match() (which will
be 3 by the way), and I got:

	table: 0x8eaf940
	table: 0x8eaf940
	fin =
	{
	  [1] =
	  {
	    [1] = "hello",
	    [2] = "world",
	  },
	  [2] = fin.[1],
	}

The first two lines are from add(), and the rest are the result of a custom
table dump routine I've written 
(https://github.com/spc476/lua-conmanorg/blob/master/lua/table.lua#L131). 
You can see that the table fin contains two references to test_constant.

> In situations like this are we expected to assume the object created with
> lpeg.Cc will be deleted and make copies ? Or is my usage of Cmt with Cc
> unjustified ?

  I also ran the code with Lua 5.1.5/LPEG 0.12 and Lua 5.2.4/LPEG 1.0.0 and
got the results shown above.

  I can't say unjustified, but something weird is going on with your code.

  -spc (I'm only reporting what I'm seeing ... )