Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

joy mondal
Hi Everybody and Sean !

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


My temporary solution is to deep clone the constant:

add = function(all, pos, constant)
 
  local copy _.extend({},constant) -- from underscore.lua library

  table.insert(fin,copy)
 
  print (fin)
 
  return pos
end

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 ?

best wishes,
Joy

Reply | Threaded
Open this post in threaded view
|

Re: Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

Sean Conner
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 ... )


Reply | Threaded
Open this post in threaded view
|

Re: Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

joy mondal
Hi Sean !

I am really sorry, its a inspect related issue. Not a LPEG issue.

https://luarocks.org/modules/kikito/inspect

I am using the most downloaded luarocks module for inspecting tables.

If you do this :

local inspect = require ('inspect')

local constant = {'hello', 'world'}

local fin = {constant, {constant, constant}}

print(inspect(fin))

You get :

{
  {"hello", "world"},
  {nil, nil }
}

I will try to figure out if its an actual issue or if I am mistaken how inspect works.

Cheers and Thanks !

Joy


On Thu, Apr 11, 2019 at 11:13 AM Sean Conner <[hidden email]> wrote:
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 ... )


Reply | Threaded
Open this post in threaded view
|

Re: Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

Dirk Laurie-2
I get under Lua 5.3.5:
> inspect._VERSION
inspect.lua 3.1.0
> inspect(fin)
{ <1>{ "hello", "world" }, { <table 1>, <table 1> } }

So maybe a simple fix is to downgrade your brand-new inspect to v3.1.0?

Op Do. 11 Apr. 2019 om 10:21 het joy mondal
<[hidden email]> geskryf:

>
> Hi Sean !
>
> I am really sorry, its a inspect related issue. Not a LPEG issue.
>
> https://luarocks.org/modules/kikito/inspect
>
> I am using the most downloaded luarocks module for inspecting tables.
>
> If you do this :
>
> local inspect = require ('inspect')
>
> local constant = {'hello', 'world'}
>
> local fin = {constant, {constant, constant}}
>
> print(inspect(fin))
>
> You get :
>
> {
>   {"hello", "world"},
>   {nil, nil }
> }
>
> I will try to figure out if its an actual issue or if I am mistaken how inspect works.
>
> Cheers and Thanks !
>
> Joy
>
>
> On Thu, Apr 11, 2019 at 11:13 AM Sean Conner <[hidden email]> wrote:
>>
>> 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 ... )
>>
>>

Reply | Threaded
Open this post in threaded view
|

Re: Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

joy mondal
In reply to this post by joy mondal
Hi Sean !

It was a serpent related issue ( not inspect )

https://github.com/pkulchenko/serpent/issues/35

I know it might seem I am too happy to bother others with my LPEG issues, but I spent 1 day chasing the nil issue due to my lack of practice using LPEG. But with time hopefully I will not bother people.

best wishes,

Joy
Reply | Threaded
Open this post in threaded view
|

Re: Unexplained object deletion when using lpeg.Cc with lpeg.Cmt

joy mondal
In reply to this post by Dirk Laurie-2
Hi Dirk !

I will just use Sean Conner's dump_value function. inspect doent't indent properly.

best regards.


On Thu, Apr 11, 2019 at 1:12 PM Dirk Laurie <[hidden email]> wrote:
I get under Lua 5.3.5:
> inspect._VERSION
inspect.lua 3.1.0
> inspect(fin)
{ <1>{ "hello", "world" }, { <table 1>, <table 1> } }

So maybe a simple fix is to downgrade your brand-new inspect to v3.1.0?

Op Do. 11 Apr. 2019 om 10:21 het joy mondal
<[hidden email]> geskryf:
>
> Hi Sean !
>
> I am really sorry, its a inspect related issue. Not a LPEG issue.
>
> https://luarocks.org/modules/kikito/inspect
>
> I am using the most downloaded luarocks module for inspecting tables.
>
> If you do this :
>
> local inspect = require ('inspect')
>
> local constant = {'hello', 'world'}
>
> local fin = {constant, {constant, constant}}
>
> print(inspect(fin))
>
> You get :
>
> {
>   {"hello", "world"},
>   {nil, nil }
> }
>
> I will try to figure out if its an actual issue or if I am mistaken how inspect works.
>
> Cheers and Thanks !
>
> Joy
>
>
> On Thu, Apr 11, 2019 at 11:13 AM Sean Conner <[hidden email]> wrote:
>>
>> 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 ... )
>>
>>