1 XObject = imports.XObject.XObject;
4 console = imports.console.console;
6 Symbol = imports.Symbol.Symbol;
10 * @extends JSDOC.Collapse
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_';
92 this.locals = this.locals || [];
96 imports.JSDOC.Collapse.Collapse,
102 * add/remove ';' after return to configure at present..
103 * @param {String} str String to output
105 debugCall : function(str) {
108 // if (this.filename.match(/Date\.js/)) return print(str);
114 buildSymbols : function()
116 if (!this.statements) {
117 this.statements = this.collapse(this.tokens);
118 //print(JSON.stringify(this.s, null,4));
120 //if (this.filename.match(/Roo\.js/)) print(JSON.stringify(this.statements, null,4));
121 //this.globalScope(this.statements);
122 this.debugCall("build Symbols");
123 //print (this.statements);
124 //print (JSON.stringify(this.statements, null,2));
126 this.walkStatements(this.global, this.statements)
138 walkStatements: function(scope, statements)
140 this.debugCall("walkStatements :" + scope + '@' + statements[0][0].line) ;
143 var isGlobal = scope == this.global;
147 statements.some(function(st ) {
148 // handle change of scope..
151 st[0].jsdoc.getTag('scope').length
155 scope = st[0].jsdoc.getTag('scope');
156 // might be an array..
157 if (typeof(scope) != 'String') {
158 scope = scope[0].desc;
161 print("SCOPE CHANGE TO:" + scope);
162 _this.global = scope;
166 res = _this.walkStatement(scope, st);
174 walkStatement: function(scope, stmt)
178 this.debugCall("walkStatement :" + this.global +'/' +scope + '@' + this.tokens[0].line );
184 var isGlobal = scope == this.global;
188 //if (this.filename.match(/pack\.js/) && isGlobal) {
189 // print(JSON.stringify(stmt , null,4));
191 // @ignore stops the parser..
194 stmt[0].jsdoc.getTag('ignore').length
196 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
197 print("GOT ignore?");
202 while (null != (token = this.next())) {
210 //walkFunction(scope, name , args, stmts )
212 if (token.name == "FUNCTION") {
213 // function a() { .... } << scope is a $this$={a}
214 if (this.lookTok(1).is('NAME')) {
215 name = isGlobal ? this.lookTok(1).data : '';
217 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
223 //a = function() { } << scope might be a $this$={a}
224 if (this.lookTok(-1).data == '=' &&
225 this.lookTok(-2).is('NAME')
228 name = this.lookTok(-2).data;
232 if (this.lookTok(-3).name == 'VAR') {
235 if (name.match(/^this\./) && scope.match(/\.prototype$/)) {
236 name = name.substring(5);
238 if (name.split('.').length > 1) {
245 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
252 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
262 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
263 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
265 if (token.is('NAME') && this.lookTok(1).data =='(') {
267 var jsdoc = token.jsdoc;
268 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
269 assign = this.lookTok(-2).data
270 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
272 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
279 // a = { .... } << scope is a $this$={a}
280 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
282 // could be var x = ..
283 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
285 var isVar = this.lookTok(-3).name == 'VAR';
287 // only register names of objects if
288 var name = this.lookTok(-2).data;
290 // some a = {} can be hidden
291 // using @type {string} for example.
292 // FIXME - they should be registered as a property...
293 if (jd && jd.jsdoc && jd.jsdoc.getTag('type').length) {
297 // wipe out the children..
301 print(JSON.stringify({
311 if (name && this.locals.indexOf(name) < 0) {
312 this.locals.push(name);
317 if (this.locals.indexOf(name) > -1 ) {
322 if (name && !name.match(/^this\./)) {
325 if (name && name.match(/^this\./) ) {
326 // see if scope ends in prototype..
327 //print("q" + JSON.stringify(scope));
329 (scope.split('.').pop() == 'prototype') &&
330 (name.split('.').length == 2)
332 name = name.split('.').pop();
342 //print(JSON.stringify(token,null,4));
343 this.walkObject(scope, name, token.props, jd);
349 // it's done on the third element to enable object assignment to work above..
351 if (this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
355 // could be var x = ..
356 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
358 var isVar = this.lookTok(-3).name == 'VAR';
360 // only register names of objects if
361 var name = this.lookTok(-2).data;
363 // some a = {} can be hidden
364 // using @type {string} for example.
365 // FIXME - they should be registered as a property...
366 if (jd && jd.jsdoc && jd.jsdoc.getTag('type').length) {
370 // wipe out the children..
374 print(JSON.stringify({
382 if (token.data.match(/imports\./)) {
390 if (name && this.locals.indexOf(name) < 0) {
391 this.locals.push(name);
396 if (this.locals.indexOf(name) > -1 ) {
400 if (name && !name.match(/^this\./)) {
405 if (name && name.match(/^this\./) ) {
406 // see if scope ends in prototype..
407 //print("SCOPE:" + JSON.stringify(scope));
409 (scope.split('.').pop() == 'prototype') &&
410 (name.split('.').length == 2)
412 name = name.split('.').pop();
420 // should not happen...
421 if (name && name.match(/^this\./)) {
428 var symbol = new Symbol( scope +'.'+ name , false , "PROPERTY" , jd.jsdoc);
429 symbol._token = this.lookTok(-2);
431 this.addSymbol(symbol, jd.jsdoc);
440 // this.xxxx = (with jsdoc...)
445 // standard flow... - ignore opening {.
446 if (token.data == '{') {
447 sn = new ScopeNamer(this);
448 //print("GOT { - walkings statuements;}");
450 continue; // object..!?!?!? = ignore ???
451 print(JSON.stringify(token,null,4));
455 //print ("GOT { = running walk statemengs on children?} @" + this.cursor);
456 sn.walkStatements(scope, token.items);
459 if (this.filename.match(/Roo\.js/) && token.data == '(') {
460 //print(JSON.stringify(token,null,4));
464 if ((token.data == '(') && (token.items[0][0].data == 'function')) {
465 sn = new ScopeNamer(this);
466 sn.walkStatements('_private_', token.items[0][2].items);
475 this.debugCall("walkStatement (END):" + this.global +'/' +scope + '@' + this.tokens[0].line );
479 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
481 this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
482 var scope = name.length ? (inscope + '.' + name) : inscope;
484 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
485 symbol._token = jsdocTok;
486 this.addSymbol(symbol, jsdocTok.jsdoc);
488 var sn = new ScopeNamer(this);
490 sn.walkStatements(scope, stmts);
491 this.debugCall("wallkFuncDef: (END)" + inscope + '#' + name + '@' + this.look(0).line );
496 walkCall : function (inscope, assign, callname, items, jsdocTok)
498 this.debugCall("walkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
499 var scope = inscope + ( assign ? ('.' + assign) : '' );
500 //scope = scope.replace(/\^_global\$\./, '');
505 // add the handers for differnt methods in here....
508 // somecall(BASE, { .. object ..})
509 // or... x = somecall(BASE, { ... object..})
510 case 'XObject.extend':
513 //print(JSON.stringify(items,null,4));
514 // SHOULD WE ADD SCOPE HERE???
516 var arg0 = items[0][0].data;
518 var topscope = scope.split('.').pop();
520 scope = (topscope == arg0) ? arg0 : scope + '.' + arg0;
522 // see if arg0 is defined has a symbol..
523 if (this.haveSymbol(arg0)) {
528 // 2nd arg is a object def
529 if (items[1][0].data != '{') {
533 if (assign != false && !scope.match(/\.prototype$/)) {
534 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
535 symbol._token = jsdocTok;
536 this.addSymbol(symbol, jsdocTok.jsdoc);
541 var sn = new ScopeNamer(this);
543 //print(JSON.stringify(items,null,4));
544 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
545 this.debugCall("walkCall(END): " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
552 case 'XObject.define':
553 // func, extends, props.
555 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
556 symbol._token = jsdocTok;
559 this.addSymbol(symbol, jsdocTok.jsdoc);
560 var sn = new ScopeNamer(this);
561 //print(JSON.stringify(items, null,4));
563 sn.walkStatements(scope + '.prototype', items[0][2].items);
566 sn.walkObject(scope + '.prototype', false, items[2][0].props );
567 this.debugCall("walkCall(END): " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
575 this.debugCall("walkCall(END): " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
580 walkObject : function(inscope, name, items, jsdocTok)
582 var scope = inscope + (( name === false) ? '' : ('.' + name));
584 if (name !== false) {
586 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
587 symbol._token = jsdocTok;
588 this.addSymbol(symbol, jsdocTok.jsdoc);
591 // items can be false in this scenaro: a = {};
595 // our other scenario is where something is flagged as private, we will not investigate further
596 // we do add it however..
597 if (jsdocTok && jsdocTok.jsdoc && jsdocTok.jsdoc.getTag('private').length) {
601 //print(typeof(items));
602 this.debugCall("wallkObject : " + scope + '#' + name + '@' + items[Object.keys(items)[0]].key.line);
603 for( var k in items) {
604 var key = items[k].key;
605 var val = items[k].val;
608 // x : function(....)
609 if (val[0].name == 'FUNCTION' ) {
612 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
618 // x: ( .... ) conditional properties? maybe detect function?
621 // x : something else - not a function..
624 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
627 this.addSymbol(symbol, key.jsdoc);
641 addSymbol: function(symbol, comment)
643 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
644 //print("PARSER addSYMBOL : " + symbol.alias);
646 // if a symbol alias is documented more than once the last one with the user docs wins
648 // dumpe some invalid symbols..
649 var ptype_ar = symbol.alias.split(/#/);
650 if (ptype_ar.length > 2) {
654 if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
655 // multiple . after #
659 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
661 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
663 if (!comment) { // we do not have a comment, and it's registered.
666 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
668 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
669 if (symbol.comment.isUserComment) { // old and new are both documented
670 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
672 else { // old is documented but new isn't
678 // we don't document anonymous things
679 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
681 // uderscored things may be treated as if they were marked private, this cascades
682 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
683 // symbol.isPrivate = true;
685 if (ScopeNamer.builder.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
686 symbol.isPrivate = true;
688 // -p flag is required to document private things
689 //if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
691 // ignored things are not documented, this doesn't cascade
692 if (symbol.isIgnored) return;
695 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
696 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
698 // add it to the file's list... (for dumping later..)
700 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
703 ScopeNamer.symbols.addSymbol(symbol);
706 haveSymbol: function(name)
708 return ScopeNamer.symbols.hasSymbol(name);
712 addBuiltin : function(name) {
714 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
715 builtin.isNamespace = false;
716 builtin.srcFile = "";
717 builtin.isPrivate = false;
718 this.addSymbol(builtin);
727 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
733 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
734 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;