lua-users home
lua-l archive

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


Consider this:

  print(math.sqrt
         (2))

Lua raises an error:

  Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
  > print(math.sqrt
  >> (2))
  stdin:2: ambiguous syntax (function call x new statement) near '('

The reference manual suggests it is invalid:

  "you cannot put a line break before the '(' in a function call."

However, there is no ambiguity in "(2)" possibly beginning a new
statement since the function call is nested inside parenthesis.  A
statement cannot exist in this context.  The attached patch removes
this unnecessary restriction.  Additional examples are shown in the
below tests:

  local function f() return function() return 2 end end

  assert(f()
          () == 2)
  local x = (f()
              ())
  assert(x == 2)
  assert(not loadstring[[x = f()
                              ()]])
  local x = {f()
              ()}
  assert(x[1] == 2)

  local x; if f()
               () then x = true end
  assert(x)
  assert(not loadstring[[repeat until f()
                                       ()]])
  assert((function() return f()
                             ()
          end)() == 2)

  _G[f()
      ()] = 3
  assert(_G[2] == 3)

  print 'PASSED'

The change in this patch is not unlike Python's implicit line joining[1].

When can this be useful?  For one thing, it would extend Lua's ability
to mimic certain types of syntax similar to Lisp s-expressions, where
arguments are inside parenthesis and delimited by spaces:

  eval (syntax
    "select * from mytable where"
    (x) " = 10 and y = " (y)
  )

[1] http://docs.python.org/reference/lexical_analysis.html#implicit-line-joining

Attachment: patch
Description: Binary data