lua-users home
lua-l archive

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


On Mar 5, 2012, at 6:40 PM, Mark Hamburg wrote:

> 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.

Indeed it causes lookahead issues:

	{ a = b [ ... }

Are we starting a second entry or dereferencing b? Now consider the fact that we are going to have to parse through an expression and the closing right brace before we get to look for an = sign.

Mark