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;
7 Parser = imports.Parser.Parser;
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);
80 //print("<B>LOG:</B>" + htmlescape(str) + "<BR/>\n");
84 //print("<B>LOG:</B>" + str + "<BR/>");
91 parseScope : function(scope, ealiases) // parse a token stream..
93 //this.timerPrint("parseScope EnterScope");
96 var fixAlias = function(str, nomore)
98 var ar = str.split('.');
101 //print(str +"?=" +aliases.toSource());
102 if (aliases[m] == undefined) {
105 var ret = aliases[m] + (ar.length? '.' : '' )+ ar.join('.');
106 if (nomore !== true) {
107 ret = fixAlias(ret, true);
117 if (ealiases != undefined) {
119 for(var i in ealiases) {
120 aliases[i] = ealiases[i];
127 //print("STARTING SCOPE WITH: " + ealiases.toSource());
133 var expressionBraceNesting = this.braceNesting;
134 var bracketNesting = 0;
135 var parensNesting = 0;
138 var l1 = '', l2 = '';
142 var locBraceNest = 0;
143 // determines if we are in object literals...
145 var isObjectLitAr = [ false ];
146 //print("SCOPE: ------------------START ----------------");
147 this.scopesIn(scope);
148 var scopeLen = this.scopes.length;
150 if (this.ts.cursor < 1) {
151 // this.ts.cursor--; // hopeflly this kludge will work
155 //print(JSON.stringify(this.ts, null, 4)); Seed.quit();
157 while (null != (token = this.ts.next())) {
158 print("TOK"+ token.toString());
159 // this.timerPrint("parseScope AFTER lookT: " + token.toString());
161 if (token.is('WHIT')) {
164 if (token.name != 'JSDOC') {
167 if (this.currentDoc) {
168 // add it to the current scope????
170 this.addSymbol('', true);
172 //throw "Unconsumed Doc (TOKwhitespace): " + this.currentDoc.toSource();
176 var newDoc = new DocComment(token.data);
178 // it's a scope changer..
179 if (newDoc.getTag("scope").length) {
180 //print(newDoc.getTag("scope").toSource());
182 scope.ident = '$private$|' + newDoc.getTag("scope")[0].desc;
186 // it's a scope changer..
187 if (newDoc.getTag("scopeAlias").length) {
188 //print(newDoc.getTag("scopeAlias").toSource());
190 var sal = newDoc.getTag("scopeAlias")[0].desc.split("=");
191 aliases[sal[0]] = sal[1];
197 /// got a doc comment..
198 //token.data might be this.??? (not sure though)
199 this.currentDoc = newDoc;
203 // catch the various issues .. - scoe changes or doc actions..
207 // things that stop comments carrying on...??
209 if (this.currentDoc && (
210 token.tokN.data == ';' ||
211 token.tokN.data == '}')) {
212 this.addSymbol('', true);
213 //throw "Unconsumed Doc ("+ token.toString() +"): " + this.currentDoc.toSource();
217 // the rest are scoping issues...
221 if (token.name == 'VAR' &&
223 this.ts.lookTok(1).type == 'NAME' &&
224 this.ts.lookTok(2).data == '-' &&
225 this.ts.lookTok(3).type == 'NAME' &&
226 this.ts.lookTok(4).data == ';'
230 //print("SET ALIAS:" + this.ts.lookTok(1).data +'=' + this.ts.lookTok(3).data);
232 aliases[this.ts.lookTok(1).data] = this.ts.lookTok(3).data;
239 // extends scoping *** not sure if the can be x = Roo.apply(....)
240 // xxx.extends(a,b, {
241 // $this$=b|b.prototype
246 if (token.type = 'NAME') {
248 //print("TOK(ident)"+ token.toString());
254 if (/\.extend$/.test(token.data) &&
255 this.ts.lookTok(1).data == '(' &&
256 this.ts.lookTok(2).type == 'NAME' &&
257 this.ts.lookTok(3).data == ',' &&
258 this.ts.lookTok(4).type == 'NAME' &&
259 this.ts.lookTok(5).data == ',' &&
260 this.ts.lookTok(6).data == '{'
263 // ignore test for ( a and ,
264 this.ts.nextTok(); /// (
265 token = this.ts.nextTok(); // a
266 scopeName = token.data;
268 if (this.currentDoc) {
269 this.addSymbol(scopeName,false,'OBJECT');
272 this.ts.nextTok(); // ,
273 this.ts.nextTok(); // b
276 this.ts.nextTok(); // ,
277 token = this.ts.nextTok(); // {
279 scopeName = fixAlias(scopeName);
281 var fnScope = new Scope(this.braceNesting, scope, token.n,
282 '$this$=' + scopeName + '|'+scopeName+'.prototype');
283 this.indexedScopes[this.ts.cursor] = fnScope;
285 this.scopesIn(fnScope);
288 //print(">>" +locBraceNest);
289 continue; // no more processing..
293 // a = Roo.extend(parentname, {
295 if (/\.extend$/.test(token.data) &&
296 this.ts.lookTok(-2).type == 'NAME' &&
297 this.ts.lookTok(-1).data == '=' &&
298 this.ts.lookTok(1).data == '(' &&
299 this.ts.lookTok(2).type == 'NAME' &&
300 this.ts.lookTok(3).data == ',' &&
301 this.ts.lookTok(4).data == '{'
303 // ignore test for ( a and ,
304 token = this.ts.lookTok(-2);
305 scopeName = token.data;
306 if (this.currentDoc) {
307 this.addSymbol(scopeName,false,'OBJECT');
310 this.ts.nextTok(); /// (
311 this.ts.nextTok(); // parent
313 this.ts.nextTok(); // ,
314 token = this.ts.nextTok(); // {
317 scopeName = fixAlias(scopeName);
318 var fnScope = new Scope(this.braceNesting, scope, token.n,
319 '$this$=' + scopeName + '|'+scopeName+'.prototype');
320 this.indexedScopes[this.ts.cursor] = fnScope;
322 this.scopesIn(fnScope);
325 //print(">>" +locBraceNest);
326 continue; // no more processing..
332 print(JSON.stringify([
334 this.ts.lookTok(2).data ,
335 this.ts.lookTok(3).type ,
336 this.ts.lookTok(4).data ,
337 this.ts.lookTok(5).data
341 if (/\.(applyIf|apply)$/.test(token.data) &&
342 this.ts.lookTok(1).data == '(' &&
343 this.ts.lookTok(2).type == 'NAME' &&
344 this.ts.lookTok(3).data == ',' &&
345 this.ts.lookTok(4).data == '{'
348 this.ts.nextTok(); /// (
350 //print("GOT : applyIF!");
352 token = this.ts.nextTok(); // b
353 scopeName = token.data;
356 if (this.currentDoc) {
357 this.addSymbol(scopeName,false,'OBJECT');
363 this.ts.nextTok(); /// ,
364 this.ts.nextTok(); // {
365 scopeName = fixAlias(scopeName);
366 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
367 this.indexedScopes[this.ts.cursor] = fnScope;
369 this.scopesIn(fnScope);
372 //print(">>" +locBraceNest);
373 continue; // no more processing..
379 // change scope to xxxx
381 print(JSON.stringify([
382 this.ts.lookTok(1).data ,
383 this.ts.lookTok(2).name ,
384 this.ts.lookTok(3).type ,
385 this.ts.lookTok(4).data ,
386 this.ts.lookTok(5).data
389 if ( this.ts.lookTok(1).data == '=' &&
390 this.ts.lookTok(2).name == 'NEW' &&
391 this.ts.lookTok(3).type == 'NAME' &&
392 this.ts.lookTok(4).data == '(' &&
393 this.ts.lookTok(5).data == '{'
395 scopeName = token.data;
396 if (this.currentDoc) {
397 this.addSymbol(scopeName,false,'OBJECT');
401 this.ts.nextTok(); /// =
402 this.ts.nextTok(); /// new
403 this.ts.nextTok(); /// yyy
404 this.ts.nextTok(); /// (
405 this.ts.nextTok(); /// {
407 scopeName = fixAlias(scopeName);
408 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
409 this.indexedScopes[this.ts.cursor] = fnScope;
411 this.scopesIn(fnScope);
414 //print(">>" +locBraceNest);
416 continue; // no more processing..
437 // eval can be prefixed with a hint hider for the compresser..
438 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
439 this.currentDoc = false;
443 if (this.currentDoc) {
444 //print(token.toString());
446 // ident : function ()
447 // ident = function ()
448 var atype = 'OBJECT';
450 if (((this.ts.lookTok(1).data == ':' )|| (this.ts.lookTok(1).data == '=')) &&
451 (this.ts.lookTok(2).name == "FUNCTION")
456 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookTok(1).toString() + this.ts.lookTok(2).toString());
459 this.ts.lookTok(-1).tokN == Script.TOKdot ? token.data : fixAlias(token.data),
466 continue; // dont care about other idents..
469 if (token.type == "STRN") {
470 if (this.currentDoc) {
471 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
476 // really we only have to deal with object constructs and function calls that change the scope...
479 if (token.name == 'FUNCTION') {
481 // see if we have an unconsumed doc...
483 if (this.currentDoc) {
484 print(this.ts.dumpToCur());
485 throw "Unhandled doc (TOKfunction)" + token.toString();
486 //this.addSymbol(this.currentDoc.getTag('class')[0].name, true);
488 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
495 /// foo = function() {} << really it set's the 'this' scope to foo.prototype
496 //$this$=foo.prototype|$private$|foo.prototype
499 (this.ts.lookTok(-1).data == '=') &&
500 (this.ts.lookTok(-2).type == 'NAME')
502 scopeName = this.ts.lookTok(-2).data;
503 this.ts.balanceN('(');
504 token = this.ts.nextTok(); // should be {
505 //print("FOO=FUNCITON() {}" + this.ts.context() + "\n" + token.toString());
508 scopeName = fixAlias(scopeName);
509 var fnScope = new Scope(this.braceNesting, scope, token.n,
510 '$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.balanceN(Script.TOKlparen);
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.balanceN('(');
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.balanceN('(');
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..
635 print(this.ts.context());
636 throw "dont know how to handle function syntax??";
643 } // end checking for TOKfunction
645 if (token.data == '{') {
647 // foo = { // !var!!!
652 (this.ts.lookTok(-1).data == '=') &&
653 (this.ts.lookTok(-2).type == 'NAME') &&
654 (this.ts.lookTok(-3).nane != 'VAR')
657 scopeName = this.ts.look(-2).data;
658 scopeName = fixAlias(scopeName);
659 var fnScope = new Scope(this.braceNesting, scope, token.n,
660 '$this$='+scopeName + '|'+scopeName
662 this.indexedScopes[this.ts.cursor] = fnScope;
664 this.scopesIn(fnScope);
668 //print(">>" +locBraceNest);
669 continue; // no more processing..
674 //print("GOT LBRACE : check for :");
676 (this.ts.lookTok(-1).data == ':') &&
677 (this.ts.lookTok(-2).type == 'NAME') &&
678 (this.ts.lookTok(-3).name != 'VAR')
681 scopeName = this.ts.lookTok(-2).data;
682 scopeName = fixAlias(scopeName);
683 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
684 this.indexedScopes[this.ts.cursor] = fnScope;
686 this.scopesIn(fnScope);
689 //print(">>" +locBraceNest);
690 continue; // no more processing..
692 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
693 this.indexedScopes[this.ts.cursor] = fnScope;
695 this.scopesIn(fnScope);
698 //print(">>" +locBraceNest);
702 if (token.data == '{') {
705 if (this.currentDoc) {
706 this.addSymbol('', true);
708 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
714 //assert braceNesting >= scope.getBraceNesting();
715 var closescope = this.scopeOut();
716 scope = this.scopes[this.scopes.length-1];
717 //print("<<:" + locBraceNest)
718 //print("<<<<<< " + locBraceNest );
719 if (locBraceNest < 0) {
720 // print("POPED OF END OF SCOPE!");
722 //var ls = this.scopeOut();
723 //ls.getUsedSymbols();
736 addSymbol: function(lastIdent, appendIt, atype )
739 /*if (!this.currentDoc.tags.length) {
742 //print(this.currentDoc.toSource());
743 // this.currentDoc = false;
745 print("SKIP ADD SYM: no tags");
746 print(this.currentDoc.src);
750 if (this.currentDoc.getTag('private').length) {
753 //print(this.currentDoc.toSource());
754 this.currentDoc = false;
755 print("SKIP ADD SYM: it's private");
759 var token = this.ts.cur();
760 if (typeof(appendIt) == 'undefined') {
763 // print(this.currentDoc.toSource(););
764 if (this.currentDoc.getTag('event').length) {
765 //?? why does it end up in desc - and not name/...
766 print(this.currentDoc.getTag('event')[0]);
767 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
768 //lastIdent = '*' + lastIdent ;
770 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
771 lastIdent = this.currentDoc.getTag('property')[0].name;
772 //lastIdent = '*' + lastIdent ;
776 if (!/\./.test(_s)) {
778 //print("WALKER ADDsymbol: " + lastIdent);
781 for (var i = 0; i < this.scopes.length;i++) {
782 s.push(this.scopes[i].ident);
786 var s = s.join('|').split('|');
787 print("Walker:ADDSymbol: " + s.join('|') );
792 for (var i = 0; i < s.length;i++) {
797 if ((s[i] == '$private$') || (s[i] == '$global$')) {
801 if (s[i].substring(0,6) == '$this$') {
802 var ts = s[i].split('=');
806 // when to use $this$ (probabl for events)
807 _s += _s.length ? '.' : '';
813 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
815 if (appendIt && !lastIdent.length) {
817 // append, and no symbol???
819 // see if it's a @class
820 if (this.currentDoc.getTag('class').length) {
821 _s = this.currentDoc.getTag('class')[0].desc;
822 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
823 Parser.addSymbol(symbol);
824 this.symbols[_s] = symbol;
828 // if (this.currentDoc.getTag('property').length) {
829 // print(Script.pretStringtyDump(this.currentDoc.toSource));
830 // throw "Add Prop?";
833 _s = _s.replace(/\.prototype.*$/, '');
834 if (typeof(this.symbols[_s]) == 'undefined') {
835 print("Symbol:" + _s);
836 print(this.currentDoc.src);
838 throw "Trying to append symbol, but no doc available";
841 for (var i =0; i < this.currentDoc.tags.length;i++) {
842 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
844 this.currentDoc = false;
848 if (typeof(this.symbols[_s]) != 'undefined') {
850 if (this.symbols[_s].comment.hasTags) {
851 // then existing comment doesnt has tags
852 throw "DUPLICATE Symbol " + _s;
854 // otherwise existing comment has tags - overwrite..
858 if (typeof(atype) == "undefined") {
859 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
862 var symbol = new Symbol(_s, [], atype, this.currentDoc);
864 Parser.addSymbol(symbol);
865 this.symbols[_s] = symbol;
867 this.currentDoc = false;
874 scopesIn : function(s)
877 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
880 scopeOut : function()
883 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
884 return this.scopes.pop();
888 scopeListToStr : function()
891 for (var i = 0; i < this.scopes.length;i++) {
892 s.push(this.scopes[i].ident);
894 return s.join('\n\t');