Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of vararg functions #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions minify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,22 @@ function CreateLuaParser(text)
local function isBinop()
return BinopSet[peek().Source] or false
end
local function expect(type, source)
local function expect(type, source, type2, source2)
local tk = peek()
if tk.Type == type and (source == nil or tk.Source == source) then
return get()
elseif tk.Type == type2 and (source2 == nil or tk.Source == source2) then
return get()
else
for i = -3, 3 do
print("Tokens["..i.."] = `"..peek(i).Source.."`")
end
if source then
error(getTokenStartPosition(tk)..": `"..source.."` expected.")
else
error(getTokenStartPosition(tk)..": "..type.." expected.")
error(getTokenStartPosition(tk)..": "..type
..(type2 and (" or "..type2) or "")
.." expected.")
end
end
end
Expand Down Expand Up @@ -619,16 +623,21 @@ function CreateLuaParser(text)
end

-- List of identifiers
local function varlist()
local varList = {}
local commaList = {}
if peek().Type == 'Ident' then
local function varlist(isFuncDecl)
local varList, commaList, token = {}, {}, peek()
if token.Type == 'Ident' then
table.insert(varList, get())
elseif isFuncDecl and (token.Source == '...') then
table.insert(varList, get())
end
while peek().Source == ',' do
table.insert(commaList, get())
local id = expect('Ident')
table.insert(varList, id)
if isFuncDecl then
token = expect('Ident', nil, 'Symbol', '...')
else
token = expect('Ident')
end
table.insert(varList, token)
end
return varList, commaList
end
Expand Down Expand Up @@ -670,7 +679,7 @@ function CreateLuaParser(text)
end
--
local oparenTk = expect('Symbol', '(')
local argList, argCommaList = varlist()
local argList, argCommaList = varlist(true)
local cparenTk = expect('Symbol', ')')
local fbody, enTk = blockbody('end')
--
Expand Down Expand Up @@ -1691,12 +1700,15 @@ function AddVariableInfo(ast)
Pre = function(expr)
pushScope()
for index, ident in pairs(expr.ArgList) do
local var = addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
-- Note: Beware ident.Type == 'Symbol', it may be "..." here!
if ident.Type == 'Ident' then
local var = addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
end
end
end;
Post = function(expr)
Expand Down Expand Up @@ -1748,12 +1760,15 @@ function AddVariableInfo(ast)
})
pushScope()
for index, ident in pairs(stat.FunctionStat.ArgList) do
addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
-- Note: Beware ident.Type == 'Symbol', it may be "..." here!
if ident.Type == 'Ident' then
addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
end
end
end;
Post = function()
Expand Down Expand Up @@ -1783,12 +1798,15 @@ function AddVariableInfo(ast)
var.AssignedTo = true
pushScope()
for index, ident in pairs(stat.ArgList) do
addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
-- Note: Beware ident.Type == 'Symbol', it may be "..." here!
if ident.Type == 'Ident' then
addLocalVar(ident.Source, function(name)
ident.Source = name
end, {
Type = 'Argument';
Index = index;
})
end
end
end;
Post = function()
Expand Down