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