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: " + token.toString())
174 //throw "Unconsumed Doc (TOKwhitespace): " + this.currentDoc.toSource();
177 // print ( "NEW COMMENT: " + token.toString())
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;
241 if ((token.data == 'eval') || /\.eval$/.test(token.data)) {
242 this.currentDoc = false;
246 // extends scoping *** not sure if the can be x = Roo.apply(....)
247 // xxx.extends(a,b, {
248 // $this$=b|b.prototype
253 if (token.type == 'NAME') {
255 //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..
428 // eval can be prefixed with a hint hider for the compresser..
431 if (this.currentDoc) {
432 //print(token.toString());
434 // ident : function ()
435 // ident = function ()
436 var atype = 'OBJECT';
438 if (((this.ts.lookTok(1).data == ':' )|| (this.ts.lookTok(1).data == '=')) &&
439 (this.ts.lookTok(2).name == "FUNCTION")
441 // this.ts.nextTok();
442 // this.ts.nextTok();
446 //print("ADD SYM:" + atype + ":" + token.toString() + this.ts.lookTok(1).toString() + this.ts.lookTok(2).toString());
449 this.ts.lookTok(-1).data == '.' ? token.data : fixAlias(token.data),
453 this.currentDoc = false;
459 continue; // dont care about other idents..
463 //print ("NOT NAME");
466 if (token.type == "STRN") { // THIS WILL NOT HAPPEN HERE?!!?
467 if (this.currentDoc) {
468 this.addSymbol(token.data.substring(1,token.data.length-1),false,'OBJECT');
473 // really we only have to deal with object constructs and function calls that change the scope...
476 if (token.name == 'FUNCTION') {
477 //print("GOT FUNCTION");
478 // see if we have an unconsumed doc...
480 if (this.currentDoc) {
482 name: "ArgumentError",
483 message: "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.balance('(');
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');
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.balance("(");
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.balance('(');
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.balance('(');
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.balance('(');
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..
637 name: "ArgumentError",
638 message: "dont know how to handle function syntax??\n" +
648 } // end checking for TOKfunction
650 if (token.data == '{') {
652 // foo = { // !var!!!
657 (this.ts.lookTok(-1).data == '=') &&
658 (this.ts.lookTok(-2).type == 'NAME') &&
659 (this.ts.lookTok(-3).name != 'VAR')
662 scopeName = this.ts.look(-2).data;
664 scopeName = fixAlias(scopeName);
666 print(this.scopes.length);
667 //var fnScope = new Scope(this.braceNesting, scope, token.n,
668 // '$this$='+scopeName + '|'+scopeName
671 //this.indexedScopes[this.ts.cursor] = fnScope;
673 // push the same scope onto the stack..
674 this.scopesIn(this.scopes[this.scopes.length-1]);
678 //print(">>" +locBraceNest);
679 continue; // no more processing..
684 //print("GOT LBRACE : check for :");
686 (this.ts.lookTok(-1).data == ':') &&
687 (this.ts.lookTok(-2).type == 'NAME') &&
688 (this.ts.lookTok(-3).name != 'VAR')
691 scopeName = this.ts.lookTok(-2).data;
692 scopeName = fixAlias(scopeName);
693 var fnScope = new Scope(this.braceNesting, scope, token.n, scopeName);
694 this.indexedScopes[this.ts.cursor] = fnScope;
696 this.scopesIn(fnScope);
699 //print(">>" +locBraceNest);
700 continue; // no more processing..
702 var fnScope = new Scope(this.braceNesting, scope, token.n, '');
703 this.indexedScopes[this.ts.cursor] = fnScope;
705 this.scopesIn(fnScope);
708 //print(">>" +locBraceNest);
712 if (token.data == '}') {
715 if (this.currentDoc) {
716 this.addSymbol('', true);
718 //throw "Unconsumed Doc: (TOKrbrace)" + this.currentDoc.toSource();
724 //assert braceNesting >= scope.getBraceNesting();
725 var closescope = this.scopeOut();
726 scope = this.scopes[this.scopes.length-1];
727 //print("<<:" + locBraceNest)
728 //print("<<<<<< " + locBraceNest );
729 if (locBraceNest < 0) {
730 // print("POPED OF END OF SCOPE!");
732 //var ls = this.scopeOut();
733 //ls.getUsedSymbols();
746 addSymbol: function(lastIdent, appendIt, atype )
748 //print("Walker.addSymbol : " + lastIdent);
749 // print("Walker.curdoc: " + JSON.stringify(this.currentDoc, null,4));
751 /*if (!this.currentDoc.tags.length) {
754 //print(this.currentDoc.toSource());
755 // this.currentDoc = false;
757 print("SKIP ADD SYM: no tags");
758 print(this.currentDoc.src);
762 if (this.currentDoc.getTag('private').length) {
765 //print(this.currentDoc.toSource());
766 this.currentDoc = false;
767 //print("SKIP ADD SYM: it's private");
771 var token = this.ts.lookTok(0);
772 if (typeof(appendIt) == 'undefined') {
775 // print(this.currentDoc.toSource(););
776 if (this.currentDoc.getTag('event').length) {
777 //?? why does it end up in desc - and not name/...
778 //print(this.currentDoc.getTag('event')[0]);
779 lastIdent = '*' + this.currentDoc.getTag('event')[0].desc;
780 //lastIdent = '*' + lastIdent ;
782 if (!lastIdent.length && this.currentDoc.getTag('property').length) {
783 lastIdent = this.currentDoc.getTag('property')[0].name;
784 //lastIdent = '*' + lastIdent ;
788 if (!/\./.test(_s)) {
790 //print("WALKER ADDsymbol: " + lastIdent);
793 for (var i = 0; i < this.scopes.length;i++) {
794 s.push(this.scopes[i].ident);
798 //print("FULLSCOPE: " + JSON.stringify(s));
801 var s = s.join('|').split('|');
802 print("FULLSCOPE: " + s);
803 // print("Walker:ADDSymbol: " + s.join('|') );
808 for (var i = 0; i < s.length;i++) {
813 if ((s[i] == '$private$') || (s[i] == '$global$')) {
817 if (s[i].substring(0,6) == '$this$') {
818 var ts = s[i].split('=');
820 _s = ''; // ??? VERY QUESTIONABLE!!!
823 // when to use $this$ (probabl for events)
824 _s += _s.length ? '.' : '';
827 //print("FULLSCOPE: s , t : " + _s +', ' + _t);
830 //print("ADDING SYMBOL: "+ s.join('|') +"\n"+ _s + "\n" +Script.prettyDump(this.currentDoc.toSource()));
831 //print("Walker.addsymbol - add : " + _s);
832 if (appendIt && !lastIdent.length) {
834 // append, and no symbol???
836 // see if it's a @class
837 if (this.currentDoc.getTag('class').length) {
838 _s = this.currentDoc.getTag('class')[0].desc;
839 var symbol = new Symbol(_s, [], "CONSTRUCTOR", this.currentDoc);
840 Parser = imports.Parser.Parser;
841 Parser.addSymbol(symbol);
842 this.symbols[_s] = symbol;
846 // if (this.currentDoc.getTag('property').length) {
847 // print(Script.pretStringtyDump(this.currentDoc.toSource));
848 // throw "Add Prop?";
851 _s = _s.replace(/\.prototype.*$/, '');
852 if (typeof(this.symbols[_s]) == 'undefined') {
853 //print("Symbol:" + _s);
854 //print(this.currentDoc.src);
857 // name: "ArgumentError",
858 // message: "Trying to append symbol '" + _s + "', but no doc available\n" +
859 // this.ts.lookTok(0).toString()
861 this.currentDoc = false;
866 for (var i =0; i < this.currentDoc.tags.length;i++) {
867 this.symbols[_s].addDocTag(this.currentDoc.tags[i]);
869 this.currentDoc = false;
873 //print("Walker.addsymbol - chkdup: " + _s);
874 if (typeof(this.symbols[_s]) != 'undefined') {
876 if (this.symbols[_s].comment.hasTags) {
877 // then existing comment doesnt has tags
879 // name: "ArgumentError",
880 // message:"DUPLICATE Symbol " + _s + "\n" + token.toString()
884 // otherwise existing comment has tags - overwrite..
888 //print("Walker.addsymbol - ATYPE: " + _s);
890 if (typeof(atype) == "undefined") {
891 atype = 'OBJECT'; //this.currentDoc.getTag('class').length ? 'OBJECT' : 'FUNCTION';;
894 //print("Walker.addsymbol - add : ");
895 var symbol = new Symbol(_s, [], atype, this.currentDoc);
896 Parser = imports.Parser.Parser;
897 Parser.addSymbol(symbol);
898 this.symbols[_s] = symbol;
900 this.currentDoc = false;
907 scopesIn : function(s)
910 //print(">>>" + this.ts.context() + "\n>>>"+this.scopes.length+":" +this.scopeListToStr());
913 scopeOut : function()
916 // print("<<<" + this.ts.context() + "\n<<<"+this.scopes.length+":" +this.scopeListToStr());
917 return this.scopes.pop();
921 scopeListToStr : function()
924 for (var i = 0; i < this.scopes.length;i++) {
925 s.push(this.scopes[i].ident);
927 return s.join('\n\t');