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