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