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