Make minify.lua more advanced.

This commit is contained in:
luk3yx 2019-10-15 17:31:03 +13:00
parent 4fa3d88608
commit 2f2d701230
2 changed files with 99 additions and 80 deletions

View File

@ -4,91 +4,106 @@
-- © 2019 by luk3yx -- © 2019 by luk3yx
-- --
-- Find multiple patterns
local function find_multiple(text, ...)
local n = select('#', ...)
local s, e, pattern
for i = 1, n do
local p = select(i, ...)
local s2, e2 = text:find(p)
if s2 and (not s or s2 < s) then
s, e, pattern = s2, e2 or s2, p
end
end
return s, e, pattern
end
-- Matches
-- These take 2-3 arguments (code, res, char) and should return code and res.
local matches = {
-- Handle multi-line strings
['%[=*%['] = function(code, res, char)
res = res .. char
char = char:sub(2, -2)
local s, e = code:find(']' .. char .. ']', nil, true)
if not s or not e then return code, res end
return code:sub(e + 1), res .. code:sub(1, e)
end,
-- Handle regular comments
['--'] = function(code, res, char)
local s, e = code:find('\n', nil, true)
if not s or not e then return code, res end
-- Shift trailing spaces back
local spaces = res:match('(%s*)$') or ''
return spaces .. code:sub(e), res:sub(1, #res - #spaces)
end,
-- Handle multi-line comments
['%-%-%[=*%['] = function(code, res, char)
char = char:sub(4, -2)
local s, e = code:find(']' .. char .. ']', nil, true)
if not s or not e then return code, res end
-- Shift trailing spaces back
local spaces = res:match('(%s*)$') or ''
return spaces .. code:sub(e + 1), res:sub(1, #res - #spaces)
end,
-- Handle quoted text
['"'] = function(code, res, char)
res = res .. char
-- Handle backslashes
repeat
local s, e, pattern = find_multiple(code, '\\', char)
if pattern == char then
res = res .. code:sub(1, e)
code = code:sub(e + 1)
elseif pattern then
res = res .. code:sub(1, e + 1)
code = code:sub(e + 2)
end
until not pattern or pattern == char
return code, res
end,
['%s*[\r\n]%s*'] = function(code, res, char)
return code, res .. '\n'
end,
['[ \t]+'] = function(code, res, char)
return code, res .. ' '
end,
}
-- Give the functions alternate names
matches["'"] = matches['"']
-- The actual transpiler
return function(code) return function(code)
assert(type(code) == 'string') assert(type(code) == 'string')
local res, last, ws1, ws2, escape = '', false, '\n', '\n', false local res = ''
local sp = {['"'] = true, ["'"] = true, ['['] = true}
for i = 1, #code do -- Split the code by "tokens"
local char = code:sub(i, i) while true do
if char == '\r' then char = '\n' end -- Search for special characters
if last == '--' or last == '--.' or last == '--[' then local s, e, pattern = find_multiple(code, '[\'"\\]', '%-%-%[=*%[',
ws1 = ws2 '%-%-', '%[=*%[', '%s*[\r\n]%s*', '[ \t]+')
if char == '\n' then if not s then break end
if ws1 ~= '\n' then res = res .. '\n' end
last = false -- Add non-matching characters
ws1 = '\n' res = res .. code:sub(1, math.max(s - 1, 0))
elseif char == '[' and last ~= '--.' then
last = last .. char -- Call the correct function
else local char = code:sub(s, e)
last = '--.' local func = matches[char] or matches[pattern]
end assert(func, 'No function found for pattern!')
elseif last == '--[[' or last == '-]' then code, res = func(code:sub(e + 1), res, char)
ws1 = ws2
if last == '-]' then
if char == ']' then
last = false
else
last = '--[['
end
elseif char == ']' then
last = '-]'
end
elseif last == '[[' or last == ']' then
if last == ']' then
if char == ']' then
last = false
else
last = '[['
end
elseif char == ']' then
last = ']'
end
res = res .. char
elseif escape then
res = res .. '\\' .. char
escape = false
elseif char == '\\' then
escape = true
elseif last == '"' or last == "'" then
if char == last then last = false end
res = res .. char
elseif last == '[' then
if char == last then
last = last .. char
else
last = false
end
res = res .. char
elseif last == '-' then
if char == '-' then
last, ws1 = '--', ws2
else
res = res .. '-' .. char
last, ws1 = false, false
end
elseif char == '-' then
last = char
ws1 = ws2
elseif char == '\n' then
if ws2 == ' ' then
res = res:sub(1, #res - 1) .. '\n'
elseif ws2 ~= '\n' then
res = res .. '\n'
end
ws1 = '\n'
elseif char == ' ' or char == '\t' then
if not ws2 then res = res .. ' ' end
ws1 = ws2 or ' '
else
if sp[char] then last = char end
res = res .. char
end end
ws2 = ws1 return (res .. code):trim()
ws1 = false
end
return res
end end

View File

@ -5,6 +5,10 @@
-- © 2019 by luk3yx -- © 2019 by luk3yx
-- --
--[==[
error("Bad")
]==]
-- Make sure the minifier is sane -- Make sure the minifier is sane
a = 0 a = 0
--[[ --[[