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;
158 res = _this.walkStatement(scope, st);
166 walkStatement: function(scope, stmt)
170 this.debugCall("walkStatement :" + JSON.stringify(scope)+ '@' + this.tokens[0].line );
175 var isGlobal = scope == '_global_';
179 //if (this.filename.match(/pack\.js/) && isGlobal) {
180 // print(JSON.stringify(stmt , null,4));
182 // @ignore stops the parser..
185 stmt[0].jsdoc.getTag('ignore').length
187 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
188 print("GOT ignore?");
193 while (null != (token = this.next())) {
196 //walkFunction(scope, name , args, stmts )
198 if (token.name == "FUNCTION") {
199 // function a() { .... } << scope is a $this$={a}
200 if (this.lookTok(1).is('NAME')) {
201 name = isGlobal ? this.lookTok(1).data : '';
203 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
206 //a = function() { } << scope might be a $this$={a}
207 if (this.lookTok(-1).data == '=' &&
208 this.lookTok(-2).is('NAME')
210 name = this.lookTok(-2).data;
211 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
216 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
226 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
227 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
229 if (token.is('NAME') && this.lookTok(1).data =='(') {
231 var jsdoc = token.jsdoc;
232 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
233 assign = this.lookTok(-2).data
234 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
236 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
241 // a = { .... } << scope is a $this$={a}
242 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
244 // could be var x = ..
245 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
247 var isVar = this.lookTok(-3).name == 'VAR';
249 // only register names of objects if
250 var name = this.lookTok(-2).data;
251 name = !isGlobal && isVar ? false : name;
253 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
254 if (!isGlobal && name && name.match(/^this\./) ) {
255 // see if scope ends in prototype..
256 print("SCOPE:" + JSON.stringify(scope));
258 (scope.split('.').pop() == 'prototype') &&
259 (name.split('.').length == 2)
261 name = name.split('.').pop();
269 //print(JSON.stringify(token,null,4));
270 this.walkObject(scope, name, token.props, jd);
274 // this.xxxx = (with jsdoc...)
280 if (token.data == '{') {
281 sn = new ScopeNamer(this);
282 //print("GOT { - walkings statuements;}");
284 continue; // object..!?!?!? = ignore ???
285 print(JSON.stringify(token,null,4));
287 sn.walkStatements(scope, token.items);
293 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
295 this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
296 var scope = name.length ? (inscope + '.' + name) : inscope;
298 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
299 symbol._token = jsdocTok;
300 this.addSymbol(symbol, jsdocTok.jsdoc);
302 var sn = new ScopeNamer(this);
303 sn.walkStatements(scope, stmts);
309 walkCall : function (inscope, assign, callname, items, jsdocTok)
311 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
312 var scope = inscope + ( assign ? ('.' + assign) : '' );
313 //scope = scope.replace(/\^_global\$\./, '');
318 // add the handers for differnt methods in here....
321 // somecall(BASE, { .. object ..})
322 // or... x = somecall(BASE, { ... object..})
323 case 'XObject.extend':
325 //print(JSON.stringify(items,null,4));
326 // SHOULD WE ADD SCOPE HERE???
327 var topscope = scope.split('.').pop();
329 scope = (topscope == items[0][0].data) ?
331 scope + '.' + items[0][0].data;
332 // 2nd arg is a object def
333 if (items[1][0].data != '{') {
337 if (assign != false && !scope.match(/\.prototype$/)) {
338 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
339 symbol._token = jsdocTok;
340 this.addSymbol(symbol, jsdocTok.jsdoc);
345 var sn = new ScopeNamer(this);
347 //print(JSON.stringify(items,null,4));
348 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
355 case 'XObject.define':
356 // func, extends, props.
358 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
359 symbol._token = jsdocTok;
362 this.addSymbol(symbol, jsdocTok.jsdoc);
363 var sn = new ScopeNamer(this);
364 //print(JSON.stringify(items, null,4));
366 sn.walkStatements(scope + '.prototype', items[0][2].items);
369 sn.walkObject(scope + '.prototype', false, items[2][0].props );
380 walkObject : function(inscope, name, items, jsdocTok)
382 var scope = inscope + (( name === false) ? '' : ('.' + name));
384 if (name !== false) {
386 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
387 symbol._token = jsdocTok;
388 this.addSymbol(symbol, jsdocTok.jsdoc);
391 // items can be false in this scenaro: a = {};
395 print(typeof(items));
396 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
397 for( var k in items) {
398 var key = items[k].key;
399 var val = items[k].val;
402 // x : function(....)
403 if (val[0].name == 'FUNCTION' ) {
406 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
412 // x: ( .... ) conditional properties? maybe detect function?
415 // x : something else - not a function..
418 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
421 this.addSymbol(symbol, key.jsdoc);
435 addSymbol: function(symbol, comment)
437 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
438 //print("PARSER addSYMBOL : " + symbol.alias);
440 // if a symbol alias is documented more than once the last one with the user docs wins
442 // dumpe some invalid symbols..
443 var ptype_ar = symbol.alias.split(/#/);
444 if (ptype_ar.length > 2) {
448 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
449 // multiple . after #
453 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
455 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
457 if (!comment) { // we do not have a comment, and it's registered.
460 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
462 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
463 if (symbol.comment.isUserComment) { // old and new are both documented
464 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
466 else { // old is documented but new isn't
472 // we don't document anonymous things
473 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
475 // uderscored things may be treated as if they were marked private, this cascades
476 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
477 // symbol.isPrivate = true;
480 // -p flag is required to document private things
481 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
483 // ignored things are not documented, this doesn't cascade
484 if (symbol.isIgnored) return;
487 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
488 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
490 // add it to the file's list... (for dumping later..)
492 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
495 ScopeNamer.symbols.addSymbol(symbol);
497 addBuiltin : function(name) {
499 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
500 builtin.isNamespace = false;
501 builtin.srcFile = "";
502 builtin.isPrivate = false;
503 this.addSymbol(builtin);
512 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
518 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
519 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;