1 //<script type="text/javscript">
2 JSDOC = imports['JSDOC.js'].JSDOC;
3 Roo = imports['Roo.js'].Roo;
4 console = imports['console.js'].console;
6 Token = imports['JSDOC/Token.js'].Token;
7 Lang = imports['JSDOC/Lang.js'].Lang;
13 TokenStream = function(tokens) {
16 this.tokens = (tokens || []);
17 // Seed.print(this.tokens.length);
22 Roo.apply( TokenStream.prototype , {
31 look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
32 if (typeof n == "undefined") n = 0;
34 if (considerWhitespace == true) {
35 if (this.cursor+n < 0 || this.cursor+n > this.tokens.length) return {};
36 return this.tokens[this.cursor+n];
43 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
44 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
46 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
47 if (n < 0) i--; else i++;
51 if (count == Math.abs(n)) {
52 return this.tokens[i];
58 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
62 lookFor : function (data)
65 var i = this.cursor < 0 ? 0 : this.cursor ;
68 if (i >= this.tokens.length) return -1;
69 if (this.tokens[i].data == data) {
75 // should not get here!
82 * look ahead (or back) x number of tokens (which are not comment or whitespace)
85 lookTok : function(/**Number*/n) {
86 if (typeof n == "undefined") n = 1;
93 if (i < 0) return false;
94 else if (i > this.tokens.length) return false;
96 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
97 if (n < 0) i--; else i++;
101 if (count == Math.abs(n)) {
102 return this.tokens[i];
108 return false; // because null isn't an object and caller always expects an object;
113 @type JSDOC.Token|JSDOC.Token[]| null!
115 next : function(/**Number*/howMany) {
116 if (typeof howMany == "undefined") howMany = 1;
117 if (howMany < 1) return null;
120 for (var i = 1; i <= howMany; i++) {
121 if (this.cursor+i >= this.tokens.length) {
124 got.push(this.tokens[this.cursor+i]);
126 this.cursor += howMany;
133 // what about comments after 'function'...
136 nextNonSpace : function ()
144 if (tok.is('WHIT') || tok.is('COMM')) {
153 balance : function(/**String*/start, /**String*/stop) {
154 if (!stop) stop = Lang.matching(start);
159 //Seed.print("STOP:" + stop);
160 while ((token = this.look())) {
161 if (token.is(start)) {
170 if (token.is(stop)) {
172 if (depth == 0) return got;
174 if (!this.next()) break;
178 getMatchingToken : function(/**String*/start, /**String*/stop) {
180 var cursor = this.cursor;
183 start = Lang.matching(stop);
186 if (!stop) stop = Lang.matching(start);
188 while ((token = this.tokens[cursor])) {
189 if (token.is(start)) {
193 if (token.is(stop) && cursor) {
195 if (depth == 0) return this.tokens[cursor];
201 insertAhead : function(/**JSDOC.Token*/token) {
202 this.tokens.splice(this.cursor+1, 0, token);
205 remaining : function() {
208 var tok = this.look(1,true);
209 if (!tok || !tok.is || tok.is('VOID')) {
212 ret.push(this.next(1));
217 arrayToString : function(ar) {
218 console.log(typeof(ar));
220 Roo.each(ar, function(e) {