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         parsePmanLayout : function()
193         {
194                 // then it's a layout style..
195             
196             //Seed.quit();
197             
198             
199             //console.dump(this.tokens);
200             //              the list of statements inside of function..?
201             
202             
203             var stmts =  this.tokens[1].items[1][2].items;
204             
205             // look for register..
206             var topp = false;
207             stmts.map( function(s, i) {
208                 if (s[0].data == 'Pman.register') {
209                     topp = brace = s[1].items[0][0].props;
210                 }
211             });
212             if (!topp) {
213                 console.dump(this.tokens);
214                 throw "could not find top props...";
215                 
216             }
217             
218             this.cfg = this.parseProps(topp);
219             this.cfg.name = this.tokens[3].data;
220             
221             
222             //                  (            {          add    { this.panel (  {
223             var cfg = this.tokens[7].items[0][0].props.add.val[2].items[2][3].items[0][0].props;
224             this.cfg.items = [ this.parseProps(cfg) ];
225             //console.dump(this.cfg); 
226         },
227         
228         
229         parsePmanDialog : function() {
230             
231             //console.dump(this.tokens);
232             this.cfg.name = this.tokens[0].data;
233             
234             
235             
236             var cfg = this.tokens[2].props.create.val[2].items[1][3].items[0][0].props;
237             //console.dump(this.tokens);
238             //print(JSON.stringify(cfg,null,4)); Seed.quit();
239             
240            // print("Trying standard dialog");Seed.quit();;
241             
242             this.cfg.items = [ this.parseProps(cfg) ];
243             return;
244             
245         },
246         
247         
248         
249         /**
250          *  parse Imports lines.
251          *
252          */
253         
254         parseImports : function()
255         {
256            // console.dump(this.tokens);
257             this.giImports = [];
258             while (true) {
259                 var pos = this.lookFor('=');
260                 if (pos < 0)  {
261                     break;
262                 }
263                 this.cursor = pos;
264                 var k = this.look(-1, true);
265                 var v = this.look(1, true);
266                // Seed.print(k.data + " => " + v.data);
267                
268                 /// finish!!! - not an import ...
269                
270                 if (!v.data.match(/^imports/)) {
271                     return; ///
272                     
273                     this.cursor++;
274                     continue;
275                 }
276                 if (v.data.match(/^imports\.gi/)) {
277                     // gi import..
278                     this.giImports.push(v.data.replace(/imports\.gi\./, ''));
279                     
280                     this.cursor++;
281                     continue;
282                 }
283                   
284                 // two types of import left
285                 // imports.xnew
286                 if (v.data.match(/^imports\./)) {
287                     this.imports[k.data] = v.data.replace(/imports\./, '') + '.js';
288                     this.cursor++;
289                     continue;
290                 }
291                 // imports[.....]
292                 this.cursor++;
293                 if (this.lookFor('[') > this.lookFor('=')) {
294                     continue;
295                 }
296                 var bpos = this.lookFor('[');
297                // console.dump(this.tokens[bpos]);
298                 
299                 this.imports[k.data] = this.tokens[bpos].items[0][0].toJS();
300                 
301                 this.cursor++;
302                 
303             }
304         //    console.dump(this.giImports);
305           //  console.dump(this.imports);
306             //Seed.quit();
307             
308         },
309         
310         ///------------------- GENERIC PARSING ------------------
311         
312         
313         
314         
315         
316         
317         parseProps:  function(o)
318         {
319             //print(JSON.stringify(o,null,4));
320             
321             var ret = { };
322             var fakeItems =  [];
323             for(var k in o) {
324                 
325                 //print( "parsing prop: " + k);
326                 if (o[k].key.data == '}') {
327                     // typo trailing comma in object def.
328                     continue;
329                 }
330                 
331                 var kv = k;
332                 if (o[k].key.type == 'STRN') {
333                     kv = o[k].key.toJS();
334                 }
335                 if (!o[k].val.length) {
336                     console.dump(o[k]);
337                 }
338                 
339                 //console.dump(o[k]);
340                 if (o[k].val[0].data == "function") {
341                     // add comments..
342                   //   console.dump(o[k].key.prefix); 
343                     var pr = typeof(o[k].key.prefix) == 'undefined' ? '' : o[k].key.prefix ;
344                     pr = this.clean_prefix( pr) ;
345                     if (pr.length) {
346                         ret['//' +kv ] =  pr;
347                     }
348                     //print("running expand");
349                     ret['|' +kv ] =  this.clean_body( this.expand(o[k].val));
350                     continue;
351                 }
352                 
353                 
354                 if (o[k].val[0].data == "[") {
355                     
356                     if (o[k].val[0].items[0][0].data == '{') {
357                         // array of objects..
358                         
359                         // this works for items..
360                         
361                         // used elsewhere for buttons: -> in which case we have a fake xtype
362                         
363                         
364                         
365                         // if K is not ITEMS - we need to add '*prop' -> and add it to the items array..
366                         var add = this.parseArray(o[k].val[0].items);
367                         if (kv == 'items') {
368                             ret[kv] = add;
369                             continue;
370                         }
371                         var fake_array = {
372                             xtype : 'Array.' + kv,
373                             '*prop' : kv,
374                             items : add
375                         }
376                         fakeItems.push(fake_array);
377                         continue;
378                     } 
379                     // raw array 
380                     
381                     
382                     ret['|' +kv ] =  this.clean_body(this.expand(o[k].val)); // remove ','...
383                     continue;
384                 }
385                 if (o[k].val[0].data == "(") {
386                     ret['|' +kv ] =  this.expand(o[k].val);
387                     continue;
388                 }
389                 // value is an object..
390                 
391                 if (o[k].val[0].data == "{") {
392                     
393                     // things that can be property of object type:
394                     // listeners, set, 
395                     var add = this.parseProps(o[k].val[0].props);
396                     
397                     
398                     
399                     if (kv == 'set' || kv =='listeners') {
400                         ret[kv ] = add;
401                         continue;
402                     }
403                     if ((typeof(add.xtype) != 'undefined') ||  ['sortInfo', 'center', 'east', 'west', 'north', 'south'].indexOf(kv) > -1) {
404                         add['*prop'] =  kv;
405                         fakeItems.push(add);
406                         continue;
407                     }
408                     
409                     
410                     ret[ '|' + kv ] =  this.expand(o[k].val);
411                     
412                     
413                     // this hsould be added to 'items', with a *prop element..
414                     continue;
415                 }
416                 //console.dump(o[k].val);
417                 
418                 if (o[k].val[1].data == ',' || o[k].val[1].data == '}') {
419                     // single item piece of data...
420                     var t1= o[k].val[0];
421                     switch(o[k].val[0].type) {
422                         case 'STRN':
423                         case 'NUMB':
424                         case 'KEYW':
425                             ret[  kv ]  = t1.toJS();
426                             continue;
427                         case 'NAME':
428                             ret[ '|' + kv ] =  t1.data;
429                             continue;
430                         
431                     }
432                 }
433                
434                 ret[ '|' + kv ] =  this.expand(o[k].val);
435                 
436             }
437             if (!ret.items && fakeItems.length) {
438                 ret.items = [];
439             }
440             fakeItems.forEach(  function(e) {
441                 ret.items.push(e);
442             });
443             // merge fakeItems;
444             //console.dump(ret);
445             
446             return ret;
447         },
448         parseArray: function(ar) {
449           // console.dump(ar);
450            // Seed.quit();
451             var ret = [];
452             ar.map(function (e) {
453                 // has no props...
454                 if (typeof(e[0].props) == 'undefined') {
455                     return;
456                 }
457                
458                 
459                 ret.push( this.parseProps(e[0].props));
460                      
461             },this);
462              
463             return ret;
464           
465         },
466         
467         
468         expand: function(ar)
469         {
470             var ret = '';
471             //print(JSON.stringify(ar,null,4));
472             
473             for(var i =0 ; i < ar.length -1; i++) {
474                 ret += ar[i].toRaw();
475             }
476             
477             return ret;
478             
479             
480         },
481         clean_body : function(str)
482         {
483             var lns = str.split("\n");
484             var mindent = -1;
485             lns.map( function(l, i) {
486                 if (!i || !l.length || l.match(/^\s+$/)) {
487                     return;
488                 }
489                 
490                 var spc = l.match(/\s+/);
491                 if (!spc || !spc[0].length) {
492                     return;
493                 }
494                 mindent = mindent < 0 ? spc[0].length : Math.min(spc[0].length, mindent);
495                 
496             });
497             //console.log(mindent + ":" + lns[0]);
498             var ar = [];
499             if (mindent < 0) {
500                 return str;
501             }
502             lns.map( function(l,i) {
503                 if (!i) {
504                     ar.push(l.replace(/^\s+/, ''));
505                     return;
506                 }
507                 ar.push(l.substring(mindent));
508             });
509             return ar.join("\n");
510         },
511         clean_prefix: function(str) {
512             
513             
514             
515             if (!str.length) {
516                 return '';
517             }
518             var ret = str.replace(/^\s+/gm, "\n").replace(/\n+/gm, "\n");
519             return ret == "\n" ? '' : ret;
520             
521         }    
522              
523     
524 });