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