[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Undocumented LPeg behavior with table and group captures and non-string names
- From: Parke <parke.nexus@...>
- Date: Sun, 19 Jul 2015 14:40:06 -0700
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).
lpeg = require 'lpeg'
p = lpeg.Ct ( lpeg.Cg ('a', 1) )
t = p : match 'a'
print ( t ) -- 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:
However, I do not believe the current behavior is documented.
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."