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  * @arg ts {TokenStream} 
8    @arg packer {Packer} 
9  */
10  
11 CompressWhite =  function (ts, packer)
12 {
13     
14     ts.rewind();
15     //var str = File.read(fn);
16     var rep_var = 1;
17     
18     while (true) {
19         var tok = ts.next();
20         if (!tok) {
21             break;
22         }
23         if (tok.type == "WHIT") {
24             continue;
25             //if (tok._isDoc) {
26             //    continue;
27             //}
28             // just spaces, not \n!
29             //if (tok.data.indexOf("\n") < 0) {
30             //    continue;
31            // }
32             
33             
34         }
35         if (tok.data == "}")  {
36             
37             if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
38             
39                 ts.look(0).outData = ts.look(0).data+"\n";
40             }
41             // restore.. 
42             
43             continue;
44         }
45         // add semi-colon's where linebreaks are used... - not foolproof yet.!
46         if (tok.type == "NAME")  {
47             //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
48             // a = new function() {} 
49             if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == 'NEW'  && 
50                 ts.lookTok(3).name == 'FUNCTION') {
51                 // freeze time.. 
52                 var cu = ts.cursor;
53                 
54                 ts.balance("(");
55                 
56                 
57                 ts.balance("{");
58                 // if next is not ';' -> make it so...
59                 
60                 if (ts.lookTok(1).data != ';'  && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
61                     ts.look(0).outData = ts.cur().data +";";
62                 }
63                 // restore.. 
64                 ts.cursor = cu;
65                 continue;
66             }
67             // a = function() { ...
68                
69             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).name == "FUNCTION") {
70                 // freeze time.. 
71                 //println("got = function() ");
72                 var cu = ts.cursor;
73                 
74                 ts.balance("(");
75                 ts.balance("{");
76                 // if next is not ';' -> make it so...
77                 // although this var a=function(){},v,c; causes 
78                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
79                     ts.look(0).outData = ts.look(0).data+";";
80                 }
81                 // restore.. 
82                 ts.cursor = cu;
83                 continue;
84             }
85             // next item is a name..
86             if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) &&  ts.look(1,true).name == "NEWLINE") {
87                 // preserve linebraek
88                 ts.look(0).outData = ts.look(0).data+"\n";
89             }
90             // method call followed by name..
91             if (ts.lookTok(1).data == "(")  {
92                 var cu = ts.cursor;
93                 
94                 ts.balance("(");
95                  // although this var a=function(){},v,c; causes 
96                 
97                 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
98                 
99                     ts.look(0).outData = ts.look(0).data+"\n";
100                 }
101                 // restore.. 
102                 ts.cursor = cu;
103                 continue;
104             }
105             
106             
107             // function a () { ... };
108                 /*
109             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
110                 // freeze time.. 
111                 //println("got = function() ");
112                 var cu = ts.cursor;
113                 
114                 ts.balance("lparen");
115                 ts.balance("lbrace");
116                 // if next is not ';' -> make it so...
117                 // although this var a=function(){},v,c; causes 
118                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
119                     ts.cur().outData = ts.cur().data+";";
120                 }
121                 // restore.. 
122                 ts.cursor = cu;
123                 continue;
124             }
125             */
126             
127             // a = { ....
128                 
129             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).data == '{') {
130                 // freeze time.. 
131                 //println("----------*** 3 *** --------------");
132                 var cu = ts.cursor;
133                 
134                 if (!ts.balance("{") ){
135                     ts.dump(cu-40, cu);
136                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
137                     ts.dump(cu, cu+40);
138                     
139                     throw "could not find end lbrace!!!";
140                 }
141                 // if next is not ';' -> make it so...
142                 print(ts.lookTok(1).toString());
143                 print(ts.lookTok(1,true).toString());
144                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
145                     ts.look(0).outData = ts.look(0).data +";";
146                 }
147                 // restore.. 
148                 ts.cursor = cu;
149                 continue;
150             }
151             
152             // any more??
153             // a = function(....) { } 
154                print("1:" +ts.lookTok(1).toString());
155                  print("2:" +ts.lookTok(2).toString());
156             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).name == 'FUNCTION' ) {
157                 // freeze time.. 
158                 print("compress white : got a = function ");
159                 var cu = ts.cursor;
160                 ts.balance("(");
161                 if (!ts.balance("(") ){
162                     ts.dump(cu-40, cu);
163                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
164                     ts.dump(cu, cu+40);
165                     
166                     throw "could not find end lbrace!!!";
167                 }
168                 if (!ts.balance("{") ){
169                     ts.dump(cu-40, cu);
170                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
171                     ts.dump(cu, cu+40);
172                     
173                     throw "could not find end lbrace!!!";
174                 }
175                 // if next is not ';' -> make it so...
176                 print(ts.lookTok(1).toString());
177                 print(ts.lookTok(1,true).toString());
178                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
179                     ts.look(0).outData = ts.look(0).data +";";
180                 }
181                 // restore.. 
182                 ts.cursor = cu;
183                 continue;
184             }
185             
186         }
187         
188         
189         
190          
191         //println("got Token: " + tok.type);
192         
193         
194         
195         switch(tok.data.toUpperCase()) {
196             // things that need space appending
197             case "FUNCTION":
198             case "BREAK":
199             case "CONTINUE":
200                 // if next item is a identifier..
201                 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
202                    tok.outData =  tok.data + " ";
203                 }
204                 continue;
205                 
206                 
207             case "RETURN": // if next item is not a semi; (or }
208                 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
209                     continue;
210                 }
211                 tok.outData =  tok.data + " ";
212                 
213                 continue;
214             
215                 
216             case "ELSE": // if next item is not a semi; (or }
217                 if (!ts.lookTok(1).name == "IF") {
218                     continue;
219                 }
220                 
221                 tok.outData =  tok.data + " ";
222                 continue;
223             
224             case "++": // if previous was a plus or next is a + add a space..
225             case "--": // if previous was a - or next is a - add a space..
226             
227                 var p = (tok.data == "--" ? '-' : '+'); 
228             
229                 if (ts.lookTok(1).data == p) {
230                     tok.outData =  tok.data + " ";
231                 }
232                 if (ts.lookTok(-1).data == p) {
233                     tok.outData =  " " +  tok.data;
234                     
235                 }
236                 continue;
237             
238             case "IN": // before and after?? 
239             case "INSTANCEOF":
240                 
241                 tok.outData = " " + tok.data + " ";
242                 continue;
243             
244             case "VAR": // always after..
245             case "NEW":
246             case "DELETE":
247             case "THROW":
248             case "CASE":
249             
250             case "VOID":
251                 tok.outData =  tok.data + " ";
252                 
253                 continue
254                 
255             case "TYPEOF": // what about typeof(
256                 if (ts.lookTok(1).data != '(') {
257                     tok.outData =  tok.data + " ";
258                 }
259                 continue;
260              case ";":
261                 //remove semicolon before brace -- 
262                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
263                 //    tok.outData = '';
264                // }
265                 continue;
266            
267             default:
268                 continue;
269         }
270     }
271     
272     ts.rewind();
273     
274     // NOW OUTPUT THE THING.
275     //var f = new File(minfile, File.NEW);
276     
277     var out = '';
278     var outoff = 0;
279     out.length = ts.slen; // prealloc.
280     out = '';
281     while (true) {
282         var tok = ts.nextTok();
283            
284         if (!tok) {
285             break;
286         }
287         
288         
289         if (tok.type == "NAME"  && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
290             //f.write(tok.identifier.mungedValue);
291             out += tok.identifier.mungedValue;
292             continue;
293         }
294         
295         // at this point we can apply a text translation kit...
296         
297         if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
298             if (packer && packer.stringHandler) {
299                 out += packer.stringHandler(tok);
300                 continue;
301             }
302         }
303      
304         out += tok.outData !== false ? tok.outData : tok.data;
305         
306         if ((tok.outData == ';') && (out.length - outoff > 255)) {
307             outoff = out.length;
308             out += "\n";
309         }
310     }
311     //f.close();
312     /*
313     // remove the last ';' !!!
314     if (out.substring(out.length-1) == ';') {
315         return out.substring(0,out.length-1);
316        }
317     */
318     return out;
319     
320 }
321     
322