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