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(/File\.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 == this.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;
160 res = _this.walkStatement(scope, st);
168 walkStatement: function(scope, stmt)
172 this.debugCall("walkStatement :" + JSON.stringify(scope)+ '@' + this.tokens[0].line );
177 var isGlobal = scope == this.global;
181 //if (this.filename.match(/pack\.js/) && isGlobal) {
182 // print(JSON.stringify(stmt , null,4));
184 // @ignore stops the parser..
187 stmt[0].jsdoc.getTag('ignore').length
189 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
190 print("GOT ignore?");
195 while (null != (token = this.next())) {
198 //walkFunction(scope, name , args, stmts )
200 if (token.name == "FUNCTION") {
201 // function a() { .... } << scope is a $this$={a}
202 if (this.lookTok(1).is('NAME')) {
203 name = isGlobal ? this.lookTok(1).data : '';
205 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
208 //a = function() { } << scope might be a $this$={a}
209 if (this.lookTok(-1).data == '=' &&
210 this.lookTok(-2).is('NAME')
212 name = this.lookTok(-2).data;
213 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
218 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
228 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
229 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
231 if (token.is('NAME') && this.lookTok(1).data =='(') {
233 var jsdoc = token.jsdoc;
234 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
235 assign = this.lookTok(-2).data
236 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
238 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
243 // a = { .... } << scope is a $this$={a}
244 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
246 // could be var x = ..
247 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
249 var isVar = this.lookTok(-3).name == 'VAR';
251 // only register names of objects if
252 var name = this.lookTok(-2).data;
264 if (name && !name.match(/^this\./)) {
267 if (name && name.match(/^this\./) ) {
268 // see if scope ends in prototype..
269 print("SCOPE:" + JSON.stringify(scope));
271 (scope.split('.').pop() == 'prototype') &&
272 (name.split('.').length == 2)
274 name = name.split('.').pop();
285 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
286 if (!isGlobal && name && name.match(/^this\./) ) {
287 // see if scope ends in prototype..
288 print("SCOPE:" + JSON.stringify(scope));
290 (scope.split('.').pop() == 'prototype') &&
291 (name.split('.').length == 2)
293 name = name.split('.').pop();
301 //print(JSON.stringify(token,null,4));
302 this.walkObject(scope, name, token.props, jd);
306 // this.xxxx = (with jsdoc...)
312 if (token.data == '{') {
313 sn = new ScopeNamer(this);
314 //print("GOT { - walkings statuements;}");
316 continue; // object..!?!?!? = ignore ???
317 print(JSON.stringify(token,null,4));
319 sn.walkStatements(scope, token.items);
325 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
327 this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
328 var scope = name.length ? (inscope + '.' + name) : inscope;
330 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
331 symbol._token = jsdocTok;
332 this.addSymbol(symbol, jsdocTok.jsdoc);
334 var sn = new ScopeNamer(this);
335 sn.walkStatements(scope, stmts);
341 walkCall : function (inscope, assign, callname, items, jsdocTok)
343 this.debugCall("walkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
344 var scope = inscope + ( assign ? ('.' + assign) : '' );
345 //scope = scope.replace(/\^_global\$\./, '');
350 // add the handers for differnt methods in here....
353 // somecall(BASE, { .. object ..})
354 // or... x = somecall(BASE, { ... object..})
355 case 'XObject.extend':
357 //print(JSON.stringify(items,null,4));
358 // SHOULD WE ADD SCOPE HERE???
359 var topscope = scope.split('.').pop();
361 scope = (topscope == items[0][0].data) ?
363 scope + '.' + items[0][0].data;
364 // 2nd arg is a object def
365 if (items[1][0].data != '{') {
369 if (assign != false && !scope.match(/\.prototype$/)) {
370 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
371 symbol._token = jsdocTok;
372 this.addSymbol(symbol, jsdocTok.jsdoc);
377 var sn = new ScopeNamer(this);
379 //print(JSON.stringify(items,null,4));
380 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
387 case 'XObject.define':
388 // func, extends, props.
390 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
391 symbol._token = jsdocTok;
394 this.addSymbol(symbol, jsdocTok.jsdoc);
395 var sn = new ScopeNamer(this);
396 //print(JSON.stringify(items, null,4));
398 sn.walkStatements(scope + '.prototype', items[0][2].items);
401 sn.walkObject(scope + '.prototype', false, items[2][0].props );
412 walkObject : function(inscope, name, items, jsdocTok)
414 var scope = inscope + (( name === false) ? '' : ('.' + name));
416 if (name !== false) {
418 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
419 symbol._token = jsdocTok;
420 this.addSymbol(symbol, jsdocTok.jsdoc);
423 // items can be false in this scenaro: a = {};
427 print(typeof(items));
428 this.debugCall("wallkObject : " + scope + '#' + name + '@' + items[Object.keys(items)[0]].key.line);
429 for( var k in items) {
430 var key = items[k].key;
431 var val = items[k].val;
434 // x : function(....)
435 if (val[0].name == 'FUNCTION' ) {
438 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
444 // x: ( .... ) conditional properties? maybe detect function?
447 // x : something else - not a function..
450 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
453 this.addSymbol(symbol, key.jsdoc);
467 addSymbol: function(symbol, comment)
469 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
470 //print("PARSER addSYMBOL : " + symbol.alias);
472 // if a symbol alias is documented more than once the last one with the user docs wins
474 // dumpe some invalid symbols..
475 var ptype_ar = symbol.alias.split(/#/);
476 if (ptype_ar.length > 2) {
480 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
481 // multiple . after #
485 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
487 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
489 if (!comment) { // we do not have a comment, and it's registered.
492 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
494 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
495 if (symbol.comment.isUserComment) { // old and new are both documented
496 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
498 else { // old is documented but new isn't
504 // we don't document anonymous things
505 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
507 // uderscored things may be treated as if they were marked private, this cascades
508 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
509 // symbol.isPrivate = true;
512 // -p flag is required to document private things
513 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
515 // ignored things are not documented, this doesn't cascade
516 if (symbol.isIgnored) return;
519 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
520 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
522 // add it to the file's list... (for dumping later..)
524 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
527 ScopeNamer.symbols.addSymbol(symbol);
529 addBuiltin : function(name) {
531 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
532 builtin.isNamespace = false;
533 builtin.srcFile = "";
534 builtin.isPrivate = false;
535 this.addSymbol(builtin);
544 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
550 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
551 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;