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).isData(';') && !ts.lookTok(1).isData('}') && ts.lookTok(1,true).isLineBreak()) {
70                     ts.cur().outData = ts.look(0).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).data == '=' &&  ts.look(2).data == '{')) {
99                 // freeze time.. 
100                 //println("----------*** 3 *** --------------");
101                 var cu = ts.cursor;
102                 
103                 if (!ts.balance("{") ){
104                     throw "could not find end lbrace!!!";
105                 }
106                 // if next is not ';' -> make it so...
107
108                 if (!ts.look(1).data == ';' && ts.look(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
109                     ts.look(0).outData = ts.look(0).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.data.toUpperCase()) {
127             // things that need space appending
128             case "FUNCTION":
129             case "BREAK":
130             case "CONTINUE":
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 "RETURN": // if next item is not a semi; (or }
139                 if (ts.look(1).data == ';' || ts.look(1).data == '}') {
140                     continue;
141                 }
142                 tok.outData =  tok.data + " ";
143                 
144                 continue;
145             
146                 
147             case "ELSE": // 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 "++": // if previous was a plus or next is a + add a space..
156             case "--": // 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 "IN": // before and after?? 
170             case "INSTANCEOF":
171                 
172                 tok.outData = " " + tok.data + " ";
173                 continue;
174             
175             case "VAR": // always after..
176             case "NEW":
177             case "DELETE":
178             case "THROW":
179             case "CASE":
180             
181             case "VOID":
182                 tok.outData =  tok.data + " ";
183                 
184                 continue
185                 
186             case "TYPEOF": // what about typeof(
187                 if (ts.lookTok(1).data != '(') {
188                     tok.outData =  tok.data + " ";
189                 }
190                 continue;
191              case ";"
192                 //remove semicolon before brace -- 
193                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
194                 //    tok.outData = '';
195                // }
196                 continue;
197            
198             default:
199                 continue;
200         }
201     }
202     
203     ts.rewind();
204     
205     // NOW OUTPUT THE THING.
206     //var f = new File(minfile, File.NEW);
207     
208     var out = '';
209     var outoff = 0;
210     out.length = ts.slen; // prealloc.
211     out = '';
212     while (true) {
213         var tok = ts.next();
214         if (!tok) {
215             break;
216         }
217         if (tok._isWS) {
218             continue;
219         }
220         
221         
222         if (tok.isTypeN(Script.TOKidentifier) && tok.identifier && tok.identifier.mungedValue.length) {
223             //f.write(tok.identifier.mungedValue);
224             out +=tok.identifier.mungedValue;
225             continue;
226         }
227         
228         // at this point we can apply a text translation kit...
229         
230         if (tok.type == 'string') {
231             if (packer.stringHandler) {
232                 out += packer.stringHandler(tok);
233                 continue;
234             }
235         }
236         //f.write(tok.outData);
237         out += tok.outData;
238         
239         if ((tok.outData == ';') && (out.length - outoff > 255)) {
240             outoff = out.length;
241             out += '\n';
242         }
243     }
244     //f.close();
245     /*
246     // remove the last ';' !!!
247     if (out.substring(out.length-1) == ';') {
248         return out.substring(0,out.length-1);
249        }
250     */
251     return out;
252     
253 }
254     
255