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