lua-users home
lua-l archive

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


On Fri, Sep 25, 2015 at 8:35 AM, John Hind <john.hind@zen.co.uk> wrote:
> As discussed in "Set Constructor Syntax (Was: Help with Lexer/Parser
> Internals Please!)" I have published a PowerPatch here:
>
> http://lua-users.org/wiki/LuaPowerPatches
>
> The patch updates the manual as well as the source code. Here is the
> relevant manual update:
>
> 3.4.9 - Table Constructors
>
> Table constructors are expressions that create tables. Every time a
> constructor is evaluated, a new table is created. A constructor can be
> used to create an empty table or to create a table and initialize some of
> its fields. The general syntax for constructors is
>
>         tableconstructor ::= '{' [fieldlist] '}'
>         fieldlist ::= field {fieldsep field} [fieldsep]
>         field ::= '[' exp ']' '=' exp | '.' Name '=' exp | '[' exp ']' | '.'
> Name |
>                 Name '=' exp | exp
>         fieldsep ::= ',' | ';'
>
> [JH-LUA-SETINIT]: The two '.Name' and the '[exp]' field syntax options are
> added by the powerpatch.
>
> Each field of the form '[exp1] = exp2' adds to the new table an entry with
> key 'exp1' and value 'exp2'. A field of the form 'name = exp' or
> '.name = exp' is equivalent to '["name"] = exp'. Fields of the form 'exp'
> are equivalent to '[i] = exp', where i are consecutive integers starting
> with 1. Fields in the other formats do not affect this counting. For
> example,
>
> (example is unchanged)
>
> A field of the form '[exp]' is equivalent to '[exp] = true' and a field of
> the form '.name' is equivalent to '["name"] = true'.
>
> (end of manual extract)
>
> Although this was motivated by the desire to provide a syntax shortcut for
> set-like tables similar to the existing one for list-like tables,
> I also think the period prefix for name keys (optional if the value is also
> specified) is an improvement more generally. By echoing the
> table access syntax, it makes clearer what is going on and preserves the
> "user illusion" that this is a name scope-limited to the table
> rather than a string key.
>
> The patch introduces no incompatibilities and adds only 16 lines of code.
>
> - John Hind

Bravo! A very practical implementation. It should be easy to try this
out for anyone who might benefit from it.

/s/ Adam