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