lua-users home
lua-l archive

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


On Mar 5, 2012, at 5:57 PM, Wolfgang Pupp wrote:

> I agree that forgetting commas in table-constructors this is a pretty
> common annoyance (speaking from personal experience :P).
> 
> I think it's possible to make this smoother and more
> beginner-friendly, without breaking *anything*:
> Make commas optional for *consequent* <tkey> = <tvalue> 'assignments'
> in table constructors.
> 
> Here is the relevant part of the Lua (5.1)-syntax:
> tableconstructor ::= '{' [fieldlist] '}'
> fieldlist ::= field {fieldsep field} [fieldsep]
> field ::= '[' exp ']' '=' exp | Name '=' exp | exp
> 
> The only change would be:
> field ::= tassign {tassign} | exp
> tassign ::= '[' exp ']' '=' exp | Name '=' exp
> 
> I think this would allow to omit commas where they annoy most
> ("statement-like" table constructors), without introducing unnecessary
> ambiguities.
> 
> Gregs initial example would only require 2 commas instead of 12 with
> this change (AFAICT).
> It would also be pretty simple to remember: Commas only needed when
> constructing sequences (without specifying the keys explicitly).
> 
> Discuss! (I like this better and better the more I think about it; are
> there any hidden flaws?)

Without delving too deeply, I wonder whether it increases the lookahead needed for the parser.

But couple this with better handling for function declarations and it becomes pretty nice.

At some point I start thinking of this as the table constructor monad and as someone once pointed out, you can think of monads as overloading the semicolon operator — which of course is optional in Lua. ;-)

Mark