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