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