lua-users home
lua-l archive

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


Hi,

If a named group capture has a name that is a number (as opposed to a
string), then when that named group is captured inside a table
capture, the key used to store the value will be the number converted
to a string (rather than the number itself).

For example:

lpeg = require 'lpeg'
p = lpeg.Ct ( lpeg.Cg ('a', 1) )
t = p : match 'a'
print ( t[1]   )    -- nil
print ( t['1'] )    -- a

I was hoping to be able to overwrite the capture at index 1, the same
way that repeated group captures with the same string name will
overwrite each other.

It would not surprise me if the current behavior is intentional.

The behavior was mentioned here:
http://lua-users.org/lists/lua-l/2013-05/msg00551.html

However, I do not believe the current behavior is documented.

http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#cap-g
http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#cap-t

At the above links, I see no requirement that names be strings.  No
error is generated telling me that a numeric name is invalid.  And the
number is silently converted to a string.  Neither link above mentions
any such conversion, and the second link simply says: "the first value
of the group is put into the table with the group name as its key."

Cheers,

Parke