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 {String} pscope Parent scope for all tokens (default $global$)
80 * @param {Array} args Local variables which do not need to be added to scope.
83 ScopeNamer = XObject.define(
87 this.pscope = this.pscope || '$global$';
88 this.bscope = this.pscope;
89 ScopeNamer.superclass.constructor.call(this, cfg);
90 this.args = this.args ? this.args.slice(0) : [];
91 Symbol.srcFile = this.filename;
92 this.createJSDOC = true;
97 imports.JSDOC.Collapse.Collapse,
102 buildSymbols : function()
104 if (!this.statements) {
105 this.statements = this.collapse(this.tokens);
106 //print(JSON.stringify(this.s, null,4));
109 //this.globalScope(this.statements);
110 print("build Symbols");
111 //print (this.statements);
112 //print (JSON.stringify(this.statements, null,2));
114 this.walkStatements('$global$', this.statements)
123 canonize : function(scope, isStatic) {
124 var s = scope.split('|');
127 s.forEach(function(n) {
128 if (ret === false || ret.length > 1) {
132 if (n == '$global$') {
142 if (n =='(' || n == 'FUNCTION(' || n == '{') {
145 if (n.match(/[^a-z\.]+/i)) {
149 if (n.match(/\.prototype$/g)) {
152 n = n.replace( /\.prototype$/g,'');
159 if (!ret || ret.length > 2) {
162 var r = ret.join(isStatic ? '.' : '#');
164 if (r.split('#').length > 1 ) {
168 print("ADD:" + scope + ' => ' + r);
169 //print("CANON:" + r);
177 * When parser comes across a change in scope,
178 * it get's added to the current scope name here
184 * @param {String} n the name
186 addScopeName: function (n)
188 var ar = this.pscope.split('|');
189 if (ar[0] == '$global$') {
193 this.pscope = ar.join('|');
200 * same as symbol ctr...
202 addTokenSymbol : function( token ) {
204 if (!token.scopeName) {
208 var isa = typeof(token.args) == 'undefined' ? 'OBJECT' : 'FUNCTION' ; // or a function if we are sure...
209 print("CREATE symbol: " + isa + ' => ' + token.scopeName );
211 if (token.jsdoc && token.jsdoc.getTag('event').length) {
212 // make sure it get's a distinct name..
213 var last = token.scopeName.split(/[.#]/).pop();
215 token.scopeName= token.scopeName.replace(new RegExp(last+'$'), '!'+ last);
219 var symbol = new Symbol( token.scopeName, token.args || [] , isa ,
220 token.jsdoc || false);
222 symbol._token = token;
223 ScopeNamer.addSymbol(symbol, token.jsdoc);
228 walkStatements: function(scope, statements)
230 print("walkStatements :" + scope + '@' + this.look(0).line) ;
235 statements.forEach(function(st ) {
236 res = _this.walkStatement(scope, st);
244 walkStatement: function(scope, stmt)
248 print("walkStatement :" + scope + '@' + this.tokens[0].line );
253 while (null != (token = this.next())) {
256 //walkFunction(scope, name , args, stmts )
258 if (token.name == "FUNCTION") {
259 // function a() { .... } << scope is a $this$={a}
260 if (this.lookTok(1).is('NAME')) {
261 name = this.lookTok(2).data;
262 this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
265 //a = function() { } << scope might be a $this$={a}
266 if (this.lookTok(-1).data == '=' &&
267 this.lookTok(-2).is('NAME')
269 name = this.lookTok(-2).data;
270 this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
275 print("+++ FUNCTION unusual context" + token.file + ':' + token.line);
285 // XX.yy(a,b, { .... }) << scope might be a or b.. $this$={a}
286 // aa = XX.yy(a,b, { .... }) << scope might aa $this$={a}
288 if (token.is('NAME') && this.lookTok(1).data =='(') {
290 var jsdoc = token.jsdoc;
291 if ((this.lookTok(-1).data == '=') && this.lookTok(-2).is('NAME')) {
292 assign = this.lookTok(-2).data
293 jsdoc = jsdoc || this.lookTok(-2).jsdoc;
295 this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
300 // a = { .... } << scope is a $this$={a}
301 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
303 // could be var x = ..
304 var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
305 this.walkObject(scope, this.lookTok(-2).data, token.props, jd);
309 // this.xxxx = (with jsdoc...)
315 if (token.data == '{') {
316 sn = new ScopeNamer(this);
317 print("GOT { - walkings statuements;}");
319 continue; // object..!?!?!? = ignore ???
320 print(JSON.stringify(token,null,4));
322 sn.walkStatements(scope, token.items);
328 walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
330 print("wallkFuncDef: " + inscope + '@' + this.look(0).line );
331 var scope = inscope + '.' + name;
334 var symbol = new Symbol( scope , args || [] , "FUNCTION" , jsdocTok.jsdoc);
335 symbol._token = jsdocTok;
336 ScopeNamer.addSymbol(symbol, jsdocTok.jsdoc);
337 var sn = new ScopeNamer(this);
338 sn.walkStatements(scope, stmts);
344 walkCall : function (inscope, assign, callname, items, jsdocTok)
346 print("wallkCall : " + inscope + '@' + this.look(0).line );
347 var scope = inscope + ( assign ? ('.' + assign) : '' );
348 scope = scope.replace(/\^$global\$\./, '');
353 // add the handers for differnt methods in here....
356 // somecall(BASE, { .. object ..})
357 // or... x = somecall(BASE, { ... object..})
358 case 'XObject.extend':
360 //print(JSON.stringify(items,null,4));
361 scope = items[0][0].data;
362 // 2nd arg is a object def
363 if (items[1][0].data != '{') {
366 var sn = new ScopeNamer(this);
367 sn.walkObject(scope + '.prototype', false, items[1][0].props );
374 case 'XObject.define':
375 // func, extends, props.
377 var symbol = new Symbol( scope , items[0][1].args || [] , "CONSTRUCTOR" , jsdocTok);
378 symbol._token = jsdocTok;
381 ScopeNamer.addSymbol(symbol, jsdocTok.jsdoc);
382 var sn = new ScopeNamer(this);
383 sn.walkStatements(scope, token.items);
384 sn.walkObject(scope + '.prototype', false, items[2].props )
395 walkObject : function(inscope, name, items, jsdocTok)
397 var scope = inscope + (( name === false) ? '' : ('.' + name));
399 if (name !== false) {
401 var symbol = new Symbol( scope , false , "OBJECT" , jsdocTok.jsdoc);
402 symbol._token = jsdocTok;
403 ScopeNamer.addSymbol(symbol, jsdocTok.jsdoc);
407 print("wallkObject : " + scope);
408 for( var k in items) {
409 var key = items[k].key;
410 var val = items[k].val;
413 // x : function(....)
414 if (val[0].name == 'FUNCTION' ) {
417 this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
423 // x: ( .... ) conditional properties? maybe detect function?
426 // x : something else - not a function..
429 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" , key.jsdoc);
432 ScopeNamer.addSymbol(key, key.jsdoc);
446 ScopeNamer.symbols = new imports.SymbolSet.SymbolSet();
452 ScopeNamer.addSymbol = function(symbol, comment)
454 //print("PARSER addSYMBOL : " + symbol.alias);
456 // if a symbol alias is documented more than once the last one with the user docs wins
457 if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
459 if (!comment) { // we do not have a comment, and it's registered.
462 var oldSymbol = ScopeNamer.symbols.getSymbol(symbol.alias);
464 if (oldSymbol.comment && oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
465 if (symbol.comment.isUserComment) { // old and new are both documented
466 this.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
468 else { // old is documented but new isn't
474 // we don't document anonymous things
475 //if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
477 // uderscored things may be treated as if they were marked private, this cascades
478 //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
479 // symbol.isPrivate = true;
482 // -p flag is required to document private things
483 if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
485 // ignored things are not documented, this doesn't cascade
486 if (symbol.isIgnored) return;
489 //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
490 print("ADD symbol: " + JSON.stringify( symbol, null, 4));
492 // add it to the file's list... (for dumping later..)
494 ScopeNamer.filesSymbols[Symbol.srcFile].addSymbol(symbol);
497 ScopeNamer.symbols.addSymbol(symbol);
500 ScopeNamer.addBuiltin = function(name) {
502 var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));
503 builtin.isNamespace = false;
504 builtin.srcFile = "";
505 builtin.isPrivate = false;
506 this.addSymbol(builtin);