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
157 while (false != (token = this.ts.next())) {
158 //print("TOK"+ token.toString());
159 // this.timerPrint("parseScope AFTER lookT: " + token.toString());
161 if (token.is('WHIT')) {
167 if (this.currentDoc) {
168 // add it to the current scope????
170 this.addSymbol('', true);
173 //throw "Unconsumed Doc (TOKwhitespace): " + this.currentDoc.toSource();
177 var newDoc = new DocComment(token.data);
179 // it's a scope changer..
180 if (newDoc.getTag("scope").length) {
181 //print(newDoc.getTag("scope").toSource());
183 scope.ident = '$private$|' + newDoc.getTag("scope")[0].desc;
187 // it's a scope changer..
188 if (newDoc.getTag("scopeAlias").length) {
189 //print(newDoc.getTag("scopeAlias").toSource());
191 var sal = newDoc.getTag("scopeAlias")[0].desc.split("=");
192 aliases[sal[0]] = sal[1];
198 /// got a doc comment..
199 //token.data might be this.??? (not sure though)
200 this.currentDoc = newDoc;
204 // catch the various issues .. - scoe changes or doc actions..
208 // things that stop comments carrying on...??
210 if (this.currentDoc && (
211 token.tokN.data == ';' ||
212 token.tokN.data == '}')) {
213 this.addSymbol('', true);
214 //throw "Unconsumed Doc ("+ token.toString() +"): " + this.currentDoc.toSource();
218 // the rest are scoping issues...
222 if (token.name == 'VAR' &&
224 this.ts.lookT(1).type == 'NAME' &&
225 this.ts.lookT(2).data == '-' &&
226 this.ts.lookT(3).type == 'NAME' &&
227 this.ts.lookT(4).data == ';'
231 //print("SET ALIAS:" + this.ts.lookT(1).data +'=' + this.ts.lookT(3).data);
233 aliases[this.ts.lookT(1).data] = this.ts.lookT(3).data;
240 // extends scoping *** not sure if the can be x = Roo.apply(....)
241 // xxx.extends(a,b, {
242 // $this$=b|b.prototype
247 if (token.type = 'NAME') {
249 //print("TOK(ident)"+ token.toString());
255 if (/\.extend$/.test(token.data) &&
256 this.ts.lookT(1).data == '(' &&
257 this.ts.lookT(2).type == 'NAME' &&
258 this.ts.lookT(3).data == ',' &&
259 this.ts.lookT(4).type == 'NAME' &&
260 this.ts.lookT(5).data == ',' &&
261 this.ts.lookT(6).data == '{'
264 // ignore test for ( a and ,
265 this.ts.nextT(); /// (
266 token = this.ts.nextT(); // a
267 scopeName = token.data;
269 if (this.currentDoc) {
270 this.addSymbol(scopeName,false,'OBJECT');
273 this.ts.nextT(); // ,
274 this.ts.nextT(); // b
277 this.ts.nextT(); // ,
278 token = this.ts.nextT(); // {
280 scopeName = fixAlias(scopeName);
282 var fnScope = new Scope(this.braceNesting, scope, token.n,
283 '$this$=' + scopeName + '|'+scopeName+'.prototype');
284 this.indexedScopes[this.ts.cursor] = fnScope;
286 this.scopesIn(fnScope);
289 //print(">>" +locBraceNest);
290 continue; // no more processing..
294 // a = Roo.extend(parentname, {
296 if (/\.extend$/.test(token.data) &&
297 this.ts.lookT(-2).type == 'NAME' &&
298 this.ts.lookT(-1).data == '=' &&
299 this.ts.lookT(1).data == '(' &&
300 this.ts.lookT(2).type == 'NAME' &&
301 this.ts.lookT(3).data == ',' &&
302 this.ts.lookT(4).data == '{'
304 // ignore test for ( a and ,
305 token = this.ts.lookT(-2);
306 scopeName = token.data;
307 if (this.currentDoc) {
308 this.addSymbol(scopeName,false,'OBJECT');
311 this.ts.nextT(); /// (
312 this.ts.nextT(); // parent
314 this.ts.nextT(); // ,
315 token = this.ts.nextT(); // {
318 scopeName = fixAlias(scopeName);
319 var fnScope = new Scope(this.braceNesting, scope, token.n,
320 '$this$=' + scopeName + '|'+scopeName+'.prototype');
321 this.indexedScopes[this.ts.cursor] = fnScope;
323 this.scopesIn(fnScope);
326 //print(">>" +locBraceNest);
327 continue; // no more processing..
334 if (/\.(applyIf|apply)$/.test(token.data) &&
335 this.ts.lookT(1).data == '(' &&
336 this.ts.lookT(2).type == 'NAME' &&
337 this.ts.lookT(3).data == ',' &&
338 this.ts.lookT(4).data == '{'
341 this.ts.nextT(); /// (
343 //print("GOT : applyIF!");
345 token = this.ts.nextT(); // b
346 scopeName = token.data;
349 if (this.currentDoc) {
350 this.addSymbol(scopeName,false,'OBJECT');
356 this.ts.nextT(); /// ,
357 this.ts.nextT(); // {
358 scopeName = fixAlias(scopeName);
359 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
360 this.indexedScopes[this.ts.cursor] = fnScope;
362 this.scopesIn(fnScope);
365 //print(">>" +locBraceNest);
366 continue; // no more processing..
372 // change scope to xxxx
374 if ( this.ts.lookT(1).data == '=' &&
375 this.ts.lookT(2).name == 'NEW' &&
376 this.ts.lookT(3).type == 'NAME' &&
377 this.ts.lookT(4).data == '(' &&
378 this.ts.lookT(5)..data == '{'
380 scopeName = token.data;
381 if (this.currentDoc) {
382 this.addSymbol(scopeName,false,'OBJECT');
386 this.ts.nextT(); /// =
387 this.ts.nextT(); /// new
388 this.ts.nextT(); /// yyy
389 this.ts.nextT(); /// (
390 this.ts.nextT(); /// {
392 scopeName = fixAlias(scopeName);
393 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
394 this.indexedScopes[this.ts.cursor] = fnScope;
396 this.scopesIn(fnScope);
399 //print(">>" +locBraceNest);
401 continue; // no more processing..
422 // eval can be prefixed with a hint hider for the compresser..
423 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
424 this.currentDoc = false;
428 if (this.currentDoc) {
429 //print(token.toString());
431 // ident : function ()
432 // ident = function ()
433 var atype = 'OBJECT';
435 if (((this.ts.lookT(1).data == ':' )|| (this.ts.lookT(1).data == '=')) &&
436 (this.ts.lookT(2).name == "FUNCTION")
441 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookT(1).toString() + this.ts.lookT(2).toString());
444 this.ts.lookT(-1).tokN == Script.TOKdot ? token.data : fixAlias(token.data),
451 continue; // dont care about other idents..
454 if (token.type == "STRN") {
455 if (this.currentDoc) {
456 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
461 // really we only have to deal with object constructs and function calls that change the scope...
464 if (token.name == 'FUNCTION') {
466 // see if we have an unconsumed doc...
468 if (this.currentDoc) {
469 print(this.ts.dumpToCur());
470 throw "Unhandled doc (TOKfunction)" + token.toString();
471 //this.addSymbol(this.currentDoc.getTag('class')[0].name, true);
473 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
480 /// foo = function() {} << really it set's the 'this' scope to foo.prototype
481 //$this$=foo.prototype|$private$|foo.prototype
484 (this.ts.lookT(-1).data == '=') &&
485 (this.ts.lookT(-2).type == 'NAME')
487 scopeName = this.ts.lookT(-2).data;
488 this.ts.balanceN('(');
489 token = this.ts.nextT(); // should be {
490 //print("FOO=FUNCITON() {}" + this.ts.context() + "\n" + token.toString());
493 scopeName = fixAlias(scopeName);
494 var fnScope = new Scope(this.braceNesting, scope, token.n,
495 '$this$='+scopeName+'.prototype|$private$|'+scopeName+'.prototype');
496 this.indexedScopes[this.ts.cursor] = fnScope;
498 //this.scopesIn(fnScope);
499 this.parseScope(fnScope, aliases);
504 //print(">>" +locBraceNest);
505 continue; // no more processing..
511 // foo = new function() {}
512 // is this actually used much!?!?!
516 (this.ts.lookT(-1).name == 'NEW') &&
517 (this.ts.lookT(-2).data == '=') &&
518 (this.ts.lookT(-3).type = 'FUNCTION')
520 //scopeName = this.ts.look(-3).data;
521 this.ts.balanceN(Script.TOKlparen);
522 token = this.ts.nextT(); // should be {
523 scopeName = fixAlias(scopeName);
524 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
525 this.indexedScopes[this.ts.cursor] = fnScope;
527 //this.scopesIn(fnScope);
528 this.parseScope(fnScope, aliases);
531 //print(">>" +locBraceNest);
532 continue; // no more processing..
538 ///==== check/set isObjectLitAr ??
541 // foo: function() {}
542 // no change to scoping..
544 //print("checking for : function() {");
545 //print( [this.ts.lookT(-3).type , this.ts.lookT(-2).type , this.ts.lookT(-1).type ].join(":"));
547 (this.ts.lookT(-1).data == ':') &&
548 (this.ts.lookT(-2).type == 'NAME') &&
549 (this.ts.lookT(-3).data == '(' || this.ts.lookT(-3).data== ',')
551 //print("got for : function() {");
553 //scopeName = this.ts.look(-3).data;
554 this.ts.balanceN(Script.TOKlparen);
555 //print(token.toString())
556 token = this.ts.nextT(); // should be {
557 //print(token.toString())
558 scopeName = fixAlias(scopeName);
559 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
560 this.indexedScopes[this.ts.cursor] = fnScope;
562 //this.scopesIn(fnScope);
563 this.parseScope(fnScope, aliases);
565 //print(">>" +locBraceNest);
566 continue; // no more processing..
569 /// function foo() {} << really it set's the 'this' scope to foo.prototype
570 //$this$=foo|$private$
574 (this.ts.lookT(1).type == 'NAME')
576 //scopeName = this.ts.look(-3).data;
577 this.ts.balanceN('(');
578 token = this.ts.nextT(); // should be {
580 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
581 this.indexedScopes[this.ts.cursor] = fnScope;
583 //this.scopesIn(fnScope);
584 this.parseScope(fnScope, aliases);
586 //print(">>" +locBraceNest);
587 continue; // no more processing..
592 // foo = new (function() { }
594 // RETURN function(...) {
597 // (this.ts.lookT(-1).tokN == Script.TOKlparen) &&
598 (this.ts.lookT(1).name != 'NAME')
600 // (this.ts.lookT(-2).tokN == Script.TOKnew) &&
601 // (this.ts.lookT(-3).tokN == Script.TOKassign) &&
602 // (this.ts.lookT(-4).tokN == Script.TOKidentifier)
604 //scopeName = this.ts.look(-3).data;
605 this.ts.balanceN('(');
606 token = this.ts.nextT(); // should be {
607 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
608 this.indexedScopes[this.ts.cursor] = fnScope;
610 //this.scopesIn(fnScope);
611 this.parseScope(fnScope, aliases);
613 //print(">>" +locBraceNest);
614 continue; // no more processing..
620 print(this.ts.context());
621 throw "dont know how to handle function syntax??";
628 } // end checking for TOKfunction
630 if (token.data == '{') {
632 // foo = { // !var!!!
637 (this.ts.lookT(-1).data == '=') &&
638 (this.ts.lookT(-2).type == 'NAME') &&
639 (this.ts.lookT(-3).nane != 'VAR')
642 scopeName = this.ts.look(-2).data;
643 scopeName = fixAlias(scopeName);
644 var fnScope = new Scope(this.braceNesting, scope, token.n,
645 '$this$='+scopeName + '|'+scopeName
647 this.indexedScopes[this.ts.cursor] = fnScope;
649 this.scopesIn(fnScope);
653 //print(">>" +locBraceNest);
654 continue; // no more processing..
659 //print("GOT LBRACE : check for :");
661 (this.ts.lookT(-1).data == ':') &&
662 (this.ts.lookT(-2).type == 'NAME') &&
663 (this.ts.lookT(-3).name != 'VAR')
666 scopeName = this.ts.lookT(-2).data;
667 scopeName = fixAlias(scopeName);
668 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
669 this.indexedScopes[this.ts.cursor] = fnScope;
671 this.scopesIn(fnScope);
674 //print(">>" +locBraceNest);
675 continue; // no more processing..
677 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
678 this.indexedScopes[this.ts.cursor] = fnScope;
680 this.scopesIn(fnScope);
683 //print(">>" +locBraceNest);
687 if (token.data == '{') {
690 if (this.currentDoc) {
691 this.addSymbol('', true);
693 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
699 //assert braceNesting >= scope.getBraceNesting();
700 var closescope = this.scopeOut();
701 scope = this.scopes[this.scopes.length-1];
702 //print("<<:" + locBraceNest)
703 //print("<<<<<< " + locBraceNest );
704 if (locBraceNest < 0) {
705 // print("POPED OF END OF SCOPE!");
707 //var ls = this.scopeOut();
708 //ls.getUsedSymbols();
721 addSymbol: function(lastIdent, appendIt, atype )
724 /*if (!this.currentDoc.tags.length) {
727 //print(this.currentDoc.toSource());
728 // this.currentDoc = false;
730 print("SKIP ADD SYM: no tags");
731 print(this.currentDoc.src);
735 if (this.currentDoc.getTag('private').length) {
738 //print(this.currentDoc.toSource());
739 this.currentDoc = false;
740 print("SKIP ADD SYM: it's private");
744 var token = this.ts.cur();
745 if (typeof(appendIt) == 'undefined') {
748 // print(this.currentDoc.toSource(););
749 if (this.currentDoc.getTag('event').length) {
750 //?? why does it end up in desc - and not name/...
751 print(this.currentDoc.getTag('event')[0]);
752 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
753 //lastIdent = '*' + lastIdent ;
755 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
756 lastIdent = this.currentDoc.getTag('property')[0].name;
757 //lastIdent = '*' + lastIdent ;
761 if (!/\./.test(_s)) {
763 //print("WALKER ADDsymbol: " + lastIdent);
766 for (var i = 0; i < this.scopes.length;i++) {
767 s.push(this.scopes[i].ident);
771 var s = s.join('|').split('|');
772 print("Walker:ADDSymbol: " + s.join('|') );
777 for (var i = 0; i < s.length;i++) {
782 if ((s[i] == '$private$') || (s[i] == '$global$')) {
786 if (s[i].substring(0,6) == '$this$') {
787 var ts = s[i].split('=');
791 // when to use $this$ (probabl for events)
792 _s += _s.length ? '.' : '';
798 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
800 if (appendIt && !lastIdent.length) {
802 // append, and no symbol???
804 // see if it's a @class
805 if (this.currentDoc.getTag('class').length) {
806 _s = this.currentDoc.getTag('class')[0].desc;
807 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
808 Parser.addSymbol(symbol);
809 this.symbols[_s] = symbol;
813 // if (this.currentDoc.getTag('property').length) {
814 // print(Script.pretStringtyDump(this.currentDoc.toSource));
815 // throw "Add Prop?";
818 _s = _s.replace(/\.prototype.*$/, '');
819 if (typeof(this.symbols[_s]) == 'undefined') {
820 print("Symbol:" + _s);
821 print(this.currentDoc.src);
823 throw "Trying to append symbol, but no doc available";
826 for (var i =0; i < this.currentDoc.tags.length;i++) {
827 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
829 this.currentDoc = false;
833 if (typeof(this.symbols[_s]) != 'undefined') {
835 if (this.symbols[_s].comment.hasTags) {
836 // then existing comment doesnt has tags
837 throw "DUPLICATE Symbol " + _s;
839 // otherwise existing comment has tags - overwrite..
843 if (typeof(atype) == "undefined") {
844 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
847 var symbol = new Symbol(_s, [], atype, this.currentDoc);
849 Parser.addSymbol(symbol);
850 this.symbols[_s] = symbol;
852 this.currentDoc = false;
859 scopesIn : function(s)
862 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
865 scopeOut : function()
868 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
869 return this.scopes.pop();
873 scopeListToStr : function()
876 for (var i = 0; i < this.scopes.length;i++) {
877 s.push(this.scopes[i].ident);
879 return s.join('\n\t');