lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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