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