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.nextTok();
80                                 tok = ts.nextTok();
81                                 tok = ts.nextTok();
82                                 //tok = ts.next();
83                                  var cu = ts.cursor;
84                                         print("CUR: %s\n", ts.lookTok(0).asString());
85                                         print("NEXT = should be brace: %s\n", ts.lookTok(1).asString());
86                                          
87                                
88                                   
89                                 if (tok.data != "(" || ts.balance("(").size < 1 ){
90                                         print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
91                                     ts.dump(cu-40, cu+2);
92                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
93                                     ts.dump(cu+2, cu+40);
94                                     
95                                     throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
96                                 }
97                                 tok = ts.lookTok(0);
98                                 
99                                 cu = ts.cursor; // set the cursor to here.. so the next bit of the code will check inside the method.
100                                 
101                                 //print("AFTER BALANCE (");
102                                 //ts.dump(cu, ts.cursor);
103                                 //ts.cursor--; // cursor at the (
104                                 if (tok.data != "{" || ts.balance("{").size < 1 ){
105
106                                     ts.dump(cu-40, cu);
107                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
108                                     ts.dump(cu, cu+40);
109                                     
110                                     throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
111                                 }
112                                 //print('FN: '+ts.tokens[cu].toString());
113                                 //print('F1: '+ts.lookTok(1).toString());
114                                 //print('F2: '+ts.look(1,true).toString());
115                                 
116                                 // if next is not ';' -> make it so...
117                                 // although this var a=function(){},v,c; causes 
118                                 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
119                                     
120                                     ts.look(0,true).outData = ts.look(0,true).data+";";
121                                    // print("ADDING SEMI: " + ts.look(0).toString());
122                                     //ts.dump(cu, ts.cursor+2);
123                                 }
124                                 
125                                  //ts.dump(cu, ts.cursor+2);
126                                 // restore.. 
127                                 ts.cursor = cu;
128                                 continue;
129                             }
130                             // next item is a name..
131                             if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) &&  ts.look(1,true).name == "NEWLINE") {
132                                 // preserve linebraek
133                                 ts.look(0,true).outData = ts.look(0,true).data+"\n";
134                             }
135                             // method call followed by name..
136                             if (ts.lookTok(1).data == "(")  {
137                                 var cu = ts.cursor;
138                                 
139                                 ts.balance("(");
140                                  // although this var a=function(){},v,c; causes 
141                                 
142                                 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
143                                 
144                                     ts.look(0,true).outData = ts.look(0,true).data+"\n";
145                                 }
146                                 // restore.. 
147                                 ts.cursor = cu;
148                                 continue;
149                             }
150                             
151                             
152                             // function a () { ... };
153                                 /*
154                             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
155                                 // freeze time.. 
156                                 //println("got = function() ");
157                                 var cu = ts.cursor;
158                                 
159                                 ts.balance("lparen");
160                                 ts.balance("lbrace");
161                                 // if next is not ';' -> make it so...
162                                 // although this var a=function(){},v,c; causes 
163                                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
164                                     ts.cur().outData = ts.cur().data+";";
165                                 }
166                                 // restore.. 
167                                 ts.cursor = cu;
168                                 continue;
169                             }
170                             */
171                             
172                             // a = { ....
173                                 
174                             if (ts.lookTok(1).data == "=" &&  ts.lookTok(2).data == "{") {
175                                 // freeze time.. 
176                                 //println("----------*** 3 *** --------------");
177                                 var cu = ts.cursor;
178                                 
179                                 if (ts.balance("{").size < 1 ){
180
181                                     ts.dump(cu-40, cu);
182                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
183                                     ts.dump(cu, cu+40);
184                                     
185                                     throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
186                                 }
187                                 // if next is not ';' -> make it so...
188                                 
189                                 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
190                                     ts.look(0,true).outData = ts.look(0,true).data +";";
191                                 }
192                                 // restore.. 
193                                 ts.cursor = cu;
194                                 continue;
195                             }
196                             
197                             // any more??
198                             // a = function(....) { } 
199                           
200                         }
201                         
202                         
203                         
204                          
205                         //println("got Token: " + tok.type);
206                         
207                         
208                         
209                         switch(tok.data.up()) {
210                             // things that need space appending
211                             case "FUNCTION":
212                             case "BREAK":
213                             case "CONTINUE":
214                                 // if next item is a identifier..
215                                 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!!
216                                    tok.outData =  tok.data + " ";
217                                 }
218                                 continue;
219                                 
220                                 
221                             case "RETURN": // if next item is not a semi; (or }
222                                 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
223                                     continue;
224                                 }
225                                 tok.outData =  tok.data + " ";
226                                 
227                                 continue;
228                             
229                                 
230                             case "ELSE": // if next item is not a semi; (or }
231                                 if (ts.lookTok(1).name != "IF") {
232                                     continue;
233                                 }
234                                 // add a space if next element is 'IF'
235                                 tok.outData =  tok.data + " ";
236                                 continue;
237                             
238                             case "++": // if previous was a plus or next is a + add a space..
239                             case "--": // if previous was a - or next is a - add a space..
240                             
241                                 var p = (tok.data == "--" ? "-" : "+"); 
242                             
243                                 if (ts.lookTok(1).data == p) {
244                                     tok.outData =  tok.data + " ";
245                                 }
246                                 if (ts.lookTok(-1).data == p) {
247                                     tok.outData =  " " +  tok.data;
248                                     
249                                 }
250                                 continue;
251                             
252                             case "IN": // before and after?? 
253                             case "INSTANCEOF":
254                                 
255                                 tok.outData = " " + tok.data + " ";
256                                 continue;
257                             
258                             case "VAR": // always after..
259                             case "NEW":
260                             case "DELETE":
261                             case "THROW":
262                             case "CASE":
263                             case "CONST":
264                             case "VOID":
265                                 tok.outData =  tok.data + " ";
266                                 
267                                 continue;
268                                 
269                             case "TYPEOF": // what about typeof(
270                                 if (ts.lookTok(1).data != "(") {
271                                     tok.outData =  tok.data + " ";
272                                 }
273                                 continue;
274                              case ";":
275                                 //remove semicolon before brace -- 
276                                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
277                                 //    tok.outData = '';
278                                // }
279                                 continue;
280                            
281                             default:
282                                 continue;
283                         }
284                 }
285         
286                 ts.rewind();
287         
288                 // NOW OUTPUT THE THING.
289                 //var f = new File(minfile, File.NEW);
290         
291                 var outstr = "";
292                 var outoff = 0;
293                 //try { out.length = ts.slen; } catch (e) {} // prealloc.
294         
295
296                 Token tok;
297                 while (true) {
298                         
299                         tok = keepWhite ? ts.next() : ts.nextTok();
300                         
301                         if (tok == null) {
302                             break;
303                         }
304                         if (tok.type == "COMM") {
305                             tok.outData = "\n";
306                         }
307                         
308                         ///print(tok.type + ':' + tok.data);
309                         
310                         if (tok.type == "NAME"  &&
311                                  tok.identifier != null  &&
312                             tok.identifier.mungedValue.length > 0) {
313                             //f.write(tok.identifier.mungedValue);
314                             //print("MUNGED: " + tok.identifier.mungedValue);
315                             outstr += tok.identifier.mungedValue;
316                             continue;
317                         }
318                         
319                         // at this point we can apply a text translation kit...
320                         // NOT SUPPORTED..
321                         //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
322                         //    if (packer && packer.stringHandler) {
323                         //        outstr += packer.stringHandler(tok);
324                         //        continue;
325                         //    }
326                         //}
327                  
328                         outstr += tok.outData != "" ? tok.outData : tok.data;
329                         
330                         if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
331                             outoff = outstr.length;
332                             outstr += "\n";
333                         }
334                 }
335                 //f.close();
336                 /*
337                 // remove the last ';' !!!
338                 if (out.substring(out.length-1) == ';') {
339                         return out.substring(0,out.length-1);
340                    }
341                 */
342                 return outstr;
343         
344         }
345          
346 }