lua-users home
lua-l archive

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


On Mon, Nov 1, 2021 at 12:48 PM Alex Light <allight@google.com> wrote:
Lua fails to parse code such as '0.."foo"' because it misinterprets '0.' as the start of a number and fails to recognize the concat operator. The lua syntax indicates this should be permitted.

For example:

% lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> 0 .. "hi"
0hi
> 0.."hi"
stdin:1: malformed number near '0..'

-Alex

The Lua syntax indicates no such thing. The grammar is defined in terms of tokens, and as with the vast majority of programming languages, tokens are always processed as the longest series of consecutive characters that matches a token. Otherwise, it would be ambiguous whether << is the left-shift operator or two less-than operators side by side. "0." matches the definition of a Numeral token, being a numeric constant with a radix point, therefore it must be parsed as a Numeral. However, the documentation only permits a Numeral to contain *A* radix point, so the appearance of a second radix point means that the token is malformed.

Now, the argument could be made that the second . should be interpreted as the start of a new token because including it would no longer match a token. C and _javascript_ both parse "0.." as meaning the floating-point constant 0, followed by the dot operator. However, that would still make the Lua code in your example erroneous, as it would fail with `<name> expected near '"hi"'`. Under no circumstances would that ever be parsed as a concat operator.

The argument could also be made that the definition I gave for a token above is not explicitly noted in the Lua documentation, making it a documentation defect. This is plausibly true, and it's up to the PUC-Rio team to decide if it's necessary to add such a call-out or if it's sufficient to assume that it's consistent with the way it's done in other languages. (The documentation also doesn't define what a numeric constant is aside from saying that it can have an optional fractional part and an optional decimal exponent. We reasonably assume that a numeric constant is formed of the ASCII digits 0 through 9.)

/s/ Adam