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