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