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