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