lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
>
> > Checking my other pet-peeve I noticed that it's still there:
> > 
> >   print("%d":format(42))    --> error: ')' expected near ':'
> >   print(("%d"):format(42))  --> 42
> > 
> > I still have to add these superfluous parentheses around the string.
> > As far as I can see, there's no real reason for the parentheses.
> > 
> > I've attached a patch to remove this requirement (patch1).
> 
> Please, do not send patches; they show the implementation, not the idea.
> For what you are proposing, a BNF would be more useful. Show how Lua's
> BNF should be modified.

Sorry, found this reply only now after Patrick Donelly wrote
something about the Lua BNF and prefixexp.

Here's a diff of the BNF as found in lparser.c:

@@@@@ start
-prefixexp  -> NAME | '(' expr ')'
 
-primaryexp -> prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs }
+primaryexp -> simpleexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs }
 
 simpleexp  -> NUMBER | STRING | `nil' | `true' | `false'
               | `...' | constructor | 'function' body
-              | primaryexp
+              | NAME | `(' exp `)'
 
-subexpr    -> (simpleexp | unop subexpr) { binop subexpr }
+subexpr    -> (primaryexp | unop subexpr) { binop subexpr }
 
 expr       -> subexpr
@@@@@ end

The point is to get rid of prefixexp folding it into simpleexp.
The prefixexp rule just limits the syntax without any reason.

The changed syntax accepts strange stuff like

   nil.foo

but also nifty constructs like

   "%d":format(42)
   { a, b, c }[i]

Of course, it's 100% backwards compatible.

Ciao, ET.