lua-users home
lua-l archive

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



On 29/09/2006, at 10:40 AM, Luiz Henrique de Figueiredo wrote:

Unfortunately, the lexer can't do that because x+10 would be two tokens,
x and +10, not three, x + 10.


The lexer can be told whether or not to consider a sign as a separate token, I have written ones that do that.

For example, after getting a symbol (x in your example), it is not syntactically correct for it to be followed by a number (eg. x 5) so the next "+" must be an operator token, not the start of a number.

However after getting an operator (eg. x + ) then the next token must *not* be another operator, so it can consider another + as part of a number. Thus you can correctly parse: x++5.

Right now Lua does not correctly parse: 7--5  (should be 12)

It works if you write 7- -5, which suggests that whitespace is more important than you might expect.

With my suggestion it could, and also then: x++10

I haven't looked at the lexer in detail, but I suspect that if you altered the handling of the '-' case to look forwards to a digit (in the correct context), then a number (like -5) can be parsed as a single "number" token, rather than a sign and a separate number.

- Nick