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) {
103 if (!this.filename.match(/BuildDocs\.js/)) return;
109 buildSymbols : function()
111 if (!this.statements) {
112 this.statements = this.collapse(this.tokens);
113 //print(JSON.stringify(this.s, null,4));
116 //this.globalScope(this.statements);
117 this.debugCall("build Symbols");
118 //print (this.statements);
119 //print (JSON.stringify(this.statements, null,2));
121 this.walkStatements('_global_', this.statements)
133 walkStatements: function(scope, statements)
135 this.debugCall("walkStatements :" + scope ) ;
138 var isGlobal = scope == '_global_';
140 statements.some(function(st ) {
141 // handle change of scope..
144 st[0].jsdoc.getTag('scope').length
146 scope = st[0].jsdoc.getTag('scope');
150 res = _this.walkStatement(scope, st);
158 walkStatement: function(scope, stmt)
162 this.debugCall("walkStatement :" + scope + '@' + this.tokens[0].line );
167 var isGlobal = scope == '_global_';
171 //if (this.filename.match(/pack\.js/) && isGlobal) {
172 // print(JSON.stringify(stmt , null,4));
174 // @ignore stops the parser..
177 stmt[0].jsdoc.getTag('ignore').length
179 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
180 print("GOT ignore?");
185 while (null != (token = this.next())) {
188 //walkFunction(scope, name , args, stmts )
190 if (token.name == "FUNCTION") {
191 // function a() { .... } << scope is a $this$={a}
192 if (this.lookTok(1).is('NAME')) {
193 name = isGlobal ? this.lookTok(1).data : '';
195 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
198 //a = function() { } << scope might be a $this$={a}
199 if (this.lookTok(-1).data == '=' &&
200 this.lookTok(-2).is('NAME')
202 name = this.lookTok(-2).data;
203 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
208 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
218 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
219 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
221 if (token.is('NAME') && this.lookTok(1).data =='(') {
223 var jsdoc = token.jsdoc;
224 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
225 assign = this.lookTok(-2).data
226 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
228 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
233 // a = { .... } << scope is a $this$={a}
234 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
236 // could be var x = ..
237 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
239 var isVar = this.lookTok(-3).name == 'VAR';
241 // only register names of objects if
242 var name = this.lookTok(-2).data;
243 name = !isGlobal && isVar ? false : name;
244 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
245 //print(JSON.stringify(token,null,4));
246 this.walkObject(scope, name, token.props, jd);
250 // this.xxxx = (with jsdoc...)
256 if (token.data == '{') {
257 sn = new ScopeNamer(this);
258 //print("GOT { - walkings statuements;}");
260 continue; // object..!?!?!? = ignore ???
261 print(JSON.stringify(token,null,4));
263 sn.walkStatements(scope, token.items);
269 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
271 this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
272 var scope = name.length ? (inscope + '.' + name) : inscope;
274 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
275 symbol._token = jsdocTok;
276 this.addSymbol(symbol, jsdocTok.jsdoc);
278 var sn = new ScopeNamer(this);
279 sn.walkStatements(scope, stmts);
285 walkCall : function (inscope, assign, callname, items, jsdocTok)
287 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
288 var scope = inscope + ( assign ? ('.' + assign) : '' );
289 //scope = scope.replace(/\^_global\$\./, '');
294 // add the handers for differnt methods in here....
297 // somecall(BASE, { .. object ..})
298 // or... x = somecall(BASE, { ... object..})
299 case 'XObject.extend':
301 //print(JSON.stringify(items,null,4));
302 scope = items[0][0].data;
303 // 2nd arg is a object def
304 if (items[1][0].data != '{') {
308 if (assign != false && !scope.match(/\.prototype$/)) {
309 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
310 symbol._token = jsdocTok;
311 this.addSymbol(symbol, jsdocTok.jsdoc);
316 var sn = new ScopeNamer(this);
318 //print(JSON.stringify(items,null,4));
319 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
326 case 'XObject.define':
327 // func, extends, props.
329 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
330 symbol._token = jsdocTok;
333 this.addSymbol(symbol, jsdocTok.jsdoc);
334 var sn = new ScopeNamer(this);
337 sn.walkStatements(scope, items[0][2].items);
338 sn.walkObject(scope + '.prototype', false, items[2].props );
349 walkObject : function(inscope, name, items, jsdocTok)
351 var scope = inscope + (( name === false) ? '' : ('.' + name));
353 if (name !== false) {
355 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
356 symbol._token = jsdocTok;
357 this.addSymbol(symbol, jsdocTok.jsdoc);
360 // items can be false in this scenaro: a = {};
364 print(typeof(items));
365 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
366 for( var k in items) {
367 var key = items[k].key;
368 var val = items[k].val;
371 // x : function(....)
372 if (val[0].name == 'FUNCTION' ) {
375 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
381 // x: ( .... ) conditional properties? maybe detect function?
384 // x : something else - not a function..
387 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
390 this.addSymbol(symbol, key.jsdoc);
404 addSymbol: function(symbol, comment)
406 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
407 //print("PARSER addSYMBOL : " + symbol.alias);
409 // if a symbol alias is documented more than once the last one with the user docs wins
411 // dumpe some invalid symbols..
412 if (symbol.alias.split(/[#.]/).length > 2) {
415 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
417 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
419 if (!comment) { // we do not have a comment, and it's registered.
422 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
424 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
425 if (symbol.comment.isUserComment) { // old and new are both documented
426 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
428 else { // old is documented but new isn't
434 // we don't document anonymous things
435 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
437 // uderscored things may be treated as if they were marked private, this cascades
438 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
439 // symbol.isPrivate = true;
442 // -p flag is required to document private things
443 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
445 // ignored things are not documented, this doesn't cascade
446 if (symbol.isIgnored) return;
449 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
450 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
452 // add it to the file's list... (for dumping later..)
454 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
457 ScopeNamer.symbols.addSymbol(symbol);
459 addBuiltin : function(name) {
461 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
462 builtin.isNamespace = false;
463 builtin.srcFile = "";
464 builtin.isPrivate = false;
465 this.addSymbol(builtin);
474 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
480 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
481 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;