15 class TokenStream : Object
18 Gee.ArrayList<Token> tokens;
22 TokenStream(Gee.ArrayList<Token> tokens) {
29 int cursor; // where are we in the stream.
38 look : function(int n, bool considerWhitespace)
40 if (typeof n == "undefined") n = 0;
42 if (considerWhitespace == true) {
43 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
44 return new Token("", "VOID", "START_OF_STREAM");
46 return this.tokens[this.cursor+n];
53 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
54 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
56 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
57 if (n < 0) i--; else i++;
61 if (count == Math.abs(n)) {
62 return this.tokens[i];
68 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
72 lookFor : function (data)
75 var i = this.cursor < 0 ? 0 : this.cursor ;
78 if (i >= this.tokens.length) return -1;
79 if (this.tokens[i].data == data) {
85 // should not get here!
92 * look ahead (or back) x number of tokens (which are not comment or whitespace)
95 lookTok : function(/**Number*/n) {
96 if (typeof n == "undefined") n = 1;
111 return new Token("", "VOID", "END_OF_STREAM");
113 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
115 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
116 if (n < 0) i--; else i++;
120 if (count == Math.abs(n)) {
121 return this.tokens[i];
126 // should never get here..
127 return false; // because null isn't an object and caller always expects an object;
132 * @return {Token|null}
133 * next token (with white space)
137 next : function(/**Number*/howMany) {
138 if (typeof howMany == "undefined") howMany = 1;
139 if (howMany < 1) return null;
142 for (var i = 1; i <= howMany; i++) {
143 if (this.cursor+i >= this.tokens.length) {
146 got.push(this.tokens[this.cursor+i]);
148 this.cursor += howMany;
155 // what about comments after 'function'...
157 nextTok : function() {
158 return this.nextNonSpace();
160 nextNonSpace : function ()
168 if (tok.is('WHIT') || tok.is('COMM')) {
175 * @type JSDOC.Token[]
176 * @param start {String} token name or data (eg. '{'
177 * @param stop {String} (Optional) token name or data (eg. '}'
179 balance : function(/**String*/start, /**String*/stop) {
182 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
184 if (!stop) stop = Lang.matching(start);
189 //Seed.print("START:" + start);
190 //Seed.print("STOP:" + stop);
191 while ((token = this.look())) {
192 if (token.is(start)) {
193 // Seed.print("balance: START : " + depth + " " + token.data);
202 if (token.is(stop)) {
204 // Seed.print("balance: STOP: " + depth + " " + token.data);
205 if (depth < 1) return got;
207 if (!this.next()) break;
212 getMatchingToken : function(/**String*/start, /**String*/stop) {
214 var cursor = this.cursor;
217 start = Lang.matching(stop);
220 if (!stop) stop = Lang.matching(start);
222 while ((token = this.tokens[cursor])) {
223 if (token.is(start)) {
227 if (token.is(stop) && cursor) {
229 if (depth == 0) return this.tokens[cursor];
236 insertAhead : function(/**JSDOC.Token*/token) {
237 this.tokens.splice(this.cursor+1, 0, token);
240 remaining : function() {
243 var tok = this.look(1,true);
244 if (!tok || !tok.is || tok.is('VOID')) {
247 ret.push(this.next(1));
252 arrayToString : function(ar) {
253 console.log(typeof(ar));
255 ar.forEach(function(e) {
260 dump: function(start, end)
262 start = Math.max(start || 0, 0);
263 end = Math.min(end || this.tokens.length, this.tokens.length);
265 for (var i =start;i < end; i++) {
267 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;