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);
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('WHIT')) {
165 if (token.name != 'JSDOC') {
168 if (this.currentDoc) {
169 // add it to the current scope????
171 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("Walker2 : doctag changes scope");
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 print("Walker2 : doctag changes scope (alias)");
192 var sal = newDoc.getTag("scopeAlias")[0].desc.split("=");
193 aliases[sal[0]] = sal[1];
199 /// got a doc comment..
200 //token.data might be this.??? (not sure though)
201 print("Walker2 : setting currentDoc");
202 this.currentDoc = newDoc;
206 // catch the various issues .. - scoe changes or doc actions..
210 // things that stop comments carrying on...??
212 if (this.currentDoc && (
213 token.tokN.data == ';' ||
214 token.tokN.data == '}')) {
215 this.addSymbol('', true);
216 //throw "Unconsumed Doc ("+ token.toString() +"): " + this.currentDoc.toSource();
220 // the rest are scoping issues...
224 if (token.name == 'VAR' &&
226 this.ts.lookTok(1).type == 'NAME' &&
227 this.ts.lookTok(2).data == '-' &&
228 this.ts.lookTok(3).type == 'NAME' &&
229 this.ts.lookTok(4).data == ';'
233 //print("SET ALIAS:" + this.ts.lookTok(1).data +'=' + this.ts.lookTok(3).data);
235 aliases[this.ts.lookTok(1).data] = this.ts.lookTok(3).data;
242 // extends scoping *** not sure if the can be x = Roo.apply(....)
243 // xxx.extends(a,b, {
244 // $this$=b|b.prototype
249 if (token.type = 'NAME') {
251 //print("TOK(ident)"+ token.toString());
257 if (/\.extend$/.test(token.data) &&
258 this.ts.lookTok(1).data == '(' &&
259 this.ts.lookTok(2).type == 'NAME' &&
260 this.ts.lookTok(3).data == ',' &&
261 this.ts.lookTok(4).type == 'NAME' &&
262 this.ts.lookTok(5).data == ',' &&
263 this.ts.lookTok(6).data == '{'
266 // ignore test for ( a and ,
267 this.ts.nextTok(); /// (
268 token = this.ts.nextTok(); // a
269 scopeName = token.data;
271 if (this.currentDoc) {
272 this.addSymbol(scopeName,false,'OBJECT');
275 this.ts.nextTok(); // ,
276 this.ts.nextTok(); // b
279 this.ts.nextTok(); // ,
280 token = this.ts.nextTok(); // {
282 scopeName = fixAlias(scopeName);
284 var fnScope = new Scope(this.braceNesting, scope, token.n,
285 '$this$=' + scopeName + '|'+scopeName+'.prototype');
286 this.indexedScopes[this.ts.cursor] = fnScope;
288 this.scopesIn(fnScope);
291 //print(">>" +locBraceNest);
292 continue; // no more processing..
296 // a = Roo.extend(parentname, {
298 if (/\.extend$/.test(token.data) &&
299 this.ts.lookTok(-2).type == 'NAME' &&
300 this.ts.lookTok(-1).data == '=' &&
301 this.ts.lookTok(1).data == '(' &&
302 this.ts.lookTok(2).type == 'NAME' &&
303 this.ts.lookTok(3).data == ',' &&
304 this.ts.lookTok(4).data == '{'
306 // ignore test for ( a and ,
307 token = this.ts.lookTok(-2);
308 scopeName = token.data;
309 if (this.currentDoc) {
310 this.addSymbol(scopeName,false,'OBJECT');
313 this.ts.nextTok(); /// (
314 this.ts.nextTok(); // parent
316 this.ts.nextTok(); // ,
317 token = this.ts.nextTok(); // {
320 scopeName = fixAlias(scopeName);
321 var fnScope = new Scope(this.braceNesting, scope, token.n,
322 '$this$=' + scopeName + '|'+scopeName+'.prototype');
323 this.indexedScopes[this.ts.cursor] = fnScope;
325 this.scopesIn(fnScope);
328 //print(">>" +locBraceNest);
329 continue; // no more processing..
336 print(JSON.stringify([
338 this.ts.lookTok(1).data ,
339 this.ts.lookTok(2).type ,
340 this.ts.lookTok(3).data ,
341 this.ts.lookTok(4).data
345 if (/\.(applyIf|apply)$/.test(token.data) &&
346 this.ts.lookTok(1).data == '(' &&
347 this.ts.lookTok(2).type == 'NAME' &&
348 this.ts.lookTok(3).data == ',' &&
349 this.ts.lookTok(4).data == '{'
352 this.ts.nextTok(); /// (
354 //print("GOT : applyIF!");
356 token = this.ts.nextTok(); // b
357 scopeName = token.data;
360 if (this.currentDoc) {
361 this.addSymbol(scopeName,false,'OBJECT');
366 this.ts.nextTok(); /// ,
367 this.ts.nextTok(); // {
368 scopeName = fixAlias(scopeName);
369 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
370 this.indexedScopes[this.ts.cursor] = fnScope;
372 this.scopesIn(fnScope);
375 //print(">>" +locBraceNest);
376 continue; // no more processing..
382 // change scope to xxxx
384 print(JSON.stringify([
385 this.ts.lookTok(1).data ,
386 this.ts.lookTok(2).name ,
387 this.ts.lookTok(3).type ,
388 this.ts.lookTok(4).data ,
389 this.ts.lookTok(5).data
392 if ( this.ts.lookTok(1).data == '=' &&
393 this.ts.lookTok(2).name == 'NEW' &&
394 this.ts.lookTok(3).type == 'NAME' &&
395 this.ts.lookTok(4).data == '(' &&
396 this.ts.lookTok(5).data == '{'
398 scopeName = token.data;
399 if (this.currentDoc) {
400 this.addSymbol(scopeName,false,'OBJECT');
404 this.ts.nextTok(); /// =
405 this.ts.nextTok(); /// new
406 this.ts.nextTok(); /// yyy
407 this.ts.nextTok(); /// (
408 this.ts.nextTok(); /// {
410 scopeName = fixAlias(scopeName);
411 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
412 this.indexedScopes[this.ts.cursor] = fnScope;
414 this.scopesIn(fnScope);
417 //print(">>" +locBraceNest);
419 continue; // no more processing..
440 // eval can be prefixed with a hint hider for the compresser..
441 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
442 this.currentDoc = false;
446 if (this.currentDoc) {
447 //print(token.toString());
449 // ident : function ()
450 // ident = function ()
451 var atype = 'OBJECT';
453 if (((this.ts.lookTok(1).data == ':' )|| (this.ts.lookTok(1).data == '=')) &&
454 (this.ts.lookTok(2).name == "FUNCTION")
459 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookTok(1).toString() + this.ts.lookTok(2).toString());
462 this.ts.lookTok(-1).tokN == Script.TOKdot ? token.data : fixAlias(token.data),
469 continue; // dont care about other idents..
472 if (token.type == "STRN") {
473 if (this.currentDoc) {
474 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
479 // really we only have to deal with object constructs and function calls that change the scope...
482 if (token.name == 'FUNCTION') {
484 // see if we have an unconsumed doc...
486 if (this.currentDoc) {
487 print(this.ts.dumpToCur());
488 throw "Unhandled doc (TOKfunction)" + token.toString();
489 //this.addSymbol(this.currentDoc.getTag('class')[0].name, true);
491 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
498 /// foo = function() {} << really it set's the 'this' scope to foo.prototype
499 //$this$=foo.prototype|$private$|foo.prototype
502 (this.ts.lookTok(-1).data == '=') &&
503 (this.ts.lookTok(-2).type == 'NAME')
505 scopeName = this.ts.lookTok(-2).data;
506 this.ts.balanceN('(');
507 token = this.ts.nextTok(); // should be {
508 //print("FOO=FUNCITON() {}" + this.ts.context() + "\n" + token.toString());
511 scopeName = fixAlias(scopeName);
512 var fnScope = new Scope(this.braceNesting, scope, token.n,
513 '$this$='+scopeName+'.prototype|$private$|'+scopeName+'.prototype');
515 this.indexedScopes[this.ts.cursor] = fnScope;
517 //this.scopesIn(fnScope);
518 this.parseScope(fnScope, aliases);
523 //print(">>" +locBraceNest);
524 continue; // no more processing..
530 // foo = new function() {}
531 // is this actually used much!?!?!
535 (this.ts.lookTok(-1).name == 'NEW') &&
536 (this.ts.lookTok(-2).data == '=') &&
537 (this.ts.lookTok(-3).type = 'FUNCTION')
539 //scopeName = this.ts.look(-3).data;
540 this.ts.balanceN(Script.TOKlparen);
541 token = this.ts.nextTok(); // should be {
542 scopeName = fixAlias(scopeName);
543 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
544 this.indexedScopes[this.ts.cursor] = fnScope;
546 //this.scopesIn(fnScope);
547 this.parseScope(fnScope, aliases);
550 //print(">>" +locBraceNest);
551 continue; // no more processing..
557 ///==== check/set isObjectLitAr ??
560 // foo: function() {}
561 // no change to scoping..
563 //print("checking for : function() {");
564 //print( [this.ts.lookTok(-3).type , this.ts.lookTok(-2).type , this.ts.lookTok(-1).type ].join(":"));
566 (this.ts.lookTok(-1).data == ':') &&
567 (this.ts.lookTok(-2).type == 'NAME') &&
568 (this.ts.lookTok(-3).data == '(' || this.ts.lookTok(-3).data== ',')
570 //print("got for : function() {");
572 //scopeName = this.ts.look(-3).data;
573 this.ts.balanceN(Script.TOKlparen);
574 //print(token.toString())
575 token = this.ts.nextTok(); // should be {
576 //print(token.toString())
577 scopeName = fixAlias(scopeName);
578 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
579 this.indexedScopes[this.ts.cursor] = fnScope;
581 //this.scopesIn(fnScope);
582 this.parseScope(fnScope, aliases);
584 //print(">>" +locBraceNest);
585 continue; // no more processing..
588 /// function foo() {} << really it set's the 'this' scope to foo.prototype
589 //$this$=foo|$private$
593 (this.ts.lookTok(1).type == 'NAME')
595 //scopeName = this.ts.look(-3).data;
596 this.ts.balanceN('(');
597 token = this.ts.nextTok(); // should be {
599 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
600 this.indexedScopes[this.ts.cursor] = fnScope;
602 //this.scopesIn(fnScope);
603 this.parseScope(fnScope, aliases);
605 //print(">>" +locBraceNest);
606 continue; // no more processing..
611 // foo = new (function() { }
613 // RETURN function(...) {
616 // (this.ts.lookTok(-1).tokN == Script.TOKlparen) &&
617 (this.ts.lookTok(1).name != 'NAME')
619 // (this.ts.lookTok(-2).tokN == Script.TOKnew) &&
620 // (this.ts.lookTok(-3).tokN == Script.TOKassign) &&
621 // (this.ts.lookTok(-4).tokN == Script.TOKidentifier)
623 //scopeName = this.ts.look(-3).data;
624 this.ts.balanceN('(');
625 token = this.ts.nextTok(); // should be {
626 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
627 this.indexedScopes[this.ts.cursor] = fnScope;
629 //this.scopesIn(fnScope);
630 this.parseScope(fnScope, aliases);
632 //print(">>" +locBraceNest);
633 continue; // no more processing..
639 print(this.ts.context());
640 throw "dont know how to handle function syntax??";
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("addSymbol : " + lastIndent);
743 /*if (!this.currentDoc.tags.length) {
746 //print(this.currentDoc.toSource());
747 // this.currentDoc = false;
749 print("SKIP ADD SYM: no tags");
750 print(this.currentDoc.src);
754 if (this.currentDoc.getTag('private').length) {
757 //print(this.currentDoc.toSource());
758 this.currentDoc = false;
759 print("SKIP ADD SYM: it's private");
763 var token = this.ts.cur();
764 if (typeof(appendIt) == 'undefined') {
767 // print(this.currentDoc.toSource(););
768 if (this.currentDoc.getTag('event').length) {
769 //?? why does it end up in desc - and not name/...
770 print(this.currentDoc.getTag('event')[0]);
771 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
772 //lastIdent = '*' + lastIdent ;
774 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
775 lastIdent = this.currentDoc.getTag('property')[0].name;
776 //lastIdent = '*' + lastIdent ;
780 if (!/\./.test(_s)) {
782 //print("WALKER ADDsymbol: " + lastIdent);
785 for (var i = 0; i < this.scopes.length;i++) {
786 s.push(this.scopes[i].ident);
790 var s = s.join('|').split('|');
791 print("Walker:ADDSymbol: " + s.join('|') );
796 for (var i = 0; i < s.length;i++) {
801 if ((s[i] == '$private$') || (s[i] == '$global$')) {
805 if (s[i].substring(0,6) == '$this$') {
806 var ts = s[i].split('=');
810 // when to use $this$ (probabl for events)
811 _s += _s.length ? '.' : '';
817 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
819 if (appendIt && !lastIdent.length) {
821 // append, and no symbol???
823 // see if it's a @class
824 if (this.currentDoc.getTag('class').length) {
825 _s = this.currentDoc.getTag('class')[0].desc;
826 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
827 Parser.addSymbol(symbol);
828 this.symbols[_s] = symbol;
832 // if (this.currentDoc.getTag('property').length) {
833 // print(Script.pretStringtyDump(this.currentDoc.toSource));
834 // throw "Add Prop?";
837 _s = _s.replace(/\.prototype.*$/, '');
838 if (typeof(this.symbols[_s]) == 'undefined') {
839 print("Symbol:" + _s);
840 print(this.currentDoc.src);
842 throw "Trying to append symbol, but no doc available";
845 for (var i =0; i < this.currentDoc.tags.length;i++) {
846 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
848 this.currentDoc = false;
852 if (typeof(this.symbols[_s]) != 'undefined') {
854 if (this.symbols[_s].comment.hasTags) {
855 // then existing comment doesnt has tags
856 throw "DUPLICATE Symbol " + _s;
858 // otherwise existing comment has tags - overwrite..
862 if (typeof(atype) == "undefined") {
863 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
866 var symbol = new Symbol(_s, [], atype, this.currentDoc);
868 Parser.addSymbol(symbol);
869 this.symbols[_s] = symbol;
871 this.currentDoc = false;
878 scopesIn : function(s)
881 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
884 scopeOut : function()
887 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
888 return this.scopes.pop();
892 scopeListToStr : function()
895 for (var i = 0; i < this.scopes.length;i++) {
896 s.push(this.scopes[i].ident);
898 return s.join('\n\t');