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