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