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