1 XObject = imports.XObject.XObject;
4 console = imports.console.console;
6 Symbol = imports.Symbol.Symbol;
11 * The point of this class is to iterate through the Collapsed tree
12 * and add the property 'scopedName' to the tokens.
17 After spending some time looking at this, a few conclusions...
19 - this will have to be the base class of specific implementations.
20 - each implementation will have to decide how to declare a new scope
22 scopes are normally changed when these things occur:
25 globalScope(stmts) => statementsScope($global$, stmts)
29 objectScope(sname, props )
30 a = { .... } << scope is a $this$={a}
33 foo: function() {} $this$={object scope}
34 foo : ( xxx ? function() { } ? function() { ...} << conditional properties types
35 foo : (function() { return x })() << a property or function??? << depends on notes
36 foo : [ ] << a property.. do not drill down?
37 foo : xxxxx << a property..
41 functionScope(sname, stmts )
42 function a() { .... } << scope is a $this$={a}
43 a = function() { } << scope might be a $this$={a}
50 XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
51 aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
53 callscope(name, pref, args)
58 foo = new (function() { }
60 RETURN function(...) {
71 * sn = new ScopeName({
72 tokens : ... result from tokenizer,
77 * @param {Array} tokens array of tokens (from collapse)
78 * @param {String} srcFile the original file
79 * @param {Array} args Local variables which do not need to be added to scope.
82 ScopeNamer = XObject.define(
86 ScopeNamer.superclass.constructor.call(this, cfg);
87 this.args = this.args ? this.args.slice(0) : [];
88 Symbol.srcFile = this.filename;
89 this.createJSDOC = true;
94 imports.JSDOC.Collapse.Collapse,
98 * add/remove ';' after return to configure at present..
99 * @param {String} str String to output
101 debugCall : function(str) { return; print(str); },
105 buildSymbols : function()
107 if (!this.statements) {
108 this.statements = this.collapse(this.tokens);
109 //print(JSON.stringify(this.s, null,4));
112 //this.globalScope(this.statements);
113 this.debugCall("build Symbols");
114 //print (this.statements);
115 //print (JSON.stringify(this.statements, null,2));
117 this.walkStatements('_global_', this.statements)
129 walkStatements: function(scope, statements)
131 this.debugCall("walkStatements :" + scope ) ;
136 statements.forEach(function(st ) {
137 res = _this.walkStatement(scope, st);
145 walkStatement: function(scope, stmt)
149 this.debugCall("walkStatement :" + scope + '@' + this.tokens[0].line );
154 var isGlobal = scope == '_global_';
157 while (null != (token = this.next())) {
160 //walkFunction(scope, name , args, stmts )
162 if (token.name == "FUNCTION") {
163 // function a() { .... } << scope is a $this$={a}
164 if (this.lookTok(1).is('NAME')) {
165 name = this.lookTok(2).data;
166 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
169 //a = function() { } << scope might be a $this$={a}
170 if (this.lookTok(-1).data == '=' &&
171 this.lookTok(-2).is('NAME')
173 name = this.lookTok(-2).data;
174 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
179 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
189 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
190 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
192 if (token.is('NAME') && this.lookTok(1).data =='(') {
194 var jsdoc = token.jsdoc;
195 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
196 assign = this.lookTok(-2).data
197 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
199 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
204 // a = { .... } << scope is a $this$={a}
205 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
207 // could be var x = ..
208 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
210 var isVar = this.lookTok(-3).name == 'VAR';
212 // only register names of objects if
213 var name = this.lookTok(-2).data;
214 name = !isGlobal && isVar ? false : name;
215 name = !isGlobal && name && !name.match(/^this\./) ? false : name;
216 //print(JSON.stringify(token,null,4));
217 this.walkObject(scope, name, token.props, jd);
221 // this.xxxx = (with jsdoc...)
227 if (token.data == '{') {
228 sn = new ScopeNamer(this);
229 //print("GOT { - walkings statuements;}");
231 continue; // object..!?!?!? = ignore ???
232 print(JSON.stringify(token,null,4));
234 sn.walkStatements(scope, token.items);
240 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
242 this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
243 var scope = inscope + '.' + name;
246 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
247 symbol._token = jsdocTok;
248 this.addSymbol(symbol, jsdocTok.jsdoc);
249 var sn = new ScopeNamer(this);
250 sn.walkStatements(scope, stmts);
256 walkCall : function (inscope, assign, callname, items, jsdocTok)
258 this.debugCall("wallkCall : " + inscope +':' + assign + '@' + this.look(0).line + ' ' + callname );
259 var scope = inscope + ( assign ? ('.' + assign) : '' );
260 //scope = scope.replace(/\^_global\$\./, '');
265 // add the handers for differnt methods in here....
268 // somecall(BASE, { .. object ..})
269 // or... x = somecall(BASE, { ... object..})
270 case 'XObject.extend':
272 //print(JSON.stringify(items,null,4));
273 scope = items[0][0].data;
274 // 2nd arg is a object def
275 if (items[1][0].data != '{') {
279 if (assign != false && !scope.match(/\.prototype$/)) {
280 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok);
281 symbol._token = jsdocTok;
282 this.addSymbol(symbol, jsdocTok.jsdoc);
287 var sn = new ScopeNamer(this);
289 //print(JSON.stringify(items,null,4));
290 sn.walkObject(scope , false, items[1][0].props, jsdocTok );
297 case 'XObject.define':
298 // func, extends, props.
300 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
301 symbol._token = jsdocTok;
304 this.addSymbol(symbol, jsdocTok.jsdoc);
305 var sn = new ScopeNamer(this);
308 sn.walkStatements(scope, items[0][2].items);
309 sn.walkObject(scope + '.prototype', false, items[2].props );
320 walkObject : function(inscope, name, items, jsdocTok)
322 var scope = inscope + (( name === false) ? '' : ('.' + name));
324 if (name !== false) {
326 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
327 symbol._token = jsdocTok;
328 this.addSymbol(symbol, jsdocTok.jsdoc);
331 // items can be false in this scenaro: a = {};
335 print(typeof(items));
336 this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
337 for( var k in items) {
338 var key = items[k].key;
339 var val = items[k].val;
342 // x : function(....)
343 if (val[0].name == 'FUNCTION' ) {
346 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
352 // x: ( .... ) conditional properties? maybe detect function?
355 // x : something else - not a function..
358 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
361 this.addSymbol(symbol, key.jsdoc);
375 addSymbol: function(symbol, comment)
377 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
378 //print("PARSER addSYMBOL : " + symbol.alias);
379 ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );
380 // if a symbol alias is documented more than once the last one with the user docs wins
381 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
383 if (!comment) { // we do not have a comment, and it's registered.
386 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
388 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
389 if (symbol.comment.isUserComment) { // old and new are both documented
390 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
392 else { // old is documented but new isn't
398 // we don't document anonymous things
399 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
401 // uderscored things may be treated as if they were marked private, this cascades
402 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
403 // symbol.isPrivate = true;
406 // -p flag is required to document private things
407 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
409 // ignored things are not documented, this doesn't cascade
410 if (symbol.isIgnored) return;
413 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
414 //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
416 // add it to the file's list... (for dumping later..)
418 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
421 ScopeNamer.symbols.addSymbol(symbol);
423 addBuiltin : function(name) {
425 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
426 builtin.isNamespace = false;
427 builtin.srcFile = "";
428 builtin.isPrivate = false;
429 this.addSymbol(builtin);
438 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
444 ScopeNamer.addSymbol = ScopeNamer.prototype.addSymbol;
445 ScopeNamer.addBuiltin = ScopeNamer.prototype.addBuiltin;