1 //<script type="text/javscript">
4 XObject = imports.XObject.XObject;
8 console = imports.console.console;
9 Token = imports.Token.Token;
10 Lang = imports.Lang.Lang;
24 TokenStream = XObject.define(
35 this.tokens = (tokens || []);
36 // Seed.print(this.tokens.length);
41 cursor : -1, // where are we in the stream.
50 look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
51 if (typeof n == "undefined") n = 0;
53 if (considerWhitespace == true) {
54 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
55 return new Token("", "VOID", "START_OF_STREAM");
57 return this.tokens[this.cursor+n];
64 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
65 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
67 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
68 if (n < 0) i--; else i++;
72 if (count == Math.abs(n)) {
73 return this.tokens[i];
79 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
83 lookFor : function (data)
86 var i = this.cursor < 0 ? 0 : this.cursor ;
89 if (i >= this.tokens.length) return -1;
90 if (this.tokens[i].data == data) {
96 // should not get here!
103 * look ahead (or back) x number of tokens (which are not comment or whitespace)
106 lookTok : function(/**Number*/n) {
107 if (typeof n == "undefined") n = 1;
119 return new Token("", "VOID", "END_OF_STREAM");
121 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
123 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
124 if (n < 0) i--; else i++;
128 if (count == Math.abs(n)) {
129 return this.tokens[i];
134 // should never get here..
135 return false; // because null isn't an object and caller always expects an object;
140 * @return {Token|null}
144 next : function(/**Number*/howMany) {
145 if (typeof howMany == "undefined") howMany = 1;
146 if (howMany < 1) return null;
149 for (var i = 1; i <= howMany; i++) {
150 if (this.cursor+i >= this.tokens.length) {
153 got.push(this.tokens[this.cursor+i]);
155 this.cursor += howMany;
162 // what about comments after 'function'...
164 nextTok : function() {
165 return this.nextNonSpace();
167 nextNonSpace : function ()
175 if (tok.is('WHIT') || tok.is('COMM')) {
182 * @type JSDOC.Token[]
183 * @param start {String} token name or data (eg. '{'
184 * @param stop {String} (Optional) token name or data (eg. '}'
186 balance : function(/**String*/start, /**String*/stop) {
188 start = typeof(Lang.matching(start)) == 'undefined' ? Lang.punc(start) : start;
190 if (!stop) stop = Lang.matching(start);
195 //Seed.print("STOP:" + stop);
196 while ((token = this.look())) {
197 if (token.is(start)) {
206 if (token.is(stop)) {
208 if (depth == 0) return got;
210 if (!this.next()) break;
214 getMatchingToken : function(/**String*/start, /**String*/stop) {
216 var cursor = this.cursor;
219 start = Lang.matching(stop);
222 if (!stop) stop = Lang.matching(start);
224 while ((token = this.tokens[cursor])) {
225 if (token.is(start)) {
229 if (token.is(stop) && cursor) {
231 if (depth == 0) return this.tokens[cursor];
237 insertAhead : function(/**JSDOC.Token*/token) {
238 this.tokens.splice(this.cursor+1, 0, token);
241 remaining : function() {
244 var tok = this.look(1,true);
245 if (!tok || !tok.is || tok.is('VOID')) {
248 ret.push(this.next(1));
253 arrayToString : function(ar) {
254 console.log(typeof(ar));
256 ar.forEach(function(e) {
263 this.tokens.forEach(function(t) {