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