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