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