JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / RooFile.js
1 //<script type="text/javscript">
2
3 XObject = imports.XObject.XObject;
4 console     = imports.console.console;
5 Collapse = imports.Collapse.Collapse;
6 /**
7  * 
8  * this takes our collased file, and turns it into the config array..
9  * 
10  * @see rconv.js (our tester file)
11  * 
12  * STATUS:
13  *
14  *  - Pman.Tab.* appears to work.
15  * .. next up ..Dialog...
16  *
17  *
18  *
19  * Current issues:
20  *  - xtype is combined on generated files. (not xns + xtype)
21  *  - listeners are prefixed with '|' ... 
22  *
23  * 
24  */
25
26 RooFile  =  XObject.define(
27     function (ar)
28     {
29         RooFile.superclass.constructor.call(this, ar);
30        // console.log("STARTING OUTPUT");
31         
32          
33
34     },
35     Collapse, 
36     {
37         
38         cfg : { },
39
40         parse: function()
41         {
42             // standard pman dialog
43             if (this.tokens[0].data == 'Pman.on') {
44                 this.parsePmanLayout();
45                 return;
46             }
47          
48             
49             // Standard Pman Dialog - 
50             if (this.tokens[2].props && typeof(this.tokens[2].props.create) != 'undefined') {
51                 this.parsePmanDialog();
52                 return;
53             }
54             
55             
56             // Seed Gtk XObject application. 
57             if (this.tokens.length > 2 && this.tokens[2].data.match(/^imports\./)) {
58                 this.parseXObject();
59                 return;
60             }
61             
62             Seed.print("Unknown format");
63             Seed.print(JSON.stringify(this.tokens,null,4));
64             Seed.quit();
65             
66             
67             
68             
69             
70             
71             
72             
73             // perfect for dialogs... - is this our 'generic, non-pman code?'
74             
75             
76             var pos = this.lookFor( 'function');
77             
78             // XXXXX = function(
79             var cfg = {};
80             
81             if (pos > -1 && (this.tokens[pos-1].data == '=') && (this.tokens[pos-2].type == 'NAME')) {
82                 
83                 this.cfg = {
84                     '*class' : this.tokens[pos-2].data,
85                     '*extends' : '',
86                     '//constructor' :  this.tokens[pos-2].prefix,
87                     '|constructor' : 'function ' + this.tokens[pos+1].toRaw() + 
88                             this.tokens[pos+2].toRaw()
89                     
90                 };
91                 this.cursor = pos+2;
92                 
93             }
94             
95             if (pos < 0) {
96                 // no function - we have a static one...
97                 pos = this.lookFor( '=');
98                 if (pos > 1 && 
99                     (this.tokens[pos-1].type == 'NAME') &&
100                     (this.tokens[pos+1].data == '{')
101                 ) {
102                     this.cfg = {
103                         '*class' : this.tokens[pos-1].data,
104                         '//*class' :  this.tokens[pos-1].prefix
105                         
106                         
107                     };
108                     XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].props));
109                     return;
110                     
111                 }
112                 
113                 
114                 
115             }
116             
117             
118               // Roo.apply (l
119             var pos = this.lookFor( 'Roo.apply');
120             //console.dump(this.tokens);
121             
122             
123             if (
124                 (pos > -1) &&
125                 (this.tokens[pos+1].items[0][0].data  == this.cfg['*class'] + '.prototype')
126             ) {
127                 // XXXXXx.prototype = {
128                 
129                 
130                     
131                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[1][0].props));
132                 return;
133                 
134             }
135             
136             
137             var pos = this.lookFor('new');
138             
139             if (pos > -1 && 
140                 (this.tokens[pos-2].type == 'NAME') &&
141                 (this.tokens[pos-1].data == '=') && 
142                 (this.tokens[pos+1].type == 'NAME') &&
143                 (this.tokens[pos+2].data == '(') 
144             ) {
145                 
146                 this.cfg = {
147                     '//*class' : this.tokens[pos-2].prefix,
148                     '*class' : this.tokens[pos-2].data,
149                     '*extends' : this.tokens[pos+1].data
150                 };
151                     
152                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+2].items[0][0].props));
153                 
154                 return;
155                 
156             }
157             
158             ///Builder.Provider.ProjectManager = new Roo.Observable({
159                 
160             
161             
162             var pos = this.lookFor( 'Roo.extend');
163             if (pos > -1) {
164                 
165                 this.cfg['*extends'] = this.tokens[pos+1].items[1][0].data;
166                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[2][0].props));
167                 
168                 // no more..
169                 return;
170             }
171             
172              
173                 
174             var pos = this.lookFor( 'Roo.extend');
175             if (pos > -1) {
176                 
177                 this.cfg['*extends'] = this.tokens[pos+1].items[1][0].data;
178                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[2][0].props));
179                 
180                 // no more..
181                 return;
182             }
183             
184             
185             
186             //console.dump(cfg);
187             //Seed.quit();
188             
189             
190             
191             // other type of layout
192           //  console.dump(this.tokens);
193             
194             // this type of file..
195             // xxxx.xxx = function
196             // -> into |constructor..
197             // -> extend + prototype
198              
199             
200             
201                 
202         },
203         
204         
205         parsePmanLayout : function()
206         {
207                 // then it's a layout style..
208             
209             //Seed.quit();
210             
211             
212             //console.dump(this.tokens);
213             //              the list of statements inside of function..?
214             
215             
216             var stmts =  this.tokens[1].items[1][2].items;
217             
218             // look for register..
219             var topp = false;
220             stmts.map( function(s, i) {
221                 if (s[0].data == 'Pman.register') {
222                     topp = brace = s[1].items[0][0].props;
223                 }
224             });
225             if (!topp) {
226                 console.dump(this.tokens);
227                 throw "could not find top props...";
228                 
229             }
230             
231             this.cfg = this.parseProps(topp);
232             this.cfg.name = this.tokens[3].data;
233             
234             
235             //                  (            {          add    { this.panel (  {
236             var cfg = this.tokens[7].items[0][0].props.add.val[2].items[2][3].items[0][0].props;
237             this.cfg.items = [ this.parseProps(cfg) ];
238             //console.dump(this.cfg); 
239         },
240         
241         
242         parsePmanDialog : function() {
243             
244             //console.dump(this.tokens);
245             this.cfg.name = this.tokens[0].data;
246             
247             
248             
249             var cfg = this.tokens[2].props.create.val[2].items[1][3].items[0][0].props;
250             //console.dump(this.tokens);
251             //print(JSON.stringify(cfg,null,4)); Seed.quit();
252             
253            // print("Trying standard dialog");Seed.quit();;
254             
255             this.cfg.items = [ this.parseProps(cfg) ];
256             return;
257             
258         },
259         
260         // Seed xobject file.
261         parseXObject : function() {
262             
263             this.parseImports();
264               
265             var pos = this.lookFor('XObject');
266             if (pos < 0)  {
267                 throw "Can not find XObject";
268             }
269             this.cfg.name = this.tokens[pos-3].data;
270             
271             var cfg = this.tokens[pos+1].items[0][0].props;
272             this.cfg.items = [ this.parseProps(cfg) ];
273             
274             //print(JSON.stringify(this.tokens[pos]));
275             
276             //print(JSON.stringify(this.tokens,null,4)); Seed.quit();
277             //Seed.quit();
278             
279         },
280         
281         
282         
283         /**
284          *  parse Imports lines.
285          *
286          */
287         
288         parseImports : function()
289         {
290            // console.dump(this.tokens);
291             this.cfg.giImports = [];
292             this.cfg.imports= [];
293             while (true) {
294                 var pos = this.lookFor('=');
295                 if (pos < 0)  {
296                     break;
297                 }
298                 this.cursor = pos;
299                 var k = this.look(-1, true);
300                 var v = this.look(1, true);
301                // Seed.print(k.data + " => " + v.data);
302                
303                 /// finish!!! - not an import ...
304                
305                 if (!v.data.match(/^imports/)) {
306                     return; ///
307                     
308                     this.cursor++;
309                     continue;
310                 }
311                 if (v.data.match(/^imports\.gi/)) {
312                     // gi import..
313                     this.cfg.giImports.push(v.data.replace(/imports\.gi\./, ''));
314                     
315                     this.cursor++;
316                     continue;
317                 }
318                   
319                 // two types of import left
320                 // imports.xnew
321                 if (v.data.match(/^imports\./)) {
322                     this.cfg.imports[k.data] = v.data.replace(/imports\./, '') + '.js';
323                     this.cursor++;
324                     continue;
325                 }
326                 // imports[.....]
327                 this.cursor++;
328                 if (this.lookFor('[') > this.lookFor('=')) {
329                     continue;
330                 }
331                 var bpos = this.lookFor('[');
332                // console.dump(this.tokens[bpos]);
333                 
334                 this.cfg.imports[k.data] = this.tokens[bpos].items[0][0].toJS();
335                 
336                 this.cursor++;
337                 
338             }
339         //    console.dump(this.giImports);
340           //  console.dump(this.imports);
341             //Seed.quit();
342             
343         },
344         
345         ///------------------- GENERIC PARSING ------------------
346         
347         
348         
349         
350         
351         
352         parseProps:  function(o)
353         {
354             //print(JSON.stringify(o,null,4));
355             
356             var ret = { };
357             var fakeItems =  [];
358             for(var k in o) {
359                 
360                 //print( "parsing prop: " + k);
361                 if (o[k].key.data == '}') {
362                     // typo trailing comma in object def.
363                     continue;
364                 }
365                 
366                 var kv = k;
367                 if (o[k].key.type == 'STRN') {
368                     kv = o[k].key.toJS();
369                 }
370                 if (!o[k].val.length) {
371                     console.dump(o[k]);
372                 }
373                 
374                 //console.dump(o[k]);
375                 if (o[k].val[0].data == "function") {
376                     // add comments..
377                   //   console.dump(o[k].key.prefix); 
378                     var pr = typeof(o[k].key.prefix) == 'undefined' ? '' : o[k].key.prefix ;
379                     pr = this.clean_prefix( pr) ;
380                     if (pr.length) {
381                         ret['//' +kv ] =  pr;
382                     }
383                     //print("running expand");
384                     ret['|' +kv ] =  this.clean_body( this.expand(o[k].val));
385                     continue;
386                 }
387                 
388                 
389                 if (o[k].val[0].data == "[") {
390                     
391                     if (o[k].val[0].items[0][0].data == '{') {
392                         // array of objects..
393                         
394                         // this works for items..
395                         
396                         // used elsewhere for buttons: -> in which case we have a fake xtype
397                         
398                         
399                         
400                         // if K is not ITEMS - we need to add '*prop' -> and add it to the items array..
401                         var add = this.parseArray(o[k].val[0].items);
402                         if (kv == 'items') {
403                             ret[kv] = add;
404                             continue;
405                         }
406                         var fake_array = {
407                             xtype : 'Array.' + kv,
408                             '*prop' : kv,
409                             items : add
410                         }
411                         fakeItems.push(fake_array);
412                         continue;
413                     } 
414                     // raw array 
415                     
416                     
417                     ret['|' +kv ] =  this.clean_body(this.expand(o[k].val)); // remove ','...
418                     continue;
419                 }
420                 if (o[k].val[0].data == "(") {
421                     ret['|' +kv ] =  this.expand(o[k].val);
422                     continue;
423                 }
424                 // value is an object..
425                 
426                 if (o[k].val[0].data == "{") {
427                     
428                     // things that can be property of object type:
429                     // listeners, set, 
430                     var add = this.parseProps(o[k].val[0].props);
431                     
432                     
433                     
434                     if (kv == 'set' || kv =='listeners') {
435                         ret[kv ] = add;
436                         continue;
437                     }
438                     if ((typeof(add.xtype) != 'undefined') ||  ['sortInfo', 'center', 'east', 'west', 'north', 'south'].indexOf(kv) > -1) {
439                         add['*prop'] =  kv;
440                         fakeItems.push(add);
441                         continue;
442                     }
443                     
444                     
445                     ret[ '|' + kv ] =  this.expand(o[k].val);
446                     
447                     
448                     // this hsould be added to 'items', with a *prop element..
449                     continue;
450                 }
451                 //console.dump(o[k].val);
452                 
453                 if (o[k].val[1].data == ',' || o[k].val[1].data == '}') {
454                     // single item piece of data...
455                     var t1= o[k].val[0];
456                     switch(o[k].val[0].type) {
457                         case 'STRN':
458                         case 'NUMB':
459                         case 'KEYW':
460                             ret[  kv ]  = t1.toJS();
461                             continue;
462                         case 'NAME':
463                             ret[ '|' + kv ] =  t1.data;
464                             continue;
465                         
466                     }
467                 }
468                
469                 ret[ '|' + kv ] =  this.expand(o[k].val);
470                 
471             }
472             if (!ret.items && fakeItems.length) {
473                 ret.items = [];
474             }
475             fakeItems.forEach(  function(e) {
476                 ret.items.push(e);
477             });
478             // merge fakeItems;
479             //console.dump(ret);
480             
481             return ret;
482         },
483         parseArray: function(ar) {
484           // console.dump(ar);
485            // Seed.quit();
486             var ret = [];
487             ar.map(function (e) {
488                 // has no props...
489                 if (typeof(e[0].props) == 'undefined') {
490                     return;
491                 }
492                
493                 
494                 ret.push( this.parseProps(e[0].props));
495                      
496             },this);
497              
498             return ret;
499           
500         },
501         
502         /**
503          * convert a function call token array back into a string
504          */
505         expand: function(ar)
506         {
507             var ret = '';
508             //print(JSON.stringify(ar,null,4));
509             
510             for(var i =0 ; i < ar.length -1; i++) {
511                 ret += ar[i].toRaw();
512             }
513             
514             return ret;
515             
516             
517         },
518         
519         /***
520          * change the indentation on a function 
521          *
522          */
523         clean_body : function(str)
524         {
525             var lns = str.split("\n");
526             var mindent = -1;
527             lns.map( function(l, i) {
528                 if (!i || !l.length || l.match(/^\s+$/)) {
529                     return;
530                 }
531                 
532                 var spc = l.match(/\s+/);
533                 if (!spc || !spc[0].length) {
534                     return;
535                 }
536                 mindent = mindent < 0 ? spc[0].length : Math.min(spc[0].length, mindent);
537                 
538             });
539             //console.log(mindent + ":" + lns[0]);
540             var ar = [];
541             if (mindent < 0) {
542                 return str;
543             }
544             lns.map( function(l,i) {
545                 if (!i) {
546                     ar.push(l.replace(/^\s+/, ''));
547                     return;
548                 }
549                 ar.push(l.substring(mindent));
550             });
551             return ar.join("\n");
552         },
553         clean_prefix: function(str) {
554             
555             
556             
557             if (!str.length) {
558                 return '';
559             }
560             var ret = str.replace(/^\s+/gm, "\n").replace(/\n+/gm, "\n");
561             return ret == "\n" ? '' : ret;
562             
563         }    
564              
565     
566 });