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