JSDOC/ScopeNamer.js
[app.jsdoc] / JSDOC / ScopeNamer.js
index 8cdad2e..82a6622 100644 (file)
@@ -7,6 +7,7 @@ Symbol = imports.Symbol.Symbol;
 
 /**
  * @class ScopeNamer
+ * @extends Collapse
  * @namespace JSDOC
  * The point of this class is to iterate through the Collapsed tree
  * and add the property 'scopedName' to the tokens.
@@ -76,7 +77,6 @@ Symbol = imports.Symbol.Symbol;
  *
  * @param {Array} tokens array of tokens (from collapse)
  * @param {String} srcFile the original file
- * @param {String} pscope Parent scope for all tokens (default $global$)
  * @param {Array} args Local variables which do not need to be added to scope.
  */ 
  
@@ -84,8 +84,6 @@ ScopeNamer = XObject.define(
     function (cfg)
     {
         
-        this.pscope = this.pscope || '$global$';
-        this.bscope =  this.pscope;
         ScopeNamer.superclass.constructor.call(this, cfg);
         this.args = this.args ? this.args.slice(0)  : [];
         Symbol.srcFile = this.filename;
@@ -96,6 +94,16 @@ ScopeNamer = XObject.define(
     }, 
     imports.JSDOC.Collapse.Collapse, 
     {
+        /**
+         * Call debugging
+         * add/remove ';' after return to configure at present..
+         * @param {String} str String to output
+         */
+        debugCall : function(str)  {
+            return;
+            if (!this.filename.match(/BuildDocs\.js/)) return;
+            return   print(str);
+        }, 
         
         collapseTop : true,
         
@@ -107,11 +115,11 @@ ScopeNamer = XObject.define(
             }
             
             //this.globalScope(this.statements);
-            print("build Symbols");
+            this.debugCall("build Symbols");
             //print (this.statements);
             //print (JSON.stringify(this.statements, null,2));
            
-            this.walkStatements('$global$', this.statements)
+            this.walkStatements('_global_', this.statements)
             
                 
                 
@@ -119,120 +127,27 @@ ScopeNamer = XObject.define(
         
         
         ignore : false,
-        
-        canonize : function(scope, isStatic) {
-            var s = scope.split('|');
-            var ret = [];
-            var brace = 0;
-            s.forEach(function(n) {
-                if (ret === false || ret.length > 1) {
-                    ret = false;
-                    return true;
-                }
-                if (n == '$global$') {
-                    return false;
-                }
-                if (n == '{') {
-                    brace++;
-                }
-                if (brace > 1) {
-                    ret = false;
-                    return true;
-                }
-                if (n =='(' || n == 'FUNCTION(' || n == '{') {
-                    return false;
-                }
-                if (n.match(/[^a-z\.]+/i)) {
-                    ret = false;
-                    return true;
-                }
-                if (n.match(/\.prototype$/g)) {
-                    isStatic = false;
-                }
-                n = n.replace(  /\.prototype$/g,'');
-                
-                ret.push(n);
-                return false;
-            });
-            
-            
-            if (!ret || ret.length > 2) {
-                return false;
-            }
-            var r = ret.join(isStatic ? '.' : '#');
-            
-            if (r.split('#').length > 1 ) {
-                return false;
-            }
-            
-             print("ADD:" + scope + ' => ' + r);
-            //print("CANON:"  + r);
-            
-            return r;
-            
-        },
-        
-        pscope  : '$global$',
-        /**
-         * When parser comes across a change in scope,
-         * it get's added to the current scope name here
-         *
-         *
-         * 
-         *
-         * 
-         * @param {String} n the name
-         */
-        addScopeName: function (n)
-        {
-            var ar = this.pscope.split('|');
-            if (ar[0] == '$global$') {
-                ar.shift();
-            }
-            ar.push(n);
-            this.pscope = ar.join('|');
-            return this.pscope;
-            
-            
-        },
-        
-        /**
-         * same as symbol ctr...
-         */
-        addTokenSymbol : function(   token )  {
-            
-            if (!token.scopeName) {
-                return;
-            }
-            
-            var isa = typeof(token.args) == 'undefined' ? 'OBJECT' : 'FUNCTION' ; // or a function if we are sure...
-            print("CREATE symbol: " +  isa + ' => ' + token.scopeName );
-            
-            if (token.jsdoc && token.jsdoc.getTag('event').length) {
-                // make sure it get's a distinct name..
-                var last = token.scopeName.split(/[.#]/).pop();
-                
-                token.scopeName= token.scopeName.replace(new RegExp(last+'$'), '!'+ last);
-                
-            }
-            
-            var symbol = new Symbol( token.scopeName, token.args || [] , isa ,
-                                    token.jsdoc || false);
-            
-            symbol._token = token;
-            ScopeNamer.addSymbol(symbol, token.jsdoc);
-        },
-        
-        
+         
+       
+         
         
         walkStatements: function(scope, statements)
         {
-            print("walkStatements :" + scope);            
+            this.debugCall("walkStatements :" + scope ) ;            
             var _this = this;
             var res = false;
-            
-            
-            statements.forEach(function(st ) {
+            var isGlobal = scope == '_global_';
+           
+            statements.some(function(st ) {
+                // handle change of scope..
+                if (isGlobal &&
+                        st[0].jsdoc &&
+                        st[0].jsdoc.getTag('scope').length
+                   ) {
+                    scope = st[0].jsdoc.getTag('scope');
+                }
+                
+                
                 res = _this.walkStatement(scope, st);
                 if (res === true) {
                     return true;
@@ -243,23 +158,42 @@ ScopeNamer = XObject.define(
         
         walkStatement: function(scope, stmt)
         {
-            print("walkStatement :" + scope);
             this.tokens = stmt;
             this.rewind();
+            this.debugCall("walkStatement :" + scope + '@' + this.tokens[0].line );
              
             var name;
             var sn;
             
-            while (null != (token = this.next())) {
+            var isGlobal = scope == '_global_';
+            
+            
+            
+            //if (this.filename.match(/pack\.js/) && isGlobal) {
+            //    print(JSON.stringify(stmt , null,4));
+            //}
+            // @ignore stops the parser..
+            if (isGlobal &&
+                    stmt[0].jsdoc &&
+                    stmt[0].jsdoc.getTag('ignore').length
+               ) {
+                this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
+                print("GOT ignore?");
+                return true;
+            }
+           
             
+            while (null != (token = this.next())) {
+                
                 //'function' 
                 //walkFunction(scope, name , args,  stmts  )
                 //
                 if (token.name == "FUNCTION") {
                     // function a() { .... } << scope is a  $this$={a}
                     if (this.lookTok(1).is('NAME')) {
-                        name = this.lookTok(2).data;
-                        this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, this.token);
+                        name = isGlobal ? this.lookTok(1).data : '';
+                        
+                        this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
                         continue;
                     }
                      //a = function() { } << scope might be a  $this$={a}
@@ -299,19 +233,46 @@ ScopeNamer = XObject.define(
                 //  'object'
                 //   a = { .... } << scope is a  $this$={a}
                 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
-                    this.walkObject(scope, this.lookTok(-2).data, token.props, this.lookTok(-2).jsdoc);
+                    
+                    // could be var x = ..
+                    var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
+                    
+                    var isVar = this.lookTok(-3).name == 'VAR';
+                    
+                    // only register names of objects if 
+                    var name = this.lookTok(-2).data;
+                    name = !isGlobal && isVar ? false : name;
+                    
+                    name = !isGlobal && name && !name.match(/^this\./) ? false : name;
+                    if (!isGlobal && name && name.match(/^this\./) ) { 
+                        // see if scope ends in prototype..
+                        if (
+                            (scope.split('.').pop() == 'prototype') &&
+                            (name.split('.').length == 2)
+                        ){
+                            name = name.split('.').pop();
+                            
+                        } else {
+                            name = false;
+                        }
+                        
+                    }  else {
+                        name = false;
+                    }
+                    //print(JSON.stringify(token,null,4));
+                    this.walkObject(scope, name, token.props, jd);
                     continue;
                 }
                  
                 // this.xxxx = (with jsdoc...)
                 
-                 
                 
                 
+                 
                 // standard flow....
                 if (token.data == '{') { 
                     sn = new ScopeNamer(this);
-                    print("GOT { - walkings statuements;}");
+                    //print("GOT { - walkings statuements;}");
                     if (!token.items) {
                         continue; // object..!?!?!? = ignore ???
                         print(JSON.stringify(token,null,4));
@@ -324,13 +285,13 @@ ScopeNamer = XObject.define(
         
         walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
         {
-            print("wallkFuncDef: " + inscope);
-            var scope = inscope + '.' + name;
-            
-            
-            var symbol = new Symbol( scope , args || [] , "FUNCTION" ,  jsdocTok.jsdoc);
-            symbol._token = jsdocTok;
-            ScopeNamer.addSymbol(symbol, jsdocTok.jsdoc);
+            this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
+            var scope = name.length ? (inscope + '.' + name) : inscope;
+            if (name.length) { 
+                var symbol = new Symbol( scope , args || [] , "FUNCTION" ,  jsdocTok.jsdoc);
+                symbol._token = jsdocTok;
+                this.addSymbol(symbol, jsdocTok.jsdoc);
+            }
             var sn = new ScopeNamer(this);
             sn.walkStatements(scope, stmts);
             
@@ -338,24 +299,46 @@ ScopeNamer = XObject.define(
         
         
         
-        walkCall : function (inscope, assign, callname, items, jsdoc)
+        walkCall : function (inscope, assign, callname, items, jsdocTok)
         {
-             print("wallkCall : " + inscope);
+            this.debugCall("wallkCall : " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
             var scope = inscope + ( assign ? ('.' + assign) : '' );
-            scope = scope.replace(/\^$global\$\./, '');
+            //scope = scope.replace(/\^_global\$\./, '');
+            
+            
+            
             
             // add the handers for differnt methods in here....
             switch (callname) {
+                
+                // somecall(BASE, { .. object ..})
+                // or... x = somecall(BASE, { ... object..})
                 case 'XObject.extend':
                 case 'Roo.apply':
                     //print(JSON.stringify(items,null,4));
-                    scope = items[0][0].data;
+                    // SHOULD WE ADD SCOPE HERE???
+                    var topscope = scope.split('.').pop();
+                    
+                    scope = (topscope == items[0][0].data) ?
+                        items[0][0].data :
+                        scope + '.' + items[0][0].data;
                     // 2nd arg is a object def
                     if (items[1][0].data != '{') {
                         return;
                     }
+                    
+                    if (assign != false   && !scope.match(/\.prototype$/)) { 
+                        var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok);
+                        symbol._token = jsdocTok;
+                        this.addSymbol(symbol, jsdocTok.jsdoc);
+                        
+                    }
+                    
+                    
                     var sn = new ScopeNamer(this);
-                    sn.walkObject(scope + '.prototype', items[1][0].props );
+                    
+                    //print(JSON.stringify(items,null,4));
+                    sn.walkObject(scope , false, items[1][0].props, jsdocTok );
                     
                     return;
                 
@@ -365,14 +348,18 @@ ScopeNamer = XObject.define(
                 case 'XObject.define':
                     // func, extends, props.
                     
-                    var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" ,  jsdoc);
-                    symbol._token = this.lookTok(0);
+                    var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" ,  jsdocTok);
+                    symbol._token = jsdocTok;
                     // extends = arg[1]
                     
-                    ScopeNamer.addSymbol(symbol, jsdoc);
+                    this.addSymbol(symbol, jsdocTok.jsdoc);
                     var sn = new ScopeNamer(this);
-                    sn.walkStatements(scope, token.items);
-                    sn.walkObject(scope + '.prototype', items[2] )
+                    //print(JSON.stringify(items, null,4));
+                    // ctr statements.
+                    sn.walkStatements(scope + '.prototype', items[0][2].items);
+                    
+                    
+                    sn.walkObject(scope + '.prototype', false, items[2][0].props );
                     return;
                 
                 
@@ -382,17 +369,33 @@ ScopeNamer = XObject.define(
             
              
         },
-        walkObject : function(scope, items)
+                            
+        walkObject : function(inscope, name, items, jsdocTok)
         {
-            print("wallkObject : " + scope);
+            var scope = inscope + (( name === false) ? '' : ('.' + name));
+           
+            if (name !== false) {
+                
+                var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok.jsdoc);
+                symbol._token = jsdocTok;
+                this.addSymbol(symbol, jsdocTok.jsdoc);
+                 
+            }
+            // items can be false in this scenaro: a = {};
+            if (!items) {
+                return;
+            }
+            print(typeof(items));
+            this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
             for( var k in items) {
                 var key = items[k].key;
                 var val = items[k].val;
                 
+                
                 // x : function(....)
                 if (val[0].name == 'FUNCTION' ) {
                     
-                  
+                    
                     this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
 
                     
@@ -408,16 +411,93 @@ ScopeNamer = XObject.define(
                 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" ,  key.jsdoc);
                 symbol._token = key;
                    
-                ScopeNamer.addSymbol(key, key.jsdoc);
+                this.addSymbol(symbol, key.jsdoc);
                 continue;
                  
                 
             }
             
             
+        },
+        
+        
+        
+        
+        
+        
+        addSymbol: function(symbol, comment)
+        {
+              //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
+              //print("PARSER addSYMBOL : " + symbol.alias);
+            
+            // if a symbol alias is documented more than once the last one with the user docs wins
+            
+            // dumpe some invalid symbols..
+            var ptype_ar = symbol.alias.split(/#/);
+            if (ptype_ar.length > 2) {
+                // multiple #
+                return;
+            }
+            if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
+                // multiple . after #
+                return;
+            }
+            
+            ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );       
+            
+            if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
+                
+                if (!comment) { // we do not have a comment, and it's registered.
+                    return;
+                }
+                var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
+                
+                if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
+                    if (symbol.comment.isUserComment) { // old and new are both documented
+                        this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
+                    }
+                    else { // old is documented but new isn't
+                        return;
+                    }
+                }
+            }
+            
+            // we don't document anonymous things
+            //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
+        
+            // uderscored things may be treated as if they were marked private, this cascades
+            //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
+            //    symbol.isPrivate = true;
+            //}
+            
+            // -p flag is required to document private things
+            if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
+            
+            // ignored things are not documented, this doesn't cascade
+            if (symbol.isIgnored) return;
+            
+            
+            //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
+            //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
+            
+            // add it to the file's list... (for dumping later..)
+            if (this.srcFile) {
+                ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
+            }
+            
+            ScopeNamer.symbols.addSymbol(symbol);
+        },
+        addBuiltin : function(name) {
+
+            var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
+            builtin.isNamespace = false;
+            builtin.srcFile = "";
+            builtin.isPrivate = false;
+            this.addSymbol(builtin);
+            return builtin;
         }
         
-         
+        
          
     }
 );
@@ -428,61 +508,5 @@ ScopeNamer.symbols =  new  imports.SymbolSet.SymbolSet();
 
 
     
-ScopeNamer.addSymbol = function(symbol, comment)
-{
-             //print("PARSER addSYMBOL : " + symbol.alias);
-            
-    // if a symbol alias is documented more than once the last one with the user docs wins
-    if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
-        
-        if (!comment) { // we do not have a comment, and it's registered.
-            return;
-        }
-        var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
-        
-        if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
-            if (symbol.comment.isUserComment) { // old and new are both documented
-                this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
-            }
-            else { // old is documented but new isn't
-                return;
-            }
-        }
-    }
-    
-    // we don't document anonymous things
-    //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
-
-    // uderscored things may be treated as if they were marked private, this cascades
-    //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
-    //    symbol.isPrivate = true;
-    //}
-    
-    // -p flag is required to document private things
-    if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
-    
-    // ignored things are not documented, this doesn't cascade
-    if (symbol.isIgnored) return;
-    
-    
-    //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
-    print("ADD symbol: " + JSON.stringify( symbol, null, 4));
-    
-    // add it to the file's list... (for dumping later..)
-    if (this.srcFile) {
-        ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
-    }
-    
-    ScopeNamer.symbols.addSymbol(symbol);
-};
-
-ScopeNamer.addBuiltin = function(name) {
-
-    var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
-    builtin.isNamespace = false;
-    builtin.srcFile = "";
-    builtin.isPrivate = false;
-    this.addSymbol(builtin);
-    return builtin;
-};
-      
+ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
+ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin; 
\ No newline at end of file