JSDOC/ScopeNamer.js
[app.jsdoc] / JSDOC / ScopeNamer.js
1 XObject = imports.XObject.XObject;
2
3  
4 console     = imports.console.console; 
5
6 Symbol = imports.Symbol.Symbol;   
7
8 /**
9  * @class ScopeNamer
10  * @extends Collapse
11  * @scope JSDOC
12  * The point of this class is to iterate through the Collapsed tree
13  * and add the property 'scopedName' to the tokens.
14  *
15  *
16  *
17  
18   After spending some time looking at this, a few conclusions...
19   
20   - this will have to be the base class of specific implementations.
21   - each implementation will have to decide how to declare a new scope
22   
23   scopes are normally changed when these things occur:
24
25
26   globalScope(stmts) => statementsScope($global$, stmts)
27   
28
29   'object'
30   objectScope(sname, props  )
31   a = { .... } << scope is a  $this$={a}
32   
33      // property scopes???
34     foo: function() {} $this$={object scope}
35     foo : ( xxx ? function()  { } ? function() { ...} << conditional properties types
36     foo : (function() { return x })() << a property or function??? << depends on notes
37     foo : [ ] << a property.. do not drill down?
38     foo : xxxxx << a property..
39   
40   
41   'function' 
42   functionScope(sname,  stmts  )
43   function a() { .... } << scope is a  $this$={a}
44   a = function() { } << scope might be a  $this$={a}
45   
46   // registers 'this.*'
47   
48   
49   
50   'call' scopes
51   XX.yy(a,b, { .... }) << scope might be a or b..  $this$={a}
52   aa = XX.yy(a,b, { .... }) << scope might aa   $this$={a}
53  
54   callscope(name, pref, args)
55  
56   can do losts ofthings
57  
58    ?? classify me...
59       foo = new (function() { }
60      (function() { }    
61     RETURN function(...) {
62
63     
64  
65     
66     
67     
68     
69  *
70  *
71  * usage  :
72  * sn = new ScopeName({
73     tokens : ... result from tokenizer,
74     
75  })
76    sn.buildSymbols();
77  *
78  * @param {Array} tokens array of tokens (from collapse)
79  * @param {String} srcFile the original file
80  * @param {Array} args Local variables which do not need to be added to scope.
81  */ 
82  
83 ScopeNamer = XObject.define(
84     function (cfg)
85     {
86         
87         ScopeNamer.superclass.constructor.call(this, cfg);
88         this.args = this.args ? this.args.slice(0)  : [];
89         Symbol.srcFile = this.filename;
90         this.createJSDOC = true;
91         this.global = '_global_';
92        // console.dump(ar);
93         
94     }, 
95     imports.JSDOC.Collapse.Collapse, 
96     {
97         
98         global : false,
99         /**
100          * Call debugging
101          * add/remove ';' after return to configure at present..
102          * @param {String} str String to output
103          */
104         debugCall : function(str)  {
105             
106             if (this.filename.match(/File\.js/)) return print(str);
107             return;
108         }, 
109         
110         collapseTop : true,
111         
112         buildSymbols : function()
113         {
114             if (!this.statements) {
115                 this.statements = this.collapse(this.tokens);
116                 //print(JSON.stringify(this.s, null,4));
117             }
118             
119             //this.globalScope(this.statements);
120             this.debugCall("build Symbols");
121             //print (this.statements);
122             //print (JSON.stringify(this.statements, null,2));
123            
124             this.walkStatements(this.global, this.statements)
125             
126                 
127                 
128         },
129         
130         
131         ignore : false,
132          
133        
134          
135         
136         walkStatements: function(scope, statements)
137         {
138             this.debugCall("walkStatements :" + scope ) ;            
139             var _this = this;
140             var res = false;
141             var isGlobal = scope == this.global;
142            
143             statements.some(function(st ) {
144                 // handle change of scope..
145                 if (isGlobal &&
146                         st[0].jsdoc &&
147                         st[0].jsdoc.getTag('scope').length
148                    ) {
149                     //print(st[0].jsdoc.getTag('scope'));
150                     scope = st[0].jsdoc.getTag('scope');
151                     // might be an array..
152                     if (typeof(scope) != 'String') {
153                         scope = scope[0].desc;
154                         
155                     }
156                     this.global = scope;
157                 }
158                 
159                 
160                 res = _this.walkStatement(scope, st);
161                 if (res === true) {
162                     return true;
163                 }
164                 return false;
165             });
166         },
167         
168         walkStatement: function(scope, stmt)
169         {
170             this.tokens = stmt;
171             this.rewind();
172             this.debugCall("walkStatement :" + JSON.stringify(scope)+ '@' + this.tokens[0].line );
173              
174             var name;
175             var sn;
176             
177             var isGlobal = scope == this.global;
178             
179             
180             
181             //if (this.filename.match(/pack\.js/) && isGlobal) {
182             //    print(JSON.stringify(stmt , null,4));
183             //}
184             // @ignore stops the parser..
185             if (isGlobal &&
186                     stmt[0].jsdoc &&
187                     stmt[0].jsdoc.getTag('ignore').length
188                ) {
189                 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
190                 print("GOT ignore?");
191                 return true;
192             }
193            
194             
195             while (null != (token = this.next())) {
196                 
197                 //'function' 
198                 //walkFunction(scope, name , args,  stmts  )
199                 //
200                 if (token.name == "FUNCTION") {
201                     // function a() { .... } << scope is a  $this$={a}
202                     if (this.lookTok(1).is('NAME')) {
203                         name = isGlobal ? this.lookTok(1).data : '';
204                         
205                         this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
206                         continue;
207                     }
208                      //a = function() { } << scope might be a  $this$={a}
209                     if (this.lookTok(-1).data == '=' &&
210                         this.lookTok(-2).is('NAME')
211                     ) {
212                         name = this.lookTok(-2).data;
213                         this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
214                         continue;
215                     }
216                     
217                     
218                     print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
219                     continue;
220                      
221                 }
222                 
223                 // control flow ...
224                 
225                 
226                  
227                 // 'call' scopes
228                 // XX.yy(a,b, { .... }) << scope might be a or b..  $this$={a}
229                 // aa = XX.yy(a,b, { .... }) << scope might aa   $this$={a}
230                 
231                 if (token.is('NAME') && this.lookTok(1).data =='(') {
232                     var assign = false;
233                     var jsdoc = token.jsdoc;
234                     if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
235                         assign = this.lookTok(-2).data
236                         jsdoc = jsdoc || this.lookTok(-2).jsdoc;
237                     }
238                     this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
239                     continue;
240                 }
241                 
242                 //  'object'
243                 //   a = { .... } << scope is a  $this$={a}
244                 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
245                     
246                     // could be var x = ..
247                     var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
248                     
249                     var isVar = this.lookTok(-3).name == 'VAR';
250                     
251                     // only register names of objects if 
252                     var name = this.lookTok(-2).data;
253                     name = !isGlobal && isVar ? false : name;
254                     
255                     name = !isGlobal && name && !name.match(/^this\./) ? false : name;
256                     if (!isGlobal && name && name.match(/^this\./) ) { 
257                         // see if scope ends in prototype..
258                         print("SCOPE:" + JSON.stringify(scope));
259                         if (
260                             (scope.split('.').pop() == 'prototype') &&
261                             (name.split('.').length == 2)
262                         ){
263                             name = name.split('.').pop();
264                         } else {
265                             name = false;
266                         }
267                         
268                     }  else {
269                         name = false;
270                     }
271                     //print(JSON.stringify(token,null,4));
272                     this.walkObject(scope, name, token.props, jd);
273                     continue;
274                 }
275                  
276                 // this.xxxx = (with jsdoc...)
277                 
278                 
279                 
280                  
281                 // standard flow....
282                 if (token.data == '{') { 
283                     sn = new ScopeNamer(this);
284                     //print("GOT { - walkings statuements;}");
285                     if (!token.items) {
286                         continue; // object..!?!?!? = ignore ???
287                         print(JSON.stringify(token,null,4));
288                     }
289                     sn.walkStatements(scope, token.items);
290                     continue;
291                 }
292             }
293         },
294         
295         walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
296         {
297             this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
298             var scope = name.length ? (inscope + '.' + name) : inscope;
299             if (name.length) { 
300                 var symbol = new Symbol( scope , args || [] , "FUNCTION" ,  jsdocTok.jsdoc);
301                 symbol._token = jsdocTok;
302                 this.addSymbol(symbol, jsdocTok.jsdoc);
303             }
304             var sn = new ScopeNamer(this);
305             sn.walkStatements(scope, stmts);
306             
307         },            
308         
309         
310         
311         walkCall : function (inscope, assign, callname, items, jsdocTok)
312         {
313             this.debugCall("walkCall : " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
314             var scope = inscope + ( assign ? ('.' + assign) : '' );
315             //scope = scope.replace(/\^_global\$\./, '');
316             
317             
318             
319             
320             // add the handers for differnt methods in here....
321             switch (callname) {
322                 
323                 // somecall(BASE, { .. object ..})
324                 // or... x = somecall(BASE, { ... object..})
325                 case 'XObject.extend':
326                 case 'Roo.apply':
327                     //print(JSON.stringify(items,null,4));
328                     // SHOULD WE ADD SCOPE HERE???
329                     var topscope = scope.split('.').pop();
330                     
331                     scope = (topscope == items[0][0].data) ?
332                         items[0][0].data :
333                         scope + '.' + items[0][0].data;
334                     // 2nd arg is a object def
335                     if (items[1][0].data != '{') {
336                         return;
337                     }
338                     
339                     if (assign != false   && !scope.match(/\.prototype$/)) { 
340                         var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok);
341                         symbol._token = jsdocTok;
342                         this.addSymbol(symbol, jsdocTok.jsdoc);
343                         
344                     }
345                     
346                     
347                     var sn = new ScopeNamer(this);
348                     
349                     //print(JSON.stringify(items,null,4));
350                     sn.walkObject(scope , false, items[1][0].props, jsdocTok );
351                     
352                     return;
353                 
354                 
355                     
356                     
357                 case 'XObject.define':
358                     // func, extends, props.
359                     
360                     var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" ,  jsdocTok);
361                     symbol._token = jsdocTok;
362                     // extends = arg[1]
363                     
364                     this.addSymbol(symbol, jsdocTok.jsdoc);
365                     var sn = new ScopeNamer(this);
366                     //print(JSON.stringify(items, null,4));
367                     // ctr statements.
368                     sn.walkStatements(scope + '.prototype', items[0][2].items);
369                     
370                     
371                     sn.walkObject(scope + '.prototype', false, items[2][0].props );
372                     return;
373                 
374                 
375                 
376             }
377             
378             
379              
380         },
381                             
382         walkObject : function(inscope, name, items, jsdocTok)
383         {
384             var scope = inscope + (( name === false) ? '' : ('.' + name));
385            
386             if (name !== false) {
387                 
388                 var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok.jsdoc);
389                 symbol._token = jsdocTok;
390                 this.addSymbol(symbol, jsdocTok.jsdoc);
391                  
392             }
393             // items can be false in this scenaro: a = {};
394             if (!items) {
395                 return;
396             }
397             print(typeof(items));
398             this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
399             for( var k in items) {
400                 var key = items[k].key;
401                 var val = items[k].val;
402                 
403                 
404                 // x : function(....)
405                 if (val[0].name == 'FUNCTION' ) {
406                     
407                     
408                     this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
409
410                     
411                     continue;
412                 }
413                 
414                 // x: ( .... ) conditional  properties? maybe detect function?
415                 
416                 
417                 // x : something else - not a function..
418                 
419                 
420                 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" ,  key.jsdoc);
421                 symbol._token = key;
422                    
423                 this.addSymbol(symbol, key.jsdoc);
424                 continue;
425                  
426                 
427             }
428             
429             
430         },
431         
432         
433         
434         
435         
436         
437         addSymbol: function(symbol, comment)
438         {
439               //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
440               //print("PARSER addSYMBOL : " + symbol.alias);
441             
442             // if a symbol alias is documented more than once the last one with the user docs wins
443             
444             // dumpe some invalid symbols..
445             var ptype_ar = symbol.alias.split(/#/);
446             if (ptype_ar.length > 2) {
447                 // multiple #
448                 return;
449             }
450             if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
451                 // multiple . after #
452                 return;
453             }
454             
455             ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );       
456             
457             if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
458                 
459                 if (!comment) { // we do not have a comment, and it's registered.
460                     return;
461                 }
462                 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
463                 
464                 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
465                     if (symbol.comment.isUserComment) { // old and new are both documented
466                         this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
467                     }
468                     else { // old is documented but new isn't
469                         return;
470                     }
471                 }
472             }
473             
474             // we don't document anonymous things
475             //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
476         
477             // uderscored things may be treated as if they were marked private, this cascades
478             //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
479             //    symbol.isPrivate = true;
480             //}
481             
482             // -p flag is required to document private things
483             if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
484             
485             // ignored things are not documented, this doesn't cascade
486             if (symbol.isIgnored) return;
487             
488             
489             //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
490             //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
491             
492             // add it to the file's list... (for dumping later..)
493             if (this.srcFile) {
494                 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
495             }
496             
497             ScopeNamer.symbols.addSymbol(symbol);
498         },
499         addBuiltin : function(name) {
500
501             var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
502             builtin.isNamespace = false;
503             builtin.srcFile = "";
504             builtin.isPrivate = false;
505             this.addSymbol(builtin);
506             return builtin;
507         }
508         
509         
510          
511     }
512 );
513
514 ScopeNamer.symbols =  new  imports.SymbolSet.SymbolSet();
515
516
517
518
519     
520 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
521 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;