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..
333 print(JSON.stringify([
335 this.ts.lookTok(1).data ,
336 this.ts.lookTok(2).type ,
337 this.ts.lookTok(3).data ,
338 this.ts.lookTok(4).data
342 if (/\.(applyIf|apply)$/.test(token.data) &&
343 this.ts.lookTok(1).data == '(' &&
344 this.ts.lookTok(2).type == 'NAME' &&
345 this.ts.lookTok(3).data == ',' &&
346 this.ts.lookTok(4).data == '{'
349 this.ts.nextTok(); /// (
351 //print("GOT : applyIF!");
353 token = this.ts.nextTok(); // b
354 scopeName = token.data;
357 if (this.currentDoc) {
358 this.addSymbol(scopeName,false,'OBJECT');
364 this.ts.nextTok(); /// ,
365 this.ts.nextTok(); // {
366 scopeName = fixAlias(scopeName);
367 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
368 this.indexedScopes[this.ts.cursor] = fnScope;
370 this.scopesIn(fnScope);
373 //print(">>" +locBraceNest);
374 continue; // no more processing..
380 // change scope to xxxx
382 print(JSON.stringify([
383 this.ts.lookTok(1).data ,
384 this.ts.lookTok(2).name ,
385 this.ts.lookTok(3).type ,
386 this.ts.lookTok(4).data ,
387 this.ts.lookTok(5).data
390 if ( this.ts.lookTok(1).data == '=' &&
391 this.ts.lookTok(2).name == 'NEW' &&
392 this.ts.lookTok(3).type == 'NAME' &&
393 this.ts.lookTok(4).data == '(' &&
394 this.ts.lookTok(5).data == '{'
396 scopeName = token.data;
397 if (this.currentDoc) {
398 this.addSymbol(scopeName,false,'OBJECT');
402 this.ts.nextTok(); /// =
403 this.ts.nextTok(); /// new
404 this.ts.nextTok(); /// yyy
405 this.ts.nextTok(); /// (
406 this.ts.nextTok(); /// {
408 scopeName = fixAlias(scopeName);
409 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
410 this.indexedScopes[this.ts.cursor] = fnScope;
412 this.scopesIn(fnScope);
415 //print(">>" +locBraceNest);
417 continue; // no more processing..
438 // eval can be prefixed with a hint hider for the compresser..
439 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
440 this.currentDoc = false;
444 if (this.currentDoc) {
445 //print(token.toString());
447 // ident : function ()
448 // ident = function ()
449 var atype = 'OBJECT';
451 if (((this.ts.lookTok(1).data == ':' )|| (this.ts.lookTok(1).data == '=')) &&
452 (this.ts.lookTok(2).name == "FUNCTION")
457 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookTok(1).toString() + this.ts.lookTok(2).toString());
460 this.ts.lookTok(-1).tokN == Script.TOKdot ? token.data : fixAlias(token.data),
467 continue; // dont care about other idents..
470 if (token.type == "STRN") {
471 if (this.currentDoc) {
472 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
477 // really we only have to deal with object constructs and function calls that change the scope...
480 if (token.name == 'FUNCTION') {
482 // see if we have an unconsumed doc...
484 if (this.currentDoc) {
485 print(this.ts.dumpToCur());
486 throw "Unhandled doc (TOKfunction)" + token.toString();
487 //this.addSymbol(this.currentDoc.getTag('class')[0].name, true);
489 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
496 /// foo = function() {} << really it set's the 'this' scope to foo.prototype
497 //$this$=foo.prototype|$private$|foo.prototype
500 (this.ts.lookTok(-1).data == '=') &&
501 (this.ts.lookTok(-2).type == 'NAME')
503 scopeName = this.ts.lookTok(-2).data;
504 this.ts.balanceN('(');
505 token = this.ts.nextTok(); // should be {
506 //print("FOO=FUNCITON() {}" + this.ts.context() + "\n" + token.toString());
509 scopeName = fixAlias(scopeName);
510 var fnScope = new Scope(this.braceNesting, scope, token.n,
511 '$this$='+scopeName+'.prototype|$private$|'+scopeName+'.prototype');
512 this.indexedScopes[this.ts.cursor] = fnScope;
514 //this.scopesIn(fnScope);
515 this.parseScope(fnScope, aliases);
520 //print(">>" +locBraceNest);
521 continue; // no more processing..
527 // foo = new function() {}
528 // is this actually used much!?!?!
532 (this.ts.lookTok(-1).name == 'NEW') &&
533 (this.ts.lookTok(-2).data == '=') &&
534 (this.ts.lookTok(-3).type = 'FUNCTION')
536 //scopeName = this.ts.look(-3).data;
537 this.ts.balanceN(Script.TOKlparen);
538 token = this.ts.nextTok(); // should be {
539 scopeName = fixAlias(scopeName);
540 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
541 this.indexedScopes[this.ts.cursor] = fnScope;
543 //this.scopesIn(fnScope);
544 this.parseScope(fnScope, aliases);
547 //print(">>" +locBraceNest);
548 continue; // no more processing..
554 ///==== check/set isObjectLitAr ??
557 // foo: function() {}
558 // no change to scoping..
560 //print("checking for : function() {");
561 //print( [this.ts.lookTok(-3).type , this.ts.lookTok(-2).type , this.ts.lookTok(-1).type ].join(":"));
563 (this.ts.lookTok(-1).data == ':') &&
564 (this.ts.lookTok(-2).type == 'NAME') &&
565 (this.ts.lookTok(-3).data == '(' || this.ts.lookTok(-3).data== ',')
567 //print("got for : function() {");
569 //scopeName = this.ts.look(-3).data;
570 this.ts.balanceN(Script.TOKlparen);
571 //print(token.toString())
572 token = this.ts.nextTok(); // should be {
573 //print(token.toString())
574 scopeName = fixAlias(scopeName);
575 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
576 this.indexedScopes[this.ts.cursor] = fnScope;
578 //this.scopesIn(fnScope);
579 this.parseScope(fnScope, aliases);
581 //print(">>" +locBraceNest);
582 continue; // no more processing..
585 /// function foo() {} << really it set's the 'this' scope to foo.prototype
586 //$this$=foo|$private$
590 (this.ts.lookTok(1).type == 'NAME')
592 //scopeName = this.ts.look(-3).data;
593 this.ts.balanceN('(');
594 token = this.ts.nextTok(); // should be {
596 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
597 this.indexedScopes[this.ts.cursor] = fnScope;
599 //this.scopesIn(fnScope);
600 this.parseScope(fnScope, aliases);
602 //print(">>" +locBraceNest);
603 continue; // no more processing..
608 // foo = new (function() { }
610 // RETURN function(...) {
613 // (this.ts.lookTok(-1).tokN == Script.TOKlparen) &&
614 (this.ts.lookTok(1).name != 'NAME')
616 // (this.ts.lookTok(-2).tokN == Script.TOKnew) &&
617 // (this.ts.lookTok(-3).tokN == Script.TOKassign) &&
618 // (this.ts.lookTok(-4).tokN == Script.TOKidentifier)
620 //scopeName = this.ts.look(-3).data;
621 this.ts.balanceN('(');
622 token = this.ts.nextTok(); // should be {
623 var fnScope = new Scope(this.braceNesting, scope, token.n, '$private$');
624 this.indexedScopes[this.ts.cursor] = fnScope;
626 //this.scopesIn(fnScope);
627 this.parseScope(fnScope, aliases);
629 //print(">>" +locBraceNest);
630 continue; // no more processing..
636 print(this.ts.context());
637 throw "dont know how to handle function syntax??";
644 } // end checking for TOKfunction
646 if (token.data == '{') {
648 // foo = { // !var!!!
653 (this.ts.lookTok(-1).data == '=') &&
654 (this.ts.lookTok(-2).type == 'NAME') &&
655 (this.ts.lookTok(-3).nane != 'VAR')
658 scopeName = this.ts.look(-2).data;
659 scopeName = fixAlias(scopeName);
660 var fnScope = new Scope(this.braceNesting, scope, token.n,
661 '$this$='+scopeName + '|'+scopeName
663 this.indexedScopes[this.ts.cursor] = fnScope;
665 this.scopesIn(fnScope);
669 //print(">>" +locBraceNest);
670 continue; // no more processing..
675 //print("GOT LBRACE : check for :");
677 (this.ts.lookTok(-1).data == ':') &&
678 (this.ts.lookTok(-2).type == 'NAME') &&
679 (this.ts.lookTok(-3).name != 'VAR')
682 scopeName = this.ts.lookTok(-2).data;
683 scopeName = fixAlias(scopeName);
684 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
685 this.indexedScopes[this.ts.cursor] = fnScope;
687 this.scopesIn(fnScope);
690 //print(">>" +locBraceNest);
691 continue; // no more processing..
693 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
694 this.indexedScopes[this.ts.cursor] = fnScope;
696 this.scopesIn(fnScope);
699 //print(">>" +locBraceNest);
703 if (token.data == '{') {
706 if (this.currentDoc) {
707 this.addSymbol('', true);
709 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
715 //assert braceNesting >= scope.getBraceNesting();
716 var closescope = this.scopeOut();
717 scope = this.scopes[this.scopes.length-1];
718 //print("<<:" + locBraceNest)
719 //print("<<<<<< " + locBraceNest );
720 if (locBraceNest < 0) {
721 // print("POPED OF END OF SCOPE!");
723 //var ls = this.scopeOut();
724 //ls.getUsedSymbols();
737 addSymbol: function(lastIdent, appendIt, atype )
740 /*if (!this.currentDoc.tags.length) {
743 //print(this.currentDoc.toSource());
744 // this.currentDoc = false;
746 print("SKIP ADD SYM: no tags");
747 print(this.currentDoc.src);
751 if (this.currentDoc.getTag('private').length) {
754 //print(this.currentDoc.toSource());
755 this.currentDoc = false;
756 print("SKIP ADD SYM: it's private");
760 var token = this.ts.cur();
761 if (typeof(appendIt) == 'undefined') {
764 // print(this.currentDoc.toSource(););
765 if (this.currentDoc.getTag('event').length) {
766 //?? why does it end up in desc - and not name/...
767 print(this.currentDoc.getTag('event')[0]);
768 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
769 //lastIdent = '*' + lastIdent ;
771 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
772 lastIdent = this.currentDoc.getTag('property')[0].name;
773 //lastIdent = '*' + lastIdent ;
777 if (!/\./.test(_s)) {
779 //print("WALKER ADDsymbol: " + lastIdent);
782 for (var i = 0; i < this.scopes.length;i++) {
783 s.push(this.scopes[i].ident);
787 var s = s.join('|').split('|');
788 print("Walker:ADDSymbol: " + s.join('|') );
793 for (var i = 0; i < s.length;i++) {
798 if ((s[i] == '$private$') || (s[i] == '$global$')) {
802 if (s[i].substring(0,6) == '$this$') {
803 var ts = s[i].split('=');
807 // when to use $this$ (probabl for events)
808 _s += _s.length ? '.' : '';
814 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
816 if (appendIt && !lastIdent.length) {
818 // append, and no symbol???
820 // see if it's a @class
821 if (this.currentDoc.getTag('class').length) {
822 _s = this.currentDoc.getTag('class')[0].desc;
823 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
824 Parser.addSymbol(symbol);
825 this.symbols[_s] = symbol;
829 // if (this.currentDoc.getTag('property').length) {
830 // print(Script.pretStringtyDump(this.currentDoc.toSource));
831 // throw "Add Prop?";
834 _s = _s.replace(/\.prototype.*$/, '');
835 if (typeof(this.symbols[_s]) == 'undefined') {
836 print("Symbol:" + _s);
837 print(this.currentDoc.src);
839 throw "Trying to append symbol, but no doc available";
842 for (var i =0; i < this.currentDoc.tags.length;i++) {
843 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
845 this.currentDoc = false;
849 if (typeof(this.symbols[_s]) != 'undefined') {
851 if (this.symbols[_s].comment.hasTags) {
852 // then existing comment doesnt has tags
853 throw "DUPLICATE Symbol " + _s;
855 // otherwise existing comment has tags - overwrite..
859 if (typeof(atype) == "undefined") {
860 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
863 var symbol = new Symbol(_s, [], atype, this.currentDoc);
865 Parser.addSymbol(symbol);
866 this.symbols[_s] = symbol;
868 this.currentDoc = false;
875 scopesIn : function(s)
878 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
881 scopeOut : function()
884 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
885 return this.scopes.pop();
889 scopeListToStr : function()
892 for (var i = 0; i < this.scopes.length;i++) {
893 s.push(this.scopes[i].ident);
895 return s.join('\n\t');