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