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         parsePmanDialog : function() {
228             
229             //console.dump(this.tokens);
230             this.cfg.name = this.tokens[0].data;
231             
232             
233             
234             var cfg = this.tokens[2].props.create.val[2].items[1][3].items[0][0].props;
235             //console.dump(this.tokens);
236             //print(JSON.stringify(cfg,null,4)); Seed.quit();
237             
238            // print("Trying standard dialog");Seed.quit();;
239             
240             this.cfg.items = [ this.parseProps(cfg) ];
241             return;
242             
243         },
244         
245         parseProps:  function(o)
246         {
247             //print(JSON.stringify(o,null,4));
248             
249             var ret = { };
250             var fakeItems =  [];
251             for(var k in o) {
252                 
253                 //print( "parsing prop: " + k);
254                 if (o[k].key.data == '}') {
255                     // typo trailing comma in object def.
256                     continue;
257                 }
258                 
259                 var kv = k;
260                 if (o[k].key.type == 'STRN') {
261                     kv = o[k].key.toJS();
262                 }
263                 if (!o[k].val.length) {
264                     console.dump(o[k]);
265                 }
266                 
267                 //console.dump(o[k]);
268                 if (o[k].val[0].data == "function") {
269                     // add comments..
270                   //   console.dump(o[k].key.prefix); 
271                     var pr = typeof(o[k].key.prefix) == 'undefined' ? '' : o[k].key.prefix ;
272                     pr = this.clean_prefix( pr) ;
273                     if (pr.length) {
274                         ret['//' +kv ] =  pr;
275                     }
276                     //print("running expand");
277                     ret['|' +kv ] =  this.clean_body( this.expand(o[k].val));
278                     continue;
279                 }
280                 
281                 
282                 if (o[k].val[0].data == "[") {
283                     
284                     if (o[k].val[0].items[0][0].data == '{') {
285                         // array of objects..
286                         
287                         // this works for items..
288                         
289                         // used elsewhere for buttons: -> in which case we have a fake xtype
290                         
291                         
292                         
293                         // if K is not ITEMS - we need to add '*prop' -> and add it to the items array..
294                         var add = this.parseArray(o[k].val[0].items);
295                         if (kv == 'items') {
296                             ret[kv] = add;
297                             continue;
298                         }
299                         var fake_array = {
300                             xtype : 'Array.' + kv,
301                             '*prop' : kv,
302                             items : add
303                         }
304                         fakeItems.push(fake_array);
305                         continue;
306                     } 
307                     // raw array 
308                     
309                     
310                     ret['|' +kv ] =  this.clean_body(this.expand(o[k].val)); // remove ','...
311                     continue;
312                 }
313                 if (o[k].val[0].data == "(") {
314                     ret['|' +kv ] =  this.expand(o[k].val);
315                     continue;
316                 }
317                 // value is an object..
318                 
319                 if (o[k].val[0].data == "{") {
320                     
321                     // things that can be property of object type:
322                     // listeners, set, 
323                     var add = this.parseProps(o[k].val[0].props);
324                     
325                     
326                     
327                     if (kv == 'set' || kv =='listeners') {
328                         ret[kv ] = add;
329                         continue;
330                     }
331                     if ((typeof(add.xtype) != 'undefined') ||  ['sortInfo', 'center', 'east', 'west', 'north', 'south'].indexOf(kv) > -1) {
332                         add['*prop'] =  kv;
333                         fakeItems.push(add);
334                         continue;
335                     }
336                     
337                     
338                     ret[ '|' + kv ] =  this.expand(o[k].val);
339                     
340                     
341                     // this hsould be added to 'items', with a *prop element..
342                     continue;
343                 }
344                 //console.dump(o[k].val);
345                 
346                 if (o[k].val[1].data == ',' || o[k].val[1].data == '}') {
347                     // single item piece of data...
348                     var t1= o[k].val[0];
349                     switch(o[k].val[0].type) {
350                         case 'STRN':
351                         case 'NUMB':
352                         case 'KEYW':
353                             ret[  kv ]  = t1.toJS();
354                             continue;
355                         case 'NAME':
356                             ret[ '|' + kv ] =  t1.data;
357                             continue;
358                         
359                     }
360                 }
361                
362                 ret[ '|' + kv ] =  this.expand(o[k].val);
363                 
364             }
365             if (!ret.items && fakeItems.length) {
366                 ret.items = [];
367             }
368             fakeItems.forEach(  function(e) {
369                 ret.items.push(e);
370             });
371             // merge fakeItems;
372             //console.dump(ret);
373             
374             return ret;
375         },
376         parseArray: function(ar) {
377           // console.dump(ar);
378            // Seed.quit();
379             var ret = [];
380             ar.map(function (e) {
381                 // has no props...
382                 if (typeof(e[0].props) == 'undefined') {
383                     return;
384                 }
385                
386                 
387                 ret.push( this.parseProps(e[0].props));
388                      
389             },this);
390              
391             return ret;
392           
393         },
394         
395         
396         expand: function(ar)
397         {
398             var ret = '';
399             //print(JSON.stringify(ar,null,4));
400             
401             for(var i =0 ; i < ar.length -1; i++) {
402                 ret += ar[i].toRaw();
403             }
404             
405             return ret;
406             
407             
408         },
409         clean_body : function(str)
410         {
411             var lns = str.split("\n");
412             var mindent = -1;
413             lns.map( function(l, i) {
414                 if (!i || !l.length || l.match(/^\s+$/)) {
415                     return;
416                 }
417                 
418                 var spc = l.match(/\s+/);
419                 if (!spc || !spc[0].length) {
420                     return;
421                 }
422                 mindent = mindent < 0 ? spc[0].length : Math.min(spc[0].length, mindent);
423                 
424             });
425             //console.log(mindent + ":" + lns[0]);
426             var ar = [];
427             if (mindent < 0) {
428                 return str;
429             }
430             lns.map( function(l,i) {
431                 if (!i) {
432                     ar.push(l.replace(/^\s+/, ''));
433                     return;
434                 }
435                 ar.push(l.substring(mindent));
436             });
437             return ar.join("\n");
438         },
439         clean_prefix: function(str) {
440             
441             
442             
443             if (!str.length) {
444                 return '';
445             }
446             var ret = str.replace(/^\s+/gm, "\n").replace(/\n+/gm, "\n");
447             return ret == "\n" ? '' : ret;
448             
449         }    
450              
451     
452 });