JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / CompressWhite.js
1  // <script type="text/javascript">
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  
11 const CompressWhite =  function (ts, packer, keepWhite)
12 {
13     keepWhite = keepWhite || false;
14     ts.rewind();
15     //var str = File.read(fn);
16     var rep_var = 1;
17     
18     
19     
20     while (true) {
21         var tok = ts.next();
22         if (!tok) {
23             break;
24         }
25         if (tok.type == "WHIT") {
26            
27             continue;
28             //if (tok._isDoc) {
29             //    continue;
30             //}
31             // just spaces, not \n!
32             //if (tok.data.indexOf("\n") < 0) {
33             //    continue;
34            // }
35             
36             
37         }
38         if (tok.data == "}")  {
39             
40             if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
41             
42                 ts.look(0).outData = ts.look(0).data+"\n";
43             }
44             // restore.. 
45             
46             continue;
47         }
48         // add semi-colon's where linebreaks are used... - not foolproof yet.!
49         if (tok.type == "NAME")  {
50             //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
51             // a = new function() {} 
52             if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == 'NEW'  && 
53                 ts.lookTok(3).name == 'FUNCTION') {
54                 // freeze time.. 
55                 var cu = ts.cursor;
56                 
57                 ts.balance("(");
58                 
59                 
60                 ts.balance("{");
61                 // if next is not ';' -> make it so...
62                 
63                 if (ts.lookTok(1).data != ';'  && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
64                     ts.look(0).outData = ts.cur().data +";";
65                 }
66                 // restore.. 
67                 ts.cursor = cu;
68                 continue;
69             }
70             // a = function() { ...
71                
72             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).name == "FUNCTION") {
73                 // freeze time.. 
74                 //println("got = function() ");
75                 var cu = ts.cursor;
76                 
77                  if (!ts.balance("(") ){
78                     ts.dump(cu-40, cu);
79                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
80                     ts.dump(cu, cu+40);
81                     
82                     throw "could not find end lbrace!!!";
83                 }
84                 //print("AFTER BALANCE (");
85                 //ts.dump(cu, ts.cursor);
86                 //ts.cursor--; // cursor at the (
87                 if (!ts.balance("{") ){
88                     ts.dump(cu-40, cu);
89                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
90                     ts.dump(cu, cu+40);
91                     
92                     throw "could not find end lbrace!!!";
93                 }
94                 //print('FN: '+ts.tokens[cu].toString());
95                 //print('F1: '+ts.lookTok(1).toString());
96                 //print('F2: '+ts.look(1,true).toString());
97                 
98                 // if next is not ';' -> make it so...
99                 // although this var a=function(){},v,c; causes 
100                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
101                     
102                     ts.look(0).outData = ts.look(0).data+";";
103                    // print("ADDING SEMI: " + ts.look(0).toString());
104                     //ts.dump(cu, ts.cursor+2);
105                 }
106                 
107                  //ts.dump(cu, ts.cursor+2);
108                 // restore.. 
109                 ts.cursor = cu;
110                 continue;
111             }
112             // next item is a name..
113             if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) &&  ts.look(1,true).name == "NEWLINE") {
114                 // preserve linebraek
115                 ts.look(0).outData = ts.look(0).data+"\n";
116             }
117             // method call followed by name..
118             if (ts.lookTok(1).data == "(")  {
119                 var cu = ts.cursor;
120                 
121                 ts.balance("(");
122                  // although this var a=function(){},v,c; causes 
123                 
124                 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
125                 
126                     ts.look(0).outData = ts.look(0).data+"\n";
127                 }
128                 // restore.. 
129                 ts.cursor = cu;
130                 continue;
131             }
132             
133             
134             // function a () { ... };
135                 /*
136             if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
137                 // freeze time.. 
138                 //println("got = function() ");
139                 var cu = ts.cursor;
140                 
141                 ts.balance("lparen");
142                 ts.balance("lbrace");
143                 // if next is not ';' -> make it so...
144                 // although this var a=function(){},v,c; causes 
145                 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
146                     ts.cur().outData = ts.cur().data+";";
147                 }
148                 // restore.. 
149                 ts.cursor = cu;
150                 continue;
151             }
152             */
153             
154             // a = { ....
155                 
156             if (ts.lookTok(1).data == '=' &&  ts.lookTok(2).data == '{') {
157                 // freeze time.. 
158                 //println("----------*** 3 *** --------------");
159                 var cu = ts.cursor;
160                 
161                 if (!ts.balance("{") ){
162                     ts.dump(cu-40, cu);
163                     print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
164                     ts.dump(cu, cu+40);
165                     
166                     throw "could not find end lbrace!!!";
167                 }
168                 // if next is not ';' -> make it so...
169                 
170                 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
171                     ts.look(0).outData = ts.look(0).data +";";
172                 }
173                 // restore.. 
174                 ts.cursor = cu;
175                 continue;
176             }
177             
178             // any more??
179             // a = function(....) { } 
180           
181         }
182         
183         
184         
185          
186         //println("got Token: " + tok.type);
187         
188         
189         
190         switch(tok.data.toUpperCase()) {
191             // things that need space appending
192             case "FUNCTION":
193             case "BREAK":
194             case "CONTINUE":
195                 // if next item is a identifier..
196                 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
197                    tok.outData =  tok.data + " ";
198                 }
199                 continue;
200                 
201                 
202             case "RETURN": // if next item is not a semi; (or }
203                 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
204                     continue;
205                 }
206                 tok.outData =  tok.data + " ";
207                 
208                 continue;
209             
210                 
211             case "ELSE": // if next item is not a semi; (or }
212                 if (!ts.lookTok(1).name == "IF") {
213                     continue;
214                 }
215                 
216                 tok.outData =  tok.data + " ";
217                 continue;
218             
219             case "++": // if previous was a plus or next is a + add a space..
220             case "--": // if previous was a - or next is a - add a space..
221             
222                 var p = (tok.data == "--" ? '-' : '+'); 
223             
224                 if (ts.lookTok(1).data == p) {
225                     tok.outData =  tok.data + " ";
226                 }
227                 if (ts.lookTok(-1).data == p) {
228                     tok.outData =  " " +  tok.data;
229                     
230                 }
231                 continue;
232             
233             case "IN": // before and after?? 
234             case "INSTANCEOF":
235                 
236                 tok.outData = " " + tok.data + " ";
237                 continue;
238             
239             case "VAR": // always after..
240             case "NEW":
241             case "DELETE":
242             case "THROW":
243             case "CASE":
244             
245             case "VOID":
246                 tok.outData =  tok.data + " ";
247                 
248                 continue
249                 
250             case "TYPEOF": // what about typeof(
251                 if (ts.lookTok(1).data != '(') {
252                     tok.outData =  tok.data + " ";
253                 }
254                 continue;
255              case ";":
256                 //remove semicolon before brace -- 
257                 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
258                 //    tok.outData = '';
259                // }
260                 continue;
261            
262             default:
263                 continue;
264         }
265     }
266     
267     ts.rewind();
268     
269     // NOW OUTPUT THE THING.
270     //var f = new File(minfile, File.NEW);
271     
272     var out = '';
273     var outoff = 0;
274     try { out.length = ts.slen; } catch (e) {} // prealloc.
275     
276     out = '';
277     var tok;
278     while (true) {
279         
280         tok = keepWhite ? ts.next() : ts.nextTok();
281         
282         if (!tok) {
283             break;
284         }
285         if (tok.type == "COMM") {
286             tok.outData = '\n';
287         }
288         
289         ///print(tok.type + ':' + tok.data);
290         
291         if (tok.type == "NAME"  && tok.identifier 
292             && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
293             //f.write(tok.identifier.mungedValue);
294             //print("MUNGED: " + tok.identifier.mungedValue);
295             out += tok.identifier.mungedValue;
296             continue;
297         }
298         
299         // at this point we can apply a text translation kit...
300         
301         if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
302             if (packer && packer.stringHandler) {
303                 out += packer.stringHandler(tok);
304                 continue;
305             }
306         }
307      
308         out += tok.outData !== false ? tok.outData : tok.data;
309         
310         if ((tok.outData == ';') && (out.length - outoff > 255)) {
311             outoff = out.length;
312             out += "\n";
313         }
314     }
315     //f.close();
316     /*
317     // remove the last ';' !!!
318     if (out.substring(out.length-1) == ';') {
319         return out.substring(0,out.length-1);
320        }
321     */
322     return out;
323     
324 }
325     
326