Skip to content

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

Description

@mpeterv

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions