From 73be382385d05f15f27e2516e91d4bcf5ebea807 Mon Sep 17 00:00:00 2001 From: NiteHawk Date: Mon, 14 Aug 2017 13:50:26 +0200 Subject: [PATCH 1/3] Make expect() accept a second set of type/source specifiers This is useful if an alternative token needs to be handled. For example: When parsing argument lists for function declarations, expect() may either accept an identifier or a "..." symbol. --- minify.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/minify.lua b/minify.lua index 41d7a60..b4473e3 100644 --- a/minify.lua +++ b/minify.lua @@ -472,10 +472,12 @@ 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.."`") @@ -483,7 +485,9 @@ function CreateLuaParser(text) 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 From 3e20c4b1e3a7c5fe5b60197c8363d369e3926f6a Mon Sep 17 00:00:00 2001 From: NiteHawk Date: Mon, 14 Aug 2017 14:13:24 +0200 Subject: [PATCH 2/3] Enable proper handling of vararg functions ("..." symbol) --- minify.lua | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/minify.lua b/minify.lua index b4473e3..142e5ea 100644 --- a/minify.lua +++ b/minify.lua @@ -623,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 @@ -674,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') -- @@ -1752,12 +1757,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() @@ -1787,12 +1795,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() From 70969f235829e9000d78bc7c7d46521b0aaac700 Mon Sep 17 00:00:00 2001 From: NiteHawk Date: Wed, 11 Apr 2018 23:13:14 +0200 Subject: [PATCH 3/3] Properly fix handling of "..." in function literals The symbol would still get treated as an identifier (and thus substituted) otherwise. --- minify.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/minify.lua b/minify.lua index 142e5ea..039aec4 100644 --- a/minify.lua +++ b/minify.lua @@ -1700,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)