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