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;
22 * @param {Object} cfg Configuration
24 TokenStream = XObject.define(
28 XObject.extend(this, cfg);
30 //this.tokens = this.tokens || []);
31 // Seed.print(this.tokens.length);
37 * @cfg {Array} tokens Original Tokens from tokenizer
41 * @cfg {String} filename srcFile the source file
47 * @type {Number} The current position in the stream.
50 cursor : -1, // where are we in the stream.
59 look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
60 if (typeof n == "undefined") n = 0;
62 if (considerWhitespace == true) {
63 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
64 return new Token("", "VOID", "START_OF_STREAM");
66 return this.tokens[this.cursor+n];
73 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
74 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
76 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
77 if (n < 0) i--; else i++;
81 if (count == Math.abs(n)) {
82 return this.tokens[i];
88 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
92 lookFor : function (data)
95 var i = this.cursor < 0 ? 0 : this.cursor ;
98 if (i >= this.tokens.length) return -1;
99 if (this.tokens[i].data == data) {
105 // should not get here!
112 * look ahead (or back) x number of tokens (which are not comment or whitespace)
115 lookTok : function(/**Number*/n) {
116 if (typeof n == "undefined") n = 1;
131 return new Token("", "VOID", "END_OF_STREAM");
133 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
135 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
136 if (n < 0) i--; else i++;
140 if (count == Math.abs(n)) {
141 return this.tokens[i];
146 // should never get here..
147 return false; // because null isn't an object and caller always expects an object;
152 * @return {Token|null}
153 * next token (with white space)
157 next : function(/**Number*/howMany) {
158 if (typeof howMany == "undefined") howMany = 1;
159 if (howMany < 1) return null;
162 for (var i = 1; i <= howMany; i++) {
163 if (this.cursor+i >= this.tokens.length) {
166 got.push(this.tokens[this.cursor+i]);
168 this.cursor += howMany;
175 // what about comments after 'function'...
177 nextTok : function() {
178 return this.nextNonSpace();
180 nextNonSpace : function ()
188 if (tok.is('WHIT') || tok.is('COMM')) {
195 * @type JSDOC.Token[]
196 * @param start {String} token name or data (eg. '{'
197 * @param stop {String} (Optional) token name or data (eg. '}'
199 balance : function(/**String*/start, /**String*/stop) {
202 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
204 if (!stop) stop = Lang.matching(start);
209 //Seed.print("START:" + start);
210 //Seed.print("STOP:" + stop);
211 while ((token = this.look())) {
212 if (token.is(start)) {
213 // Seed.print("balance: START : " + depth + " " + token.data);
222 if (token.is(stop)) {
224 // Seed.print("balance: STOP: " + depth + " " + token.data);
225 if (depth < 1) return got;
227 if (!this.next()) break;
231 getMatchingToken : function(/**String*/start, /**String*/stop) {
233 var cursor = this.cursor;
236 start = Lang.matching(stop);
239 if (!stop) stop = Lang.matching(start);
241 while ((token = this.tokens[cursor])) {
242 if (token.is(start)) {
246 if (token.is(stop) && cursor) {
248 if (depth == 0) return this.tokens[cursor];
254 insertAhead : function(/**JSDOC.Token*/token) {
255 this.tokens.splice(this.cursor+1, 0, token);
258 remaining : function() {
261 var tok = this.look(1,true);
262 if (!tok || !tok.is || tok.is('VOID')) {
265 ret.push(this.next(1));
270 arrayToString : function(ar) {
271 console.log(typeof(ar));
273 ar.forEach(function(e) {
278 dump: function(start, end)
280 start = Math.max(start || 0, 0);
281 end = Math.min(end || this.tokens.length, this.tokens.length);
283 for (var i =start;i < end; i++) {
285 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;