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) { return ; print(str); },
106 buildSymbols : function()
108 if (!this.statements) {
109 this.statements = this.collapse(this.tokens);
110 //print(JSON.stringify(this.s, null,4));
113 //this.globalScope(this.statements);
114 this.debugCall("build Symbols");
115 //print (this.statements);
116 //print (JSON.stringify(this.statements, null,2));
118 this.walkStatements('_global_', this.statements)
130 walkStatements: function(scope, statements)
132 this.debugCall("walkStatements :" + scope ) ;
137 statements.forEach(function(st ) {
140 res = _this.walkStatement(scope, st);
148 walkStatement: function(scope, stmt)
152 this.debugCall("walkStatement :" + scope + '@' + this.tokens[0].line );
157 var isGlobal = scope == '_global_';
161 //if (this.filename.match(/pack\.js/) && isGlobal) {
162 // print(JSON.stringify(stmt , null,4));
164 // @ignore stops the parser..
167 stmt[0].jsdoc.getTag('ignore').length
169 this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
170 print("GOT ignore?");
173 while (null != (token = this.next())) {
176 //walkFunction(scope, name , args, stmts )
178 if (token.name == "FUNCTION") {
179 // function a() { .... } << scope is a $this$={a}
180 if (this.lookTok(1).is('NAME')) {
181 name = isGlobal ? this.lookTok(1).data : '';
183 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
186 //a = function() { } << scope might be a $this$={a}
187 if (this.lookTok(-1).data == '=' &&
188 this.lookTok(-2).is('NAME')
190 name = this.lookTok(-2).data;
191 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
196 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
206 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
207 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
209 if (token.is('NAME') && this.lookTok(1).data =='(') {
211 var jsdoc = token.jsdoc;
212 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
213 assign = this.lookTok(-2).data
214 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
216 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
221 // a = { .... } << scope is a $this$={a}
222 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
224 // could be var x = ..
225 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
227 var isVar = this.lookTok(-3).name == 'VAR';
229 // only register names of objects if
230 var name = this.lookTok(-2).data;
231 name = !isGlobal && isVar ? false : name;
232 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
233 //print(JSON.stringify(token,null,4));
234 this.walkObject(scope, name, token.props, jd);
238 // this.xxxx = (with jsdoc...)
244 if (token.data == '{') {
245 sn = new ScopeNamer(this);
246 //print("GOT { - walkings statuements;}");
248 continue; // object..!?!?!? = ignore ???
249 print(JSON.stringify(token,null,4));
251 sn.walkStatements(scope, token.items);
257 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
259 this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
260 var scope = name.length ? (inscope + '.' + name) : inscope;
262 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
263 symbol._token = jsdocTok;
264 this.addSymbol(symbol, jsdocTok.jsdoc);
266 var sn = new ScopeNamer(this);
267 sn.walkStatements(scope, stmts);
273 walkCall : function (inscope, assign, callname, items, jsdocTok)
275 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
276 var scope = inscope + ( assign ? ('.' + assign) : '' );
277 //scope = scope.replace(/\^_global\$\./, '');
282 // add the handers for differnt methods in here....
285 // somecall(BASE, { .. object ..})
286 // or... x = somecall(BASE, { ... object..})
287 case 'XObject.extend':
289 //print(JSON.stringify(items,null,4));
290 scope = items[0][0].data;
291 // 2nd arg is a object def
292 if (items[1][0].data != '{') {
296 if (assign != false && !scope.match(/\.prototype$/)) {
297 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
298 symbol._token = jsdocTok;
299 this.addSymbol(symbol, jsdocTok.jsdoc);
304 var sn = new ScopeNamer(this);
306 //print(JSON.stringify(items,null,4));
307 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
314 case 'XObject.define':
315 // func, extends, props.
317 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
318 symbol._token = jsdocTok;
321 this.addSymbol(symbol, jsdocTok.jsdoc);
322 var sn = new ScopeNamer(this);
325 sn.walkStatements(scope, items[0][2].items);
326 sn.walkObject(scope + '.prototype', false, items[2].props );
337 walkObject : function(inscope, name, items, jsdocTok)
339 var scope = inscope + (( name === false) ? '' : ('.' + name));
341 if (name !== false) {
343 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
344 symbol._token = jsdocTok;
345 this.addSymbol(symbol, jsdocTok.jsdoc);
348 // items can be false in this scenaro: a = {};
352 print(typeof(items));
353 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
354 for( var k in items) {
355 var key = items[k].key;
356 var val = items[k].val;
359 // x : function(....)
360 if (val[0].name == 'FUNCTION' ) {
363 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
369 // x: ( .... ) conditional properties? maybe detect function?
372 // x : something else - not a function..
375 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
378 this.addSymbol(symbol, key.jsdoc);
392 addSymbol: function(symbol, comment)
394 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
395 //print("PARSER addSYMBOL : " + symbol.alias);
397 // if a symbol alias is documented more than once the last one with the user docs wins
399 // dumpe some invalid symbols..
400 if (symbol.alias.split(/[#.]/).length > 2) {
403 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
405 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
407 if (!comment) { // we do not have a comment, and it's registered.
410 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
412 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
413 if (symbol.comment.isUserComment) { // old and new are both documented
414 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
416 else { // old is documented but new isn't
422 // we don't document anonymous things
423 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
425 // uderscored things may be treated as if they were marked private, this cascades
426 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
427 // symbol.isPrivate = true;
430 // -p flag is required to document private things
431 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
433 // ignored things are not documented, this doesn't cascade
434 if (symbol.isIgnored) return;
437 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
438 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
440 // add it to the file's list... (for dumping later..)
442 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
445 ScopeNamer.symbols.addSymbol(symbol);
447 addBuiltin : function(name) {
449 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
450 builtin.isNamespace = false;
451 builtin.srcFile = "";
452 builtin.isPrivate = false;
453 this.addSymbol(builtin);
462 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
468 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
469 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;