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