lua-users home
lua-l archive

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




Em sex, 10 de set de 2021 às 17:58:17 -0700, Parke <parke.nexus@gmail.com> escreveu:
The comment preceding the read_numeral() function in version 5.4.3 of
Lua's lexer says:

  Roughly, [read_numeral()] accepts the following pattern:

  %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*

The above pattern starts with "%d(%x".

Why the %x?

I suspect that one reason is that the implementation is a bit simpler. There is a single "%x|'.'" loop that covers both halves of the regex.

The only time that this difference could possibly matter is if a number is touching a letter. If we write a=1b=2, should that be parsed as a=1 followed by b=2, or should the 1b be an "invalid number" error?

In Lua 5.3 or earlier, it depends on whether the letter is a hex digit. a=1b=2 is an invalid number error, but x=1y=2 is allowed. If the regex used %d in that place you asked, then a=1b=2 would become valid as well.

In Lua 5.4, numbers touching a letter always result in an invalid number error. It doesn't matter if the regex is %d or %x, or if the letter is a hex letter. If you ask me, I prefer this behavior instead of the 5.3 one.

-- Hugo