lua-users home
lua-l archive

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


Help Guys!

I wonder if anyone can offer me some help with a powerpatch to the Lua
lexer/parser (lparser.c)?

I have long published a powerpatch which adds "syntax sugar" to default a
missing value in a constructor list to Boolean true, intended for
tables implementing sets, on the analogy of the sugar for lists:

t = {["cat"], ["dog"]}  -- Shortcut for t = {["cat"] = true, ["dog"] = true}

I've always wanted to extend this to:

t = {[cat],[dog]}

So, similarly to keys for methods, you can drop the quotes if the string is
a valid Lua name.

Whatever you think of this as a language innovation (which we can discuss
later when it exists as a powerpatch) at this point I just need
help implementing it. I'm modifying the function recfield in lparser.c. The
problem is I need to look two tokens ahead (the closing ']' and
then NOT '='). I tried the following test with luaX_lookahead:

/* Input -> [name]} */
int x1 = ls->t.token;                    /* x1 = TK_NAME */
int x2 = luaX_lookahead(ls);   /* x2 = ']' */
int x3 = luaX_lookahead(ls);   /* x3 = '}' */
luaX_next(ls);
int x4 = ls->t.token;                   /* x4 = '}'; expected ']' */

It works as expected with a single luaX_lookahead, but with two in a row the
result is returned correctly but it has the side-effect of moving
the parse point forward one token (is this a bug?).

Searching the Lua code base, luaX_lookahead is only used in one place and
then only for a single-token look ahead, so if it is a bug it has no
effect in standard Lua.

Is it possible to achieve this? Either I would need to be able to look two
tokens forward without moving the parse point, or I'd need to be
able to back up two tokens and re-parse once I'd hit (or not hit) the '='
token.

Anyone managed anything like this before?

- John


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus