JSDOC/CompressWhite.js
[gnome.introspection-doc-generator] / JSDOC / CompressWhite.js
1  // <script type="text/javascript">
2 /**
3  * 
4  * pack a javascript file, and return a shorter version!
5  * 
6  * a bit picky at present with ; and crlf reading...
7  * 
8  */
9 JSDOC.CompressWhite =  function (ts, packer)
10 {
11     
12     ts.rewind();
13     //var str = File.read(fn);
14     var rep_var = 1;
15     
16     while (true) {
17         var tok = ts.next();
18         if (!tok) {
19             break;
20         }
21         if (tok._isWS) {
22             continue;
23             //if (tok._isDoc) {
24             //    continue;
25             //}
26             // just spaces, not \n!
27             //if (tok.data.indexOf("\n") < 0) {
28             //    continue;
29            // }
30             
31             
32         }
33         
34         // add semi-colon's where linebreaks are used... - not foolproof yet.!
35         if (tok.isTypeN(Script.TOKidentifier)) {
36             //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
37             // a = new function() {} 
38             if (ts.look(1).isTypeN(Script.TOKassign) && ts.look(2).isTypeN(Script.TOKnew) && 
39                 ts.look(3).isTypeN(Script.TOKfunction)) {
40                 // freeze time.. 
41                 var cu = ts.cursor;
42                 
43                 ts.balance("lparen");
44                 
45                 
46                 ts.balance("lbrace");
47                 // if next is not ';' -> make it so...
48                 
49                 if (!ts.look(1).isTypeN(Script.TOKsemicolon) && !ts.look(1).isTypeN(Script.TOKrbrace) && ts.look(1,true).isLineBreak()) {
50                     ts.cur().outData = ts.cur().data +";";
51                 }
52                 // restore.. 
53                 ts.cursor = cu;
54                 continue;
55             }
56             // a = function() { ...
57                
58             if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKfunction)) {
59                 // freeze time.. 
60                 //println("got = function() ");
61                 var cu = ts.cursor;
62                 
63                 ts.balance("lparen");
64                 ts.balance("lbrace");
65                 // if next is not ';' -> make it so...
66                 // although this var a=function(){},v,c; causes 
67                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
68                     ts.cur().outData = ts.cur().data+";";
69                 }
70                 // restore.. 
71                 ts.cursor = cu;
72                 continue;
73             }
74             // function a () { ... };
75                 /*
76             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
77                 // freeze time.. 
78                 //println("got = function() ");
79                 var cu = ts.cursor;
80                 
81                 ts.balance("lparen");
82                 ts.balance("lbrace");
83                 // if next is not ';' -> make it so...
84                 // although this var a=function(){},v,c; causes 
85                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
86                     ts.cur().outData = ts.cur().data+";";
87                 }
88                 // restore.. 
89                 ts.cursor = cu;
90                 continue;
91             }
92             */
93             
94             // a = { ....
95                 
96             if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKlbrace)) {
97                 // freeze time.. 
98                 //println("----------*** 3 *** --------------");
99                 var cu = ts.cursor;
100                 
101                 if (!ts.balance("lbrace") ){
102                     throw "could not find end lbrace!!!";
103                 }
104                 // if next is not ';' -> make it so...
105
106                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
107                     ts.cur().outData = ts.cur().data +";";
108                 }
109                 // restore.. 
110                 ts.cursor = cu;
111                 continue;
112             }
113             
114             // any more??
115         }
116         
117         
118         
119          
120         //println("got Token: " + tok.type);
121         
122         
123         
124         switch(tok.tokN) {
125             // things that need space appending
126             case Script.TOKfunction:
127             case Script.TOKbreak:
128             case Script.TOKcontinue:
129                 // if next item is a identifier..
130                 if (ts.look(1).isTypeN(Script.TOKidentifier) || ts.look(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
131                    tok.outData =  tok.data + " ";
132                 }
133                 continue;
134                 
135                 
136             case Script.TOKreturn: // if next item is not a semi; (or }
137                 if (ts.look(1).isData(';') || ts.look(1).isData('}')) {
138                     continue;
139                 }
140                 tok.outData =  tok.data + " ";
141                 
142                 continue;
143             
144                 
145             case Script.TOKelse: // if next item is not a semi; (or }
146                 if (!ts.look(1).isTypeN(Script.TOKif)) {
147                     continue;
148                 }
149                 
150                 tok.outData =  tok.data + " ";
151                 continue;
152             
153             case Script.TOKplusplus: // if previous was a plus or next is a + add a space..
154             case Script.TOKminusminus: // if previous was a - or next is a - add a space..
155             
156                 var p = (Script.TOKminusminus == tok.tokN ? '-' : '+'); 
157             
158                 if (ts.look(1).data == p) {
159                     tok.outData =  tok.data + " ";
160                 }
161                 if (ts.look(-1).data == p) {
162                     tok.outData =  " " +  tok.data;
163                     
164                 }
165                 continue;
166             
167             case Script.TOKin: // before and after?? 
168             case Script.TOKinstanceof:
169                 
170                 tok.outData = " " + tok.data + " ";
171                 continue;
172             
173             case Script.TOKvar: // always after..
174             case Script.TOKnew:
175             case Script.TOKdelete:
176             case Script.TOKthrow:
177             case Script.TOKnew:
178             case Script.TOKcase:
179             case Script.TOKtypeof:
180             case Script.TOKvoid:
181                 tok.outData =  tok.data + " ";
182                 
183                 continue
184              case Script.TOKsemicolon:
185                 //remove semicolon before brace -- 
186                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
187                 //    tok.outData = '';
188                // }
189                 continue;
190            
191             default:
192                 continue;
193         }
194     }
195     
196     ts.rewind();
197     
198     
199     //var f = new File(minfile, File.NEW);
200     
201     var out = '';
202     var outoff = 0;
203     out.length = ts.slen; // prealloc.
204     out = '';
205     while (true) {
206         var tok = ts.next();
207         if (!tok) {
208             break;
209         }
210         if (tok._isWS) {
211             continue;
212         }
213         
214         
215         if (tok.isTypeN(Script.TOKidentifier) && tok.identifier && tok.identifier.mungedValue.length) {
216             //f.write(tok.identifier.mungedValue);
217             out +=tok.identifier.mungedValue;
218             continue;
219         }
220         
221         // at this point we can apply a text translation kit...
222         
223         if (tok.type == 'string') {
224             if (packer.stringHandler) {
225                 out += packer.stringHandler(tok);
226                 continue;
227             }
228         }
229         //f.write(tok.outData);
230         out += tok.outData;
231         
232         if ((tok.outData == ';') && (out.length - outoff > 255)) {
233             outoff = out.length;
234             out += '\n';
235         }
236     }
237     //f.close();
238     /*
239     // remove the last ';' !!!
240     if (out.substring(out.length-1) == ';') {
241         return out.substring(0,out.length-1);
242        }
243     */
244     return out;
245     
246 }
247     
248