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.look(3).name == 'FUNCTION') {
42                 // freeze time.. 
43                 var cu = ts.cursor;
44                 
45                 ts.balance("lparen");
46                 
47                 
48                 ts.balance("lbrace");
49                 // if next is not ';' -> make it so...
50                 
51                 if (!ts.look(1).isTypeN(Script.TOKsemicolon) && !ts.look(1).isTypeN(Script.TOKrbrace) && ts.look(1,true).isLineBreak()) {
52                     ts.cur().outData = ts.cur().data +";";
53                 }
54                 // restore.. 
55                 ts.cursor = cu;
56                 continue;
57             }
58             // a = function() { ...
59                
60             if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKfunction)) {
61                 // freeze time.. 
62                 //println("got = function() ");
63                 var cu = ts.cursor;
64                 
65                 ts.balance("lparen");
66                 ts.balance("lbrace");
67                 // if next is not ';' -> make it so...
68                 // although this var a=function(){},v,c; causes 
69                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
70                     ts.cur().outData = ts.cur().data+";";
71                 }
72                 // restore.. 
73                 ts.cursor = cu;
74                 continue;
75             }
76             // function a () { ... };
77                 /*
78             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
79                 // freeze time.. 
80                 //println("got = function() ");
81                 var cu = ts.cursor;
82                 
83                 ts.balance("lparen");
84                 ts.balance("lbrace");
85                 // if next is not ';' -> make it so...
86                 // although this var a=function(){},v,c; causes 
87                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
88                     ts.cur().outData = ts.cur().data+";";
89                 }
90                 // restore.. 
91                 ts.cursor = cu;
92                 continue;
93             }
94             */
95             
96             // a = { ....
97                 
98             if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKlbrace)) {
99                 // freeze time.. 
100                 //println("----------*** 3 *** --------------");
101                 var cu = ts.cursor;
102                 
103                 if (!ts.balance("lbrace") ){
104                     throw "could not find end lbrace!!!";
105                 }
106                 // if next is not ';' -> make it so...
107
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             // any more??
117         }
118         
119         
120         
121          
122         //println("got Token: " + tok.type);
123         
124         
125         
126         switch(tok.tokN) {
127             // things that need space appending
128             case Script.TOKfunction:
129             case Script.TOKbreak:
130             case Script.TOKcontinue:
131                 // if next item is a identifier..
132                 if (ts.look(1).isTypeN(Script.TOKidentifier) || ts.look(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
133                    tok.outData =  tok.data + " ";
134                 }
135                 continue;
136                 
137                 
138             case Script.TOKreturn: // if next item is not a semi; (or }
139                 if (ts.look(1).isData(';') || ts.look(1).isData('}')) {
140                     continue;
141                 }
142                 tok.outData =  tok.data + " ";
143                 
144                 continue;
145             
146                 
147             case Script.TOKelse: // if next item is not a semi; (or }
148                 if (!ts.look(1).isTypeN(Script.TOKif)) {
149                     continue;
150                 }
151                 
152                 tok.outData =  tok.data + " ";
153                 continue;
154             
155             case Script.TOKplusplus: // if previous was a plus or next is a + add a space..
156             case Script.TOKminusminus: // if previous was a - or next is a - add a space..
157             
158                 var p = (Script.TOKminusminus == tok.tokN ? '-' : '+'); 
159             
160                 if (ts.look(1).data == p) {
161                     tok.outData =  tok.data + " ";
162                 }
163                 if (ts.look(-1).data == p) {
164                     tok.outData =  " " +  tok.data;
165                     
166                 }
167                 continue;
168             
169             case Script.TOKin: // before and after?? 
170             case Script.TOKinstanceof:
171                 
172                 tok.outData = " " + tok.data + " ";
173                 continue;
174             
175             case Script.TOKvar: // always after..
176             case Script.TOKnew:
177             case Script.TOKdelete:
178             case Script.TOKthrow:
179             case Script.TOKnew:
180             case Script.TOKcase:
181             case Script.TOKtypeof:
182             case Script.TOKvoid:
183                 tok.outData =  tok.data + " ";
184                 
185                 continue
186              case Script.TOKsemicolon:
187                 //remove semicolon before brace -- 
188                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
189                 //    tok.outData = '';
190                // }
191                 continue;
192            
193             default:
194                 continue;
195         }
196     }
197     
198     ts.rewind();
199     
200     
201     //var f = new File(minfile, File.NEW);
202     
203     var out = '';
204     var outoff = 0;
205     out.length = ts.slen; // prealloc.
206     out = '';
207     while (true) {
208         var tok = ts.next();
209         if (!tok) {
210             break;
211         }
212         if (tok._isWS) {
213             continue;
214         }
215         
216         
217         if (tok.isTypeN(Script.TOKidentifier) && tok.identifier && tok.identifier.mungedValue.length) {
218             //f.write(tok.identifier.mungedValue);
219             out +=tok.identifier.mungedValue;
220             continue;
221         }
222         
223         // at this point we can apply a text translation kit...
224         
225         if (tok.type == 'string') {
226             if (packer.stringHandler) {
227                 out += packer.stringHandler(tok);
228                 continue;
229             }
230         }
231         //f.write(tok.outData);
232         out += tok.outData;
233         
234         if ((tok.outData == ';') && (out.length - outoff > 255)) {
235             outoff = out.length;
236             out += '\n';
237         }
238     }
239     //f.close();
240     /*
241     // remove the last ';' !!!
242     if (out.substring(out.length-1) == ';') {
243         return out.substring(0,out.length-1);
244        }
245     */
246     return out;
247     
248 }
249     
250