1 XObject = imports.XObject.XObject;
4 console = imports.console.console;
6 Symbol = imports.Symbol.Symbol;
12 * The point of this class is to iterate through the Collapsed tree
13 * and add the property 'scopedName' to the tokens.
18 After spending some time looking at this, a few conclusions...
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
23 scopes are normally changed when these things occur:
26 globalScope(stmts) => statementsScope($global$, stmts)
30 objectScope(sname, props )
31 a = { .... } << scope is a $this$={a}
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..
42 functionScope(sname, stmts )
43 function a() { .... } << scope is a $this$={a}
44 a = function() { } << scope might be a $this$={a}
51 XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
52 aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
54 callscope(name, pref, args)
59 foo = new (function() { }
61 RETURN function(...) {
72 * sn = new ScopeName({
73 tokens : ... result from tokenizer,
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.
83 ScopeNamer = XObject.define(
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 this.global = '_global_';
95 imports.JSDOC.Collapse.Collapse,
101 * add/remove ';' after return to configure at present..
102 * @param {String} str String to output
104 debugCall : function(str) {
106 if (this.filename.match(/Scope\.js/)) return print(str);
112 buildSymbols : function()
114 if (!this.statements) {
115 this.statements = this.collapse(this.tokens);
116 //print(JSON.stringify(this.s, null,4));
119 //this.globalScope(this.statements);
120 this.debugCall("build Symbols");
121 //print (this.statements);
122 //print (JSON.stringify(this.statements, null,2));
124 this.walkStatements(this.global, this.statements)
136 walkStatements: function(scope, statements)
138 this.debugCall("walkStatements :" + scope ) ;
141 var isGlobal = scope == '_global_';
143 statements.some(function(st ) {
144 // handle change of scope..
147 st[0].jsdoc.getTag('scope').length
149 //print(st[0].jsdoc.getTag('scope'));
150 scope = st[0].jsdoc.getTag('scope');
151 // might be an array..
152 if (typeof(scope) != 'String') {
153 scope = scope[0].desc;
159 res = _this.walkStatement(scope, st);
167 walkStatement: function(scope, stmt)
171 this.debugCall("walkStatement :" + JSON.stringify(scope)+ '@' + this.tokens[0].line );
176 var isGlobal = scope == '_global_';
180 //if (this.filename.match(/pack\.js/) && isGlobal) {
181 // print(JSON.stringify(stmt , null,4));
183 // @ignore stops the parser..
186 stmt[0].jsdoc.getTag('ignore').length
188 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
189 print("GOT ignore?");
194 while (null != (token = this.next())) {
197 //walkFunction(scope, name , args, stmts )
199 if (token.name == "FUNCTION") {
200 // function a() { .... } << scope is a $this$={a}
201 if (this.lookTok(1).is('NAME')) {
202 name = isGlobal ? this.lookTok(1).data : '';
204 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
207 //a = function() { } << scope might be a $this$={a}
208 if (this.lookTok(-1).data == '=' &&
209 this.lookTok(-2).is('NAME')
211 name = this.lookTok(-2).data;
212 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
217 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
227 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
228 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
230 if (token.is('NAME') && this.lookTok(1).data =='(') {
232 var jsdoc = token.jsdoc;
233 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
234 assign = this.lookTok(-2).data
235 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
237 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
242 // a = { .... } << scope is a $this$={a}
243 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
245 // could be var x = ..
246 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
248 var isVar = this.lookTok(-3).name == 'VAR';
250 // only register names of objects if
251 var name = this.lookTok(-2).data;
252 name = !isGlobal && isVar ? false : name;
254 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
255 if (!isGlobal && name && name.match(/^this\./) ) {
256 // see if scope ends in prototype..
257 print("SCOPE:" + JSON.stringify(scope));
259 (scope.split('.').pop() == 'prototype') &&
260 (name.split('.').length == 2)
262 name = name.split('.').pop();
270 //print(JSON.stringify(token,null,4));
271 this.walkObject(scope, name, token.props, jd);
275 // this.xxxx = (with jsdoc...)
281 if (token.data == '{') {
282 sn = new ScopeNamer(this);
283 //print("GOT { - walkings statuements;}");
285 continue; // object..!?!?!? = ignore ???
286 print(JSON.stringify(token,null,4));
288 sn.walkStatements(scope, token.items);
294 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
296 this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
297 var scope = name.length ? (inscope + '.' + name) : inscope;
299 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
300 symbol._token = jsdocTok;
301 this.addSymbol(symbol, jsdocTok.jsdoc);
303 var sn = new ScopeNamer(this);
304 sn.walkStatements(scope, stmts);
310 walkCall : function (inscope, assign, callname, items, jsdocTok)
312 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
313 var scope = inscope + ( assign ? ('.' + assign) : '' );
314 //scope = scope.replace(/\^_global\$\./, '');
319 // add the handers for differnt methods in here....
322 // somecall(BASE, { .. object ..})
323 // or... x = somecall(BASE, { ... object..})
324 case 'XObject.extend':
326 //print(JSON.stringify(items,null,4));
327 // SHOULD WE ADD SCOPE HERE???
328 var topscope = scope.split('.').pop();
330 scope = (topscope == items[0][0].data) ?
332 scope + '.' + items[0][0].data;
333 // 2nd arg is a object def
334 if (items[1][0].data != '{') {
338 if (assign != false && !scope.match(/\.prototype$/)) {
339 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
340 symbol._token = jsdocTok;
341 this.addSymbol(symbol, jsdocTok.jsdoc);
346 var sn = new ScopeNamer(this);
348 //print(JSON.stringify(items,null,4));
349 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
356 case 'XObject.define':
357 // func, extends, props.
359 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
360 symbol._token = jsdocTok;
363 this.addSymbol(symbol, jsdocTok.jsdoc);
364 var sn = new ScopeNamer(this);
365 //print(JSON.stringify(items, null,4));
367 sn.walkStatements(scope + '.prototype', items[0][2].items);
370 sn.walkObject(scope + '.prototype', false, items[2][0].props );
381 walkObject : function(inscope, name, items, jsdocTok)
383 var scope = inscope + (( name === false) ? '' : ('.' + name));
385 if (name !== false) {
387 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
388 symbol._token = jsdocTok;
389 this.addSymbol(symbol, jsdocTok.jsdoc);
392 // items can be false in this scenaro: a = {};
396 print(typeof(items));
397 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
398 for( var k in items) {
399 var key = items[k].key;
400 var val = items[k].val;
403 // x : function(....)
404 if (val[0].name == 'FUNCTION' ) {
407 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
413 // x: ( .... ) conditional properties? maybe detect function?
416 // x : something else - not a function..
419 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
422 this.addSymbol(symbol, key.jsdoc);
436 addSymbol: function(symbol, comment)
438 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
439 //print("PARSER addSYMBOL : " + symbol.alias);
441 // if a symbol alias is documented more than once the last one with the user docs wins
443 // dumpe some invalid symbols..
444 var ptype_ar = symbol.alias.split(/#/);
445 if (ptype_ar.length > 2) {
449 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
450 // multiple . after #
454 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
456 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
458 if (!comment) { // we do not have a comment, and it's registered.
461 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
463 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
464 if (symbol.comment.isUserComment) { // old and new are both documented
465 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
467 else { // old is documented but new isn't
473 // we don't document anonymous things
474 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
476 // uderscored things may be treated as if they were marked private, this cascades
477 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
478 // symbol.isPrivate = true;
481 // -p flag is required to document private things
482 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
484 // ignored things are not documented, this doesn't cascade
485 if (symbol.isIgnored) return;
488 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
489 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
491 // add it to the file's list... (for dumping later..)
493 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
496 ScopeNamer.symbols.addSymbol(symbol);
498 addBuiltin : function(name) {
500 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
501 builtin.isNamespace = false;
502 builtin.srcFile = "";
503 builtin.isPrivate = false;
504 this.addSymbol(builtin);
513 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
519 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
520 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;