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;
95 imports.JSDOC.Collapse.Collapse,
99 * add/remove ';' after return to configure at present..
100 * @param {String} str String to output
102 debugCall : function(str) {
104 if (!this.filename.match(/Identifier\.js/)) return;
110 buildSymbols : function()
112 if (!this.statements) {
113 this.statements = this.collapse(this.tokens);
114 //print(JSON.stringify(this.s, null,4));
117 //this.globalScope(this.statements);
118 this.debugCall("build Symbols");
119 //print (this.statements);
120 //print (JSON.stringify(this.statements, null,2));
122 this.walkStatements('_global_', this.statements)
134 walkStatements: function(scope, statements)
136 this.debugCall("walkStatements :" + scope ) ;
139 var isGlobal = scope == '_global_';
141 statements.some(function(st ) {
142 // handle change of scope..
145 st[0].jsdoc.getTag('scope').length
147 print(st[0].jsdoc.getTag('scope'));
148 scope = st[0].jsdoc.getTag('scope');
152 res = _this.walkStatement(scope, st);
160 walkStatement: function(scope, stmt)
164 this.debugCall("walkStatement :" + JSON.stringify(scope)+ '@' + this.tokens[0].line );
169 var isGlobal = scope == '_global_';
173 //if (this.filename.match(/pack\.js/) && isGlobal) {
174 // print(JSON.stringify(stmt , null,4));
176 // @ignore stops the parser..
179 stmt[0].jsdoc.getTag('ignore').length
181 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
182 print("GOT ignore?");
187 while (null != (token = this.next())) {
190 //walkFunction(scope, name , args, stmts )
192 if (token.name == "FUNCTION") {
193 // function a() { .... } << scope is a $this$={a}
194 if (this.lookTok(1).is('NAME')) {
195 name = isGlobal ? this.lookTok(1).data : '';
197 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
200 //a = function() { } << scope might be a $this$={a}
201 if (this.lookTok(-1).data == '=' &&
202 this.lookTok(-2).is('NAME')
204 name = this.lookTok(-2).data;
205 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
210 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
220 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
221 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
223 if (token.is('NAME') && this.lookTok(1).data =='(') {
225 var jsdoc = token.jsdoc;
226 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
227 assign = this.lookTok(-2).data
228 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
230 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
235 // a = { .... } << scope is a $this$={a}
236 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
238 // could be var x = ..
239 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
241 var isVar = this.lookTok(-3).name == 'VAR';
243 // only register names of objects if
244 var name = this.lookTok(-2).data;
245 name = !isGlobal && isVar ? false : name;
247 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
248 if (!isGlobal && name && name.match(/^this\./) ) {
249 // see if scope ends in prototype..
250 print("SCOPE:" + JSON.stringify(scope));
252 (scope.split('.').pop() == 'prototype') &&
253 (name.split('.').length == 2)
255 name = name.split('.').pop();
263 //print(JSON.stringify(token,null,4));
264 this.walkObject(scope, name, token.props, jd);
268 // this.xxxx = (with jsdoc...)
274 if (token.data == '{') {
275 sn = new ScopeNamer(this);
276 //print("GOT { - walkings statuements;}");
278 continue; // object..!?!?!? = ignore ???
279 print(JSON.stringify(token,null,4));
281 sn.walkStatements(scope, token.items);
287 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
289 this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
290 var scope = name.length ? (inscope + '.' + name) : inscope;
292 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
293 symbol._token = jsdocTok;
294 this.addSymbol(symbol, jsdocTok.jsdoc);
296 var sn = new ScopeNamer(this);
297 sn.walkStatements(scope, stmts);
303 walkCall : function (inscope, assign, callname, items, jsdocTok)
305 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
306 var scope = inscope + ( assign ? ('.' + assign) : '' );
307 //scope = scope.replace(/\^_global\$\./, '');
312 // add the handers for differnt methods in here....
315 // somecall(BASE, { .. object ..})
316 // or... x = somecall(BASE, { ... object..})
317 case 'XObject.extend':
319 //print(JSON.stringify(items,null,4));
320 // SHOULD WE ADD SCOPE HERE???
321 var topscope = scope.split('.').pop();
323 scope = (topscope == items[0][0].data) ?
325 scope + '.' + items[0][0].data;
326 // 2nd arg is a object def
327 if (items[1][0].data != '{') {
331 if (assign != false && !scope.match(/\.prototype$/)) {
332 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
333 symbol._token = jsdocTok;
334 this.addSymbol(symbol, jsdocTok.jsdoc);
339 var sn = new ScopeNamer(this);
341 //print(JSON.stringify(items,null,4));
342 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
349 case 'XObject.define':
350 // func, extends, props.
352 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
353 symbol._token = jsdocTok;
356 this.addSymbol(symbol, jsdocTok.jsdoc);
357 var sn = new ScopeNamer(this);
358 //print(JSON.stringify(items, null,4));
360 sn.walkStatements(scope + '.prototype', items[0][2].items);
363 sn.walkObject(scope + '.prototype', false, items[2][0].props );
374 walkObject : function(inscope, name, items, jsdocTok)
376 var scope = inscope + (( name === false) ? '' : ('.' + name));
378 if (name !== false) {
380 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
381 symbol._token = jsdocTok;
382 this.addSymbol(symbol, jsdocTok.jsdoc);
385 // items can be false in this scenaro: a = {};
389 print(typeof(items));
390 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
391 for( var k in items) {
392 var key = items[k].key;
393 var val = items[k].val;
396 // x : function(....)
397 if (val[0].name == 'FUNCTION' ) {
400 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
406 // x: ( .... ) conditional properties? maybe detect function?
409 // x : something else - not a function..
412 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
415 this.addSymbol(symbol, key.jsdoc);
429 addSymbol: function(symbol, comment)
431 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
432 //print("PARSER addSYMBOL : " + symbol.alias);
434 // if a symbol alias is documented more than once the last one with the user docs wins
436 // dumpe some invalid symbols..
437 var ptype_ar = symbol.alias.split(/#/);
438 if (ptype_ar.length > 2) {
442 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
443 // multiple . after #
447 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
449 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
451 if (!comment) { // we do not have a comment, and it's registered.
454 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
456 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
457 if (symbol.comment.isUserComment) { // old and new are both documented
458 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
460 else { // old is documented but new isn't
466 // we don't document anonymous things
467 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
469 // uderscored things may be treated as if they were marked private, this cascades
470 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
471 // symbol.isPrivate = true;
474 // -p flag is required to document private things
475 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
477 // ignored things are not documented, this doesn't cascade
478 if (symbol.isIgnored) return;
481 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
482 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
484 // add it to the file's list... (for dumping later..)
486 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
489 ScopeNamer.symbols.addSymbol(symbol);
491 addBuiltin : function(name) {
493 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
494 builtin.isNamespace = false;
495 builtin.srcFile = "";
496 builtin.isPrivate = false;
497 this.addSymbol(builtin);
506 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
512 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
513 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;