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