1 //<script type="text/javscript">
4 XObject = imports.XObject.XObject;
8 console = imports.console.console;
9 Token = imports.Token.Token;
10 Lang = imports.Lange.Lang;
16 TokenStream = XObject.define(
20 this.tokens = (tokens || []);
21 // Seed.print(this.tokens.length);
26 cursor : -1, // where are we in the stream.
35 look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
36 if (typeof n == "undefined") n = 0;
38 if (considerWhitespace == true) {
39 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
40 return new Token("", "VOID", "START_OF_STREAM");
42 return this.tokens[this.cursor+n];
49 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
50 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
52 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
53 if (n < 0) i--; else i++;
57 if (count == Math.abs(n)) {
58 return this.tokens[i];
64 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
68 lookFor : function (data)
71 var i = this.cursor < 0 ? 0 : this.cursor ;
74 if (i >= this.tokens.length) return -1;
75 if (this.tokens[i].data == data) {
81 // should not get here!
88 * look ahead (or back) x number of tokens (which are not comment or whitespace)
91 lookTok : function(/**Number*/n) {
92 if (typeof n == "undefined") n = 1;
104 return new Token("", "VOID", "END_OF_STREAM");
106 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
108 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
109 if (n < 0) i--; else i++;
113 if (count == Math.abs(n)) {
114 return this.tokens[i];
119 // should never get here..
120 return false; // because null isn't an object and caller always expects an object;
125 @type JSDOC.Token|JSDOC.Token[]| null!
127 next : function(/**Number*/howMany) {
128 if (typeof howMany == "undefined") howMany = 1;
129 if (howMany < 1) return null;
132 for (var i = 1; i <= howMany; i++) {
133 if (this.cursor+i >= this.tokens.length) {
136 got.push(this.tokens[this.cursor+i]);
138 this.cursor += howMany;
145 // what about comments after 'function'...
147 nextTok : function() {
148 return this.nextNonSpace();
150 nextNonSpace : function ()
158 if (tok.is('WHIT') || tok.is('COMM')) {
165 * @type JSDOC.Token[]
166 * @param start {String} token name or data (eg. '{'
167 * @param stop {String} (Optional) token name or data (eg. '}'
169 balance : function(/**String*/start, /**String*/stop) {
171 start = typeof(Lang.matching(start)) == 'undefined' ? Lang.punc(start) : start;
173 if (!stop) stop = Lang.matching(start);
178 //Seed.print("STOP:" + stop);
179 while ((token = this.look())) {
180 if (token.is(start)) {
189 if (token.is(stop)) {
191 if (depth == 0) return got;
193 if (!this.next()) break;
197 getMatchingToken : function(/**String*/start, /**String*/stop) {
199 var cursor = this.cursor;
202 start = Lang.matching(stop);
205 if (!stop) stop = Lang.matching(start);
207 while ((token = this.tokens[cursor])) {
208 if (token.is(start)) {
212 if (token.is(stop) && cursor) {
214 if (depth == 0) return this.tokens[cursor];
220 insertAhead : function(/**JSDOC.Token*/token) {
221 this.tokens.splice(this.cursor+1, 0, token);
224 remaining : function() {
227 var tok = this.look(1,true);
228 if (!tok || !tok.is || tok.is('VOID')) {
231 ret.push(this.next(1));
236 arrayToString : function(ar) {
237 console.log(typeof(ar));
239 ar.forEach(function(e) {
246 this.tokens.forEach(function(t) {