fab13n / metalua-parser

luarock packaging of Metalua's parser
48 stars 2 forks source link

Wrong AST is genereted when parentheses follow a function expression #10

Open mpeterv opened 10 years ago

mpeterv commented 10 years ago

test.lua:

x = function(y) print(y) end
(function() end)()

Standard Lua interprets this block as two separate statements, and executing it prints nothing. However, metalua-parser considers it one chain call:

case.lua:

local parser = require "metalua.compiler".new()
pprint(inspect(parser:srcfile_to_ast("test.lua")))
{ { { { -- `Id "x"
         "x",
        tag = "Id"
      } }, { { { { { { -- `Id "y"
                 "y",
                tag = "Id"
              } }, { { { -- `Id "print"
                   "print",
                  tag = "Id"
                }, { -- `Id "y"
                   "y",
                  tag = "Id"
                },
                tag = "Call"
              } },
            tag = "Function"
          }, { {}, {},
            tag = "Function"
          },
          tag = "Call"
        },
        tag = "Call"
      } },
    tag = "Set"
  },
  source = "@test.lua"
}

... which is equivalent to

x = (function(y) print(y) end)(function() end)()

Expected AST is

{ { { { -- `Id "x"
         "x",
        tag = "Id"
      } }, { { { { -- `Id "y"
             "y",
            tag = "Id"
          } }, { { { -- `Id "print"
               "print",
              tag = "Id"
            }, { -- `Id "y"
               "y",
              tag = "Id"
            },
            tag = "Call"
          } },
        tag = "Function"
      } },
    tag = "Set"
  }, { { { {}, {},
        tag = "Function"
      },
      tag = "Paren"
    },
    tag = "Call"
  },
  source = "@test.lua"
}

... which can be currently produced by adding a semicolon:

x = function(y) print(y) end;
(function() end)()

Observed with metalua-parser 0.7.3-2