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