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