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) {
107 if (this.filename.match(/Scope\.js/)) return print(str);
113 buildSymbols : function()
115 if (!this.statements) {
116 this.statements = this.collapse(this.tokens);
117 //print(JSON.stringify(this.s, null,4));
120 //this.globalScope(this.statements);
121 this.debugCall("build Symbols");
122 //print (this.statements);
123 //print (JSON.stringify(this.statements, null,2));
125 this.walkStatements(this.global, this.statements)
137 walkStatements: function(scope, statements)
139 this.debugCall("walkStatements :" + scope ) ;
142 var isGlobal = scope == this.global;
144 statements.some(function(st ) {
145 // handle change of scope..
148 st[0].jsdoc.getTag('scope').length
150 //print(st[0].jsdoc.getTag('scope'));
151 scope = st[0].jsdoc.getTag('scope');
152 // might be an array..
153 if (typeof(scope) != 'String') {
154 scope = scope[0].desc;
161 res = _this.walkStatement(scope, st);
169 walkStatement: function(scope, stmt)
173 this.debugCall("walkStatement :" + this.global +'/' +scope + '@' + this.tokens[0].line );
178 var isGlobal = scope == this.global;
182 //if (this.filename.match(/pack\.js/) && isGlobal) {
183 // print(JSON.stringify(stmt , null,4));
185 // @ignore stops the parser..
188 stmt[0].jsdoc.getTag('ignore').length
190 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
191 print("GOT ignore?");
196 while (null != (token = this.next())) {
199 //walkFunction(scope, name , args, stmts )
201 if (token.name == "FUNCTION") {
202 // function a() { .... } << scope is a $this$={a}
203 if (this.lookTok(1).is('NAME')) {
204 name = isGlobal ? this.lookTok(1).data : '';
206 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
209 //a = function() { } << scope might be a $this$={a}
210 if (this.lookTok(-1).data == '=' &&
211 this.lookTok(-2).is('NAME')
213 name = this.lookTok(-2).data;
214 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
219 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
229 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
230 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
232 if (token.is('NAME') && this.lookTok(1).data =='(') {
234 var jsdoc = token.jsdoc;
235 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
236 assign = this.lookTok(-2).data
237 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
239 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
244 // a = { .... } << scope is a $this$={a}
245 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
247 // could be var x = ..
248 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
250 var isVar = this.lookTok(-3).name == 'VAR';
252 // only register names of objects if
253 var name = this.lookTok(-2).data;
265 if (name && !name.match(/^this\./)) {
268 if (name && name.match(/^this\./) ) {
269 // see if scope ends in prototype..
270 print("SCOPE:" + JSON.stringify(scope));
272 (scope.split('.').pop() == 'prototype') &&
273 (name.split('.').length == 2)
275 name = name.split('.').pop();
285 //print(JSON.stringify(token,null,4));
286 this.walkObject(scope, name, token.props, jd);
290 // this.xxxx = (with jsdoc...)
296 if (token.data == '{') {
297 sn = new ScopeNamer(this);
298 //print("GOT { - walkings statuements;}");
300 continue; // object..!?!?!? = ignore ???
301 print(JSON.stringify(token,null,4));
303 sn.walkStatements(scope, token.items);
309 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
311 this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
312 var scope = name.length ? (inscope + '.' + name) : inscope;
314 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
315 symbol._token = jsdocTok;
316 this.addSymbol(symbol, jsdocTok.jsdoc);
318 var sn = new ScopeNamer(this);
319 sn.walkStatements(scope, stmts);
325 walkCall : function (inscope, assign, callname, items, jsdocTok)
327 this.debugCall("walkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
328 var scope = inscope + ( assign ? ('.' + assign) : '' );
329 //scope = scope.replace(/\^_global\$\./, '');
334 // add the handers for differnt methods in here....
337 // somecall(BASE, { .. object ..})
338 // or... x = somecall(BASE, { ... object..})
339 case 'XObject.extend':
341 //print(JSON.stringify(items,null,4));
342 // SHOULD WE ADD SCOPE HERE???
343 var topscope = scope.split('.').pop();
345 scope = (topscope == items[0][0].data) ?
347 scope + '.' + items[0][0].data;
348 // 2nd arg is a object def
349 if (items[1][0].data != '{') {
353 if (assign != false && !scope.match(/\.prototype$/)) {
354 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
355 symbol._token = jsdocTok;
356 this.addSymbol(symbol, jsdocTok.jsdoc);
361 var sn = new ScopeNamer(this);
363 //print(JSON.stringify(items,null,4));
364 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
371 case 'XObject.define':
372 // func, extends, props.
374 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
375 symbol._token = jsdocTok;
378 this.addSymbol(symbol, jsdocTok.jsdoc);
379 var sn = new ScopeNamer(this);
380 //print(JSON.stringify(items, null,4));
382 sn.walkStatements(scope + '.prototype', items[0][2].items);
385 sn.walkObject(scope + '.prototype', false, items[2][0].props );
396 walkObject : function(inscope, name, items, jsdocTok)
398 var scope = inscope + (( name === false) ? '' : ('.' + name));
400 if (name !== false) {
402 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
403 symbol._token = jsdocTok;
404 this.addSymbol(symbol, jsdocTok.jsdoc);
407 // items can be false in this scenaro: a = {};
411 print(typeof(items));
412 this.debugCall("wallkObject : " + scope + '#' + name + '@' + items[Object.keys(items)[0]].key.line);
413 for( var k in items) {
414 var key = items[k].key;
415 var val = items[k].val;
418 // x : function(....)
419 if (val[0].name == 'FUNCTION' ) {
422 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
428 // x: ( .... ) conditional properties? maybe detect function?
431 // x : something else - not a function..
434 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
437 this.addSymbol(symbol, key.jsdoc);
451 addSymbol: function(symbol, comment)
453 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
454 //print("PARSER addSYMBOL : " + symbol.alias);
456 // if a symbol alias is documented more than once the last one with the user docs wins
458 // dumpe some invalid symbols..
459 var ptype_ar = symbol.alias.split(/#/);
460 if (ptype_ar.length > 2) {
464 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
465 // multiple . after #
469 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
471 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
473 if (!comment) { // we do not have a comment, and it's registered.
476 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
478 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
479 if (symbol.comment.isUserComment) { // old and new are both documented
480 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
482 else { // old is documented but new isn't
488 // we don't document anonymous things
489 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
491 // uderscored things may be treated as if they were marked private, this cascades
492 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
493 // symbol.isPrivate = true;
496 // -p flag is required to document private things
497 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
499 // ignored things are not documented, this doesn't cascade
500 if (symbol.isIgnored) return;
503 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
504 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
506 // add it to the file's list... (for dumping later..)
508 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
511 ScopeNamer.symbols.addSymbol(symbol);
513 addBuiltin : function(name) {
515 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
516 builtin.isNamespace = false;
517 builtin.srcFile = "";
518 builtin.isPrivate = false;
519 this.addSymbol(builtin);
528 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
534 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
535 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;