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