22
33local function Squish (fileName )
44 local f , e = io.open (fileName , " r" )
5+
56 if not f then
67 print (e )
78 return
89 end
10+
911 local function tokenizeFile (file )
1012 local data , tokens , pos = file :read (" all" ), {}, 1
1113
12- if data :sub (1 , 2 ) == " #!" then
14+ if data :sub (1 , 2 ) == " #!" then -- Handle shebang if there is one
1315 local lineEnd = data :find (" \n " , 1 ) or # data + 1
14- table.insert (tokens , {type = " shebang" , value = data :sub (1 , lineEnd - 1 )})
16+ table.insert (tokens , { type = " shebang" , value = data :sub (1 , lineEnd - 1 ) })
1517 pos = lineEnd
1618 end
1719
1820 while pos <= # data do
1921 local c = data :sub (pos , pos )
2022
21- -- Handle comments
22- if c == " -" and data :sub (pos , pos + 1 ) == " --" then
23+ if c == " -" and data :sub (pos , pos + 1 ) == " --" then -- Handle comments
2324 local commentStart = pos
24- -- Check for multi-line comment
25- if data :sub (pos + 2 , pos + 3 ) == " [[" then
25+
26+ if data :sub (pos + 2 , pos + 3 ) == " [[" then -- Check for multi-line comment
2627 local commentEnd = data :find (" ]]" , pos + 4 , true )
28+
2729 if commentEnd then
28- table.insert (tokens , {type = " comment" , value = data :sub (commentStart , commentEnd + 1 )})
30+ table.insert (tokens , { type = " comment" , value = data :sub (commentStart , commentEnd + 1 ) })
2931 pos = commentEnd + 2
30- else
31- -- Unclosed comment, treat as single line
32+ else -- Unclosed comment, treat as single line
3233 local lineEnd = data :find (" \n " , pos ) or # data + 1
33- table.insert (tokens , {type = " comment" , value = data :sub (commentStart , lineEnd - 1 )})
34+ table.insert (tokens , { type = " comment" , value = data :sub (commentStart , lineEnd - 1 ) })
3435 pos = lineEnd
3536 end
36- -- Standard single-line comment
37- else
37+ else -- Standard single-line comment
3838 local lineEnd = data :find (" \n " , pos ) or # data + 1
39- table.insert (tokens , {type = " comment" , value = data :sub (commentStart , lineEnd - 1 )})
39+ table.insert (tokens , { type = " comment" , value = data :sub (commentStart , lineEnd - 1 ) })
4040 pos = lineEnd
4141 end
4242
43- -- Handle quoted strings
44- elseif c == ' "' or c == " '" then
43+ elseif c == ' "' or c == " '" then -- Handle quoted strings
4544 local stringStart = pos
4645 pos = pos + 1
4746 while pos <= # data do
@@ -54,31 +53,28 @@ local function Squish(fileName)
5453 end
5554 end
5655 if pos <= # data then
57- table.insert (tokens , {type = " string" , value = data :sub (stringStart , pos )})
56+ table.insert (tokens , { type = " string" , value = data :sub (stringStart , pos ) })
5857 pos = pos + 1
59- else
60- -- Unclosed string, treat as code (this is an error in Lua)
61- table.insert (tokens , {type = " code" , value = data :sub (stringStart , pos - 1 )})
58+ else -- Unclosed string, treat as code (this is an error in Lua)
59+ table.insert (tokens , { type = " code" , value = data :sub (stringStart , pos - 1 ) })
6260 end
6361
64- -- Handle bracket strings
65- elseif c == " [" and data :sub (pos , pos + 1 ):match (" %[=*%[" ) then
62+ elseif c == " [" and data :sub (pos , pos + 1 ):match (" %[=*%[" ) then -- Handle bracket strings
6663 local stringStart = pos
6764 local openBracket = data :match (" %[=*%[" , pos )
6865 local closeBracket = openBracket :gsub (" %[" , " ]" )
6966 local stringEnd = data :find (closeBracket , pos + # openBracket , true )
7067
7168 if stringEnd then
72- table.insert (tokens , {type = " string" , value = data :sub (stringStart , stringEnd + # closeBracket - 1 )})
69+ table.insert (tokens , { type = " string" , value = data :sub (stringStart , stringEnd + # closeBracket - 1 ) })
7370 pos = stringEnd + # closeBracket
7471 else
7572 -- Unclosed long bracket, treat as code (this is an error in Lua)
76- table.insert (tokens , {type = " code" , value = data :sub (stringStart , pos )})
73+ table.insert (tokens , { type = " code" , value = data :sub (stringStart , pos ) })
7774 pos = pos + 1
7875 end
7976
80- -- Handle normal code
81- else
77+ else -- Handle normal code
8278 local codeStart = pos
8379 while pos <= # data do
8480 local next_char = data :sub (pos , pos )
@@ -91,7 +87,7 @@ local function Squish(fileName)
9187 end
9288
9389 if codeStart < pos then
94- table.insert (tokens , {type = " code" , value = data :sub (codeStart , pos - 1 )})
90+ table.insert (tokens , { type = " code" , value = data :sub (codeStart , pos - 1 ) })
9591 end
9692 end
9793 end
@@ -119,13 +115,12 @@ local function Squish(fileName)
119115 -- Include the shebang on its own line if there is one
120116 local data = # tokens > 0 and tokens [1 ].type == " shebang" and table.remove (tokens , 1 ).value .. ' \n ' or " "
121117
122- -- Concatenate data together
123- for _ , token in ipairs (tokens ) do
118+ for _ , token in ipairs (tokens ) do -- Drop comments and concatenate code and strings back together
124119 if token .type ~= " comment" then
125120 local function pad ()
126121 local last , start = # data > 1 and data :sub (- 1 , - 1 ), # token .value > 1 and token .value :sub (1 , 1 )
127- if ( last and (last :find (" %a" ) or last :find (" %d" )))
128- and ( start and (start : find ( " %a " ) or start : find ( " %d " )) ) then
122+ local function needsPadding ( x ) return x and (x :find (" %a" ) or x :find (" %d" )) end
123+ if needsPadding ( last ) and needsPadding (start ) then
129124 return " "
130125 end
131126 return " "
@@ -149,11 +144,13 @@ local function Squish(fileName)
149144end
150145
151146if # arg < 1 then
152- print (arg [0 ],[[ [LUA SCRIPT...]
147+ print (arg [0 ], [[ [LUA SCRIPT...]
153148
154149Removes comments, lines and whitespace from Lua scripts to save disk space.
155150Always backup any script before running it with ]] .. arg [0 ])
156151 os.exit (1 )
157152end
158153
159- for _ ,v in ipairs (arg ) do Squish (v ) end
154+ for _ , v in ipairs (arg ) do
155+ Squish (v )
156+ end
0 commit comments