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