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