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