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         public class  CompressWhite : Object
16         {
17                 public CompressWhite (TokenStream ts, Packer packer, bool keepWhite) throws CompressWhiteError,TokenStreamError
18                 {
19                         //keepWhite = keepWhite || false;
20                         ts.rewind();
21                         //var str = File.read(fn);
22                         var rep_var = 1;
23                 
24                 
25                 
26                         while (true) {
27                                 var tok = ts.next();
28                                 if (tok == null) {
29                                     break;
30                                 }
31                                 if (tok.type == "WHIT") {
32                                    
33                                     continue;
34                                     //if (tok._isDoc) {
35                                     //    continue;
36                                     //}
37                                     // just spaces, not \n!
38                                     //if (tok.data.indexOf("\n") < 0) {
39                                     //    continue;
40                                    // }
41                                     
42                                     
43                                 }
44                                 if (tok.data == "}")  {
45                                     
46                                     if (ts.lookTok(0).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
47                                         ts.look(0,true).outData = ts.look(0,true).data+"\n";
48                                     }
49                                     // restore.. 
50                                     
51                                     continue;
52                                 }
53                                 // add semi-colon's where linebreaks are used... - not foolproof yet.!
54                                 if (tok.type == "NAME")  {
55                                     //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
56                                     // a = new function() {} 
57                                     if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "NEW"  && 
58                                         ts.lookTok(3).name == "FUNCTION") {
59                                         // freeze time.. 
60                                         var cu = ts.cursor;
61                                         
62                                         ts.balance("(");
63                                         
64                                         
65                                         ts.balance("{");
66                                         // if next is not ';' -> make it so...
67                                         
68                                         if (ts.lookTok(1).data != ";"  && ts.lookTok(1).data != "}" && ts.lookTok(1).name == "NEWLINE") {
69                                             ts.look(0,true).outData = ts.lookTok(0).data +";";
70                                         }
71                                         // restore.. 
72                                         ts.cursor = cu;
73                                         continue;
74                                     }
75                                     // a = function() { ...
76                                        
77                                     if (ts.lookTok(1).data == "=" &&  ts.lookTok(2).name == "FUNCTION") {
78                                         // freeze time.. 
79                                         //println("got = function() ");
80                                         var cu = ts.cursor;
81                                          
82                                         if (ts.balance("(").size < 1 ){
83                                             ts.dump(cu-40, cu);
84                                             print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
85                                             ts.dump(cu, 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                                     
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 out = '';
278                         var outoff = 0;
279                         //try { out.length = ts.slen; } catch (e) {} // prealloc.
280                 
281                         out = '';
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"  && tok.identifier != null 
297                                     && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
298                                     //f.write(tok.identifier.mungedValue);
299                                     //print("MUNGED: " + tok.identifier.mungedValue);
300                                     out += tok.identifier.mungedValue;
301                                     continue;
302                                 }
303                                 
304                                 // at this point we can apply a text translation kit...
305                                 
306                                 if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
307                                     if (packer && packer.stringHandler) {
308                                         out += packer.stringHandler(tok);
309                                         continue;
310                                     }
311                                 }
312                          
313                                 out += tok.outData != "" ? tok.outData : tok.data;
314                                 
315                                 if ((tok.outData == ";") && (out.length - outoff > 255)) {
316                                     outoff = out.length;
317                                     out += "\n";
318                                 }
319                         }
320                         //f.close();
321                         /*
322                         // remove the last ';' !!!
323                         if (out.substring(out.length-1) == ';') {
324                                 return out.substring(0,out.length-1);
325                            }
326                         */
327                         return out;
328                 
329                 }
330         }       
331 }