JSDOC/CompressWhite.vala
[gnome.introspection-doc-generator] / JSDOC / CompressWhite.vala
1  
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 namespace JSDOC 
11 {
12         public errordomain CompressWhiteError {
13             BRACE
14     }
15          
16         public string CompressWhite (TokenStream ts, Packer packer, bool keepWhite) // throws CompressWhiteError,TokenStreamError
17         {
18                 //keepWhite = keepWhite || false;
19                 ts.rewind();
20                 //var str = File.read(fn);
21                 var rep_var = 1;
22         
23         
24         
25                 while (true) {
26                         var tok = ts.next();
27                         if (tok == null) {
28                             break;
29                         }
30                         if (tok.type == "WHIT") {
31                            
32                             continue;
33                             //if (tok._isDoc) {
34                             //    continue;
35                             //}
36                             // just spaces, not \n!
37                             //if (tok.data.indexOf("\n") < 0) {
38                             //    continue;
39                            // }
40                             
41                             
42                         }
43                         if (tok.data == "}")  {
44                             
45                             if (ts.lookTok(0).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
46                                 ts.look(0,true).outData = ts.look(0,true).data+"\n";
47                             }
48                             // restore.. 
49                             
50                             continue;
51                         }
52                         // add semi-colon's where linebreaks are used... - not foolproof yet.!
53                         if (tok.type == "NAME")  {
54                             //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
55                             // a = new function() {} 
56                             if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "NEW"  && 
57                                 ts.lookTok(3).name == "FUNCTION") {
58                                 // freeze time.. 
59                                 var cu = ts.cursor;
60                                 
61                                 ts.balance("(");
62                                 
63                                 
64                                 ts.balance("{");
65                                 // if next is not ';' -> make it so...
66                                 
67                                 if (ts.lookTok(1).data != ";"  && ts.lookTok(1).data != "}" && ts.lookTok(1).name == "NEWLINE") {
68                                     ts.look(0,true).outData = ts.lookTok(0).data +";";
69                                 }
70                                 // restore.. 
71                                 ts.cursor = cu;
72                                 continue;
73                             }
74                             // a = function() { ... -- add a semi colon a tthe end if not one there..
75                                
76                             if (ts.lookTok(1).data == "=" &&  ts.lookTok(2).name == "FUNCTION") {
77                                 // freeze time.. 
78                                 //println("got = function() ");
79                                 tok = ts.next();
80                                 tok = ts.next();
81                                 tok = ts.next();
82
83                                 var cu = ts.cursor;
84                                 ts.cursor+=2; // move two along, and balance...
85                                 
86                                 if (tok.data != "(" || ts.balance("(").size < 1 ){
87                                 
88                                         print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
89                                     ts.dump(cu-40, cu+2);
90                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
91                                     ts.dump(cu+2, cu+40);
92                                     
93                                     throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
94                                 }
95                                 tok = ts.lookTok(0);
96                                 //print("AFTER BALANCE (");
97                                 //ts.dump(cu, ts.cursor);
98                                 //ts.cursor--; // cursor at the (
99                                 if (tok.data != "{" || ts.balance("{").size < 1 ){
100
101                                     ts.dump(cu-40, cu);
102                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
103                                     ts.dump(cu, cu+40);
104                                     
105                                     throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
106                                 }
107                                 //print('FN: '+ts.tokens[cu].toString());
108                                 //print('F1: '+ts.lookTok(1).toString());
109                                 //print('F2: '+ts.look(1,true).toString());
110                                 
111                                 // if next is not ';' -> make it so...
112                                 // although this var a=function(){},v,c; causes 
113                                 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
114                                     
115                                     ts.look(0,true).outData = ts.look(0,true).data+";";
116                                    // print("ADDING SEMI: " + ts.look(0).toString());
117                                     //ts.dump(cu, ts.cursor+2);
118                                 }
119                                 
120                                  //ts.dump(cu, ts.cursor+2);
121                                 // restore.. 
122                                 ts.cursor = cu;
123                                 continue;
124                             }
125                             // next item is a name..
126                             if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) &&  ts.look(1,true).name == "NEWLINE") {
127                                 // preserve linebraek
128                                 ts.look(0,true).outData = ts.look(0,true).data+"\n";
129                             }
130                             // method call followed by name..
131                             if (ts.lookTok(1).data == "(")  {
132                                 var cu = ts.cursor;
133                                 
134                                 ts.balance("(");
135                                  // although this var a=function(){},v,c; causes 
136                                 
137                                 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
138                                 
139                                     ts.look(0,true).outData = ts.look(0,true).data+"\n";
140                                 }
141                                 // restore.. 
142                                 ts.cursor = cu;
143                                 continue;
144                             }
145                             
146                             
147                             // function a () { ... };
148                                 /*
149                             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
150                                 // freeze time.. 
151                                 //println("got = function() ");
152                                 var cu = ts.cursor;
153                                 
154                                 ts.balance("lparen");
155                                 ts.balance("lbrace");
156                                 // if next is not ';' -> make it so...
157                                 // although this var a=function(){},v,c; causes 
158                                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
159                                     ts.cur().outData = ts.cur().data+";";
160                                 }
161                                 // restore.. 
162                                 ts.cursor = cu;
163                                 continue;
164                             }
165                             */
166                             
167                             // a = { ....
168                                 
169                             if (ts.lookTok(1).data == "=" &&  ts.lookTok(2).data == "{") {
170                                 // freeze time.. 
171                                 //println("----------*** 3 *** --------------");
172                                 var cu = ts.cursor;
173                                 
174                                 if (ts.balance("{").size < 1 ){
175
176                                     ts.dump(cu-40, cu);
177                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
178                                     ts.dump(cu, cu+40);
179                                     
180                                     throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
181                                 }
182                                 // if next is not ';' -> make it so...
183                                 
184                                 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
185                                     ts.look(0,true).outData = ts.look(0,true).data +";";
186                                 }
187                                 // restore.. 
188                                 ts.cursor = cu;
189                                 continue;
190                             }
191                             
192                             // any more??
193                             // a = function(....) { } 
194                           
195                         }
196                         
197                         
198                         
199                          
200                         //println("got Token: " + tok.type);
201                         
202                         
203                         
204                         switch(tok.data.up()) {
205                             // things that need space appending
206                             case "FUNCTION":
207                             case "BREAK":
208                             case "CONTINUE":
209                                 // if next item is a identifier..
210                                 if (ts.lookTok(1).type == "NAME" || Regex.match_simple("^[a-z]+$", ts.lookTok(1).data, GLib.RegexCompileFlags.CASELESS) ) { // as include is a keyword for us!!
211                                    tok.outData =  tok.data + " ";
212                                 }
213                                 continue;
214                                 
215                                 
216                             case "RETURN": // if next item is not a semi; (or }
217                                 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
218                                     continue;
219                                 }
220                                 tok.outData =  tok.data + " ";
221                                 
222                                 continue;
223                             
224                                 
225                             case "ELSE": // if next item is not a semi; (or }
226                                 if (ts.lookTok(1).name != "IF") {
227                                     continue;
228                                 }
229                                 // add a space if next element is 'IF'
230                                 tok.outData =  tok.data + " ";
231                                 continue;
232                             
233                             case "++": // if previous was a plus or next is a + add a space..
234                             case "--": // if previous was a - or next is a - add a space..
235                             
236                                 var p = (tok.data == "--" ? "-" : "+"); 
237                             
238                                 if (ts.lookTok(1).data == p) {
239                                     tok.outData =  tok.data + " ";
240                                 }
241                                 if (ts.lookTok(-1).data == p) {
242                                     tok.outData =  " " +  tok.data;
243                                     
244                                 }
245                                 continue;
246                             
247                             case "IN": // before and after?? 
248                             case "INSTANCEOF":
249                                 
250                                 tok.outData = " " + tok.data + " ";
251                                 continue;
252                             
253                             case "VAR": // always after..
254                             case "NEW":
255                             case "DELETE":
256                             case "THROW":
257                             case "CASE":
258                             case "CONST":
259                             case "VOID":
260                                 tok.outData =  tok.data + " ";
261                                 
262                                 continue;
263                                 
264                             case "TYPEOF": // what about typeof(
265                                 if (ts.lookTok(1).data != "(") {
266                                     tok.outData =  tok.data + " ";
267                                 }
268                                 continue;
269                              case ";":
270                                 //remove semicolon before brace -- 
271                                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
272                                 //    tok.outData = '';
273                                // }
274                                 continue;
275                            
276                             default:
277                                 continue;
278                         }
279                 }
280         
281                 ts.rewind();
282         
283                 // NOW OUTPUT THE THING.
284                 //var f = new File(minfile, File.NEW);
285         
286                 var outstr = "";
287                 var outoff = 0;
288                 //try { out.length = ts.slen; } catch (e) {} // prealloc.
289         
290
291                 Token tok;
292                 while (true) {
293                         
294                         tok = keepWhite ? ts.next() : ts.nextTok();
295                         
296                         if (tok == null) {
297                             break;
298                         }
299                         if (tok.type == "COMM") {
300                             tok.outData = "\n";
301                         }
302                         
303                         ///print(tok.type + ':' + tok.data);
304                         
305                         if (tok.type == "NAME"  &&
306                                  tok.identifier != null  &&
307                             tok.identifier.mungedValue.length > 0) {
308                             //f.write(tok.identifier.mungedValue);
309                             //print("MUNGED: " + tok.identifier.mungedValue);
310                             outstr += tok.identifier.mungedValue;
311                             continue;
312                         }
313                         
314                         // at this point we can apply a text translation kit...
315                         // NOT SUPPORTED..
316                         //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
317                         //    if (packer && packer.stringHandler) {
318                         //        outstr += packer.stringHandler(tok);
319                         //        continue;
320                         //    }
321                         //}
322                  
323                         outstr += tok.outData != "" ? tok.outData : tok.data;
324                         
325                         if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
326                             outoff = outstr.length;
327                             outstr += "\n";
328                         }
329                 }
330                 //f.close();
331                 /*
332                 // remove the last ';' !!!
333                 if (out.substring(out.length-1) == ';') {
334                         return out.substring(0,out.length-1);
335                    }
336                 */
337                 return outstr;
338         
339         }
340          
341 }