sync fixes
[app.jsdoc] / JSDOC / CompressWhite.js
1  // <script type="text/javascript">
2 /**
3
4  * FIXME = need to class'ify this..
5  *
6  * pack a javascript file, and return a shorter version!
7  * 
8  * a bit picky at present with ; and crlf reading...
9  *
10  * 
11  * @scope JSDOC.CompressWhite
12  * @function
13  * @arg  {TokenStream} ts 
14  * @arg {Packer} packer
15  */
16  
17 CompressWhite =  function (ts, packer, keepWhite)
18 {
19     keepWhite = keepWhite || false;
20     ts.rewind();
21     //var str = File.read(fn);
22     var rep_var = 1;
23     
24     while (true) {
25         var tok = ts.next();
26         if (!tok) {
27             break;
28         }
29         if (tok.type == "WHIT") {
30            
31             continue;
32             //if (tok._isDoc) {
33             //    continue;
34             //}
35             // just spaces, not \n!
36             //if (tok.data.indexOf("\n") < 0) {
37             //    continue;
38            // }
39             
40             
41         }
42         if (tok.data == "}")  {
43             
44             if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
45             
46                 ts.look(0).outData = ts.look(0).data+"\n";
47             }
48             // restore.. 
49             
50             continue;
51         }
52         // add semi-colon's where linebreaks are used... - not foolproof yet.!
53         if (tok.type == "NAME")  {
54             //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
55             // a = new function() {} 
56             if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == 'NEW'  && 
57                 ts.lookTok(3).name == 'FUNCTION') {
58                 // freeze time.. 
59                 var cu = ts.cursor;
60                 
61                 ts.balance("(");
62                 
63                 
64                 ts.balance("{");
65                 // if next is not ';' -> make it so...
66                 
67                 if (ts.lookTok(1).data != ';'  && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
68                     ts.look(0).outData = ts.cur().data +";";
69                 }
70                 // restore.. 
71                 ts.cursor = cu;
72                 continue;
73             }
74             // a = function() { ...
75                
76             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).name == "FUNCTION") {
77                 // freeze time.. 
78                 //println("got = function() ");
79                 var cu = ts.cursor;
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("AFTER BALANCE (");
89                 //ts.dump(cu, ts.cursor);
90                 //ts.cursor--; // cursor at the (
91                 if (!ts.balance("{") ){
92                     ts.dump(cu-40, cu);
93                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
94                     ts.dump(cu, cu+40);
95                     
96                     throw "could not find end lbrace!!!";
97                 }
98                 //print('FN: '+ts.tokens[cu].toString());
99                 //print('F1: '+ts.lookTok(1).toString());
100                 //print('F2: '+ts.look(1,true).toString());
101                 
102                 // if next is not ';' -> make it so...
103                 // although this var a=function(){},v,c; causes 
104                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
105                     
106                     ts.look(0).outData = ts.look(0).data+";";
107                    // print("ADDING SEMI: " + ts.look(0).toString());
108                     //ts.dump(cu, ts.cursor+2);
109                 }
110                 
111                  //ts.dump(cu, ts.cursor+2);
112                 // restore.. 
113                 ts.cursor = cu;
114                 continue;
115             }
116             // next item is a name..
117             if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) &&  ts.look(1,true).name == "NEWLINE") {
118                 // preserve linebraek
119                 ts.look(0).outData = ts.look(0).data+"\n";
120             }
121             // method call followed by name..
122             if (ts.lookTok(1).data == "(")  {
123                 var cu = ts.cursor;
124                 
125                 ts.balance("(");
126                  // although this var a=function(){},v,c; causes 
127                 
128                 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
129                 
130                     ts.look(0).outData = ts.look(0).data+"\n";
131                 }
132                 // restore.. 
133                 ts.cursor = cu;
134                 continue;
135             }
136             
137             
138             // function a () { ... };
139                 /*
140             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
141                 // freeze time.. 
142                 //println("got = function() ");
143                 var cu = ts.cursor;
144                 
145                 ts.balance("lparen");
146                 ts.balance("lbrace");
147                 // if next is not ';' -> make it so...
148                 // although this var a=function(){},v,c; causes 
149                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
150                     ts.cur().outData = ts.cur().data+";";
151                 }
152                 // restore.. 
153                 ts.cursor = cu;
154                 continue;
155             }
156             */
157             
158             // a = { ....
159                 
160             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).data == '{') {
161                 // freeze time.. 
162                 //println("----------*** 3 *** --------------");
163                 var cu = ts.cursor;
164                 
165                 if (!ts.balance("{") ){
166                     ts.dump(cu-40, cu);
167                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
168                     ts.dump(cu, cu+40);
169                     
170                     throw "could not find end lbrace!!!";
171                 }
172                 // if next is not ';' -> make it so...
173                 
174                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
175                     ts.look(0).outData = ts.look(0).data +";";
176                 }
177                 // restore.. 
178                 ts.cursor = cu;
179                 continue;
180             }
181             
182             // any more??
183             // a = function(....) { } 
184           
185         }
186         
187         
188         
189          
190         //println("got Token: " + tok.type);
191         
192         
193         
194         switch(tok.data.toUpperCase()) {
195             // things that need space appending
196             case "FUNCTION":
197             case "BREAK":
198             case "CONTINUE":
199                 // if next item is a identifier..
200                 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
201                    tok.outData =  tok.data + " ";
202                 }
203                 continue;
204                 
205                 
206             case "RETURN": // if next item is not a semi; (or }
207                 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
208                     continue;
209                 }
210                 tok.outData =  tok.data + " ";
211                 
212                 continue;
213             
214                 
215             case "ELSE": // if next item is not a semi; (or }
216                 if (!ts.lookTok(1).name == "IF") {
217                     continue;
218                 }
219                 
220                 tok.outData =  tok.data + " ";
221                 continue;
222             
223             case "++": // if previous was a plus or next is a + add a space..
224             case "--": // if previous was a - or next is a - add a space..
225             
226                 var p = (tok.data == "--" ? '-' : '+'); 
227             
228                 if (ts.lookTok(1).data == p) {
229                     tok.outData =  tok.data + " ";
230                 }
231                 if (ts.lookTok(-1).data == p) {
232                     tok.outData =  " " +  tok.data;
233                     
234                 }
235                 continue;
236             
237             case "IN": // before and after?? 
238             case "INSTANCEOF":
239                 
240                 tok.outData = " " + tok.data + " ";
241                 continue;
242             
243             case "VAR": // always after..
244             case "NEW":
245             case "DELETE":
246             case "THROW":
247             case "CASE":
248             
249             case "VOID":
250                 tok.outData =  tok.data + " ";
251                 
252                 continue
253                 
254             case "TYPEOF": // what about typeof(
255                 if (ts.lookTok(1).data != '(') {
256                     tok.outData =  tok.data + " ";
257                 }
258                 continue;
259              case ";":
260                 //remove semicolon before brace -- 
261                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
262                 //    tok.outData = '';
263                // }
264                 continue;
265            
266             default:
267                 continue;
268         }
269     }
270     
271     ts.rewind();
272     
273     // NOW OUTPUT THE THING.
274     //var f = new File(minfile, File.NEW);
275     
276     var out = '';
277     var outoff = 0;
278     out.length = ts.slen; // prealloc.
279     out = '';
280     var tok;
281     while (true) {
282         
283         tok = keepWhite ? ts.next() : ts.nextTok();
284         
285         if (!tok) {
286             break;
287         }
288         if (tok.type == "COMM") {
289             tok.outData = '\n';
290         }
291         
292         if (tok.type == "NAME"  && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
293             //f.write(tok.identifier.mungedValue);
294             out += tok.identifier.mungedValue;
295             continue;
296         }
297         
298         // at this point we can apply a text translation kit...
299         
300         if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
301             if (packer && packer.stringHandler) {
302                 out += packer.stringHandler(tok);
303                 continue;
304             }
305         }
306      
307         out += tok.outData !== false ? tok.outData : tok.data;
308         
309         if ((tok.outData == ';') && (out.length - outoff > 255)) {
310             outoff = out.length;
311             out += "\n";
312         }
313     }
314     //f.close();
315     /*
316     // remove the last ';' !!!
317     if (out.substring(out.length-1) == ';') {
318         return out.substring(0,out.length-1);
319        }
320     */
321     return out;
322     
323 }
324     
325