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(ts.lookTok(2).toString());
155             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).name == 'FUNCTION' ) {
156                 // freeze time.. 
157                 print("compress white : got a = function ");
158                 var cu = ts.cursor;
159                 ts.balance("(");
160                 if (!ts.balance("(") ){
161                     ts.dump(cu-40, cu);
162                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
163                     ts.dump(cu, cu+40);
164                     
165                     throw "could not find end lbrace!!!";
166                 }
167                 if (!ts.balance("{") ){
168                     ts.dump(cu-40, cu);
169                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
170                     ts.dump(cu, cu+40);
171                     
172                     throw "could not find end lbrace!!!";
173                 }
174                 // if next is not ';' -> make it so...
175                 print(ts.lookTok(1).toString());
176                 print(ts.lookTok(1,true).toString());
177                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
178                     ts.look(0).outData = ts.look(0).data +";";
179                 }
180                 // restore.. 
181                 ts.cursor = cu;
182                 continue;
183             }
184             
185         }
186         
187         
188         
189          
190         //println("got Token: " + tok.type);
191         
192         
193         
194         switch(tok.data.toUpperCase()) {
195             // things that need space appending
196             case "FUNCTION":
197             case "BREAK":
198             case "CONTINUE":
199                 // if next item is a identifier..
200                 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
201                    tok.outData =  tok.data + " ";
202                 }
203                 continue;
204                 
205                 
206             case "RETURN": // if next item is not a semi; (or }
207                 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
208                     continue;
209                 }
210                 tok.outData =  tok.data + " ";
211                 
212                 continue;
213             
214                 
215             case "ELSE": // if next item is not a semi; (or }
216                 if (!ts.lookTok(1).name == "IF") {
217                     continue;
218                 }
219                 
220                 tok.outData =  tok.data + " ";
221                 continue;
222             
223             case "++": // if previous was a plus or next is a + add a space..
224             case "--": // if previous was a - or next is a - add a space..
225             
226                 var p = (tok.data == "--" ? '-' : '+'); 
227             
228                 if (ts.lookTok(1).data == p) {
229                     tok.outData =  tok.data + " ";
230                 }
231                 if (ts.lookTok(-1).data == p) {
232                     tok.outData =  " " +  tok.data;
233                     
234                 }
235                 continue;
236             
237             case "IN": // before and after?? 
238             case "INSTANCEOF":
239                 
240                 tok.outData = " " + tok.data + " ";
241                 continue;
242             
243             case "VAR": // always after..
244             case "NEW":
245             case "DELETE":
246             case "THROW":
247             case "CASE":
248             
249             case "VOID":
250                 tok.outData =  tok.data + " ";
251                 
252                 continue
253                 
254             case "TYPEOF": // what about typeof(
255                 if (ts.lookTok(1).data != '(') {
256                     tok.outData =  tok.data + " ";
257                 }
258                 continue;
259              case ";":
260                 //remove semicolon before brace -- 
261                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
262                 //    tok.outData = '';
263                // }
264                 continue;
265            
266             default:
267                 continue;
268         }
269     }
270     
271     ts.rewind();
272     
273     // NOW OUTPUT THE THING.
274     //var f = new File(minfile, File.NEW);
275     
276     var out = '';
277     var outoff = 0;
278     out.length = ts.slen; // prealloc.
279     out = '';
280     while (true) {
281         var tok = ts.nextTok();
282            
283         if (!tok) {
284             break;
285         }
286         
287         
288         if (tok.type == "NAME"  && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
289             //f.write(tok.identifier.mungedValue);
290             out += tok.identifier.mungedValue;
291             continue;
292         }
293         
294         // at this point we can apply a text translation kit...
295         
296         if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
297             if (packer && packer.stringHandler) {
298                 out += packer.stringHandler(tok);
299                 continue;
300             }
301         }
302      
303         out += tok.outData !== false ? tok.outData : tok.data;
304         
305         if ((tok.outData == ';') && (out.length - outoff > 255)) {
306             outoff = out.length;
307             out += "\n";
308         }
309     }
310     //f.close();
311     /*
312     // remove the last ';' !!!
313     if (out.substring(out.length-1) == ';') {
314         return out.substring(0,out.length-1);
315        }
316     */
317     return out;
318     
319 }
320     
321