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