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             
36             if (this.tokens[0].data == 'Pman.on') {
37                 // then it's a layout style..
38                 
39                 //Seed.quit();
40                 
41                 
42                 //console.dump(this.tokens);
43                 //              the list of statements inside of function..?
44                 
45                 
46                 var stmts =  this.tokens[1].items[1][2].items;
47                 
48                 // look for register..
49                 var topp = false;
50                 stmts.map( function(s, i) {
51                     if (s[0].data == 'Pman.register') {
52                         topp = brace = s[1].items[0][0].props;
53                     }
54                 });
55                 if (!topp) {
56                     console.dump(this.tokens);
57                     throw "could not find top props...";
58                     
59                 }
60                 
61                 this.cfg = this.parseProps(topp);
62                 this.cfg.name = this.tokens[3].data;
63                 
64                 
65                 //                  (            {          add    { this.panel (  {
66                 var cfg = this.tokens[7].items[0][0].props.add.val[2].items[2][3].items[0][0].props;
67                 this.cfg.items = [ this.parseProps(cfg) ];
68                 //console.dump(this.cfg); 
69                 
70                 return;
71             }
72             
73             //print(JSON.stringify(this.tokens,null,4)); Seed.quit();
74             
75             
76             
77             // Standard Pman Dialog - 
78             if (this.tokens[2].props && typeof(this.tokens[2].props.create) != 'undefined') {
79                 //console.dump(this.tokens);
80                 this.cfg.name = this.tokens[0].data;
81                 
82                 
83                 
84                 var cfg = this.tokens[2].props.create.val[2].items[1][3].items[0][0].props;
85                 //console.dump(this.tokens);
86                 this.cfg.items = [ this.parseProps(cfg) ];
87                 return;
88                 
89             }
90             // perfect for dialogs...
91             
92             
93             var pos = this.lookFor( 'function');
94             
95             // XXXXX = function(
96             var cfg = {};
97             
98             if (pos > -1 && (this.tokens[pos-1].data == '=') && (this.tokens[pos-2].type == 'NAME')) {
99                 
100                 this.cfg = {
101                     '*class' : this.tokens[pos-2].data,
102                     '*extends' : '',
103                     '//constructor' :  this.tokens[pos-2].prefix,
104                     '|constructor' : 'function ' + this.tokens[pos+1].toRaw() + 
105                             this.tokens[pos+2].toRaw()
106                     
107                 };
108                 this.cursor = pos+2;
109                 
110             }
111             
112             if (pos < 0) {
113                 // no function - we have a static one...
114                 pos = this.lookFor( '=');
115                 if (pos > 1 && 
116                     (this.tokens[pos-1].type == 'NAME') &&
117                     (this.tokens[pos+1].data == '{')
118                 ) {
119                     this.cfg = {
120                         '*class' : this.tokens[pos-1].data,
121                         '//*class' :  this.tokens[pos-1].prefix
122                         
123                         
124                     };
125                     XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].props));
126                     return;
127                     
128                 }
129                 
130                 
131                 
132             }
133             
134             
135               // Roo.apply (l
136             var pos = this.lookFor( 'Roo.apply');
137             //console.dump(this.tokens);
138             
139             
140             if (
141                 (pos > -1) &&
142                 (this.tokens[pos+1].items[0][0].data  == this.cfg['*class'] + '.prototype')
143             ) {
144                 // XXXXXx.prototype = {
145                 
146                 
147                     
148                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[1][0].props));
149                 return;
150                 
151             }
152             
153             
154             var pos = this.lookFor('new');
155             
156             if (pos > -1 && 
157                 (this.tokens[pos-2].type == 'NAME') &&
158                 (this.tokens[pos-1].data == '=') && 
159                 (this.tokens[pos+1].type == 'NAME') &&
160                 (this.tokens[pos+2].data == '(') 
161             ) {
162                 
163                 this.cfg = {
164                     '//*class' : this.tokens[pos-2].prefix,
165                     '*class' : this.tokens[pos-2].data,
166                     '*extends' : this.tokens[pos+1].data
167                 };
168                     
169                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+2].items[0][0].props));
170                 
171                 return;
172                 
173             }
174             
175             ///Builder.Provider.ProjectManager = new Roo.Observable({
176                 
177             
178             
179             var pos = this.lookFor( 'Roo.extend');
180             if (pos > -1) {
181                 
182                 this.cfg['*extends'] = this.tokens[pos+1].items[1][0].data;
183                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[2][0].props));
184                 
185                 // no more..
186                 return;
187             }
188             
189              
190                 
191             var pos = this.lookFor( 'Roo.extend');
192             if (pos > -1) {
193                 
194                 this.cfg['*extends'] = this.tokens[pos+1].items[1][0].data;
195                 XObject.extend(this.cfg, this.parseProps(this.tokens[pos+1].items[2][0].props));
196                 
197                 // no more..
198                 return;
199             }
200             
201             
202             
203             //console.dump(cfg);
204             //Seed.quit();
205             
206             
207             
208             // other type of layout
209           //  console.dump(this.tokens);
210             
211             // this type of file..
212             // xxxx.xxx = function
213             // -> into |constructor..
214             // -> extend + prototype
215              
216             
217             
218                 
219         },
220         
221         parseProps:  function(o)
222         {
223             print(JSON.stringify(o,null,4));
224             
225             var ret = { };
226             var fakeItems =  [];
227             for(var k in o) {
228                 
229                 
230                 if (o[k].key.data == '}') {
231                     // typo trailing comma in object def.
232                     continue;
233                 }
234                 
235                 var kv = k;
236                 if (o[k].key.type == 'STRN') {
237                     kv = o[k].key.toJS();
238                 }
239                 if (!o[k].val.length) {
240                     console.dump(o[k]);
241                 }
242                 
243                 //console.dump(o[k]);
244                 if (o[k].val[0].data == "function") {
245                     // add comments..
246                   //   console.dump(o[k].key.prefix); 
247                     var pr = typeof(o[k].key.prefix) == 'undefined' ? '' : o[k].key.prefix ;
248                     pr = this.clean_prefix( pr) ;
249                     if (pr.length) {
250                         ret['//' +kv ] =  pr;
251                     }
252                     
253                     ret['|' +kv ] =  this.clean_body( this.expand(o[k].val));
254                     continue;
255                 }
256                 
257                 
258                 if (o[k].val[0].data == "[") {
259                     
260                     if (o[k].val[0].items[0][0].data == '{') {
261                         // array of objects..
262                         
263                         // this works for items..
264                         
265                         // used elsewhere for buttons: -> in which case we have a fake xtype
266                         
267                         
268                         
269                         // if K is not ITEMS - we need to add '*prop' -> and add it to the items array..
270                         var add = this.parseArray(o[k].val[0].items);
271                         if (kv == 'items') {
272                             ret[kv] = add;
273                             continue;
274                         }
275                         var fake_array = {
276                             xtype : 'Array.' + kv,
277                             '*prop' : kv,
278                             items : add
279                         }
280                         fakeItems.push(fake_array);
281                         continue;
282                     } 
283                     // raw array 
284                     
285                     
286                     ret['|' +kv ] =  this.clean_body(this.expand(o[k].val)); // remove ','...
287                     continue;
288                 }
289                 if (o[k].val[0].data == "(") {
290                     ret['|' +kv ] =  this.expand(o[k].val);
291                     continue;
292                 }
293                 // value is an object..
294                 
295                 if (o[k].val[0].data == "{") {
296                     
297                     // things that can be property of object type:
298                     // listeners, set, 
299                     var add = this.parseProps(o[k].val[0].props);
300                     
301                     
302                     
303                     if (kv == 'set' || kv =='listeners') {
304                         ret[kv ] = add;
305                         continue;
306                     }
307                     if ((typeof(add.xtype) != 'undefined') ||  ['sortInfo', 'center', 'east', 'west', 'north', 'south'].indexOf(kv) > -1) {
308                         add['*prop'] =  kv;
309                         fakeItems.push(add);
310                         continue;
311                     }
312                     
313                     
314                     ret[ '|' + kv ] =  this.expand(o[k].val);
315                     
316                     
317                     // this hsould be added to 'items', with a *prop element..
318                     continue;
319                 }
320                 //console.dump(o[k].val);
321                 
322                 if (o[k].val[1].data == ',' || o[k].val[1].data == '}') {
323                     // single item piece of data...
324                     var t1= o[k].val[0];
325                     switch(o[k].val[0].type) {
326                         case 'STRN':
327                         case 'NUMB':
328                         case 'KEYW':
329                             ret[  kv ]  = t1.toJS();
330                             continue;
331                         case 'NAME':
332                             ret[ '|' + kv ] =  t1.data;
333                             continue;
334                         
335                     }
336                 }
337                
338                 ret[ '|' + kv ] =  this.expand(o[k].val);
339                 
340             }
341             if (!ret.items && fakeItems.length) {
342                 ret.items = [];
343             }
344             fakeItems.forEach(  function(e) {
345                 ret.items.push(e);
346             });
347             // merge fakeItems;
348             //console.dump(ret);
349             
350             return ret;
351         },
352         parseArray: function(ar) {
353           // console.dump(ar);
354            // Seed.quit();
355             var ret = [];
356             ar.map(function (e) {
357                 // has no props...
358                 if (typeof(e[0].props) == 'undefined') {
359                     return;
360                 }
361                
362                 
363                 ret.push( this.parseProps(e[0].props));
364                      
365             },this);
366              
367             return ret;
368           
369         },
370         
371         
372         expand: function(ar)
373         {
374             var ret = '';
375            
376             
377             for(var i =0 ; i < ar.length -1; i++) {
378                 ret += ar[i].toRaw();
379             }
380             
381            
382            
383             return ret;
384             
385             
386         },
387         clean_body : function(str)
388         {
389             var lns = str.split("\n");
390             var mindent = -1;
391             lns.map( function(l, i) {
392                 if (!i || !l.length || l.match(/^\s+$/)) {
393                     return;
394                 }
395                 
396                 var spc = l.match(/\s+/);
397                 if (!spc || !spc[0].length) {
398                     return;
399                 }
400                 mindent = mindent < 0 ? spc[0].length : Math.min(spc[0].length, mindent);
401                 
402             });
403             //console.log(mindent + ":" + lns[0]);
404             var ar = [];
405             if (mindent < 0) {
406                 return str;
407             }
408             lns.map( function(l,i) {
409                 if (!i) {
410                     ar.push(l.replace(/^\s+/, ''));
411                     return;
412                 }
413                 ar.push(l.substring(mindent));
414             });
415             return ar.join("\n");
416         },
417         clean_prefix: function(str) {
418             
419             
420             
421             if (!str.length) {
422                 return '';
423             }
424             var ret = str.replace(/^\s+/gm, "\n").replace(/\n+/gm, "\n");
425             return ret == "\n" ? '' : ret;
426             
427         }    
428              
429     
430 });