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