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