-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsquish
executable file
·2633 lines (2624 loc) · 42.1 KB
/
squish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env lua
package.preload['optlex']=(function(...)
local h=_G
local c=require"string"
module"optlex"
local i=c.match
local e=c.sub
local d=c.find
local l=c.rep
local m
error=h.error
warn={}
local n,o,r
local j={
TK_KEYWORD=true,
TK_NAME=true,
TK_NUMBER=true,
TK_STRING=true,
TK_LSTRING=true,
TK_OP=true,
TK_EOS=true,
}
local k={
TK_COMMENT=true,
TK_LCOMMENT=true,
TK_EOL=true,
TK_SPACE=true,
}
local s
local function b(e)
local t=n[e-1]
if e<=1 or t=="TK_EOL"then
return true
elseif t==""then
return b(e-1)
end
return false
end
local function v(e)
local t=n[e+1]
if e>=#n or t=="TK_EOL"or t=="TK_EOS"then
return true
elseif t==""then
return v(e+1)
end
return false
end
local function E(a)
local t=#i(a,"^%-%-%[=*%[")
local a=e(a,t+1,-(t-1))
local e,t=1,0
while true do
local o,n,i,a=d(a,"([\r\n])([\r\n]?)",e)
if not o then break end
e=o+1
t=t+1
if#a>0 and i~=a then
e=e+1
end
end
return t
end
local function g(s,h)
local a=i
local t,e=n[s],n[h]
if t=="TK_STRING"or t=="TK_LSTRING"or
e=="TK_STRING"or e=="TK_LSTRING"then
return""
elseif t=="TK_OP"or e=="TK_OP"then
if(t=="TK_OP"and(e=="TK_KEYWORD"or e=="TK_NAME"))or
(e=="TK_OP"and(t=="TK_KEYWORD"or t=="TK_NAME"))then
return""
end
if t=="TK_OP"and e=="TK_OP"then
local t,e=o[s],o[h]
if(a(t,"^%.%.?$")and a(e,"^%."))or
(a(t,"^[~=<>]$")and e=="=")or
(t=="["and(e=="["or e=="="))then
return" "
end
return""
end
local t=o[s]
if e=="TK_OP"then t=o[h]end
if a(t,"^%.%.?%.?$")then
return" "
end
return""
else
return" "
end
end
local function q()
local h,s,i={},{},{}
local e=1
for t=1,#n do
local a=n[t]
if a~=""then
h[e],s[e],i[e]=a,o[t],r[t]
e=e+1
end
end
n,o,r=h,s,i
end
local function _(d)
local t=o[d]
local t=t
local n
if i(t,"^0[xX]")then
local e=h.tostring(h.tonumber(t))
if#e<=#t then
t=e
else
return
end
end
if i(t,"^%d+%.?0*$")then
t=i(t,"^(%d+)%.?0*$")
if t+0>0 then
t=i(t,"^0*([1-9]%d*)$")
local a=#i(t,"0*$")
local o=h.tostring(a)
if a>#o+1 then
t=e(t,1,#t-a).."e"..o
end
n=t
else
n="0"
end
elseif not i(t,"[eE]")then
local a,t=i(t,"^(%d*)%.(%d+)$")
if a==""then a=0 end
if t+0==0 and a==0 then
n="0"
else
local o=#i(t,"0*$")
if o>0 then
t=e(t,1,#t-o)
end
if a+0>0 then
n=a.."."..t
else
n="."..t
local a=#i(t,"^0*")
local o=#t-a
local a=h.tostring(#t)
if o+2+#a<1+#t then
n=e(t,-o).."e-"..a
end
end
end
else
local t,a=i(t,"^([^eE]+)[eE]([%+%-]?%d+)$")
a=h.tonumber(a)
local o,s=i(t,"^(%d*)%.(%d*)$")
if o then
a=a-#s
t=o..s
end
if t+0==0 then
n="0"
else
local o=#i(t,"^0*")
t=e(t,o+1)
o=#i(t,"0*$")
if o>0 then
t=e(t,1,#t-o)
a=a+o
end
local i=h.tostring(a)
if a==0 then
n=t
elseif a>0 and(a<=1+#i)then
n=t..l("0",a)
elseif a<0 and(a>=-#t)then
o=#t+a
n=e(t,1,o).."."..e(t,o+1)
elseif a<0 and(#i>=-a-#t)then
o=-a-#t
n="."..l("0",o)..t
else
n=t.."e"..a
end
end
end
if n and n~=o[d]then
if s then
m("<number> (line "..r[d]..") "..o[d].." -> "..n)
s=s+1
end
o[d]=n
end
end
local function x(l)
local t=o[l]
local h=e(t,1,1)
local f=(h=="'")and'"'or"'"
local t=e(t,2,-2)
local a=1
local u,n=0,0
while a<=#t do
local m=e(t,a,a)
if m=="\\"then
local o=a+1
local r=e(t,o,o)
local s=d("abfnrtv\\\n\r\"\'0123456789",r,1,true)
if not s then
t=e(t,1,a-1)..e(t,o)
a=a+1
elseif s<=8 then
a=a+2
elseif s<=10 then
local i=e(t,o,o+1)
if i=="\r\n"or i=="\n\r"then
t=e(t,1,a).."\n"..e(t,o+2)
elseif s==10 then
t=e(t,1,a).."\n"..e(t,o+1)
end
a=a+2
elseif s<=12 then
if r==h then
u=u+1
a=a+2
else
n=n+1
t=e(t,1,a-1)..e(t,o)
a=a+1
end
else
local i=i(t,"^(%d%d?%d?)",o)
o=a+1+#i
local l=i+0
local s=c.char(l)
local r=d("\a\b\f\n\r\t\v",s,1,true)
if r then
i="\\"..e("abfnrtv",r,r)
elseif l<32 then
i="\\"..l
elseif s==h then
i="\\"..s
u=u+1
elseif s=="\\"then
i="\\\\"
else
i=s
if s==f then
n=n+1
end
end
t=e(t,1,a-1)..i..e(t,o)
a=a+#i
end
else
a=a+1
if m==f then
n=n+1
end
end
end
if u>n then
a=1
while a<=#t do
local o,n,i=d(t,"([\'\"])",a)
if not o then break end
if i==h then
t=e(t,1,o-2)..e(t,o)
a=o
else
t=e(t,1,o-1).."\\"..e(t,o)
a=o+2
end
end
h=f
end
t=h..t..h
if t~=o[l]then
if s then
m("<string> (line "..r[l]..") "..o[l].." -> "..t)
s=s+1
end
o[l]=t
end
end
local function z(s)
local t=o[s]
local h=i(t,"^%[=*%[")
local a=#h
local c=e(t,-a,-1)
local u=e(t,a+1,-(a+1))
local n=""
local t=1
while true do
local a,o,d,h=d(u,"([\r\n])([\r\n]?)",t)
local o
if not a then
o=e(u,t)
elseif a>=t then
o=e(u,t,a-1)
end
if o~=""then
if i(o,"%s+$")then
warn.lstring="trailing whitespace in long string near line "..r[s]
end
n=n..o
end
if not a then
break
end
t=a+1
if a then
if#h>0 and d~=h then
t=t+1
end
if not(t==1 and t==a)then
n=n.."\n"
end
end
end
if a>=3 then
local e,t=a-1
while e>=2 do
local a="%]"..l("=",e-2).."%]"
if not i(n,a)then t=e end
e=e-1
end
if t then
a=l("=",t-2)
h,c="["..a.."[","]"..a.."]"
end
end
o[s]=h..n..c
end
local function w(r)
local a=o[r]
local s=i(a,"^%-%-%[=*%[")
local t=#s
local u=e(a,-t,-1)
local h=e(a,t+1,-(t-1))
local n=""
local a=1
while true do
local o,t,r,s=d(h,"([\r\n])([\r\n]?)",a)
local t
if not o then
t=e(h,a)
elseif o>=a then
t=e(h,a,o-1)
end
if t~=""then
local a=i(t,"%s*$")
if#a>0 then t=e(t,1,-(a+1))end
n=n..t
end
if not o then
break
end
a=o+1
if o then
if#s>0 and r~=s then
a=a+1
end
n=n.."\n"
end
end
t=t-2
if t>=3 then
local e,a=t-1
while e>=2 do
local t="%]"..l("=",e-2).."%]"
if not i(n,t)then a=e end
e=e-1
end
if a then
t=l("=",a-2)
s,u="--["..t.."[","]"..t.."]"
end
end
o[r]=s..n..u
end
local function p(n)
local t=o[n]
local a=i(t,"%s*$")
if#a>0 then
t=e(t,1,-(a+1))
end
o[n]=t
end
local function I(o,t)
if not o then return false end
local a=i(t,"^%-%-%[=*%[")
local a=#a
local i=e(t,-a,-1)
local e=e(t,a+1,-(a-1))
if d(e,o,1,true)then
return true
end
end
function optimize(t,a,d,i)
local c=t["opt-comments"]
local u=t["opt-whitespace"]
local f=t["opt-emptylines"]
local y=t["opt-eols"]
local A=t["opt-strings"]
local T=t["opt-numbers"]
local O=t.KEEP
s=t.DETAILS and 0
m=m or h.print
if y then
c=true
u=true
f=true
end
n,o,r
=a,d,i
local t=1
local a,d
local h
local function i(i,a,e)
e=e or t
n[e]=i or""
o[e]=a or""
end
while true do
a,d=n[t],o[t]
local s=b(t)
if s then h=nil end
if a=="TK_EOS"then
break
elseif a=="TK_KEYWORD"or
a=="TK_NAME"or
a=="TK_OP"then
h=t
elseif a=="TK_NUMBER"then
if T then
_(t)
end
h=t
elseif a=="TK_STRING"or
a=="TK_LSTRING"then
if A then
if a=="TK_STRING"then
x(t)
else
z(t)
end
end
h=t
elseif a=="TK_COMMENT"then
if c then
if t==1 and e(d,1,1)=="#"then
p(t)
else
i()
end
elseif u then
p(t)
end
elseif a=="TK_LCOMMENT"then
if I(O,d)then
if u then
w(t)
end
h=t
elseif c then
local e=E(d)
if k[n[t+1]]then
i()
a=""
else
i("TK_SPACE"," ")
end
if not f and e>0 then
i("TK_EOL",l("\n",e))
end
if u and a~=""then
t=t-1
end
else
if u then
w(t)
end
h=t
end
elseif a=="TK_EOL"then
if s and f then
i()
elseif d=="\r\n"or d=="\n\r"then
i("TK_EOL","\n")
end
elseif a=="TK_SPACE"then
if u then
if s or v(t)then
i()
else
local a=n[h]
if a=="TK_LCOMMENT"then
i()
else
local e=n[t+1]
if k[e]then
if(e=="TK_COMMENT"or e=="TK_LCOMMENT")and
a=="TK_OP"and o[h]=="-"then
else
i()
end
else
local e=g(h,t+1)
if e==""then
i()
else
i("TK_SPACE"," ")
end
end
end
end
end
else
error("unidentified token encountered")
end
t=t+1
end
q()
if y then
t=1
if n[1]=="TK_COMMENT"then
t=3
end
while true do
a,d=n[t],o[t]
if a=="TK_EOS"then
break
elseif a=="TK_EOL"then
local e,a=n[t-1],n[t+1]
if j[e]and j[a]then
local e=g(t-1,t+1)
if e==""then
i()
end
end
end
t=t+1
end
q()
end
if s and s>0 then m()end
return n,o,r
end
end)
package.preload['optparser']=(function(...)
local e=_G
local a=require"string"
local w=require"table"
module"optparser"
local n="etaoinshrdlucmfwypvbgkqjxz_ETAOINSHRDLUCMFWYPVBGKQJXZ"
local r="etaoinshrdlucmfwypvbgkqjxz_0123456789ETAOINSHRDLUCMFWYPVBGKQJXZ"
local y={}
for e in a.gmatch([[
and break do else elseif end false for function if in
local nil not or repeat return then true until while
self]],"%S+")do
y[e]=true
end
local d,f,
u,o,
c,p,
l,
h
local function m(e)
local i={}
for n=1,#e do
local e=e[n]
local o=e.name
if not i[o]then
i[o]={
decl=0,token=0,size=0,
}
end
local t=i[o]
t.decl=t.decl+1
local i=e.xref
local a=#i
t.token=t.token+a
t.size=t.size+a*#o
if e.decl then
e.id=n
e.xcount=a
if a>1 then
e.first=i[2]
e.last=i[a]
end
else
t.id=n
end
end
return i
end
local function v(t)
local i=a.byte
local s=a.char
local e={
TK_KEYWORD=true,TK_NAME=true,TK_NUMBER=true,
TK_STRING=true,TK_LSTRING=true,
}
if not t["opt-comments"]then
e.TK_COMMENT=true
e.TK_LCOMMENT=true
end
local a={}
for e=1,#d do
a[e]=f[e]
end
for e=1,#o do
local e=o[e]
local t=e.xref
for e=1,e.xcount do
local e=t[e]
a[e]=""
end
end
local t={}
for e=0,255 do t[e]=0 end
for o=1,#d do
local o,a=d[o],a[o]
if e[o]then
for e=1,#a do
local e=i(a,e)
t[e]=t[e]+1
end
end
end
local function a(a)
local e={}
for o=1,#a do
local a=i(a,o)
e[o]={c=a,freq=t[a],}
end
w.sort(e,
function(e,t)
return e.freq>t.freq
end
)
local t={}
for a=1,#e do
t[a]=s(e[a].c)
end
return w.concat(t)
end
n=a(n)
r=a(r)
end
local function b()
local t
local s,h=#n,#r
local e=l
if e<s then
e=e+1
t=a.sub(n,e,e)
else
local i,o=s,1
repeat
e=e-i
i=i*h
o=o+1
until i>e
local i=e%s
e=(e-i)/s
i=i+1
t=a.sub(n,i,i)
while o>1 do
local i=e%h
e=(e-i)/h
i=i+1
t=t..a.sub(r,i,i)
o=o-1
end
end
l=l+1
return t,c[t]~=nil
end
function optimize(e,t,a,i,n)
d,f,u,o
=t,a,i,n
l=0
h={}
c=m(u)
p=m(o)
if e["opt-entropy"]then
v(e)
end
local e={}
for t=1,#o do
e[t]=o[t]
end
w.sort(e,
function(e,t)
return e.xcount>t.xcount
end
)
local a,t,r={},1,false
for o=1,#e do
local e=e[o]
if not e.isself then
a[t]=e
t=t+1
else
r=true
end
end
e=a
local s=#e
while s>0 do
local n,a
repeat
n,a=b()
until not y[n]
h[#h+1]=n
local t=s
if a then
local i=u[c[n].id].xref
local n=#i
for a=1,s do
local a=e[a]
local s,e=a.act,a.rem
while e<0 do
e=o[-e].rem
end
local o
for t=1,n do
local t=i[t]
if t>=s and t<=e then o=true end
end
if o then
a.skip=true
t=t-1
end
end
end
while t>0 do
local a=1
while e[a].skip do
a=a+1
end
t=t-1
local i=e[a]
a=a+1
i.newname=n
i.skip=true
i.done=true
local s,r=i.first,i.last
local h=i.xref
if s and t>0 then
local n=t
while n>0 do
while e[a].skip do
a=a+1
end
n=n-1
local e=e[a]
a=a+1
local n,a=e.act,e.rem
while a<0 do
a=o[-a].rem
end
if not(r<n or s>a)then
if n>=i.act then
for o=1,i.xcount do
local o=h[o]
if o>=n and o<=a then
t=t-1
e.skip=true
break
end
end
else
if e.last and e.last>=i.act then
t=t-1
e.skip=true
end
end
end
if t==0 then break end
end
end
end
local a,t={},1
for o=1,s do
local e=e[o]
if not e.done then
e.skip=false
a[t]=e
t=t+1
end
end
e=a
s=#e
end
for e=1,#o do
local e=o[e]
local a=e.xref
if e.newname then
for t=1,e.xcount do
local t=a[t]
f[t]=e.newname
end
e.name,e.oldname
=e.newname,e.name
else
e.oldname=e.name
end
end
if r then
h[#h+1]="self"
end
local e=m(o)
end
end)
package.preload['llex']=(function(...)
local c=_G
local h=require"string"
module"llex"
local d=h.find
local m=h.match
local i=h.sub
local w={}
for e in h.gmatch([[
and break do else elseif end false for function if in
local nil not or repeat return then true until while]],"%S+")do
w[e]=true
end
local e,
r,
a,
n,
s
local function o(t,a)
local e=#tok+1
tok[e]=t
seminfo[e]=a
tokln[e]=s
end
local function l(t,h)
local n=i
local i=n(e,t,t)
t=t+1
local e=n(e,t,t)
if(e=="\n"or e=="\r")and(e~=i)then
t=t+1
i=i..e
end
if h then o("TK_EOL",i)end
s=s+1
a=t
return t
end
function init(i,t)
e=i
r=t
a=1
s=1
tok={}
seminfo={}
tokln={}
local t,n,e,i=d(e,"^(#[^\r\n]*)(\r?\n?)")
if t then
a=a+#e
o("TK_COMMENT",e)
if#i>0 then l(a,true)end
end
end
function chunkid()
if r and m(r,"^[=@]")then
return i(r,2)
end
return"[string]"
end
function errorline(t,a)
local e=error or c.error
e(h.format("%s:%d: %s",chunkid(),a or s,t))
end
local r=errorline
local function u(t)
local i=i
local n=i(e,t,t)
t=t+1
local o=#m(e,"=*",t)
t=t+o
a=t
return(i(e,t,t)==n)and o or(-o)-1
end
local function f(c,h)
local t=a+1
local i=i
local o=i(e,t,t)
if o=="\r"or o=="\n"then
t=l(t)
end
local o=t
while true do
local o,d,s=d(e,"([\r\n%]])",t)
if not o then
r(c and"unfinished long string"or
"unfinished long comment")
end
t=o
if s=="]"then
if u(t)==h then
n=i(e,n,a)
a=a+1
return n
end
t=a
else
n=n.."\n"
t=l(t)
end
end
end
local function y(u)
local t=a
local s=d
local h=i
while true do
local i,d,o=s(e,"([\n\r\\\"\'])",t)
if i then
if o=="\n"or o=="\r"then
r("unfinished string")
end
t=i
if o=="\\"then
t=t+1
o=h(e,t,t)
if o==""then break end
i=s("abfnrtv\n\r",o,1,true)
if i then
if i>7 then
t=l(t)
else
t=t+1
end
elseif s(o,"%D")then
t=t+1
else
local o,a,e=s(e,"^(%d%d?%d?)",t)
t=a+1
if e+1>256 then
r("escape sequence too large")
end
end
else
t=t+1
if o==u then
a=t
return h(e,n,t-1)
end
end
else
break
end
end
r("unfinished string")
end
function llex()
local h=d
local d=m
while true do
local t=a
while true do
local m,p,s=h(e,"^([_%a][_%w]*)",t)
if m then
a=t+#s
if w[s]then
o("TK_KEYWORD",s)
else
o("TK_NAME",s)
end
break
end
local s,w,m=h(e,"^(%.?)%d",t)
if s then
if m=="."then t=t+1 end
local u,n,l=h(e,"^%d*[%.%d]*([eE]?)",t)
t=n+1
if#l==1 then
if d(e,"^[%+%-]",t)then
t=t+1
end
end
local n,t=h(e,"^[_%w]*",t)
a=t+1
local e=i(e,s,t)
if not c.tonumber(e)then
r("malformed number")
end
o("TK_NUMBER",e)
break
end
local c,m,w,s=h(e,"^((%s)[ \t\v\f]*)",t)
if c then
if s=="\n"or s=="\r"then
l(t,true)