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