forked from osmarks/potatOS
performance improvements, probably
This commit is contained in:
121
minify/CommandLineBeautify.lua
Normal file
121
minify/CommandLineBeautify.lua
Normal file
@@ -0,0 +1,121 @@
|
||||
--
|
||||
-- beautify
|
||||
--
|
||||
-- A command line utility for beautifying lua source code using the beautifier.
|
||||
--
|
||||
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Beautify = require'FormatBeautiful'
|
||||
local ParseLua = Parser.ParseLua
|
||||
local PrintTable = util.PrintTable
|
||||
|
||||
local function splitFilename(name)
|
||||
--table.foreach(arg, print)
|
||||
if name:find(".") then
|
||||
local p, ext = name:match("()%.([^%.]*)$")
|
||||
if p and ext then
|
||||
if #ext == 0 then
|
||||
return name, nil
|
||||
else
|
||||
local filename = name:sub(1,p-1)
|
||||
return filename, ext
|
||||
end
|
||||
else
|
||||
return name, nil
|
||||
end
|
||||
else
|
||||
return name, nil
|
||||
end
|
||||
end
|
||||
|
||||
if #arg == 1 then
|
||||
local name, ext = splitFilename(arg[1])
|
||||
local outname = name.."_formatted"
|
||||
if ext then outname = outname.."."..ext end
|
||||
--
|
||||
local inf = io.open(arg[1], 'r')
|
||||
if not inf then
|
||||
print("Failed to open '"..arg[1].."' for reading")
|
||||
return
|
||||
end
|
||||
--
|
||||
local sourceText = inf:read('*all')
|
||||
inf:close()
|
||||
--
|
||||
local st, ast = ParseLua(sourceText)
|
||||
if not st then
|
||||
--we failed to parse the file, show why
|
||||
print(ast)
|
||||
return
|
||||
end
|
||||
--
|
||||
local outf = io.open(outname, 'w')
|
||||
if not outf then
|
||||
print("Failed to open '"..outname.."' for writing")
|
||||
return
|
||||
end
|
||||
--
|
||||
outf:write(Format_Beautify(ast))
|
||||
outf:close()
|
||||
--
|
||||
print("Beautification complete")
|
||||
|
||||
elseif #arg == 2 then
|
||||
--keep the user from accidentally overwriting their non-minified file with
|
||||
if arg[1]:find("_formatted") then
|
||||
print("Did you mix up the argument order?\n"..
|
||||
"Current command will beautify '"..arg[1].."' and overwrite '"..arg[2].."' with the results")
|
||||
while true do
|
||||
io.write("Confirm (yes/no): ")
|
||||
local msg = io.read('*line')
|
||||
if msg == 'yes' then
|
||||
break
|
||||
elseif msg == 'no' then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
local inf = io.open(arg[1], 'r')
|
||||
if not inf then
|
||||
print("Failed to open '"..arg[1].."' for reading")
|
||||
return
|
||||
end
|
||||
--
|
||||
local sourceText = inf:read('*all')
|
||||
inf:close()
|
||||
--
|
||||
local st, ast = ParseLua(sourceText)
|
||||
if not st then
|
||||
--we failed to parse the file, show why
|
||||
print(ast)
|
||||
return
|
||||
end
|
||||
--
|
||||
if arg[1] == arg[2] then
|
||||
print("Are you SURE you want to overwrite the source file with a beautified version?\n"..
|
||||
"You will be UNABLE to get the original source back!")
|
||||
while true do
|
||||
io.write("Confirm (yes/no): ")
|
||||
local msg = io.read('*line')
|
||||
if msg == 'yes' then
|
||||
break
|
||||
elseif msg == 'no' then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
local outf = io.open(arg[2], 'w')
|
||||
if not outf then
|
||||
print("Failed to open '"..arg[2].."' for writing")
|
||||
return
|
||||
end
|
||||
--
|
||||
outf:write(Format_Beautify(ast))
|
||||
outf:close()
|
||||
--
|
||||
print("Beautification complete")
|
||||
|
||||
else
|
||||
print("Invalid arguments!\nUsage: lua CommandLineLuaBeautify.lua source_file [destination_file]")
|
||||
end
|
47
minify/CommandLineLiveBeautify.lua
Normal file
47
minify/CommandLineLiveBeautify.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
--
|
||||
-- beautify.interactive
|
||||
--
|
||||
-- For testing: Lets you enter lines of text to be beautified to verify the
|
||||
-- correctness of their implementation.
|
||||
--
|
||||
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Beautify = require'FormatBeautiful'
|
||||
local ParseLua = Parser.ParseLua
|
||||
local PrintTable = util.PrintTable
|
||||
|
||||
while true do
|
||||
io.write('> ')
|
||||
local line = io.read('*line')
|
||||
local fileFrom, fileTo = line:match("^file (.*) (.*)")
|
||||
if fileFrom and fileTo then
|
||||
local file = io.open(fileFrom, 'r')
|
||||
local fileTo = io.open(fileTo, 'w')
|
||||
if file and fileTo then
|
||||
local st, ast = ParseLua(file:read('*all'))
|
||||
if st then
|
||||
fileTo:write(Format_Beautify(ast)..'\n')
|
||||
io.write("Beautification Complete\n")
|
||||
else
|
||||
io.write(""..tostring(ast).."\n")
|
||||
end
|
||||
file:close()
|
||||
fileTo:close()
|
||||
else
|
||||
io.write("File does not exist\n")
|
||||
end
|
||||
else
|
||||
local st, ast = ParseLua(line)
|
||||
if st then
|
||||
io.write("====== AST =======\n")
|
||||
io.write(PrintTable(ast)..'\n')
|
||||
io.write("==== BEAUTIFIED ====\n")
|
||||
io.write(Format_Beautify(ast))
|
||||
io.write("==================\n")
|
||||
else
|
||||
io.write(""..tostring(ast).."\n")
|
||||
end
|
||||
end
|
||||
end
|
47
minify/CommandLineLiveMinify.lua
Normal file
47
minify/CommandLineLiveMinify.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
--
|
||||
-- CommandLineLiveMinify.lua
|
||||
--
|
||||
-- For testing: Lets you enter lines of text to be minified to verify the
|
||||
-- correctness of their implementation.
|
||||
--
|
||||
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Mini = require'FormatMini'
|
||||
local ParseLua = Parser.ParseLua
|
||||
local PrintTable = util.PrintTable
|
||||
|
||||
while true do
|
||||
io.write('> ')
|
||||
local line = io.read('*line')
|
||||
local fileFrom, fileTo = line:match("^file (.*) (.*)")
|
||||
if fileFrom and fileTo then
|
||||
local file = io.open(fileFrom, 'r')
|
||||
local fileTo = io.open(fileTo, 'w')
|
||||
if file and fileTo then
|
||||
local st, ast = ParseLua(file:read('*all'))
|
||||
if st then
|
||||
fileTo:write(Format_Mini(ast)..'\n')
|
||||
io.write("Minification Complete\n")
|
||||
else
|
||||
io.write(""..tostring(ast).."\n")
|
||||
end
|
||||
file:close()
|
||||
fileTo:close()
|
||||
else
|
||||
io.write("File does not exist\n")
|
||||
end
|
||||
else
|
||||
local st, ast = ParseLua(line)
|
||||
if st then
|
||||
io.write("====== AST =======\n")
|
||||
io.write(PrintTable(ast)..'\n')
|
||||
io.write("==== MINIFIED ====\n")
|
||||
io.write(Format_Mini(ast)..'\n')
|
||||
io.write("==================\n")
|
||||
else
|
||||
io.write(""..tostring(ast).."\n")
|
||||
end
|
||||
end
|
||||
end
|
122
minify/CommandLineMinify.lua
Normal file
122
minify/CommandLineMinify.lua
Normal file
@@ -0,0 +1,122 @@
|
||||
|
||||
--
|
||||
-- CommandlineMinify.lua
|
||||
--
|
||||
-- A command line utility for minifying lua source code using the minifier.
|
||||
--
|
||||
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Mini = require'FormatMini'
|
||||
local ParseLua = Parser.ParseLua
|
||||
local PrintTable = util.PrintTable
|
||||
|
||||
local function splitFilename(name)
|
||||
table.foreach(arg, print)
|
||||
if name:find(".") then
|
||||
local p, ext = name:match("()%.([^%.]*)$")
|
||||
if p and ext then
|
||||
if #ext == 0 then
|
||||
return name, nil
|
||||
else
|
||||
local filename = name:sub(1,p-1)
|
||||
return filename, ext
|
||||
end
|
||||
else
|
||||
return name, nil
|
||||
end
|
||||
else
|
||||
return name, nil
|
||||
end
|
||||
end
|
||||
|
||||
if #arg == 1 then
|
||||
local name, ext = splitFilename(arg[1])
|
||||
local outname = name.."_min"
|
||||
if ext then outname = outname.."."..ext end
|
||||
--
|
||||
local inf = io.open(arg[1], 'r')
|
||||
if not inf then
|
||||
print("Failed to open `"..arg[1].."` for reading")
|
||||
return
|
||||
end
|
||||
--
|
||||
local sourceText = inf:read('*all')
|
||||
inf:close()
|
||||
--
|
||||
local st, ast = ParseLua(sourceText)
|
||||
if not st then
|
||||
--we failed to parse the file, show why
|
||||
print(ast)
|
||||
return
|
||||
end
|
||||
--
|
||||
local outf = io.open(outname, 'w')
|
||||
if not outf then
|
||||
print("Failed to open `"..outname.."` for writing")
|
||||
return
|
||||
end
|
||||
--
|
||||
outf:write(Format_Mini(ast))
|
||||
outf:close()
|
||||
--
|
||||
print("Minification complete")
|
||||
|
||||
elseif #arg == 2 then
|
||||
--keep the user from accidentally overwriting their non-minified file with
|
||||
if arg[1]:find("_min") then
|
||||
print("Did you mix up the argument order?\n"..
|
||||
"Current command will minify `"..arg[1].."` and OVERWRITE `"..arg[2].."` with the results")
|
||||
while true do
|
||||
io.write("Confirm (yes/cancel): ")
|
||||
local msg = io.read('*line')
|
||||
if msg == 'yes' then
|
||||
break
|
||||
elseif msg == 'cancel' then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
local inf = io.open(arg[1], 'r')
|
||||
if not inf then
|
||||
print("Failed to open `"..arg[1].."` for reading")
|
||||
return
|
||||
end
|
||||
--
|
||||
local sourceText = inf:read('*all')
|
||||
inf:close()
|
||||
--
|
||||
local st, ast = ParseLua(sourceText)
|
||||
if not st then
|
||||
--we failed to parse the file, show why
|
||||
print(ast)
|
||||
return
|
||||
end
|
||||
--
|
||||
if arg[1] == arg[2] then
|
||||
print("Are you SURE you want to overwrite the source file with a minified version?\n"..
|
||||
"You will be UNABLE to get the original source back!")
|
||||
while true do
|
||||
io.write("Confirm (yes/cancel): ")
|
||||
local msg = io.read('*line')
|
||||
if msg == 'yes' then
|
||||
break
|
||||
elseif msg == 'cancel' then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
local outf = io.open(arg[2], 'w')
|
||||
if not outf then
|
||||
print("Failed to open `"..arg[2].."` for writing")
|
||||
return
|
||||
end
|
||||
--
|
||||
outf:write(Format_Mini(ast))
|
||||
outf:close()
|
||||
--
|
||||
print("Minification complete")
|
||||
|
||||
else
|
||||
print("Invalid arguments, Usage:\nLuaMinify source [destination]")
|
||||
end
|
347
minify/FormatBeautiful.lua
Normal file
347
minify/FormatBeautiful.lua
Normal file
@@ -0,0 +1,347 @@
|
||||
--
|
||||
-- Beautifier
|
||||
--
|
||||
-- Returns a beautified version of the code, including comments
|
||||
--
|
||||
|
||||
local parser = require"ParseLua"
|
||||
local ParseLua = parser.ParseLua
|
||||
local util = require'Util'
|
||||
local lookupify = util.lookupify
|
||||
|
||||
local LowerChars = lookupify{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
|
||||
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
|
||||
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
|
||||
local UpperChars = lookupify{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
||||
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
|
||||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
|
||||
local Digits = lookupify{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
|
||||
|
||||
local function Format_Beautify(ast)
|
||||
local formatStatlist, formatExpr
|
||||
local indent = 0
|
||||
local EOL = "\n"
|
||||
|
||||
local function getIndentation()
|
||||
return string.rep(" ", indent)
|
||||
end
|
||||
|
||||
local function joinStatementsSafe(a, b, sep)
|
||||
sep = sep or ''
|
||||
local aa, bb = a:sub(-1,-1), b:sub(1,1)
|
||||
if UpperChars[aa] or LowerChars[aa] or aa == '_' then
|
||||
if not (UpperChars[bb] or LowerChars[bb] or bb == '_' or Digits[bb]) then
|
||||
--bb is a symbol, can join without sep
|
||||
return a .. b
|
||||
elseif bb == '(' then
|
||||
--prevent ambiguous syntax
|
||||
return a..sep..b
|
||||
else
|
||||
return a..sep..b
|
||||
end
|
||||
elseif Digits[aa] then
|
||||
if bb == '(' then
|
||||
--can join statements directly
|
||||
return a..b
|
||||
else
|
||||
return a..sep..b
|
||||
end
|
||||
elseif aa == '' then
|
||||
return a..b
|
||||
else
|
||||
if bb == '(' then
|
||||
--don't want to accidentally call last statement, can't join directly
|
||||
return a..sep..b
|
||||
else
|
||||
return a..b
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
formatExpr = function(expr)
|
||||
local out = string.rep('(', expr.ParenCount or 0)
|
||||
if expr.AstType == 'VarExpr' then
|
||||
if expr.Variable then
|
||||
out = out .. expr.Variable.Name
|
||||
else
|
||||
out = out .. expr.Name
|
||||
end
|
||||
|
||||
elseif expr.AstType == 'NumberExpr' then
|
||||
out = out..expr.Value.Data
|
||||
|
||||
elseif expr.AstType == 'StringExpr' then
|
||||
out = out..expr.Value.Data
|
||||
|
||||
elseif expr.AstType == 'BooleanExpr' then
|
||||
out = out..tostring(expr.Value)
|
||||
|
||||
elseif expr.AstType == 'NilExpr' then
|
||||
out = joinStatementsSafe(out, "nil")
|
||||
|
||||
elseif expr.AstType == 'BinopExpr' then
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Lhs)) .. " "
|
||||
out = joinStatementsSafe(out, expr.Op) .. " "
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Rhs))
|
||||
|
||||
elseif expr.AstType == 'UnopExpr' then
|
||||
out = joinStatementsSafe(out, expr.Op) .. (#expr.Op ~= 1 and " " or "")
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Rhs))
|
||||
|
||||
elseif expr.AstType == 'DotsExpr' then
|
||||
out = out.."..."
|
||||
|
||||
elseif expr.AstType == 'CallExpr' then
|
||||
out = out..formatExpr(expr.Base)
|
||||
out = out.."("
|
||||
for i = 1, #expr.Arguments do
|
||||
out = out..formatExpr(expr.Arguments[i])
|
||||
if i ~= #expr.Arguments then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
out = out..")"
|
||||
|
||||
elseif expr.AstType == 'TableCallExpr' then
|
||||
out = out..formatExpr(expr.Base) .. " "
|
||||
out = out..formatExpr(expr.Arguments[1])
|
||||
|
||||
elseif expr.AstType == 'StringCallExpr' then
|
||||
out = out..formatExpr(expr.Base) .. " "
|
||||
out = out..expr.Arguments[1].Data
|
||||
|
||||
elseif expr.AstType == 'IndexExpr' then
|
||||
out = out..formatExpr(expr.Base).."["..formatExpr(expr.Index).."]"
|
||||
|
||||
elseif expr.AstType == 'MemberExpr' then
|
||||
out = out..formatExpr(expr.Base)..expr.Indexer..expr.Ident.Data
|
||||
|
||||
elseif expr.AstType == 'Function' then
|
||||
-- anonymous function
|
||||
out = out.."function("
|
||||
if #expr.Arguments > 0 then
|
||||
for i = 1, #expr.Arguments do
|
||||
out = out..expr.Arguments[i].Name
|
||||
if i ~= #expr.Arguments then
|
||||
out = out..", "
|
||||
elseif expr.VarArg then
|
||||
out = out..", ..."
|
||||
end
|
||||
end
|
||||
elseif expr.VarArg then
|
||||
out = out.."..."
|
||||
end
|
||||
out = out..")" .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(expr.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end")
|
||||
elseif expr.AstType == 'ConstructorExpr' then
|
||||
out = out.."{ "
|
||||
for i = 1, #expr.EntryList do
|
||||
local entry = expr.EntryList[i]
|
||||
if entry.Type == 'Key' then
|
||||
out = out.."["..formatExpr(entry.Key).."] = "..formatExpr(entry.Value)
|
||||
elseif entry.Type == 'Value' then
|
||||
out = out..formatExpr(entry.Value)
|
||||
elseif entry.Type == 'KeyString' then
|
||||
out = out..entry.Key.." = "..formatExpr(entry.Value)
|
||||
end
|
||||
if i ~= #expr.EntryList then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
out = out.." }"
|
||||
|
||||
elseif expr.AstType == 'Parentheses' then
|
||||
out = out.."("..formatExpr(expr.Inner)..")"
|
||||
|
||||
end
|
||||
out = out..string.rep(')', expr.ParenCount or 0)
|
||||
return out
|
||||
end
|
||||
|
||||
local formatStatement = function(statement)
|
||||
local out = ""
|
||||
if statement.AstType == 'AssignmentStatement' then
|
||||
out = getIndentation()
|
||||
for i = 1, #statement.Lhs do
|
||||
out = out..formatExpr(statement.Lhs[i])
|
||||
if i ~= #statement.Lhs then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
if #statement.Rhs > 0 then
|
||||
out = out.." = "
|
||||
for i = 1, #statement.Rhs do
|
||||
out = out..formatExpr(statement.Rhs[i])
|
||||
if i ~= #statement.Rhs then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif statement.AstType == 'CallStatement' then
|
||||
out = getIndentation() .. formatExpr(statement.Expression)
|
||||
elseif statement.AstType == 'LocalStatement' then
|
||||
out = getIndentation() .. out.."local "
|
||||
for i = 1, #statement.LocalList do
|
||||
out = out..statement.LocalList[i].Name
|
||||
if i ~= #statement.LocalList then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
if #statement.InitList > 0 then
|
||||
out = out.." = "
|
||||
for i = 1, #statement.InitList do
|
||||
out = out..formatExpr(statement.InitList[i])
|
||||
if i ~= #statement.InitList then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif statement.AstType == 'IfStatement' then
|
||||
out = getIndentation() .. joinStatementsSafe("if ", formatExpr(statement.Clauses[1].Condition))
|
||||
out = joinStatementsSafe(out, " then") .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Clauses[1].Body))
|
||||
indent = indent - 1
|
||||
for i = 2, #statement.Clauses do
|
||||
local st = statement.Clauses[i]
|
||||
if st.Condition then
|
||||
out = getIndentation() .. joinStatementsSafe(out, getIndentation() .. "elseif ")
|
||||
out = joinStatementsSafe(out, formatExpr(st.Condition))
|
||||
out = joinStatementsSafe(out, " then") .. EOL
|
||||
else
|
||||
out = joinStatementsSafe(out, getIndentation() .. "else") .. EOL
|
||||
end
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(st.Body))
|
||||
indent = indent - 1
|
||||
end
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'WhileStatement' then
|
||||
out = getIndentation() .. joinStatementsSafe("while ", formatExpr(statement.Condition))
|
||||
out = joinStatementsSafe(out, " do") .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'DoStatement' then
|
||||
out = getIndentation() .. joinStatementsSafe(out, "do") .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'ReturnStatement' then
|
||||
out = getIndentation() .. "return "
|
||||
for i = 1, #statement.Arguments do
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Arguments[i]))
|
||||
if i ~= #statement.Arguments then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
elseif statement.AstType == 'BreakStatement' then
|
||||
out = getIndentation() .. "break"
|
||||
elseif statement.AstType == 'RepeatStatement' then
|
||||
out = getIndentation() .. "repeat" .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "until ")
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Condition)) .. EOL
|
||||
elseif statement.AstType == 'Function' then
|
||||
if statement.IsLocal then
|
||||
out = "local "
|
||||
end
|
||||
out = joinStatementsSafe(out, "function ")
|
||||
out = getIndentation() .. out
|
||||
if statement.IsLocal then
|
||||
out = out..statement.Name.Name
|
||||
else
|
||||
out = out..formatExpr(statement.Name)
|
||||
end
|
||||
out = out.."("
|
||||
if #statement.Arguments > 0 then
|
||||
for i = 1, #statement.Arguments do
|
||||
out = out..statement.Arguments[i].Name
|
||||
if i ~= #statement.Arguments then
|
||||
out = out..", "
|
||||
elseif statement.VarArg then
|
||||
out = out..",..."
|
||||
end
|
||||
end
|
||||
elseif statement.VarArg then
|
||||
out = out.."..."
|
||||
end
|
||||
out = out..")" .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'GenericForStatement' then
|
||||
out = getIndentation() .. "for "
|
||||
for i = 1, #statement.VariableList do
|
||||
out = out..statement.VariableList[i].Name
|
||||
if i ~= #statement.VariableList then
|
||||
out = out..", "
|
||||
end
|
||||
end
|
||||
out = out.." in "
|
||||
for i = 1, #statement.Generators do
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Generators[i]))
|
||||
if i ~= #statement.Generators then
|
||||
out = joinStatementsSafe(out, ', ')
|
||||
end
|
||||
end
|
||||
out = joinStatementsSafe(out, " do") .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'NumericForStatement' then
|
||||
out = getIndentation() .. "for "
|
||||
out = out..statement.Variable.Name.." = "
|
||||
out = out..formatExpr(statement.Start)..", "..formatExpr(statement.End)
|
||||
if statement.Step then
|
||||
out = out..", "..formatExpr(statement.Step)
|
||||
end
|
||||
out = joinStatementsSafe(out, " do") .. EOL
|
||||
indent = indent + 1
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
indent = indent - 1
|
||||
out = joinStatementsSafe(out, getIndentation() .. "end") .. EOL
|
||||
elseif statement.AstType == 'LabelStatement' then
|
||||
out = getIndentation() .. "::" .. statement.Label .. "::" .. EOL
|
||||
elseif statement.AstType == 'GotoStatement' then
|
||||
out = getIndentation() .. "goto " .. statement.Label .. EOL
|
||||
elseif statement.AstType == 'Comment' then
|
||||
if statement.CommentType == 'Shebang' then
|
||||
out = getIndentation() .. statement.Data
|
||||
--out = out .. EOL
|
||||
elseif statement.CommentType == 'Comment' then
|
||||
out = getIndentation() .. statement.Data
|
||||
--out = out .. EOL
|
||||
elseif statement.CommentType == 'LongComment' then
|
||||
out = getIndentation() .. statement.Data
|
||||
--out = out .. EOL
|
||||
end
|
||||
elseif statement.AstType == 'Eof' then
|
||||
-- Ignore
|
||||
else
|
||||
print("Unknown AST Type: ", statement.AstType)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
formatStatlist = function(statList)
|
||||
local out = ''
|
||||
for _, stat in pairs(statList.Body) do
|
||||
out = joinStatementsSafe(out, formatStatement(stat) .. EOL)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return formatStatlist(ast)
|
||||
end
|
||||
|
||||
return Format_Beautify
|
436
minify/FormatIdentity.lua
Normal file
436
minify/FormatIdentity.lua
Normal file
@@ -0,0 +1,436 @@
|
||||
require'strict'
|
||||
require'ParseLua'
|
||||
local util = require'Util'
|
||||
|
||||
local function debug_printf(...)
|
||||
--[[
|
||||
util.printf(...)
|
||||
--]]
|
||||
end
|
||||
|
||||
--
|
||||
-- FormatIdentity.lua
|
||||
--
|
||||
-- Returns the exact source code that was used to create an AST, preserving all
|
||||
-- comments and whitespace.
|
||||
-- This can be used to get back a Lua source after renaming some variables in
|
||||
-- an AST.
|
||||
--
|
||||
|
||||
local function Format_Identity(ast)
|
||||
local out = {
|
||||
rope = {}, -- List of strings
|
||||
line = 1,
|
||||
char = 1,
|
||||
|
||||
appendStr = function(self, str)
|
||||
table.insert(self.rope, str)
|
||||
|
||||
local lines = util.splitLines(str)
|
||||
if #lines == 1 then
|
||||
self.char = self.char + #str
|
||||
else
|
||||
self.line = self.line + #lines - 1
|
||||
local lastLine = lines[#lines]
|
||||
self.char = #lastLine
|
||||
end
|
||||
end,
|
||||
|
||||
appendToken = function(self, token)
|
||||
self:appendWhite(token)
|
||||
--[*[
|
||||
--debug_printf("appendToken(%q)", token.Data)
|
||||
local data = token.Data
|
||||
local lines = util.splitLines(data)
|
||||
while self.line + #lines < token.Line do
|
||||
print("Inserting extra line")
|
||||
self.str = self.str .. '\n'
|
||||
self.line = self.line + 1
|
||||
self.char = 1
|
||||
end
|
||||
--]]
|
||||
self:appendStr(token.Data)
|
||||
end,
|
||||
|
||||
appendTokens = function(self, tokens)
|
||||
for _,token in ipairs(tokens) do
|
||||
self:appendToken( token )
|
||||
end
|
||||
end,
|
||||
|
||||
appendWhite = function(self, token)
|
||||
if token.LeadingWhite then
|
||||
self:appendTokens( token.LeadingWhite )
|
||||
--self.str = self.str .. ' '
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
local formatStatlist, formatExpr;
|
||||
|
||||
formatExpr = function(expr)
|
||||
local tok_it = 1
|
||||
local function appendNextToken(str)
|
||||
local tok = expr.Tokens[tok_it];
|
||||
if str and tok.Data ~= str then
|
||||
error("Expected token '" .. str .. "'. Tokens: " .. util.PrintTable(expr.Tokens))
|
||||
end
|
||||
out:appendToken( tok )
|
||||
tok_it = tok_it + 1
|
||||
end
|
||||
local function appendToken(token)
|
||||
out:appendToken( token )
|
||||
tok_it = tok_it + 1
|
||||
end
|
||||
local function appendWhite()
|
||||
local tok = expr.Tokens[tok_it];
|
||||
if not tok then error(util.PrintTable(expr)) end
|
||||
out:appendWhite( tok )
|
||||
tok_it = tok_it + 1
|
||||
end
|
||||
local function appendStr(str)
|
||||
appendWhite()
|
||||
out:appendStr(str)
|
||||
end
|
||||
local function peek()
|
||||
if tok_it < #expr.Tokens then
|
||||
return expr.Tokens[tok_it].Data
|
||||
end
|
||||
end
|
||||
local function appendComma(mandatory, seperators)
|
||||
if true then
|
||||
seperators = seperators or { "," }
|
||||
seperators = util.lookupify( seperators )
|
||||
if not mandatory and not seperators[peek()] then
|
||||
return
|
||||
end
|
||||
assert(seperators[peek()], "Missing comma or semicolon")
|
||||
appendNextToken()
|
||||
else
|
||||
local p = peek()
|
||||
if p == "," or p == ";" then
|
||||
appendNextToken()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
debug_printf("formatExpr(%s) at line %i", expr.AstType, expr.Tokens[1] and expr.Tokens[1].Line or -1)
|
||||
|
||||
if expr.AstType == 'VarExpr' then
|
||||
if expr.Variable then
|
||||
appendStr( expr.Variable.Name )
|
||||
else
|
||||
appendStr( expr.Name )
|
||||
end
|
||||
|
||||
elseif expr.AstType == 'NumberExpr' then
|
||||
appendToken( expr.Value )
|
||||
|
||||
elseif expr.AstType == 'StringExpr' then
|
||||
appendToken( expr.Value )
|
||||
|
||||
elseif expr.AstType == 'BooleanExpr' then
|
||||
appendNextToken( expr.Value and "true" or "false" )
|
||||
|
||||
elseif expr.AstType == 'NilExpr' then
|
||||
appendNextToken( "nil" )
|
||||
|
||||
elseif expr.AstType == 'BinopExpr' then
|
||||
formatExpr(expr.Lhs)
|
||||
appendStr( expr.Op )
|
||||
formatExpr(expr.Rhs)
|
||||
|
||||
elseif expr.AstType == 'UnopExpr' then
|
||||
appendStr( expr.Op )
|
||||
formatExpr(expr.Rhs)
|
||||
|
||||
elseif expr.AstType == 'DotsExpr' then
|
||||
appendNextToken( "..." )
|
||||
|
||||
elseif expr.AstType == 'CallExpr' then
|
||||
formatExpr(expr.Base)
|
||||
appendNextToken( "(" )
|
||||
for i,arg in ipairs( expr.Arguments ) do
|
||||
formatExpr(arg)
|
||||
appendComma( i ~= #expr.Arguments )
|
||||
end
|
||||
appendNextToken( ")" )
|
||||
|
||||
elseif expr.AstType == 'TableCallExpr' then
|
||||
formatExpr( expr.Base )
|
||||
formatExpr( expr.Arguments[1] )
|
||||
|
||||
elseif expr.AstType == 'StringCallExpr' then
|
||||
formatExpr(expr.Base)
|
||||
appendToken( expr.Arguments[1] )
|
||||
|
||||
elseif expr.AstType == 'IndexExpr' then
|
||||
formatExpr(expr.Base)
|
||||
appendNextToken( "[" )
|
||||
formatExpr(expr.Index)
|
||||
appendNextToken( "]" )
|
||||
|
||||
elseif expr.AstType == 'MemberExpr' then
|
||||
formatExpr(expr.Base)
|
||||
appendNextToken() -- . or :
|
||||
appendToken(expr.Ident)
|
||||
|
||||
elseif expr.AstType == 'Function' then
|
||||
-- anonymous function
|
||||
appendNextToken( "function" )
|
||||
appendNextToken( "(" )
|
||||
if #expr.Arguments > 0 then
|
||||
for i = 1, #expr.Arguments do
|
||||
appendStr( expr.Arguments[i].Name )
|
||||
if i ~= #expr.Arguments then
|
||||
appendNextToken(",")
|
||||
elseif expr.VarArg then
|
||||
appendNextToken(",")
|
||||
appendNextToken("...")
|
||||
end
|
||||
end
|
||||
elseif expr.VarArg then
|
||||
appendNextToken("...")
|
||||
end
|
||||
appendNextToken(")")
|
||||
formatStatlist(expr.Body)
|
||||
appendNextToken("end")
|
||||
|
||||
elseif expr.AstType == 'ConstructorExpr' then
|
||||
appendNextToken( "{" )
|
||||
for i = 1, #expr.EntryList do
|
||||
local entry = expr.EntryList[i]
|
||||
if entry.Type == 'Key' then
|
||||
appendNextToken( "[" )
|
||||
formatExpr(entry.Key)
|
||||
appendNextToken( "]" )
|
||||
appendNextToken( "=" )
|
||||
formatExpr(entry.Value)
|
||||
elseif entry.Type == 'Value' then
|
||||
formatExpr(entry.Value)
|
||||
elseif entry.Type == 'KeyString' then
|
||||
appendStr(entry.Key)
|
||||
appendNextToken( "=" )
|
||||
formatExpr(entry.Value)
|
||||
end
|
||||
appendComma( i ~= #expr.EntryList, { ",", ";" } )
|
||||
end
|
||||
appendNextToken( "}" )
|
||||
|
||||
elseif expr.AstType == 'Parentheses' then
|
||||
appendNextToken( "(" )
|
||||
formatExpr(expr.Inner)
|
||||
appendNextToken( ")" )
|
||||
|
||||
else
|
||||
print("Unknown AST Type: ", statement.AstType)
|
||||
end
|
||||
|
||||
assert(tok_it == #expr.Tokens + 1)
|
||||
debug_printf("/formatExpr")
|
||||
end
|
||||
|
||||
|
||||
local formatStatement = function(statement)
|
||||
local tok_it = 1
|
||||
local function appendNextToken(str)
|
||||
local tok = statement.Tokens[tok_it];
|
||||
assert(tok, string.format("Not enough tokens for %q. First token at %i:%i",
|
||||
str, statement.Tokens[1].Line, statement.Tokens[1].Char))
|
||||
assert(tok.Data == str,
|
||||
string.format('Expected token %q, got %q', str, tok.Data))
|
||||
out:appendToken( tok )
|
||||
tok_it = tok_it + 1
|
||||
end
|
||||
local function appendWhite()
|
||||
local tok = statement.Tokens[tok_it];
|
||||
out:appendWhite( tok )
|
||||
tok_it = tok_it + 1
|
||||
end
|
||||
local function appendStr(str)
|
||||
appendWhite()
|
||||
out:appendStr(str)
|
||||
end
|
||||
local function appendComma(mandatory)
|
||||
if mandatory
|
||||
or (tok_it < #statement.Tokens and statement.Tokens[tok_it].Data == ",") then
|
||||
appendNextToken( "," )
|
||||
end
|
||||
end
|
||||
|
||||
debug_printf("")
|
||||
debug_printf(string.format("formatStatement(%s) at line %i", statement.AstType, statement.Tokens[1] and statement.Tokens[1].Line or -1))
|
||||
|
||||
if statement.AstType == 'AssignmentStatement' then
|
||||
for i,v in ipairs(statement.Lhs) do
|
||||
formatExpr(v)
|
||||
appendComma( i ~= #statement.Lhs )
|
||||
end
|
||||
if #statement.Rhs > 0 then
|
||||
appendNextToken( "=" )
|
||||
for i,v in ipairs(statement.Rhs) do
|
||||
formatExpr(v)
|
||||
appendComma( i ~= #statement.Rhs )
|
||||
end
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'CallStatement' then
|
||||
formatExpr(statement.Expression)
|
||||
|
||||
elseif statement.AstType == 'LocalStatement' then
|
||||
appendNextToken( "local" )
|
||||
for i = 1, #statement.LocalList do
|
||||
appendStr( statement.LocalList[i].Name )
|
||||
appendComma( i ~= #statement.LocalList )
|
||||
end
|
||||
if #statement.InitList > 0 then
|
||||
appendNextToken( "=" )
|
||||
for i = 1, #statement.InitList do
|
||||
formatExpr(statement.InitList[i])
|
||||
appendComma( i ~= #statement.InitList )
|
||||
end
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'IfStatement' then
|
||||
appendNextToken( "if" )
|
||||
formatExpr( statement.Clauses[1].Condition )
|
||||
appendNextToken( "then" )
|
||||
formatStatlist( statement.Clauses[1].Body )
|
||||
for i = 2, #statement.Clauses do
|
||||
local st = statement.Clauses[i]
|
||||
if st.Condition then
|
||||
appendNextToken( "elseif" )
|
||||
formatExpr(st.Condition)
|
||||
appendNextToken( "then" )
|
||||
else
|
||||
appendNextToken( "else" )
|
||||
end
|
||||
formatStatlist(st.Body)
|
||||
end
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'WhileStatement' then
|
||||
appendNextToken( "while" )
|
||||
formatExpr(statement.Condition)
|
||||
appendNextToken( "do" )
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'DoStatement' then
|
||||
appendNextToken( "do" )
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'ReturnStatement' then
|
||||
appendNextToken( "return" )
|
||||
for i = 1, #statement.Arguments do
|
||||
formatExpr(statement.Arguments[i])
|
||||
appendComma( i ~= #statement.Arguments )
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'BreakStatement' then
|
||||
appendNextToken( "break" )
|
||||
|
||||
elseif statement.AstType == 'RepeatStatement' then
|
||||
appendNextToken( "repeat" )
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "until" )
|
||||
formatExpr(statement.Condition)
|
||||
|
||||
elseif statement.AstType == 'Function' then
|
||||
--print(util.PrintTable(statement))
|
||||
|
||||
if statement.IsLocal then
|
||||
appendNextToken( "local" )
|
||||
end
|
||||
appendNextToken( "function" )
|
||||
|
||||
if statement.IsLocal then
|
||||
appendStr(statement.Name.Name)
|
||||
else
|
||||
formatExpr(statement.Name)
|
||||
end
|
||||
|
||||
appendNextToken( "(" )
|
||||
if #statement.Arguments > 0 then
|
||||
for i = 1, #statement.Arguments do
|
||||
appendStr( statement.Arguments[i].Name )
|
||||
appendComma( i ~= #statement.Arguments or statement.VarArg )
|
||||
if i == #statement.Arguments and statement.VarArg then
|
||||
appendNextToken( "..." )
|
||||
end
|
||||
end
|
||||
elseif statement.VarArg then
|
||||
appendNextToken( "..." )
|
||||
end
|
||||
appendNextToken( ")" )
|
||||
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'GenericForStatement' then
|
||||
appendNextToken( "for" )
|
||||
for i = 1, #statement.VariableList do
|
||||
appendStr( statement.VariableList[i].Name )
|
||||
appendComma( i ~= #statement.VariableList )
|
||||
end
|
||||
appendNextToken( "in" )
|
||||
for i = 1, #statement.Generators do
|
||||
formatExpr(statement.Generators[i])
|
||||
appendComma( i ~= #statement.Generators )
|
||||
end
|
||||
appendNextToken( "do" )
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'NumericForStatement' then
|
||||
appendNextToken( "for" )
|
||||
appendStr( statement.Variable.Name )
|
||||
appendNextToken( "=" )
|
||||
formatExpr(statement.Start)
|
||||
appendNextToken( "," )
|
||||
formatExpr(statement.End)
|
||||
if statement.Step then
|
||||
appendNextToken( "," )
|
||||
formatExpr(statement.Step)
|
||||
end
|
||||
appendNextToken( "do" )
|
||||
formatStatlist(statement.Body)
|
||||
appendNextToken( "end" )
|
||||
|
||||
elseif statement.AstType == 'LabelStatement' then
|
||||
appendNextToken( "::" )
|
||||
appendStr( statement.Label )
|
||||
appendNextToken( "::" )
|
||||
|
||||
elseif statement.AstType == 'GotoStatement' then
|
||||
appendNextToken( "goto" )
|
||||
appendStr( statement.Label )
|
||||
|
||||
elseif statement.AstType == 'Eof' then
|
||||
appendWhite()
|
||||
|
||||
else
|
||||
print("Unknown AST Type: ", statement.AstType)
|
||||
end
|
||||
|
||||
if statement.Semicolon then
|
||||
appendNextToken(";")
|
||||
end
|
||||
|
||||
assert(tok_it == #statement.Tokens + 1)
|
||||
debug_printf("/formatStatment")
|
||||
end
|
||||
|
||||
formatStatlist = function(statList)
|
||||
for _, stat in ipairs(statList.Body) do
|
||||
formatStatement(stat)
|
||||
end
|
||||
end
|
||||
|
||||
formatStatlist(ast)
|
||||
|
||||
return true, table.concat(out.rope)
|
||||
end
|
||||
|
||||
return Format_Identity
|
365
minify/FormatMini.lua
Normal file
365
minify/FormatMini.lua
Normal file
@@ -0,0 +1,365 @@
|
||||
|
||||
local parser = require'ParseLua'
|
||||
local ParseLua = parser.ParseLua
|
||||
local util = require'Util'
|
||||
local lookupify = util.lookupify
|
||||
|
||||
--
|
||||
-- FormatMini.lua
|
||||
--
|
||||
-- Returns the minified version of an AST. Operations which are performed:
|
||||
-- - All comments and whitespace are ignored
|
||||
-- - All local variables are renamed
|
||||
--
|
||||
|
||||
local LowerChars = lookupify{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
|
||||
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
|
||||
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
|
||||
local UpperChars = lookupify{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
||||
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
|
||||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
|
||||
local Digits = lookupify{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
|
||||
local Symbols = lookupify{'+', '-', '*', '/', '^', '%', ',', '{', '}', '[', ']', '(', ')', ';', '#'}
|
||||
|
||||
local function Format_Mini(ast)
|
||||
local formatStatlist, formatExpr;
|
||||
local count = 0
|
||||
--
|
||||
local function joinStatementsSafe(a, b, sep)
|
||||
--print(a, b)
|
||||
if count > 150 then
|
||||
count = 0
|
||||
return a.."\n"..b
|
||||
end
|
||||
sep = sep or ' '
|
||||
local aa, bb = a:sub(-1,-1), b:sub(1,1)
|
||||
if UpperChars[aa] or LowerChars[aa] or aa == '_' then
|
||||
if not (UpperChars[bb] or LowerChars[bb] or bb == '_' or Digits[bb]) then
|
||||
--bb is a symbol, can join without sep
|
||||
return a..b
|
||||
elseif bb == '(' then
|
||||
print("==============>>>",aa,bb)
|
||||
--prevent ambiguous syntax
|
||||
return a..sep..b
|
||||
else
|
||||
return a..sep..b
|
||||
end
|
||||
elseif Digits[aa] then
|
||||
if bb == '(' then
|
||||
--can join statements directly
|
||||
return a..b
|
||||
elseif Symbols[bb] then
|
||||
return a .. b
|
||||
else
|
||||
return a..sep..b
|
||||
end
|
||||
elseif aa == '' then
|
||||
return a..b
|
||||
else
|
||||
if bb == '(' then
|
||||
--don't want to accidentally call last statement, can't join directly
|
||||
return a..sep..b
|
||||
else
|
||||
--print("asdf", '"'..a..'"', '"'..b..'"')
|
||||
return a..b
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
formatExpr = function(expr, precedence)
|
||||
local precedence = precedence or 0
|
||||
local currentPrecedence = 0
|
||||
local skipParens = false
|
||||
local out = ""
|
||||
if expr.AstType == 'VarExpr' then
|
||||
if expr.Variable then
|
||||
out = out..expr.Variable.Name
|
||||
else
|
||||
out = out..expr.Name
|
||||
end
|
||||
|
||||
elseif expr.AstType == 'NumberExpr' then
|
||||
out = out..expr.Value.Data
|
||||
|
||||
elseif expr.AstType == 'StringExpr' then
|
||||
out = out..expr.Value.Data
|
||||
|
||||
elseif expr.AstType == 'BooleanExpr' then
|
||||
out = out..tostring(expr.Value)
|
||||
|
||||
elseif expr.AstType == 'NilExpr' then
|
||||
out = joinStatementsSafe(out, "nil")
|
||||
|
||||
elseif expr.AstType == 'BinopExpr' then
|
||||
currentPrecedence = expr.OperatorPrecedence
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Lhs, currentPrecedence))
|
||||
out = joinStatementsSafe(out, expr.Op)
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Rhs))
|
||||
if expr.Op == '^' or expr.Op == '..' then
|
||||
currentPrecedence = currentPrecedence - 1
|
||||
end
|
||||
|
||||
if currentPrecedence < precedence then
|
||||
skipParens = false
|
||||
else
|
||||
skipParens = true
|
||||
end
|
||||
--print(skipParens, precedence, currentPrecedence)
|
||||
elseif expr.AstType == 'UnopExpr' then
|
||||
out = joinStatementsSafe(out, expr.Op)
|
||||
out = joinStatementsSafe(out, formatExpr(expr.Rhs))
|
||||
|
||||
elseif expr.AstType == 'DotsExpr' then
|
||||
out = out.."..."
|
||||
|
||||
elseif expr.AstType == 'CallExpr' then
|
||||
out = out..formatExpr(expr.Base)
|
||||
out = out.."("
|
||||
for i = 1, #expr.Arguments do
|
||||
out = out..formatExpr(expr.Arguments[i])
|
||||
if i ~= #expr.Arguments then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
out = out..")"
|
||||
|
||||
elseif expr.AstType == 'TableCallExpr' then
|
||||
out = out..formatExpr(expr.Base)
|
||||
out = out..formatExpr(expr.Arguments[1])
|
||||
|
||||
elseif expr.AstType == 'StringCallExpr' then
|
||||
out = out..formatExpr(expr.Base)
|
||||
out = out..expr.Arguments[1].Data
|
||||
|
||||
elseif expr.AstType == 'IndexExpr' then
|
||||
out = out..formatExpr(expr.Base).."["..formatExpr(expr.Index).."]"
|
||||
|
||||
elseif expr.AstType == 'MemberExpr' then
|
||||
out = out..formatExpr(expr.Base)..expr.Indexer..expr.Ident.Data
|
||||
|
||||
elseif expr.AstType == 'Function' then
|
||||
expr.Scope:ObfuscateVariables()
|
||||
out = out.."function("
|
||||
if #expr.Arguments > 0 then
|
||||
for i = 1, #expr.Arguments do
|
||||
out = out..expr.Arguments[i].Name
|
||||
if i ~= #expr.Arguments then
|
||||
out = out..","
|
||||
elseif expr.VarArg then
|
||||
out = out..",..."
|
||||
end
|
||||
end
|
||||
elseif expr.VarArg then
|
||||
out = out.."..."
|
||||
end
|
||||
out = out..")"
|
||||
out = joinStatementsSafe(out, formatStatlist(expr.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif expr.AstType == 'ConstructorExpr' then
|
||||
out = out.."{"
|
||||
for i = 1, #expr.EntryList do
|
||||
local entry = expr.EntryList[i]
|
||||
if entry.Type == 'Key' then
|
||||
out = out.."["..formatExpr(entry.Key).."]="..formatExpr(entry.Value)
|
||||
elseif entry.Type == 'Value' then
|
||||
out = out..formatExpr(entry.Value)
|
||||
elseif entry.Type == 'KeyString' then
|
||||
out = out..entry.Key.."="..formatExpr(entry.Value)
|
||||
end
|
||||
if i ~= #expr.EntryList then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
out = out.."}"
|
||||
|
||||
elseif expr.AstType == 'Parentheses' then
|
||||
out = out.."("..formatExpr(expr.Inner)..")"
|
||||
|
||||
end
|
||||
--print(">>", skipParens, expr.ParenCount, out)
|
||||
if not skipParens then
|
||||
--print("hehe")
|
||||
out = string.rep('(', expr.ParenCount or 0) .. out
|
||||
out = out .. string.rep(')', expr.ParenCount or 0)
|
||||
--print("", out)
|
||||
end
|
||||
count = count + #out
|
||||
return --[[print(out) or]] out
|
||||
end
|
||||
|
||||
local formatStatement = function(statement)
|
||||
local out = ''
|
||||
if statement.AstType == 'AssignmentStatement' then
|
||||
for i = 1, #statement.Lhs do
|
||||
out = out..formatExpr(statement.Lhs[i])
|
||||
if i ~= #statement.Lhs then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
if #statement.Rhs > 0 then
|
||||
out = out.."="
|
||||
for i = 1, #statement.Rhs do
|
||||
out = out..formatExpr(statement.Rhs[i])
|
||||
if i ~= #statement.Rhs then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'CallStatement' then
|
||||
out = formatExpr(statement.Expression)
|
||||
|
||||
elseif statement.AstType == 'LocalStatement' then
|
||||
out = out.."local "
|
||||
for i = 1, #statement.LocalList do
|
||||
out = out..statement.LocalList[i].Name
|
||||
if i ~= #statement.LocalList then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
if #statement.InitList > 0 then
|
||||
out = out.."="
|
||||
for i = 1, #statement.InitList do
|
||||
out = out..formatExpr(statement.InitList[i])
|
||||
if i ~= #statement.InitList then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'IfStatement' then
|
||||
out = joinStatementsSafe("if", formatExpr(statement.Clauses[1].Condition))
|
||||
out = joinStatementsSafe(out, "then")
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Clauses[1].Body))
|
||||
for i = 2, #statement.Clauses do
|
||||
local st = statement.Clauses[i]
|
||||
if st.Condition then
|
||||
out = joinStatementsSafe(out, "elseif")
|
||||
out = joinStatementsSafe(out, formatExpr(st.Condition))
|
||||
out = joinStatementsSafe(out, "then")
|
||||
else
|
||||
out = joinStatementsSafe(out, "else")
|
||||
end
|
||||
out = joinStatementsSafe(out, formatStatlist(st.Body))
|
||||
end
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif statement.AstType == 'WhileStatement' then
|
||||
out = joinStatementsSafe("while", formatExpr(statement.Condition))
|
||||
out = joinStatementsSafe(out, "do")
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif statement.AstType == 'DoStatement' then
|
||||
out = joinStatementsSafe(out, "do")
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif statement.AstType == 'ReturnStatement' then
|
||||
out = "return"
|
||||
for i = 1, #statement.Arguments do
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Arguments[i]))
|
||||
if i ~= #statement.Arguments then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
|
||||
elseif statement.AstType == 'BreakStatement' then
|
||||
out = "break"
|
||||
|
||||
elseif statement.AstType == 'RepeatStatement' then
|
||||
out = "repeat"
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "until")
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Condition))
|
||||
|
||||
elseif statement.AstType == 'Function' then
|
||||
statement.Scope:ObfuscateVariables()
|
||||
if statement.IsLocal then
|
||||
out = "local"
|
||||
end
|
||||
out = joinStatementsSafe(out, "function ")
|
||||
if statement.IsLocal then
|
||||
out = out..statement.Name.Name
|
||||
else
|
||||
out = out..formatExpr(statement.Name)
|
||||
end
|
||||
out = out.."("
|
||||
if #statement.Arguments > 0 then
|
||||
for i = 1, #statement.Arguments do
|
||||
out = out..statement.Arguments[i].Name
|
||||
if i ~= #statement.Arguments then
|
||||
out = out..","
|
||||
elseif statement.VarArg then
|
||||
--print("Apply vararg")
|
||||
out = out..",..."
|
||||
end
|
||||
end
|
||||
elseif statement.VarArg then
|
||||
out = out.."..."
|
||||
end
|
||||
out = out..")"
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif statement.AstType == 'GenericForStatement' then
|
||||
statement.Scope:ObfuscateVariables()
|
||||
out = "for "
|
||||
for i = 1, #statement.VariableList do
|
||||
out = out..statement.VariableList[i].Name
|
||||
if i ~= #statement.VariableList then
|
||||
out = out..","
|
||||
end
|
||||
end
|
||||
out = out.." in"
|
||||
for i = 1, #statement.Generators do
|
||||
out = joinStatementsSafe(out, formatExpr(statement.Generators[i]))
|
||||
if i ~= #statement.Generators then
|
||||
out = joinStatementsSafe(out, ',')
|
||||
end
|
||||
end
|
||||
out = joinStatementsSafe(out, "do")
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
|
||||
elseif statement.AstType == 'NumericForStatement' then
|
||||
statement.Scope:ObfuscateVariables()
|
||||
out = "for "
|
||||
out = out..statement.Variable.Name.."="
|
||||
out = out..formatExpr(statement.Start)..","..formatExpr(statement.End)
|
||||
if statement.Step then
|
||||
out = out..","..formatExpr(statement.Step)
|
||||
end
|
||||
out = joinStatementsSafe(out, "do")
|
||||
out = joinStatementsSafe(out, formatStatlist(statement.Body))
|
||||
out = joinStatementsSafe(out, "end")
|
||||
elseif statement.AstType == 'LabelStatement' then
|
||||
out = getIndentation() .. "::" .. statement.Label .. "::"
|
||||
elseif statement.AstType == 'GotoStatement' then
|
||||
out = getIndentation() .. "goto " .. statement.Label
|
||||
elseif statement.AstType == 'Comment' then
|
||||
-- ignore
|
||||
elseif statement.AstType == 'Eof' then
|
||||
-- ignore
|
||||
else
|
||||
print("Unknown AST Type: " .. statement.AstType)
|
||||
end
|
||||
count = count + #out
|
||||
return out
|
||||
end
|
||||
|
||||
formatStatlist = function(statList)
|
||||
local out = ''
|
||||
statList.Scope:ObfuscateVariables()
|
||||
for _, stat in pairs(statList.Body) do
|
||||
out = joinStatementsSafe(out, formatStatement(stat), ';')
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
ast.Scope:ObfuscateVariables()
|
||||
return formatStatlist(ast)
|
||||
end
|
||||
|
||||
return Format_Mini
|
20
minify/LICENSE.md
Normal file
20
minify/LICENSE.md
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2012-2013
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1411
minify/ParseLua.lua
Normal file
1411
minify/ParseLua.lua
Normal file
File diff suppressed because it is too large
Load Diff
44
minify/README.md
Normal file
44
minify/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
Lua Parsing and Refactorization tools
|
||||
=========
|
||||
|
||||
A collection of tools for working with Lua source code. Primarily a Lua source code minifier, but also includes some static analysis tools and a general Lua lexer and parser.
|
||||
|
||||
Currently the minifier performs:
|
||||
|
||||
- Stripping of all comments and whitespace
|
||||
- True semantic renaming of all local variables to a reduced form
|
||||
- Reduces the source to the minimal spacing, spaces are only inserted where actually needed.
|
||||
|
||||
|
||||
LuaMinify Command Line Utility Usage
|
||||
------------------------------------
|
||||
|
||||
The `LuaMinify` shell and batch files are given as shortcuts to running a command line instance of the minifier with the following usage:
|
||||
|
||||
LuaMinify sourcefile [destfile]
|
||||
|
||||
Which will minify to a given destination file, or to a copy of the source file with _min appended to the filename if no output file is given.
|
||||
|
||||
|
||||
LuaMinify Roblox Plugin Usage
|
||||
-----------------------------
|
||||
|
||||
First, download the source code, which you can do by hitting this button:
|
||||
|
||||

|
||||
|
||||
Then copy the `RobloxPlugin` folder from the source into your Roblox Plugins directory, which can be found by hitting `Tools->Open Plugins Folder` in Roblox Studio.
|
||||
|
||||
Features/Todo
|
||||
-------------
|
||||
Features:
|
||||
|
||||
- Lua scanner/parser, which generates a full AST
|
||||
- Lua reconstructor
|
||||
- minimal
|
||||
- full reconstruction (TODO: options, comments)
|
||||
- TODO: exact reconstructor
|
||||
- support for embedded long strings/comments e.g. [[abc [[ def ]] ghi]]
|
||||
|
||||
Todo:
|
||||
- use table.concat instead of appends in the reconstructors
|
217
minify/Scope.lua
Normal file
217
minify/Scope.lua
Normal file
@@ -0,0 +1,217 @@
|
||||
local var_count = 0
|
||||
local util = require'Util'
|
||||
local lookupify = util.lookupify
|
||||
local Keywords = lookupify{
|
||||
'and', 'break', 'do', 'else', 'elseif',
|
||||
'end', 'false', 'for', 'function', 'goto', 'if',
|
||||
'in', 'local', 'nil', 'not', 'or', 'repeat',
|
||||
'return', 'then', 'true', 'until', 'while',
|
||||
};
|
||||
|
||||
local Scope = {
|
||||
new = function(self, parent)
|
||||
local s = {
|
||||
Parent = parent,
|
||||
Locals = { },
|
||||
Globals = { },
|
||||
oldLocalNamesMap = { },
|
||||
oldGlobalNamesMap = { },
|
||||
Children = { },
|
||||
}
|
||||
|
||||
if parent then
|
||||
table.insert(parent.Children, s)
|
||||
end
|
||||
|
||||
return setmetatable(s, { __index = self })
|
||||
end,
|
||||
|
||||
AddLocal = function(self, v)
|
||||
table.insert(self.Locals, v)
|
||||
end,
|
||||
|
||||
AddGlobal = function(self, v)
|
||||
table.insert(self.Globals, v)
|
||||
end,
|
||||
|
||||
CreateLocal = function(self, name)
|
||||
local v
|
||||
v = self:GetLocal(name)
|
||||
if v then return v end
|
||||
v = { }
|
||||
v.Scope = self
|
||||
v.Name = name
|
||||
v.IsGlobal = false
|
||||
v.CanRename = true
|
||||
v.References = 1
|
||||
self:AddLocal(v)
|
||||
return v
|
||||
end,
|
||||
|
||||
GetLocal = function(self, name)
|
||||
for k, var in pairs(self.Locals) do
|
||||
if var.Name == name then return var end
|
||||
end
|
||||
|
||||
if self.Parent then
|
||||
return self.Parent:GetLocal(name)
|
||||
end
|
||||
end,
|
||||
|
||||
GetOldLocal = function(self, name)
|
||||
if self.oldLocalNamesMap[name] then
|
||||
return self.oldLocalNamesMap[name]
|
||||
end
|
||||
return self:GetLocal(name)
|
||||
end,
|
||||
|
||||
mapLocal = function(self, name, var)
|
||||
self.oldLocalNamesMap[name] = var
|
||||
end,
|
||||
|
||||
GetOldGlobal = function(self, name)
|
||||
if self.oldGlobalNamesMap[name] then
|
||||
return self.oldGlobalNamesMap[name]
|
||||
end
|
||||
return self:GetGlobal(name)
|
||||
end,
|
||||
|
||||
mapGlobal = function(self, name, var)
|
||||
self.oldGlobalNamesMap[name] = var
|
||||
end,
|
||||
|
||||
GetOldVariable = function(self, name)
|
||||
return self:GetOldLocal(name) or self:GetOldGlobal(name)
|
||||
end,
|
||||
|
||||
RenameLocal = function(self, oldName, newName)
|
||||
oldName = type(oldName) == 'string' and oldName or oldName.Name
|
||||
local found = false
|
||||
local var = self:GetLocal(oldName)
|
||||
if var then
|
||||
var.Name = newName
|
||||
self:mapLocal(oldName, var)
|
||||
found = true
|
||||
end
|
||||
if not found and self.Parent then
|
||||
self.Parent:RenameLocal(oldName, newName)
|
||||
end
|
||||
end,
|
||||
|
||||
RenameGlobal = function(self, oldName, newName)
|
||||
oldName = type(oldName) == 'string' and oldName or oldName.Name
|
||||
local found = false
|
||||
local var = self:GetGlobal(oldName)
|
||||
if var then
|
||||
var.Name = newName
|
||||
self:mapGlobal(oldName, var)
|
||||
found = true
|
||||
end
|
||||
if not found and self.Parent then
|
||||
self.Parent:RenameGlobal(oldName, newName)
|
||||
end
|
||||
end,
|
||||
|
||||
RenameVariable = function(self, oldName, newName)
|
||||
oldName = type(oldName) == 'string' and oldName or oldName.Name
|
||||
if self:GetLocal(oldName) then
|
||||
self:RenameLocal(oldName, newName)
|
||||
else
|
||||
self:RenameGlobal(oldName, newName)
|
||||
end
|
||||
end,
|
||||
|
||||
GetAllVariables = function(self)
|
||||
local ret = self:getVars(true) -- down
|
||||
for k, v in pairs(self:getVars(false)) do -- up
|
||||
table.insert(ret, v)
|
||||
end
|
||||
return ret
|
||||
end,
|
||||
|
||||
getVars = function(self, top)
|
||||
local ret = { }
|
||||
if top then
|
||||
for k, v in pairs(self.Children) do
|
||||
for k2, v2 in pairs(v:getVars(true)) do
|
||||
table.insert(ret, v2)
|
||||
end
|
||||
end
|
||||
else
|
||||
for k, v in pairs(self.Locals) do
|
||||
table.insert(ret, v)
|
||||
end
|
||||
for k, v in pairs(self.Globals) do
|
||||
table.insert(ret, v)
|
||||
end
|
||||
if self.Parent then
|
||||
for k, v in pairs(self.Parent:getVars(false)) do
|
||||
table.insert(ret, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end,
|
||||
|
||||
CreateGlobal = function(self, name)
|
||||
local v
|
||||
v = self:GetGlobal(name)
|
||||
if v then return v end
|
||||
v = { }
|
||||
v.Scope = self
|
||||
v.Name = name
|
||||
v.IsGlobal = true
|
||||
v.CanRename = true
|
||||
v.References = 1
|
||||
self:AddGlobal(v)
|
||||
return v
|
||||
end,
|
||||
|
||||
GetGlobal = function(self, name)
|
||||
for k, v in pairs(self.Globals) do
|
||||
if v.Name == name then return v end
|
||||
end
|
||||
|
||||
if self.Parent then
|
||||
return self.Parent:GetGlobal(name)
|
||||
end
|
||||
end,
|
||||
|
||||
GetVariable = function(self, name)
|
||||
return self:GetLocal(name) or self:GetGlobal(name)
|
||||
end,
|
||||
|
||||
ObfuscateLocals = function(self, recommendedMaxLength, validNameChars)
|
||||
recommendedMaxLength = recommendedMaxLength or 7
|
||||
local chars = validNameChars or "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm_"
|
||||
--local chars2 = validNameChars or "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm_1234567890"
|
||||
for _, var in pairs(self.Locals) do
|
||||
local id = ""
|
||||
local tries = 0
|
||||
--[[
|
||||
repeat
|
||||
local n = math.random(1, #chars)
|
||||
id = id .. chars:sub(n, n)
|
||||
for i = 1, math.random(0, tries > 5 and 30 or recommendedMaxLength) do
|
||||
local n = math.random(1, #chars2)
|
||||
id = id .. chars2:sub(n, n)
|
||||
end
|
||||
tries = tries + 1
|
||||
until not self:GetVariable(id)]]
|
||||
local id
|
||||
repeat
|
||||
local n = var_count
|
||||
id = ""
|
||||
repeat
|
||||
local x = n % #chars
|
||||
id = id .. chars:sub(x + 1, x + 1)
|
||||
n = math.floor(n / #chars)
|
||||
until n == 0
|
||||
var_count = var_count + 1
|
||||
until not Keywords[id]
|
||||
self:RenameLocal(var.Name, id)
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
return Scope
|
91
minify/Util.lua
Normal file
91
minify/Util.lua
Normal file
@@ -0,0 +1,91 @@
|
||||
|
||||
--
|
||||
-- Util.lua
|
||||
--
|
||||
-- Provides some common utilities shared throughout the project.
|
||||
--
|
||||
|
||||
local function lookupify(tb)
|
||||
for _, v in pairs(tb) do
|
||||
tb[v] = true
|
||||
end
|
||||
return tb
|
||||
end
|
||||
|
||||
|
||||
local function CountTable(tb)
|
||||
local c = 0
|
||||
for _ in pairs(tb) do c = c + 1 end
|
||||
return c
|
||||
end
|
||||
|
||||
|
||||
local function PrintTable(tb, atIndent)
|
||||
if tb.Print then
|
||||
return tb.Print()
|
||||
end
|
||||
atIndent = atIndent or 0
|
||||
local useNewlines = (CountTable(tb) > 1)
|
||||
local baseIndent = string.rep(' ', atIndent+1)
|
||||
local out = "{"..(useNewlines and '\n' or '')
|
||||
for k, v in pairs(tb) do
|
||||
if type(v) ~= 'function' then
|
||||
--do
|
||||
out = out..(useNewlines and baseIndent or '')
|
||||
if type(k) == 'number' then
|
||||
--nothing to do
|
||||
elseif type(k) == 'string' and k:match("^[A-Za-z_][A-Za-z0-9_]*$") then
|
||||
out = out..k.." = "
|
||||
elseif type(k) == 'string' then
|
||||
out = out.."[\""..k.."\"] = "
|
||||
else
|
||||
out = out.."["..tostring(k).."] = "
|
||||
end
|
||||
if type(v) == 'string' then
|
||||
out = out.."\""..v.."\""
|
||||
elseif type(v) == 'number' then
|
||||
out = out..v
|
||||
elseif type(v) == 'table' then
|
||||
out = out..PrintTable(v, atIndent+(useNewlines and 1 or 0))
|
||||
else
|
||||
out = out..tostring(v)
|
||||
end
|
||||
if next(tb, k) then
|
||||
out = out..","
|
||||
end
|
||||
if useNewlines then
|
||||
out = out..'\n'
|
||||
end
|
||||
end
|
||||
end
|
||||
out = out..(useNewlines and string.rep(' ', atIndent) or '').."}"
|
||||
return out
|
||||
end
|
||||
|
||||
|
||||
local function splitLines(str)
|
||||
if str:match("\n") then
|
||||
local lines = {}
|
||||
for line in str:gmatch("[^\n]*") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
assert(#lines > 0)
|
||||
return lines
|
||||
else
|
||||
return { str }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function printf(fmt, ...)
|
||||
return print(string.format(fmt, ...))
|
||||
end
|
||||
|
||||
|
||||
return {
|
||||
PrintTable = PrintTable,
|
||||
CountTable = CountTable,
|
||||
lookupify = lookupify,
|
||||
splitLines = splitLines,
|
||||
printf = printf,
|
||||
}
|
39
minify/strict.lua
Normal file
39
minify/strict.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
-- From http://metalua.luaforge.net/src/lib/strict.lua.html
|
||||
--
|
||||
-- strict.lua
|
||||
-- checks uses of undeclared global variables
|
||||
-- All global variables must be 'declared' through a regular assignment
|
||||
-- (even assigning nil will do) in a main chunk before being used
|
||||
-- anywhere or assigned to inside a function.
|
||||
--
|
||||
|
||||
local mt = getmetatable(_G)
|
||||
if mt == nil then
|
||||
mt = {}
|
||||
setmetatable(_G, mt)
|
||||
end
|
||||
|
||||
__STRICT = true
|
||||
mt.__declared = {}
|
||||
|
||||
mt.__newindex = function (t, n, v)
|
||||
if __STRICT and not mt.__declared[n] then
|
||||
local w = debug.getinfo(2, "S").what
|
||||
if w ~= "main" and w ~= "C" then
|
||||
error("assign to undeclared variable '"..n.."'", 2)
|
||||
end
|
||||
mt.__declared[n] = true
|
||||
end
|
||||
rawset(t, n, v)
|
||||
end
|
||||
|
||||
mt.__index = function (t, n)
|
||||
if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
|
||||
error("variable '"..n.."' is not declared", 2)
|
||||
end
|
||||
return rawget(t, n)
|
||||
end
|
||||
|
||||
function global(...)
|
||||
for _, v in ipairs{...} do mt.__declared[v] = true end
|
||||
end
|
60
minify/tests/test_beautifier.lua
Normal file
60
minify/tests/test_beautifier.lua
Normal file
@@ -0,0 +1,60 @@
|
||||
-- Adapted from Yueliang
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format = require'FormatBeautiful'
|
||||
|
||||
for w in io.lines("test_lines.txt") do
|
||||
--print(w)
|
||||
local success, ast = Parser.ParseLua(w)
|
||||
if w:find("FAIL") then
|
||||
--[[if success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: true. Did fail: " .. tostring(not success))
|
||||
print("Line: " .. w)
|
||||
else
|
||||
--print("Suceeded!")
|
||||
end]]
|
||||
else
|
||||
if not success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: false. Did fail: " .. tostring(not success))
|
||||
print("Line: " .. w)
|
||||
else
|
||||
success, ast = Format(ast)
|
||||
--print(success, ast)
|
||||
if not success then
|
||||
print("ERROR BEAUTIFYING LINE:")
|
||||
print("Message: " .. ast)
|
||||
print("Line: " .. w)
|
||||
end
|
||||
local success_ = success
|
||||
success, ast = loadstring(success)
|
||||
if not success then
|
||||
print("ERROR PARSING BEAUTIFIED LINE:")
|
||||
print("Message: " .. ast)
|
||||
print("Line: " .. success_)
|
||||
end
|
||||
--print("Suceeded!")
|
||||
end
|
||||
end
|
||||
end
|
||||
print"Done!"
|
||||
os.remove("tmp")
|
||||
|
||||
|
||||
--[[
|
||||
function readAll(file)
|
||||
local f = io.open(file, "rb")
|
||||
local content = f:read("*all")
|
||||
f:close()
|
||||
return content
|
||||
end
|
||||
|
||||
local text = readAll('../ParseLua.lua')
|
||||
local success, ast = Parser.ParseLua(text)
|
||||
local nice
|
||||
nice = Format(ast)
|
||||
print(nice)
|
||||
--]]
|
124
minify/tests/test_identity.lua
Normal file
124
minify/tests/test_identity.lua
Normal file
@@ -0,0 +1,124 @@
|
||||
package.path = "../?.lua;" .. package.path
|
||||
local Parser = require'ParseLua'
|
||||
local util = require'Util'
|
||||
local FormatIdentity = require'FormatIdentity'
|
||||
local FormatMini = require'FormatMini'
|
||||
local FormatBeautiful = require'FormatBeautiful'
|
||||
require'strict'
|
||||
|
||||
function readAll(file)
|
||||
local f = io.open(file, "rb")
|
||||
local content = f:read("*all")
|
||||
f:close()
|
||||
return content
|
||||
end
|
||||
|
||||
local g_lexTime = 0
|
||||
local g_parseTime = 0
|
||||
local g_reconstructTime = 0
|
||||
|
||||
function reconstructText(text)
|
||||
local preLex = os.clock()
|
||||
|
||||
local success, tokens, ast, reconstructed
|
||||
success, tokens = Parser.LexLua(text)
|
||||
if not success then
|
||||
print("ERROR: " .. tokens)
|
||||
return
|
||||
end
|
||||
|
||||
local preParse = os.clock()
|
||||
|
||||
success, ast = Parser.ParseLua(tokens)
|
||||
if not success then
|
||||
print("ERROR: " .. ast)
|
||||
return
|
||||
end
|
||||
|
||||
local preReconstruct = os.clock()
|
||||
|
||||
local DO_MINI = false
|
||||
local DO_CHECK = false
|
||||
|
||||
if DO_MINI then
|
||||
success, reconstructed = FormatMini(ast)
|
||||
else
|
||||
success, reconstructed = FormatIdentity(ast)
|
||||
end
|
||||
|
||||
if not success then
|
||||
print("ERROR: " .. reconstructed)
|
||||
return
|
||||
end
|
||||
|
||||
local post = os.clock()
|
||||
g_lexTime = g_lexTime + (preParse - preLex)
|
||||
g_parseTime = g_parseTime + (preReconstruct - preParse)
|
||||
g_reconstructTime = g_reconstructTime + (post - preReconstruct)
|
||||
|
||||
if DO_CHECK then
|
||||
--[[
|
||||
print()
|
||||
print("Reconstructed: ")
|
||||
print("--------------------")
|
||||
print(reconstructed)
|
||||
print("--------------------")
|
||||
print("Done. ")
|
||||
--]]
|
||||
|
||||
if reconstructed == text then
|
||||
--print("Reconstruction succeeded")
|
||||
else
|
||||
print("Reconstruction failed")
|
||||
|
||||
local inputLines = util.splitLines(text)
|
||||
local outputLines = util.splitLines(reconstructed)
|
||||
local n = math.max(#inputLines, #outputLines)
|
||||
for i = 1,n do
|
||||
if inputLines[i] ~= outputLines[i] then
|
||||
util.printf("ERROR on line %i", i)
|
||||
util.printf("Input: %q", inputLines[i])
|
||||
util.printf("Output: %q", outputLines[i])
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--[*[
|
||||
local files = {
|
||||
"../ParseLua.lua",
|
||||
"../FormatIdentity.lua",
|
||||
"../Scope.lua",
|
||||
"../strict.lua",
|
||||
"../Type.lua",
|
||||
"Test_identity.lua"
|
||||
}
|
||||
|
||||
for _,path in ipairs(files) do
|
||||
print(path)
|
||||
local text = readAll(path)
|
||||
reconstructText(text)
|
||||
end
|
||||
|
||||
--]]
|
||||
|
||||
print("test_lines.txt")
|
||||
|
||||
local line_nr = 0
|
||||
for text in io.lines("test_lines.txt") do
|
||||
line_nr = line_nr + 1
|
||||
if not text:find("FAIL") then
|
||||
--util.printf("\nText: %q", text)
|
||||
reconstructText(text)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
reconstructText('function a(p,q,r,...) end')
|
||||
|
||||
util.printf("Lex time: %f s", g_lexTime)
|
||||
util.printf("Parse time: %f s", g_parseTime)
|
||||
util.printf("Format time: %f s", g_reconstructTime)
|
523
minify/tests/test_lines.txt
Normal file
523
minify/tests/test_lines.txt
Normal file
@@ -0,0 +1,523 @@
|
||||
; -- FAIL
|
||||
local -- FAIL
|
||||
local; -- FAIL
|
||||
local = -- FAIL
|
||||
local end -- FAIL
|
||||
local a
|
||||
local a;
|
||||
local a, b, c
|
||||
local a; local b local c;
|
||||
local a = 1
|
||||
local a local b = a
|
||||
local a, b = 1, 2
|
||||
local a, b, c = 1, 2, 3
|
||||
local a, b, c = 1
|
||||
local a = 1, 2, 3
|
||||
local a, local -- FAIL
|
||||
local 1 -- FAIL
|
||||
local "foo" -- FAIL
|
||||
local a = local -- FAIL
|
||||
local a, b, = -- FAIL
|
||||
local a, b = 1, local -- FAIL
|
||||
local a, b = , local -- FAIL
|
||||
do -- FAIL
|
||||
end -- FAIL
|
||||
do end
|
||||
do ; end -- FAIL
|
||||
do 1 end -- FAIL
|
||||
do "foo" end -- FAIL
|
||||
do local a, b end
|
||||
do local a local b end
|
||||
do local a; local b; end
|
||||
do local a = 1 end
|
||||
do do end end
|
||||
do do end; end
|
||||
do do do end end end
|
||||
do do do end; end; end
|
||||
do do do return end end end
|
||||
do end do -- FAIL
|
||||
do end end -- FAIL
|
||||
do return end
|
||||
do return return end -- FAIL
|
||||
do break end -- FAIL
|
||||
while -- FAIL
|
||||
while do -- FAIL
|
||||
while = -- FAIL
|
||||
while 1 do -- FAIL
|
||||
while 1 do end
|
||||
while 1 do local a end
|
||||
while 1 do local a local b end
|
||||
while 1 do local a; local b; end
|
||||
while 1 do 2 end -- FAIL
|
||||
while 1 do "foo" end -- FAIL
|
||||
while true do end
|
||||
while 1 do ; end -- FAIL
|
||||
while 1 do while -- FAIL
|
||||
while 1 end -- FAIL
|
||||
while 1 2 do -- FAIL
|
||||
while 1 = 2 do -- FAIL
|
||||
while 1 do return end
|
||||
while 1 do return return end -- FAIL
|
||||
while 1 do do end end
|
||||
while 1 do do return end end
|
||||
while 1 do break end
|
||||
while 1 do break break end -- FAIL
|
||||
while 1 do do break end end
|
||||
repeat -- FAIL
|
||||
repeat until -- FAIL
|
||||
repeat until 0
|
||||
repeat until false
|
||||
repeat until local -- FAIL
|
||||
repeat end -- FAIL
|
||||
repeat 1 -- FAIL
|
||||
repeat = -- FAIL
|
||||
repeat local a until 1
|
||||
repeat local a local b until 0
|
||||
repeat local a; local b; until 0
|
||||
repeat ; until 1 -- FAIL
|
||||
repeat 2 until 1 -- FAIL
|
||||
repeat "foo" until 1 -- FAIL
|
||||
repeat return until 0
|
||||
repeat return return until 0 -- FAIL
|
||||
repeat break until 0
|
||||
repeat break break until 0 -- FAIL
|
||||
repeat do end until 0
|
||||
repeat do return end until 0
|
||||
repeat do break end until 0
|
||||
for -- FAIL
|
||||
for do -- FAIL
|
||||
for end -- FAIL
|
||||
for 1 -- FAIL
|
||||
for a -- FAIL
|
||||
for true -- FAIL
|
||||
for a, in -- FAIL
|
||||
for a in -- FAIL
|
||||
for a do -- FAIL
|
||||
for a in do -- FAIL
|
||||
for a in b do -- FAIL
|
||||
for a in b end -- FAIL
|
||||
for a in b, do -- FAIL
|
||||
for a in b do end
|
||||
for a in b do local a local b end
|
||||
for a in b do local a; local b; end
|
||||
for a in b do 1 end -- FAIL
|
||||
for a in b do "foo" end -- FAIL
|
||||
for a b in -- FAIL
|
||||
for a, b, c in p do end
|
||||
for a, b, c in p, q, r do end
|
||||
for a in 1 do end
|
||||
for a in true do end
|
||||
for a in "foo" do end
|
||||
for a in b do break end
|
||||
for a in b do break break end -- FAIL
|
||||
for a in b do return end
|
||||
for a in b do return return end -- FAIL
|
||||
for a in b do do end end
|
||||
for a in b do do break end end
|
||||
for a in b do do return end end
|
||||
for = -- FAIL
|
||||
for a = -- FAIL
|
||||
for a, b = -- FAIL
|
||||
for a = do -- FAIL
|
||||
for a = 1, do -- FAIL
|
||||
for a = p, q, do -- FAIL
|
||||
for a = p q do -- FAIL
|
||||
for a = b do end -- FAIL
|
||||
for a = 1, 2, 3, 4 do end -- FAIL
|
||||
for a = p, q do end
|
||||
for a = 1, 2 do end
|
||||
for a = 1, 2 do local a local b end
|
||||
for a = 1, 2 do local a; local b; end
|
||||
for a = 1, 2 do 3 end -- FAIL
|
||||
for a = 1, 2 do "foo" end -- FAIL
|
||||
for a = p, q, r do end
|
||||
for a = 1, 2, 3 do end
|
||||
for a = p, q do break end
|
||||
for a = p, q do break break end -- FAIL
|
||||
for a = 1, 2 do return end
|
||||
for a = 1, 2 do return return end -- FAIL
|
||||
for a = p, q do do end end
|
||||
for a = p, q do do break end end
|
||||
for a = p, q do do return end end
|
||||
break -- FAIL
|
||||
return
|
||||
return;
|
||||
return return -- FAIL
|
||||
return 1
|
||||
return local -- FAIL
|
||||
return "foo"
|
||||
return 1, -- FAIL
|
||||
return 1,2,3
|
||||
return a,b,c,d
|
||||
return 1,2;
|
||||
return ...
|
||||
return 1,a,...
|
||||
if -- FAIL
|
||||
elseif -- FAIL
|
||||
else -- FAIL
|
||||
then -- FAIL
|
||||
if then -- FAIL
|
||||
if 1 -- FAIL
|
||||
if 1 then -- FAIL
|
||||
if 1 else -- FAIL
|
||||
if 1 then else -- FAIL
|
||||
if 1 then elseif -- FAIL
|
||||
if 1 then end
|
||||
if 1 then local a end
|
||||
if 1 then local a local b end
|
||||
if 1 then local a; local b; end
|
||||
if 1 then else end
|
||||
if 1 then local a else local b end
|
||||
if 1 then local a; else local b; end
|
||||
if 1 then elseif 2 -- FAIL
|
||||
if 1 then elseif 2 then -- FAIL
|
||||
if 1 then elseif 2 then end
|
||||
if 1 then local a elseif 2 then local b end
|
||||
if 1 then local a; elseif 2 then local b; end
|
||||
if 1 then elseif 2 then else end
|
||||
if 1 then else if 2 then end end
|
||||
if 1 then else if 2 then end -- FAIL
|
||||
if 1 then break end -- FAIL
|
||||
if 1 then return end
|
||||
if 1 then return return end -- FAIL
|
||||
if 1 then end; if 1 then end;
|
||||
function -- FAIL
|
||||
function 1 -- FAIL
|
||||
function end -- FAIL
|
||||
function a -- FAIL
|
||||
function a end -- FAIL
|
||||
function a( end -- FAIL
|
||||
function a() end
|
||||
function a(1 -- FAIL
|
||||
function a("foo" -- FAIL
|
||||
function a(p -- FAIL
|
||||
function a(p,) -- FAIL
|
||||
function a(p q -- FAIL
|
||||
function a(p) end
|
||||
function a(p,q,) end -- FAIL
|
||||
function a(p,q,r) end
|
||||
function a(p,q,1 -- FAIL
|
||||
function a(p) do -- FAIL
|
||||
function a(p) 1 end -- FAIL
|
||||
function a(p) return end
|
||||
function a(p) break end -- FAIL
|
||||
function a(p) return return end -- FAIL
|
||||
function a(p) do end end
|
||||
function a.( -- FAIL
|
||||
function a.1 -- FAIL
|
||||
function a.b() end
|
||||
function a.b, -- FAIL
|
||||
function a.b.( -- FAIL
|
||||
function a.b.c.d() end
|
||||
function a: -- FAIL
|
||||
function a:1 -- FAIL
|
||||
function a:b() end
|
||||
function a:b: -- FAIL
|
||||
function a:b. -- FAIL
|
||||
function a.b.c:d() end
|
||||
function a(...) end
|
||||
function a(..., -- FAIL
|
||||
function a(p,...) end
|
||||
function a(p,q,r,...) end
|
||||
function a() local a local b end
|
||||
function a() local a; local b; end
|
||||
function a() end; function a() end;
|
||||
local function -- FAIL
|
||||
local function 1 -- FAIL
|
||||
local function end -- FAIL
|
||||
local function a -- FAIL
|
||||
local function a end -- FAIL
|
||||
local function a( end -- FAIL
|
||||
local function a() end
|
||||
local function a(1 -- FAIL
|
||||
local function a("foo" -- FAIL
|
||||
local function a(p -- FAIL
|
||||
local function a(p,) -- FAIL
|
||||
local function a(p q -- FAIL
|
||||
local function a(p) end
|
||||
local function a(p,q,) end -- FAIL
|
||||
local function a(p,q,r) end
|
||||
local function a(p,q,1 -- FAIL
|
||||
local function a(p) do -- FAIL
|
||||
local function a(p) 1 end -- FAIL
|
||||
local function a(p) return end
|
||||
local function a(p) break end -- FAIL
|
||||
local function a(p) return return end -- FAIL
|
||||
local function a(p) do end end
|
||||
local function a. -- FAIL
|
||||
local function a: -- FAIL
|
||||
local function a(...) end
|
||||
local function a(..., -- FAIL
|
||||
local function a(p,...) end
|
||||
local function a(p,q,r,...) end
|
||||
local function a() local a local b end
|
||||
local function a() local a; local b; end
|
||||
local function a() end; local function a() end;
|
||||
a -- FAIL
|
||||
a, -- FAIL
|
||||
a,b,c -- FAIL
|
||||
a,b = -- FAIL
|
||||
a = 1
|
||||
a = 1,2,3
|
||||
a,b,c = 1
|
||||
a,b,c = 1,2,3
|
||||
a.b = 1
|
||||
a.b.c = 1
|
||||
a[b] = 1
|
||||
a[b][c] = 1
|
||||
a.b[c] = 1
|
||||
a[b].c = 1
|
||||
0 = -- FAIL
|
||||
"foo" = -- FAIL
|
||||
true = -- FAIL
|
||||
(a) = -- FAIL
|
||||
{} = -- FAIL
|
||||
a:b() = -- FAIL
|
||||
a() = -- FAIL
|
||||
a.b:c() = -- FAIL
|
||||
a[b]() = -- FAIL
|
||||
a = a b -- FAIL
|
||||
a = 1 2 -- FAIL
|
||||
a = a = 1 -- FAIL
|
||||
a( -- FAIL
|
||||
a()
|
||||
a(1)
|
||||
a(1,) -- FAIL
|
||||
a(1,2,3)
|
||||
1() -- FAIL
|
||||
a()()
|
||||
a.b()
|
||||
a[b]()
|
||||
a.1 -- FAIL
|
||||
a.b -- FAIL
|
||||
a[b] -- FAIL
|
||||
a.b.( -- FAIL
|
||||
a.b.c()
|
||||
a[b][c]()
|
||||
a[b].c()
|
||||
a.b[c]()
|
||||
a:b()
|
||||
a:b -- FAIL
|
||||
a:1 -- FAIL
|
||||
a.b:c()
|
||||
a[b]:c()
|
||||
a:b: -- FAIL
|
||||
a:b():c()
|
||||
a:b().c[d]:e()
|
||||
a:b()[c].d:e()
|
||||
(a)()
|
||||
()() -- FAIL
|
||||
(1)()
|
||||
("foo")()
|
||||
(true)()
|
||||
(a)()()
|
||||
(a.b)()
|
||||
(a[b])()
|
||||
(a).b()
|
||||
(a)[b]()
|
||||
(a):b()
|
||||
(a).b[c]:d()
|
||||
(a)[b].c:d()
|
||||
(a):b():c()
|
||||
(a):b().c[d]:e()
|
||||
(a):b()[c].d:e()
|
||||
a"foo"
|
||||
a[[foo]]
|
||||
a.b"foo"
|
||||
a[b]"foo"
|
||||
a:b"foo"
|
||||
a{}
|
||||
a.b{}
|
||||
a[b]{}
|
||||
a:b{}
|
||||
a()"foo"
|
||||
a"foo"()
|
||||
a"foo".b()
|
||||
a"foo"[b]()
|
||||
a"foo":c()
|
||||
a"foo""bar"
|
||||
a"foo"{}
|
||||
(a):b"foo".c[d]:e"bar"
|
||||
(a):b"foo"[c].d:e"bar"
|
||||
a(){}
|
||||
a{}()
|
||||
a{}.b()
|
||||
a{}[b]()
|
||||
a{}:c()
|
||||
a{}"foo"
|
||||
a{}{}
|
||||
(a):b{}.c[d]:e{}
|
||||
(a):b{}[c].d:e{}
|
||||
a = -- FAIL
|
||||
a = a
|
||||
a = nil
|
||||
a = false
|
||||
a = 1
|
||||
a = "foo"
|
||||
a = [[foo]]
|
||||
a = {}
|
||||
a = (a)
|
||||
a = (nil)
|
||||
a = (true)
|
||||
a = (1)
|
||||
a = ("foo")
|
||||
a = ([[foo]])
|
||||
a = ({})
|
||||
a = a.b
|
||||
a = a.b. -- FAIL
|
||||
a = a.b.c
|
||||
a = a:b -- FAIL
|
||||
a = a[b]
|
||||
a = a[1]
|
||||
a = a["foo"]
|
||||
a = a[b][c]
|
||||
a = a.b[c]
|
||||
a = a[b].c
|
||||
a = (a)[b]
|
||||
a = (a).c
|
||||
a = () -- FAIL
|
||||
a = a()
|
||||
a = a.b()
|
||||
a = a[b]()
|
||||
a = a:b()
|
||||
a = (a)()
|
||||
a = (a).b()
|
||||
a = (a)[b]()
|
||||
a = (a):b()
|
||||
a = a"foo"
|
||||
a = a{}
|
||||
a = function -- FAIL
|
||||
a = function 1 -- FAIL
|
||||
a = function a -- FAIL
|
||||
a = function end -- FAIL
|
||||
a = function( -- FAIL
|
||||
a = function() end
|
||||
a = function(1 -- FAIL
|
||||
a = function(p) end
|
||||
a = function(p,) -- FAIL
|
||||
a = function(p q -- FAIL
|
||||
a = function(p,q,r) end
|
||||
a = function(p,q,1 -- FAIL
|
||||
a = function(...) end
|
||||
a = function(..., -- FAIL
|
||||
a = function(p,...) end
|
||||
a = function(p,q,r,...) end
|
||||
a = ...
|
||||
a = a, b, ...
|
||||
a = (...)
|
||||
a = ..., 1, 2
|
||||
a = function() return ... end -- FAIL
|
||||
a = -10
|
||||
a = -"foo"
|
||||
a = -a
|
||||
a = -nil
|
||||
a = -true
|
||||
a = -{}
|
||||
a = -function() end
|
||||
a = -a()
|
||||
a = -(a)
|
||||
a = - -- FAIL
|
||||
a = not 10
|
||||
a = not "foo"
|
||||
a = not a
|
||||
a = not nil
|
||||
a = not true
|
||||
a = not {}
|
||||
a = not function() end
|
||||
a = not a()
|
||||
a = not (a)
|
||||
a = not -- FAIL
|
||||
a = #10
|
||||
a = #"foo"
|
||||
a = #a
|
||||
a = #nil
|
||||
a = #true
|
||||
a = #{}
|
||||
a = #function() end
|
||||
a = #a()
|
||||
a = #(a)
|
||||
a = # -- FAIL
|
||||
a = 1 + 2; a = 1 - 2
|
||||
a = 1 * 2; a = 1 / 2
|
||||
a = 1 ^ 2; a = 1 % 2
|
||||
a = 1 .. 2
|
||||
a = 1 + -- FAIL
|
||||
a = 1 .. -- FAIL
|
||||
a = 1 * / -- FAIL
|
||||
a = 1 + -2; a = 1 - -2
|
||||
a = 1 * - -- FAIL
|
||||
a = 1 * not 2; a = 1 / not 2
|
||||
a = 1 / not -- FAIL
|
||||
a = 1 * #"foo"; a = 1 / #"foo"
|
||||
a = 1 / # -- FAIL
|
||||
a = 1 + 2 - 3 * 4 / 5 % 6 ^ 7
|
||||
a = ((1 + 2) - 3) * (4 / (5 % 6 ^ 7))
|
||||
a = (1 + (2 - (3 * (4 / (5 % 6 ^ ((7)))))))
|
||||
a = ((1 -- FAIL
|
||||
a = ((1 + 2) -- FAIL
|
||||
a = 1) -- FAIL
|
||||
a = a + b - c
|
||||
a = "foo" + "bar"
|
||||
a = "foo".."bar".."baz"
|
||||
a = true + false - nil
|
||||
a = {} * {}
|
||||
a = function() end / function() end
|
||||
a = a() ^ b()
|
||||
a = ... % ...
|
||||
a = 1 == 2; a = 1 ~= 2
|
||||
a = 1 < 2; a = 1 <= 2
|
||||
a = 1 > 2; a = 1 >= 2
|
||||
a = 1 < 2 < 3
|
||||
a = 1 >= 2 >= 3
|
||||
a = 1 == -- FAIL
|
||||
a = ~= 2 -- FAIL
|
||||
a = "foo" == "bar"
|
||||
a = "foo" > "bar"
|
||||
a = a ~= b
|
||||
a = true == false
|
||||
a = 1 and 2; a = 1 or 2
|
||||
a = 1 and -- FAIL
|
||||
a = or 1 -- FAIL
|
||||
a = 1 and 2 and 3
|
||||
a = 1 or 2 or 3
|
||||
a = 1 and 2 or 3
|
||||
a = a and b or c
|
||||
a = a() and (b)() or c.d
|
||||
a = "foo" and "bar"
|
||||
a = true or false
|
||||
a = {} and {} or {}
|
||||
a = (1) and ("foo") or (nil)
|
||||
a = function() end == function() end
|
||||
a = function() end or function() end
|
||||
a = { -- FAIL
|
||||
a = {}
|
||||
a = {,} -- FAIL
|
||||
a = {;} -- FAIL
|
||||
a = {,,} -- FAIL
|
||||
a = {;;} -- FAIL
|
||||
a = {{ -- FAIL
|
||||
a = {{{}}}
|
||||
a = {{},{},{{}},}
|
||||
a = { 1 }
|
||||
a = { 1, }
|
||||
a = { 1; }
|
||||
a = { 1, 2 }
|
||||
a = { a, b, c, }
|
||||
a = { true; false, nil; }
|
||||
a = { a.b, a[b]; a:c(), }
|
||||
a = { 1 + 2, a > b, "a" or "b" }
|
||||
a = { a=1, }
|
||||
a = { a=1, b="foo", c=nil }
|
||||
a = { a -- FAIL
|
||||
a = { a= -- FAIL
|
||||
a = { a=, -- FAIL
|
||||
a = { a=; -- FAIL
|
||||
a = { 1, a="foo" -- FAIL
|
||||
a = { 1, a="foo"; b={}, d=true; }
|
||||
a = { [ -- FAIL
|
||||
a = { [1 -- FAIL
|
||||
a = { [1] -- FAIL
|
||||
a = { [a]= -- FAIL
|
||||
a = { ["foo"]="bar" }
|
||||
a = { [1]=a, [2]=b, }
|
||||
a = { true, a=1; ["foo"]="bar", }
|
61
minify/tests/test_minifier.lua
Normal file
61
minify/tests/test_minifier.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
-- Adapted from Yueliang
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Mini = require'FormatMini'
|
||||
local line_nr = 0
|
||||
|
||||
for w in io.lines("test_lines.txt") do
|
||||
line_nr = line_nr + 1
|
||||
--print(w)
|
||||
local success, ast = Parser.ParseLua(w)
|
||||
if w:find("FAIL") then
|
||||
--[[if success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: true. Did fail: " .. tostring(not success))
|
||||
print("Line: " .. w)
|
||||
else
|
||||
--print("Suceeded!")
|
||||
end]]
|
||||
else
|
||||
if not success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: false. Did fail: " .. tostring(not success))
|
||||
print("Line: " .. w)
|
||||
else
|
||||
success, ast = Format_Mini(ast)
|
||||
--print(success, ast)
|
||||
if not success then
|
||||
print("ERROR MINIFYING LINE:")
|
||||
print("Message: " .. ast)
|
||||
print("Line: " .. w)
|
||||
end
|
||||
success, ast = loadstring(success)
|
||||
if not success then
|
||||
print("ERROR PARSING MINIFIED LINE:")
|
||||
print("Message: " .. ast)
|
||||
print("Line nr: " .. line_nr)
|
||||
print("Line: " .. w)
|
||||
end
|
||||
--print("Suceeded!")
|
||||
end
|
||||
end
|
||||
end
|
||||
print"Done!"
|
||||
os.remove("tmp")
|
||||
|
||||
--[[
|
||||
function readAll(file)
|
||||
local f = io.open(file, "rb")
|
||||
local content = f:read("*all")
|
||||
f:close()
|
||||
return content
|
||||
end
|
||||
|
||||
local text = readAll('../ParseLua.lua')
|
||||
local success, ast = Parser.ParseLua(text)
|
||||
local nice
|
||||
nice = Format_Mini(ast)
|
||||
print(nice)
|
||||
--]]
|
561
minify/tests/test_parser.lua
Normal file
561
minify/tests/test_parser.lua
Normal file
@@ -0,0 +1,561 @@
|
||||
-- Adapted from Yueliang
|
||||
|
||||
local source = [=[
|
||||
; -- FAIL
|
||||
local -- FAIL
|
||||
local; -- FAIL
|
||||
local = -- FAIL
|
||||
local end -- FAIL
|
||||
local a
|
||||
local a;
|
||||
local a, b, c
|
||||
local a; local b local c;
|
||||
local a = 1
|
||||
local a local b = a
|
||||
local a, b = 1, 2
|
||||
local a, b, c = 1, 2, 3
|
||||
local a, b, c = 1
|
||||
local a = 1, 2, 3
|
||||
local a, local -- FAIL
|
||||
local 1 -- FAIL
|
||||
local "foo" -- FAIL
|
||||
local a = local -- FAIL
|
||||
local a, b, = -- FAIL
|
||||
local a, b = 1, local -- FAIL
|
||||
local a, b = , local -- FAIL
|
||||
do -- FAIL
|
||||
end -- FAIL
|
||||
do end
|
||||
do ; end -- FAIL
|
||||
do 1 end -- FAIL
|
||||
do "foo" end -- FAIL
|
||||
do local a, b end
|
||||
do local a local b end
|
||||
do local a; local b; end
|
||||
do local a = 1 end
|
||||
do do end end
|
||||
do do end; end
|
||||
do do do end end end
|
||||
do do do end; end; end
|
||||
do do do return end end end
|
||||
do end do -- FAIL
|
||||
do end end -- FAIL
|
||||
do return end
|
||||
do return return end -- FAIL
|
||||
do break end -- FAIL
|
||||
while -- FAIL
|
||||
while do -- FAIL
|
||||
while = -- FAIL
|
||||
while 1 do -- FAIL
|
||||
while 1 do end
|
||||
while 1 do local a end
|
||||
while 1 do local a local b end
|
||||
while 1 do local a; local b; end
|
||||
while 1 do 2 end -- FAIL
|
||||
while 1 do "foo" end -- FAIL
|
||||
while true do end
|
||||
while 1 do ; end -- FAIL
|
||||
while 1 do while -- FAIL
|
||||
while 1 end -- FAIL
|
||||
while 1 2 do -- FAIL
|
||||
while 1 = 2 do -- FAIL
|
||||
while 1 do return end
|
||||
while 1 do return return end -- FAIL
|
||||
while 1 do do end end
|
||||
while 1 do do return end end
|
||||
while 1 do break end
|
||||
while 1 do break break end -- FAIL
|
||||
while 1 do do break end end
|
||||
repeat -- FAIL
|
||||
repeat until -- FAIL
|
||||
repeat until 0
|
||||
repeat until false
|
||||
repeat until local -- FAIL
|
||||
repeat end -- FAIL
|
||||
repeat 1 -- FAIL
|
||||
repeat = -- FAIL
|
||||
repeat local a until 1
|
||||
repeat local a local b until 0
|
||||
repeat local a; local b; until 0
|
||||
repeat ; until 1 -- FAIL
|
||||
repeat 2 until 1 -- FAIL
|
||||
repeat "foo" until 1 -- FAIL
|
||||
repeat return until 0
|
||||
repeat return return until 0 -- FAIL
|
||||
repeat break until 0
|
||||
repeat break break until 0 -- FAIL
|
||||
repeat do end until 0
|
||||
repeat do return end until 0
|
||||
repeat do break end until 0
|
||||
for -- FAIL
|
||||
for do -- FAIL
|
||||
for end -- FAIL
|
||||
for 1 -- FAIL
|
||||
for a -- FAIL
|
||||
for true -- FAIL
|
||||
for a, in -- FAIL
|
||||
for a in -- FAIL
|
||||
for a do -- FAIL
|
||||
for a in do -- FAIL
|
||||
for a in b do -- FAIL
|
||||
for a in b end -- FAIL
|
||||
for a in b, do -- FAIL
|
||||
for a in b do end
|
||||
for a in b do local a local b end
|
||||
for a in b do local a; local b; end
|
||||
for a in b do 1 end -- FAIL
|
||||
for a in b do "foo" end -- FAIL
|
||||
for a b in -- FAIL
|
||||
for a, b, c in p do end
|
||||
for a, b, c in p, q, r do end
|
||||
for a in 1 do end
|
||||
for a in true do end
|
||||
for a in "foo" do end
|
||||
for a in b do break end
|
||||
for a in b do break break end -- FAIL
|
||||
for a in b do return end
|
||||
for a in b do return return end -- FAIL
|
||||
for a in b do do end end
|
||||
for a in b do do break end end
|
||||
for a in b do do return end end
|
||||
for = -- FAIL
|
||||
for a = -- FAIL
|
||||
for a, b = -- FAIL
|
||||
for a = do -- FAIL
|
||||
for a = 1, do -- FAIL
|
||||
for a = p, q, do -- FAIL
|
||||
for a = p q do -- FAIL
|
||||
for a = b do end -- FAIL
|
||||
for a = 1, 2, 3, 4 do end -- FAIL
|
||||
for a = p, q do end
|
||||
for a = 1, 2 do end
|
||||
for a = 1, 2 do local a local b end
|
||||
for a = 1, 2 do local a; local b; end
|
||||
for a = 1, 2 do 3 end -- FAIL
|
||||
for a = 1, 2 do "foo" end -- FAIL
|
||||
for a = p, q, r do end
|
||||
for a = 1, 2, 3 do end
|
||||
for a = p, q do break end
|
||||
for a = p, q do break break end -- FAIL
|
||||
for a = 1, 2 do return end
|
||||
for a = 1, 2 do return return end -- FAIL
|
||||
for a = p, q do do end end
|
||||
for a = p, q do do break end end
|
||||
for a = p, q do do return end end
|
||||
break -- FAIL
|
||||
return
|
||||
return;
|
||||
return return -- FAIL
|
||||
return 1
|
||||
return local -- FAIL
|
||||
return "foo"
|
||||
return 1, -- FAIL
|
||||
return 1,2,3
|
||||
return a,b,c,d
|
||||
return 1,2;
|
||||
return ...
|
||||
return 1,a,...
|
||||
if -- FAIL
|
||||
elseif -- FAIL
|
||||
else -- FAIL
|
||||
then -- FAIL
|
||||
if then -- FAIL
|
||||
if 1 -- FAIL
|
||||
if 1 then -- FAIL
|
||||
if 1 else -- FAIL
|
||||
if 1 then else -- FAIL
|
||||
if 1 then elseif -- FAIL
|
||||
if 1 then end
|
||||
if 1 then local a end
|
||||
if 1 then local a local b end
|
||||
if 1 then local a; local b; end
|
||||
if 1 then else end
|
||||
if 1 then local a else local b end
|
||||
if 1 then local a; else local b; end
|
||||
if 1 then elseif 2 -- FAIL
|
||||
if 1 then elseif 2 then -- FAIL
|
||||
if 1 then elseif 2 then end
|
||||
if 1 then local a elseif 2 then local b end
|
||||
if 1 then local a; elseif 2 then local b; end
|
||||
if 1 then elseif 2 then else end
|
||||
if 1 then else if 2 then end end
|
||||
if 1 then else if 2 then end -- FAIL
|
||||
if 1 then break end -- FAIL
|
||||
if 1 then return end
|
||||
if 1 then return return end -- FAIL
|
||||
if 1 then end; if 1 then end;
|
||||
function -- FAIL
|
||||
function 1 -- FAIL
|
||||
function end -- FAIL
|
||||
function a -- FAIL
|
||||
function a end -- FAIL
|
||||
function a( end -- FAIL
|
||||
function a() end
|
||||
function a(1 -- FAIL
|
||||
function a("foo" -- FAIL
|
||||
function a(p -- FAIL
|
||||
function a(p,) -- FAIL
|
||||
function a(p q -- FAIL
|
||||
function a(p) end
|
||||
function a(p,q,) end -- FAIL
|
||||
function a(p,q,r) end
|
||||
function a(p,q,1 -- FAIL
|
||||
function a(p) do -- FAIL
|
||||
function a(p) 1 end -- FAIL
|
||||
function a(p) return end
|
||||
function a(p) break end -- FAIL
|
||||
function a(p) return return end -- FAIL
|
||||
function a(p) do end end
|
||||
function a.( -- FAIL
|
||||
function a.1 -- FAIL
|
||||
function a.b() end
|
||||
function a.b, -- FAIL
|
||||
function a.b.( -- FAIL
|
||||
function a.b.c.d() end
|
||||
function a: -- FAIL
|
||||
function a:1 -- FAIL
|
||||
function a:b() end
|
||||
function a:b: -- FAIL
|
||||
function a:b. -- FAIL
|
||||
function a.b.c:d() end
|
||||
function a(...) end
|
||||
function a(..., -- FAIL
|
||||
function a(p,...) end
|
||||
function a(p,q,r,...) end
|
||||
function a() local a local b end
|
||||
function a() local a; local b; end
|
||||
function a() end; function a() end;
|
||||
local function -- FAIL
|
||||
local function 1 -- FAIL
|
||||
local function end -- FAIL
|
||||
local function a -- FAIL
|
||||
local function a end -- FAIL
|
||||
local function a( end -- FAIL
|
||||
local function a() end
|
||||
local function a(1 -- FAIL
|
||||
local function a("foo" -- FAIL
|
||||
local function a(p -- FAIL
|
||||
local function a(p,) -- FAIL
|
||||
local function a(p q -- FAIL
|
||||
local function a(p) end
|
||||
local function a(p,q,) end -- FAIL
|
||||
local function a(p,q,r) end
|
||||
local function a(p,q,1 -- FAIL
|
||||
local function a(p) do -- FAIL
|
||||
local function a(p) 1 end -- FAIL
|
||||
local function a(p) return end
|
||||
local function a(p) break end -- FAIL
|
||||
local function a(p) return return end -- FAIL
|
||||
local function a(p) do end end
|
||||
local function a. -- FAIL
|
||||
local function a: -- FAIL
|
||||
local function a(...) end
|
||||
local function a(..., -- FAIL
|
||||
local function a(p,...) end
|
||||
local function a(p,q,r,...) end
|
||||
local function a() local a local b end
|
||||
local function a() local a; local b; end
|
||||
local function a() end; local function a() end;
|
||||
a -- FAIL
|
||||
a, -- FAIL
|
||||
a,b,c -- FAIL
|
||||
a,b = -- FAIL
|
||||
a = 1
|
||||
a = 1,2,3
|
||||
a,b,c = 1
|
||||
a,b,c = 1,2,3
|
||||
a.b = 1
|
||||
a.b.c = 1
|
||||
a[b] = 1
|
||||
a[b][c] = 1
|
||||
a.b[c] = 1
|
||||
a[b].c = 1
|
||||
0 = -- FAIL
|
||||
"foo" = -- FAIL
|
||||
true = -- FAIL
|
||||
(a) = -- FAIL
|
||||
{} = -- FAIL
|
||||
a:b() = -- FAIL
|
||||
a() = -- FAIL
|
||||
a.b:c() = -- FAIL
|
||||
a[b]() = -- FAIL
|
||||
a = a b -- FAIL
|
||||
a = 1 2 -- FAIL
|
||||
a = a = 1 -- FAIL
|
||||
a( -- FAIL
|
||||
a()
|
||||
a(1)
|
||||
a(1,) -- FAIL
|
||||
a(1,2,3)
|
||||
1() -- FAIL
|
||||
a()()
|
||||
a.b()
|
||||
a[b]()
|
||||
a.1 -- FAIL
|
||||
a.b -- FAIL
|
||||
a[b] -- FAIL
|
||||
a.b.( -- FAIL
|
||||
a.b.c()
|
||||
a[b][c]()
|
||||
a[b].c()
|
||||
a.b[c]()
|
||||
a:b()
|
||||
a:b -- FAIL
|
||||
a:1 -- FAIL
|
||||
a.b:c()
|
||||
a[b]:c()
|
||||
a:b: -- FAIL
|
||||
a:b():c()
|
||||
a:b().c[d]:e()
|
||||
a:b()[c].d:e()
|
||||
(a)()
|
||||
()() -- FAIL
|
||||
(1)()
|
||||
("foo")()
|
||||
(true)()
|
||||
(a)()()
|
||||
(a.b)()
|
||||
(a[b])()
|
||||
(a).b()
|
||||
(a)[b]()
|
||||
(a):b()
|
||||
(a).b[c]:d()
|
||||
(a)[b].c:d()
|
||||
(a):b():c()
|
||||
(a):b().c[d]:e()
|
||||
(a):b()[c].d:e()
|
||||
a"foo"
|
||||
a[[foo]]
|
||||
a.b"foo"
|
||||
a[b]"foo"
|
||||
a:b"foo"
|
||||
a{}
|
||||
a.b{}
|
||||
a[b]{}
|
||||
a:b{}
|
||||
a()"foo"
|
||||
a"foo"()
|
||||
a"foo".b()
|
||||
a"foo"[b]()
|
||||
a"foo":c()
|
||||
a"foo""bar"
|
||||
a"foo"{}
|
||||
(a):b"foo".c[d]:e"bar"
|
||||
(a):b"foo"[c].d:e"bar"
|
||||
a(){}
|
||||
a{}()
|
||||
a{}.b()
|
||||
a{}[b]()
|
||||
a{}:c()
|
||||
a{}"foo"
|
||||
a{}{}
|
||||
(a):b{}.c[d]:e{}
|
||||
(a):b{}[c].d:e{}
|
||||
a = -- FAIL
|
||||
a = a
|
||||
a = nil
|
||||
a = false
|
||||
a = 1
|
||||
a = "foo"
|
||||
a = [[foo]]
|
||||
a = {}
|
||||
a = (a)
|
||||
a = (nil)
|
||||
a = (true)
|
||||
a = (1)
|
||||
a = ("foo")
|
||||
a = ([[foo]])
|
||||
a = ({})
|
||||
a = a.b
|
||||
a = a.b. -- FAIL
|
||||
a = a.b.c
|
||||
a = a:b -- FAIL
|
||||
a = a[b]
|
||||
a = a[1]
|
||||
a = a["foo"]
|
||||
a = a[b][c]
|
||||
a = a.b[c]
|
||||
a = a[b].c
|
||||
a = (a)[b]
|
||||
a = (a).c
|
||||
a = () -- FAIL
|
||||
a = a()
|
||||
a = a.b()
|
||||
a = a[b]()
|
||||
a = a:b()
|
||||
a = (a)()
|
||||
a = (a).b()
|
||||
a = (a)[b]()
|
||||
a = (a):b()
|
||||
a = a"foo"
|
||||
a = a{}
|
||||
a = function -- FAIL
|
||||
a = function 1 -- FAIL
|
||||
a = function a -- FAIL
|
||||
a = function end -- FAIL
|
||||
a = function( -- FAIL
|
||||
a = function() end
|
||||
a = function(1 -- FAIL
|
||||
a = function(p) end
|
||||
a = function(p,) -- FAIL
|
||||
a = function(p q -- FAIL
|
||||
a = function(p,q,r) end
|
||||
a = function(p,q,1 -- FAIL
|
||||
a = function(...) end
|
||||
a = function(..., -- FAIL
|
||||
a = function(p,...) end
|
||||
a = function(p,q,r,...) end
|
||||
a = ...
|
||||
a = a, b, ...
|
||||
a = (...)
|
||||
a = ..., 1, 2
|
||||
a = function() return ... end -- FAIL
|
||||
a = -10
|
||||
a = -"foo"
|
||||
a = -a
|
||||
a = -nil
|
||||
a = -true
|
||||
a = -{}
|
||||
a = -function() end
|
||||
a = -a()
|
||||
a = -(a)
|
||||
a = - -- FAIL
|
||||
a = not 10
|
||||
a = not "foo"
|
||||
a = not a
|
||||
a = not nil
|
||||
a = not true
|
||||
a = not {}
|
||||
a = not function() end
|
||||
a = not a()
|
||||
a = not (a)
|
||||
a = not -- FAIL
|
||||
a = #10
|
||||
a = #"foo"
|
||||
a = #a
|
||||
a = #nil
|
||||
a = #true
|
||||
a = #{}
|
||||
a = #function() end
|
||||
a = #a()
|
||||
a = #(a)
|
||||
a = # -- FAIL
|
||||
a = 1 + 2; a = 1 - 2
|
||||
a = 1 * 2; a = 1 / 2
|
||||
a = 1 ^ 2; a = 1 % 2
|
||||
a = 1 .. 2
|
||||
a = 1 + -- FAIL
|
||||
a = 1 .. -- FAIL
|
||||
a = 1 * / -- FAIL
|
||||
a = 1 + -2; a = 1 - -2
|
||||
a = 1 * - -- FAIL
|
||||
a = 1 * not 2; a = 1 / not 2
|
||||
a = 1 / not -- FAIL
|
||||
a = 1 * #"foo"; a = 1 / #"foo"
|
||||
a = 1 / # -- FAIL
|
||||
a = 1 + 2 - 3 * 4 / 5 % 6 ^ 7
|
||||
a = ((1 + 2) - 3) * (4 / (5 % 6 ^ 7))
|
||||
a = (1 + (2 - (3 * (4 / (5 % 6 ^ ((7)))))))
|
||||
a = ((1 -- FAIL
|
||||
a = ((1 + 2) -- FAIL
|
||||
a = 1) -- FAIL
|
||||
a = a + b - c
|
||||
a = "foo" + "bar"
|
||||
a = "foo".."bar".."baz"
|
||||
a = true + false - nil
|
||||
a = {} * {}
|
||||
a = function() end / function() end
|
||||
a = a() ^ b()
|
||||
a = ... % ...
|
||||
a = 1 == 2; a = 1 ~= 2
|
||||
a = 1 < 2; a = 1 <= 2
|
||||
a = 1 > 2; a = 1 >= 2
|
||||
a = 1 < 2 < 3
|
||||
a = 1 >= 2 >= 3
|
||||
a = 1 == -- FAIL
|
||||
a = ~= 2 -- FAIL
|
||||
a = "foo" == "bar"
|
||||
a = "foo" > "bar"
|
||||
a = a ~= b
|
||||
a = true == false
|
||||
a = 1 and 2; a = 1 or 2
|
||||
a = 1 and -- FAIL
|
||||
a = or 1 -- FAIL
|
||||
a = 1 and 2 and 3
|
||||
a = 1 or 2 or 3
|
||||
a = 1 and 2 or 3
|
||||
a = a and b or c
|
||||
a = a() and (b)() or c.d
|
||||
a = "foo" and "bar"
|
||||
a = true or false
|
||||
a = {} and {} or {}
|
||||
a = (1) and ("foo") or (nil)
|
||||
a = function() end == function() end
|
||||
a = function() end or function() end
|
||||
a = { -- FAIL
|
||||
a = {}
|
||||
a = {,} -- FAIL
|
||||
a = {;} -- FAIL
|
||||
a = {,,} -- FAIL
|
||||
a = {;;} -- FAIL
|
||||
a = {{ -- FAIL
|
||||
a = {{{}}}
|
||||
a = {{},{},{{}},}
|
||||
a = { 1 }
|
||||
a = { 1, }
|
||||
a = { 1; }
|
||||
a = { 1, 2 }
|
||||
a = { a, b, c, }
|
||||
a = { true; false, nil; }
|
||||
a = { a.b, a[b]; a:c(), }
|
||||
a = { 1 + 2, a > b, "a" or "b" }
|
||||
a = { a=1, }
|
||||
a = { a=1, b="foo", c=nil }
|
||||
a = { a -- FAIL
|
||||
a = { a= -- FAIL
|
||||
a = { a=, -- FAIL
|
||||
a = { a=; -- FAIL
|
||||
a = { 1, a="foo" -- FAIL
|
||||
a = { 1, a="foo"; b={}, d=true; }
|
||||
a = { [ -- FAIL
|
||||
a = { [1 -- FAIL
|
||||
a = { [1] -- FAIL
|
||||
a = { [a]= -- FAIL
|
||||
a = { ["foo"]="bar" }
|
||||
a = { [1]=a, [2]=b, }
|
||||
a = { true, a=1; ["foo"]="bar", }
|
||||
]=]
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
local util = require'Util'
|
||||
local Parser = require'ParseLua'
|
||||
local Format_Mini = require'FormatMini'
|
||||
|
||||
local f = io.open("tmp", 'wb')
|
||||
f:write(source)
|
||||
f:close()
|
||||
for w in io.lines("tmp") do
|
||||
--print(w)
|
||||
local success, ast = Parser.ParseLua(w)
|
||||
if w:find("FAIL") then
|
||||
if success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: true. Did fail: " .. tostring(not success))
|
||||
--print("Message: " .. ast)
|
||||
print("Line: " .. w)
|
||||
else
|
||||
--print("Suceeded!")
|
||||
end
|
||||
else
|
||||
if not success then
|
||||
print("ERROR PARSING LINE:")
|
||||
print("Should fail: false. Did fail: " .. tostring(not success))
|
||||
print("Message: " .. ast)
|
||||
print("Line: " .. w)
|
||||
else
|
||||
--print("Suceeded!")
|
||||
end
|
||||
end
|
||||
end
|
||||
print"Done!"
|
||||
os.remove("tmp")
|
Reference in New Issue
Block a user