257c3d58ec10a5c52f3093454aa03d4ccf6fb073
[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("balnaced issue\n");
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                                     ts.dump(cu-40, cu);
171                                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
172                                     ts.dump(cu, cu+40);
173                                     
174                                     throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
175                                 }
176                                 // if next is not ';' -> make it so...
177                                 
178                                 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
179                                     ts.look(0,true).outData = ts.look(0,true).data +";";
180                                 }
181                                 // restore.. 
182                                 ts.cursor = cu;
183                                 continue;
184                             }
185                             
186                             // any more??
187                             // a = function(....) { } 
188                           
189                         }
190                         
191                         
192                         
193                          
194                         //println("got Token: " + tok.type);
195                         
196                         
197                         
198                         switch(tok.data.up()) {
199                             // things that need space appending
200                             case "FUNCTION":
201                             case "BREAK":
202                             case "CONTINUE":
203                                 // if next item is a identifier..
204                                 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!!
205                                    tok.outData =  tok.data + " ";
206                                 }
207                                 continue;
208                                 
209                                 
210                             case "RETURN": // if next item is not a semi; (or }
211                                 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
212                                     continue;
213                                 }
214                                 tok.outData =  tok.data + " ";
215                                 
216                                 continue;
217                             
218                                 
219                             case "ELSE": // if next item is not a semi; (or }
220                                 if (ts.lookTok(1).name != "IF") {
221                                     continue;
222                                 }
223                                 // add a space if next element is 'IF'
224                                 tok.outData =  tok.data + " ";
225                                 continue;
226                             
227                             case "++": // if previous was a plus or next is a + add a space..
228                             case "--": // if previous was a - or next is a - add a space..
229                             
230                                 var p = (tok.data == "--" ? "-" : "+"); 
231                             
232                                 if (ts.lookTok(1).data == p) {
233                                     tok.outData =  tok.data + " ";
234                                 }
235                                 if (ts.lookTok(-1).data == p) {
236                                     tok.outData =  " " +  tok.data;
237                                     
238                                 }
239                                 continue;
240                             
241                             case "IN": // before and after?? 
242                             case "INSTANCEOF":
243                                 
244                                 tok.outData = " " + tok.data + " ";
245                                 continue;
246                             
247                             case "VAR": // always after..
248                             case "NEW":
249                             case "DELETE":
250                             case "THROW":
251                             case "CASE":
252                             case "CONST":
253                             case "VOID":
254                                 tok.outData =  tok.data + " ";
255                                 
256                                 continue;
257                                 
258                             case "TYPEOF": // what about typeof(
259                                 if (ts.lookTok(1).data != "(") {
260                                     tok.outData =  tok.data + " ";
261                                 }
262                                 continue;
263                              case ";":
264                                 //remove semicolon before brace -- 
265                                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
266                                 //    tok.outData = '';
267                                // }
268                                 continue;
269                            
270                             default:
271                                 continue;
272                         }
273                 }
274         
275                 ts.rewind();
276         
277                 // NOW OUTPUT THE THING.
278                 //var f = new File(minfile, File.NEW);
279         
280                 var outstr = "";
281                 var outoff = 0;
282                 //try { out.length = ts.slen; } catch (e) {} // prealloc.
283         
284
285                 Token tok;
286                 while (true) {
287                         
288                         tok = keepWhite ? ts.next() : ts.nextTok();
289                         
290                         if (tok == null) {
291                             break;
292                         }
293                         if (tok.type == "COMM") {
294                             tok.outData = "\n";
295                         }
296                         
297                         ///print(tok.type + ':' + tok.data);
298                         
299                         if (tok.type == "NAME"  &&
300                                  tok.identifier != null  &&
301                             tok.identifier.mungedValue.length > 0) {
302                             //f.write(tok.identifier.mungedValue);
303                             //print("MUNGED: " + tok.identifier.mungedValue);
304                             outstr += tok.identifier.mungedValue;
305                             continue;
306                         }
307                         
308                         // at this point we can apply a text translation kit...
309                         // NOT SUPPORTED..
310                         //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
311                         //    if (packer && packer.stringHandler) {
312                         //        outstr += packer.stringHandler(tok);
313                         //        continue;
314                         //    }
315                         //}
316                  
317                         outstr += tok.outData != "" ? tok.outData : tok.data;
318                         
319                         if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
320                             outoff = outstr.length;
321                             outstr += "\n";
322                         }
323                 }
324                 //f.close();
325                 /*
326                 // remove the last ';' !!!
327                 if (out.substring(out.length-1) == ';') {
328                         return out.substring(0,out.length-1);
329                    }
330                 */
331                 return outstr;
332         
333         }
334          
335 }