1 //<Script type="text/javascript">
2 XObject = imports.XObject.XObject;
4 Scope = imports.Scope.Scope;
5 DocComment = imports.DocComment.DocComment;
6 Symbol = imports.Symbol.Symbol;
12 * // FIXME - I need this to do next() without doccomments..
15 Walker2 = XObject.define(
20 this.indexedScopes = {};
22 //this.timer = new Date() * 1;
30 timerPrint: function (str) {
31 var ntime = new Date() * 1;
32 var tdif = ntime -this.timer;
35 if (tdif > 100) { //slower ones..
38 print(pref+'['+tdif+']'+str);
43 //this.warnings.push(s);
44 print("WARNING:" + htmlescape(s) + "<BR>");
46 // defaults should not be initialized here =- otherwise they get duped on new, rather than initalized..
51 mode : "", //"BUILDING_SYMBOL_TREE",
53 indexedScopes : false,
55 symbols: false, /// object store of sumbols..
60 buildSymbolTree : function()
65 this.braceNesting = 0;
69 this.globalScope = new Scope(-1, false, -1, '$global$');
70 indexedScopes = { 0 : this.globalScope };
72 this.mode = 'BUILDING_SYMBOL_TREE';
73 this.parseScope(this.globalScope);
81 //print("<B>LOG:</B>" + htmlescape(str) + "<BR/>\n");
85 //print("<B>LOG:</B>" + str + "<BR/>");
92 parseScope : function(scope, ealiases) // parse a token stream..
94 //this.timerPrint("parseScope EnterScope");
97 var fixAlias = function(str, nomore)
99 var ar = str.split('.');
102 //print(str +"?=" +aliases.toSource());
103 if (aliases[m] == undefined) {
106 var ret = aliases[m] + (ar.length? '.' : '' )+ ar.join('.');
107 if (nomore !== true) {
108 ret = fixAlias(ret, true);
118 if (ealiases != undefined) {
120 for(var i in ealiases) {
121 aliases[i] = ealiases[i];
128 //print("STARTING SCOPE WITH: " + ealiases.toSource());
134 var expressionBraceNesting = this.braceNesting;
135 var bracketNesting = 0;
136 var parensNesting = 0;
139 var l1 = '', l2 = '';
143 var locBraceNest = 0;
144 // determines if we are in object literals...
146 var isObjectLitAr = [ false ];
147 //print("SCOPE: ------------------START ----------------");
148 this.scopesIn(scope);
149 var scopeLen = this.scopes.length;
151 if (this.ts.cursor < 1) {
152 // this.ts.cursor--; // hopeflly this kludge will work
156 //print(JSON.stringify(this.ts, null, 4)); Seed.quit();
158 while (null != (token = this.ts.next())) {
159 //print("TOK"+ token.toString());
160 // this.timerPrint("parseScope AFTER lookT: " + token.toString());
162 if (token.is('COMM')) {
165 if (token.name != 'JSDOC') {
166 //print("Walker2 : spce is not JSDOC");
169 if (this.currentDoc) {
170 // add it to the current scope????
172 this.addSymbol('', true);
173 print ( "Unconsumed Doc (TOKwhitespace): " + token.toString())
174 //throw "Unconsumed Doc (TOKwhitespace): " + this.currentDoc.toSource();
178 var newDoc = new DocComment(token.data);
180 // it's a scope changer..
181 if (newDoc.getTag("scope").length) {
182 //print("Walker2 : doctag changes scope");
184 scope.ident = '$private$|' + newDoc.getTag("scope")[0].desc;
188 // it's a scope changer..
189 if (newDoc.getTag("scopeAlias").length) {
190 //print(newDoc.getTag("scopeAlias").toSource());
192 //print("Walker2 : doctag changes scope (alias)");
193 var sal = newDoc.getTag("scopeAlias")[0].desc.split("=");
194 aliases[sal[0]] = sal[1];
200 /// got a doc comment..
201 //token.data might be this.??? (not sure though)
202 //print("Walker2 : setting currentDoc");
203 this.currentDoc = newDoc;
207 // catch the various issues .. - scoe changes or doc actions..
211 // things that stop comments carrying on...??
213 if (this.currentDoc && (
215 token.data == '}')) {
216 this.addSymbol('', true);
217 //throw "Unconsumed Doc ("+ token.toString() +"): " + this.currentDoc.toSource();
221 // the rest are scoping issues...
225 if (token.name == 'VAR' &&
227 this.ts.lookTok(1).type == 'NAME' &&
228 this.ts.lookTok(2).data == '-' &&
229 this.ts.lookTok(3).type == 'NAME' &&
230 this.ts.lookTok(4).data == ';'
234 //print("SET ALIAS:" + this.ts.lookTok(1).data +'=' + this.ts.lookTok(3).data);
236 aliases[this.ts.lookTok(1).data] = this.ts.lookTok(3).data;
243 // extends scoping *** not sure if the can be x = Roo.apply(....)
244 // xxx.extends(a,b, {
245 // $this$=b|b.prototype
250 if (token.type == 'NAME') {
252 //print("TOK(ident)"+ token.toString());
258 if (/\.extend$/.test(token.data) &&
259 this.ts.lookTok(1).data == '(' &&
260 this.ts.lookTok(2).type == 'NAME' &&
261 this.ts.lookTok(3).data == ',' &&
262 this.ts.lookTok(4).type == 'NAME' &&
263 this.ts.lookTok(5).data == ',' &&
264 this.ts.lookTok(6).data == '{'
267 // ignore test for ( a and ,
268 this.ts.nextTok(); /// (
269 token = this.ts.nextTok(); // a
270 scopeName = token.data;
272 if (this.currentDoc) {
273 this.addSymbol(scopeName,false,'OBJECT');
276 this.ts.nextTok(); // ,
277 this.ts.nextTok(); // b
280 this.ts.nextTok(); // ,
281 token = this.ts.nextTok(); // {
283 scopeName = fixAlias(scopeName);
285 var fnScope = new Scope(this.braceNesting, scope, token.n,
286 '$this$=' + scopeName + '|'+scopeName+'.prototype');
287 this.indexedScopes[this.ts.cursor] = fnScope;
289 this.scopesIn(fnScope);
292 //print(">>" +locBraceNest);
293 continue; // no more processing..
297 // a = Roo.extend(parentname, {
299 if (/\.extend$/.test(token.data) &&
300 this.ts.lookTok(-2).type == 'NAME' &&
301 this.ts.lookTok(-1).data == '=' &&
302 this.ts.lookTok(1).data == '(' &&
303 this.ts.lookTok(2).type == 'NAME' &&
304 this.ts.lookTok(3).data == ',' &&
305 this.ts.lookTok(4).data == '{'
307 // ignore test for ( a and ,
308 token = this.ts.lookTok(-2);
309 scopeName = token.data;
310 if (this.currentDoc) {
311 this.addSymbol(scopeName,false,'OBJECT');
314 this.ts.nextTok(); /// (
315 this.ts.nextTok(); // parent
317 this.ts.nextTok(); // ,
318 token = this.ts.nextTok(); // {
321 scopeName = fixAlias(scopeName);
322 var fnScope = new Scope(this.braceNesting, scope, token.n,
323 '$this$=' + scopeName + '|'+scopeName+'.prototype');
324 this.indexedScopes[this.ts.cursor] = fnScope;
326 this.scopesIn(fnScope);
329 //print(">>" +locBraceNest);
330 continue; // no more processing..
337 print(JSON.stringify([
339 this.ts.lookTok(1).data ,
340 this.ts.lookTok(2).type ,
341 this.ts.lookTok(3).data ,
342 this.ts.lookTok(4).data
346 if (/\.(applyIf|apply)$/.test(token.data) &&
347 this.ts.lookTok(1).data == '(' &&
348 this.ts.lookTok(2).type == 'NAME' &&
349 this.ts.lookTok(3).data == ',' &&
350 this.ts.lookTok(4).data == '{'
353 this.ts.nextTok(); /// (
355 //print("GOT : applyIF!");
357 token = this.ts.nextTok(); // b
358 scopeName = token.data;
361 if (this.currentDoc) {
362 this.addSymbol(scopeName,false,'OBJECT');
367 this.ts.nextTok(); /// ,
368 this.ts.nextTok(); // {
369 scopeName = fixAlias(scopeName);
370 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
371 this.indexedScopes[this.ts.cursor] = fnScope;
373 this.scopesIn(fnScope);
376 //print(">>" +locBraceNest);
377 continue; // no more processing..
383 // change scope to xxxx
385 print(JSON.stringify([
386 this.ts.lookTok(1).data ,
387 this.ts.lookTok(2).name ,
388 this.ts.lookTok(3).type ,
389 this.ts.lookTok(4).data ,
390 this.ts.lookTok(5).data
393 if ( this.ts.lookTok(1).data == '=' &&
394 this.ts.lookTok(2).name == 'NEW' &&
395 this.ts.lookTok(3).type == 'NAME' &&
396 this.ts.lookTok(4).data == '(' &&
397 this.ts.lookTok(5).data == '{'
399 scopeName = token.data;
400 if (this.currentDoc) {
401 this.addSymbol(scopeName,false,'OBJECT');
405 this.ts.nextTok(); /// =
406 this.ts.nextTok(); /// new
407 this.ts.nextTok(); /// yyy
408 this.ts.nextTok(); /// (
409 this.ts.nextTok(); /// {
411 scopeName = fixAlias(scopeName);
412 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
413 this.indexedScopes[this.ts.cursor] = fnScope;
415 this.scopesIn(fnScope);
418 //print(">>" +locBraceNest);
420 continue; // no more processing..
429 // eval can be prefixed with a hint hider for the compresser..
430 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
431 this.currentDoc = false;
435 if (this.currentDoc) {
436 //print(token.toString());
438 // ident : function ()
439 // ident = function ()
440 var atype = 'OBJECT';
442 if (((this.ts.lookTok(1).data == ':' )|| (this.ts.lookTok(1).data == '=')) &&
443 (this.ts.lookTok(2).name == "FUNCTION")
448 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookTok(1).toString() + this.ts.lookTok(2).toString());
451 this.ts.lookTok(-1).data == '.' ? token.data : fixAlias(token.data),
458 continue; // dont care about other idents..
462 //print ("NOT NAME");
465 if (token.type == "STRN") { // THIS WILL NOT HAPPEN HERE?!!?
466 if (this.currentDoc) {
467 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
472 // really we only have to deal with object constructs and function calls that change the scope...
475 if (token.name == 'FUNCTION') {
476 //print("GOT FUNCTION");
477 // see if we have an unconsumed doc...
479 if (this.currentDoc) {
481 name: "ArgumentError",
482 message: "Unhandled doc (TOKfunction)" + token.toString()
485 //this.addSymbol(this.currentDoc.getTag('class')[0].name, true);
487 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
494 /// foo = function() {} << really it set's the 'this' scope to foo.prototype
495 //$this$=foo.prototype|$private$|foo.prototype
498 (this.ts.lookTok(-1).data == '=') &&
499 (this.ts.lookTok(-2).type == 'NAME')
501 scopeName = this.ts.lookTok(-2).data;
502 this.ts.balance('(');
503 token = this.ts.nextTok(); // should be {
504 //print("FOO=FUNCITON() {}" + this.ts.context() + "\n" + token.toString());
507 scopeName = fixAlias(scopeName);
508 var fnScope = new Scope(this.braceNesting, scope, token.n,
509 '$this$='+scopeName+'.prototype|$private$|'+scopeName+'.prototype');
511 this.indexedScopes[this.ts.cursor] = fnScope;
513 //this.scopesIn(fnScope);
514 this.parseScope(fnScope, aliases);
519 //print(">>" +locBraceNest);
520 continue; // no more processing..
526 // foo = new function() {}
527 // is this actually used much!?!?!
531 (this.ts.lookTok(-1).name == 'NEW') &&
532 (this.ts.lookTok(-2).data == '=') &&
533 (this.ts.lookTok(-3).type = 'FUNCTION')
535 //scopeName = this.ts.look(-3).data;
536 this.ts.balanceN(Script.TOKlparen);
537 token = this.ts.nextTok(); // should be {
538 scopeName = fixAlias(scopeName);
539 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
540 this.indexedScopes[this.ts.cursor] = fnScope;
542 //this.scopesIn(fnScope);
543 this.parseScope(fnScope, aliases);
546 //print(">>" +locBraceNest);
547 continue; // no more processing..
553 ///==== check/set isObjectLitAr ??
556 // foo: function() {}
557 // no change to scoping..
559 //print("checking for : function() {");
560 //print( [this.ts.lookTok(-3).type , this.ts.lookTok(-2).type , this.ts.lookTok(-1).type ].join(":"));
562 (this.ts.lookTok(-1).data == ':') &&
563 (this.ts.lookTok(-2).type == 'NAME') &&
564 (this.ts.lookTok(-3).data == '(' || this.ts.lookTok(-3).data== ',')
566 //print("got for : function() {");
568 //scopeName = this.ts.look(-3).data;
569 this.ts.balance('(');
570 //print(token.toString())
571 token = this.ts.nextTok(); // should be {
572 //print(token.toString())
573 scopeName = fixAlias(scopeName);
574 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
575 this.indexedScopes[this.ts.cursor] = fnScope;
577 //this.scopesIn(fnScope);
578 this.parseScope(fnScope, aliases);
580 //print(">>" +locBraceNest);
581 continue; // no more processing..
584 /// function foo() {} << really it set's the 'this' scope to foo.prototype
585 //$this$=foo|$private$
589 (this.ts.lookTok(1).type == 'NAME')
591 //scopeName = this.ts.look(-3).data;
592 this.ts.balance('(');
593 token = this.ts.nextTok(); // should be {
595 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
596 this.indexedScopes[this.ts.cursor] = fnScope;
598 //this.scopesIn(fnScope);
599 this.parseScope(fnScope, aliases);
601 //print(">>" +locBraceNest);
602 continue; // no more processing..
607 // foo = new (function() { }
609 // RETURN function(...) {
612 // (this.ts.lookTok(-1).tokN == Script.TOKlparen) &&
613 (this.ts.lookTok(1).name != 'NAME')
615 // (this.ts.lookTok(-2).tokN == Script.TOKnew) &&
616 // (this.ts.lookTok(-3).tokN == Script.TOKassign) &&
617 // (this.ts.lookTok(-4).tokN == Script.TOKidentifier)
619 //scopeName = this.ts.look(-3).data;
620 this.ts.balance('(');
621 token = this.ts.nextTok(); // should be {
622 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
623 this.indexedScopes[this.ts.cursor] = fnScope;
625 //this.scopesIn(fnScope);
626 this.parseScope(fnScope, aliases);
628 //print(">>" +locBraceNest);
629 continue; // no more processing..
636 name: "ArgumentError",
637 message: "dont know how to handle function syntax??\n" +
647 } // end checking for TOKfunction
649 if (token.data == '{') {
651 // foo = { // !var!!!
656 (this.ts.lookTok(-1).data == '=') &&
657 (this.ts.lookTok(-2).type == 'NAME') &&
658 (this.ts.lookTok(-3).nane != 'VAR')
661 scopeName = this.ts.look(-2).data;
662 scopeName = fixAlias(scopeName);
663 var fnScope = new Scope(this.braceNesting, scope, token.n,
664 '$this$='+scopeName + '|'+scopeName
666 this.indexedScopes[this.ts.cursor] = fnScope;
668 this.scopesIn(fnScope);
672 //print(">>" +locBraceNest);
673 continue; // no more processing..
678 //print("GOT LBRACE : check for :");
680 (this.ts.lookTok(-1).data == ':') &&
681 (this.ts.lookTok(-2).type == 'NAME') &&
682 (this.ts.lookTok(-3).name != 'VAR')
685 scopeName = this.ts.lookTok(-2).data;
686 scopeName = fixAlias(scopeName);
687 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
688 this.indexedScopes[this.ts.cursor] = fnScope;
690 this.scopesIn(fnScope);
693 //print(">>" +locBraceNest);
694 continue; // no more processing..
696 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
697 this.indexedScopes[this.ts.cursor] = fnScope;
699 this.scopesIn(fnScope);
702 //print(">>" +locBraceNest);
706 if (token.data == '{') {
709 if (this.currentDoc) {
710 this.addSymbol('', true);
712 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
718 //assert braceNesting >= scope.getBraceNesting();
719 var closescope = this.scopeOut();
720 scope = this.scopes[this.scopes.length-1];
721 //print("<<:" + locBraceNest)
722 //print("<<<<<< " + locBraceNest );
723 if (locBraceNest < 0) {
724 // print("POPED OF END OF SCOPE!");
726 //var ls = this.scopeOut();
727 //ls.getUsedSymbols();
740 addSymbol: function(lastIdent, appendIt, atype )
742 //print("Walker.addSymbol : " + lastIdent);
744 /*if (!this.currentDoc.tags.length) {
747 //print(this.currentDoc.toSource());
748 // this.currentDoc = false;
750 print("SKIP ADD SYM: no tags");
751 print(this.currentDoc.src);
755 if (this.currentDoc.getTag('private').length) {
758 //print(this.currentDoc.toSource());
759 this.currentDoc = false;
760 //print("SKIP ADD SYM: it's private");
764 var token = this.ts.lookTok(0);
765 if (typeof(appendIt) == 'undefined') {
768 // print(this.currentDoc.toSource(););
769 if (this.currentDoc.getTag('event').length) {
770 //?? why does it end up in desc - and not name/...
771 //print(this.currentDoc.getTag('event')[0]);
772 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
773 //lastIdent = '*' + lastIdent ;
775 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
776 lastIdent = this.currentDoc.getTag('property')[0].name;
777 //lastIdent = '*' + lastIdent ;
781 if (!/\./.test(_s)) {
783 //print("WALKER ADDsymbol: " + lastIdent);
786 for (var i = 0; i < this.scopes.length;i++) {
787 s.push(this.scopes[i].ident);
791 var s = s.join('|').split('|');
792 //print("Walker:ADDSymbol: " + s.join('|') );
797 for (var i = 0; i < s.length;i++) {
802 if ((s[i] == '$private$') || (s[i] == '$global$')) {
806 if (s[i].substring(0,6) == '$this$') {
807 var ts = s[i].split('=');
811 // when to use $this$ (probabl for events)
812 _s += _s.length ? '.' : '';
818 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
819 //print("Walker.addsymbol - add : " + _s);
820 if (appendIt && !lastIdent.length) {
822 // append, and no symbol???
824 // see if it's a @class
825 if (this.currentDoc.getTag('class').length) {
826 _s = this.currentDoc.getTag('class')[0].desc;
827 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
828 Parser = imports.Parser.Parser;
829 Parser.addSymbol(symbol);
830 this.symbols[_s] = symbol;
834 // if (this.currentDoc.getTag('property').length) {
835 // print(Script.pretStringtyDump(this.currentDoc.toSource));
836 // throw "Add Prop?";
839 _s = _s.replace(/\.prototype.*$/, '');
840 if (typeof(this.symbols[_s]) == 'undefined') {
841 //print("Symbol:" + _s);
842 //print(this.currentDoc.src);
845 name: "ArgumentError",
846 message: "Trying to append symbol '" + _s + "', but no doc available\n" +
853 for (var i =0; i < this.currentDoc.tags.length;i++) {
854 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
856 this.currentDoc = false;
860 //print("Walker.addsymbol - chkdup: " + _s);
861 if (typeof(this.symbols[_s]) != 'undefined') {
863 if (this.symbols[_s].comment.hasTags) {
864 // then existing comment doesnt has tags
866 // name: "ArgumentError",
867 // message:"DUPLICATE Symbol " + _s + "\n" + token.toString()
871 // otherwise existing comment has tags - overwrite..
875 //print("Walker.addsymbol - ATYPE: " + _s);
877 if (typeof(atype) == "undefined") {
878 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
881 //print("Walker.addsymbol - add : ");
882 var symbol = new Symbol(_s, [], atype, this.currentDoc);
883 Parser = imports.Parser.Parser;
884 Parser.addSymbol(symbol);
885 this.symbols[_s] = symbol;
887 this.currentDoc = false;
894 scopesIn : function(s)
897 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
900 scopeOut : function()
903 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
904 return this.scopes.pop();
908 scopeListToStr : function()
911 for (var i = 0; i < this.scopes.length;i++) {
912 s.push(this.scopes[i].ident);
914 return s.join('\n\t');