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