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                  if (!ts.balance("(") ){
75                     ts.dump(cu-40, cu);
76                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
77                     ts.dump(cu, cu+40);
78                     
79                     throw "could not find end lbrace!!!";
80                 }
81                 //print("AFTER BALANCE (");
82                 //ts.dump(cu, ts.cursor);
83                 //ts.cursor--; // cursor at the (
84                 if (!ts.balance("{") ){
85                     ts.dump(cu-40, cu);
86                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
87                     ts.dump(cu, cu+40);
88                     
89                     throw "could not find end lbrace!!!";
90                 }
91                 //print('FN: '+ts.tokens[cu].toString());
92                 //print('F1: '+ts.lookTok(1).toString());
93                 //print('F2: '+ts.look(1,true).toString());
94                 
95                 // if next is not ';' -> make it so...
96                 // although this var a=function(){},v,c; causes 
97                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
98                     
99                     ts.look(0).outData = ts.look(0).data+";";
100                    // print("ADDING SEMI: " + ts.look(0).toString());
101                     //ts.dump(cu, ts.cursor+2);
102                 }
103                 
104                  //ts.dump(cu, ts.cursor+2);
105                 // restore.. 
106                 ts.cursor = cu;
107                 continue;
108             }
109             // next item is a name..
110             if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) &&  ts.look(1,true).name == "NEWLINE") {
111                 // preserve linebraek
112                 ts.look(0).outData = ts.look(0).data+"\n";
113             }
114             // method call followed by name..
115             if (ts.lookTok(1).data == "(")  {
116                 var cu = ts.cursor;
117                 
118                 ts.balance("(");
119                  // although this var a=function(){},v,c; causes 
120                 
121                 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
122                 
123                     ts.look(0).outData = ts.look(0).data+"\n";
124                 }
125                 // restore.. 
126                 ts.cursor = cu;
127                 continue;
128             }
129             
130             
131             // function a () { ... };
132                 /*
133             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
134                 // freeze time.. 
135                 //println("got = function() ");
136                 var cu = ts.cursor;
137                 
138                 ts.balance("lparen");
139                 ts.balance("lbrace");
140                 // if next is not ';' -> make it so...
141                 // although this var a=function(){},v,c; causes 
142                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
143                     ts.cur().outData = ts.cur().data+";";
144                 }
145                 // restore.. 
146                 ts.cursor = cu;
147                 continue;
148             }
149             */
150             
151             // a = { ....
152                 
153             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).data == '{') {
154                 // freeze time.. 
155                 //println("----------*** 3 *** --------------");
156                 var cu = ts.cursor;
157                 
158                 if (!ts.balance("{") ){
159                     ts.dump(cu-40, cu);
160                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
161                     ts.dump(cu, cu+40);
162                     
163                     throw "could not find end lbrace!!!";
164                 }
165                 // if next is not ';' -> make it so...
166                 
167                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
168                     ts.look(0).outData = ts.look(0).data +";";
169                 }
170                 // restore.. 
171                 ts.cursor = cu;
172                 continue;
173             }
174             
175             // any more??
176             // a = function(....) { } 
177           
178         }
179         
180         
181         
182          
183         //println("got Token: " + tok.type);
184         
185         
186         
187         switch(tok.data.toUpperCase()) {
188             // things that need space appending
189             case "FUNCTION":
190             case "BREAK":
191             case "CONTINUE":
192                 // if next item is a identifier..
193                 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
194                    tok.outData =  tok.data + " ";
195                 }
196                 continue;
197                 
198                 
199             case "RETURN": // if next item is not a semi; (or }
200                 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
201                     continue;
202                 }
203                 tok.outData =  tok.data + " ";
204                 
205                 continue;
206             
207                 
208             case "ELSE": // if next item is not a semi; (or }
209                 if (!ts.lookTok(1).name == "IF") {
210                     continue;
211                 }
212                 
213                 tok.outData =  tok.data + " ";
214                 continue;
215             
216             case "++": // if previous was a plus or next is a + add a space..
217             case "--": // if previous was a - or next is a - add a space..
218             
219                 var p = (tok.data == "--" ? '-' : '+'); 
220             
221                 if (ts.lookTok(1).data == p) {
222                     tok.outData =  tok.data + " ";
223                 }
224                 if (ts.lookTok(-1).data == p) {
225                     tok.outData =  " " +  tok.data;
226                     
227                 }
228                 continue;
229             
230             case "IN": // before and after?? 
231             case "INSTANCEOF":
232                 
233                 tok.outData = " " + tok.data + " ";
234                 continue;
235             
236             case "VAR": // always after..
237             case "NEW":
238             case "DELETE":
239             case "THROW":
240             case "CASE":
241             
242             case "VOID":
243                 tok.outData =  tok.data + " ";
244                 
245                 continue
246                 
247             case "TYPEOF": // what about typeof(
248                 if (ts.lookTok(1).data != '(') {
249                     tok.outData =  tok.data + " ";
250                 }
251                 continue;
252              case ";":
253                 //remove semicolon before brace -- 
254                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
255                 //    tok.outData = '';
256                // }
257                 continue;
258            
259             default:
260                 continue;
261         }
262     }
263     
264     ts.rewind();
265     
266     // NOW OUTPUT THE THING.
267     //var f = new File(minfile, File.NEW);
268     
269     var out = '';
270     var outoff = 0;
271     out.length = ts.slen; // prealloc.
272     out = '';
273     while (true) {
274         var tok = ts.nextTok();
275            
276         if (!tok) {
277             break;
278         }
279         
280         
281         if (tok.type == "NAME"  && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
282             //f.write(tok.identifier.mungedValue);
283             out += tok.identifier.mungedValue;
284             continue;
285         }
286         
287         // at this point we can apply a text translation kit...
288         
289         if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
290             if (packer && packer.stringHandler) {
291                 out += packer.stringHandler(tok);
292                 continue;
293             }
294         }
295      
296         out += tok.outData !== false ? tok.outData : tok.data;
297         
298         if ((tok.outData == ';') && (out.length - outoff > 255)) {
299             outoff = out.length;
300             out += "\n";
301         }
302     }
303     //f.close();
304     /*
305     // remove the last ';' !!!
306     if (out.substring(out.length-1) == ';') {
307         return out.substring(0,out.length-1);
308        }
309     */
310     return out;
311     
312 }
313     
314