15 public class TokenStream : Object
18 Gee.ArrayList<Token> tokens;
19 int cursor; // where are we in the stream.
22 public TokenStream(Gee.ArrayList<Token> tokens) {
31 public void rewind() {
38 public Token? look (int n, bool considerWhitespace)
42 if (considerWhitespace == true) {
44 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.size -1)) {
45 return new Token("", "VOID", "START_OF_STREAM");
47 return this.tokens.get(this.cursor+n);
56 return new Token("", "VOID", "START_OF_STREAM");
58 if (i > this.tokens.size) {
59 return new Token("", "VOID", "END_OF_STREAM");
62 if (i != this.cursor && this.tokens.get(i).is("WHIT")) {
63 i += (n < 0) ? -1 : 1;
68 return this.tokens.get(i);
71 i += (n < 0) ? -1 : 1;
74 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
78 public int lookFor (string data)
81 var i = this.cursor < 0 ? 0 : this.cursor ;
84 if (i >= this.tokens.size) {
87 if (this.tokens.get(i).data == data) {
93 // should not get here!
100 * look ahead (or back) x number of tokens (which are not comment or whitespace)
103 public Token? lookTok (int n) {
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}
141 * next token (with white space)
145 next : function(/**Number*/howMany) {
146 if (typeof howMany == "undefined") howMany = 1;
147 if (howMany < 1) return null;
150 for (var i = 1; i <= howMany; i++) {
151 if (this.cursor+i >= this.tokens.length) {
154 got.push(this.tokens[this.cursor+i]);
156 this.cursor += howMany;
163 // what about comments after 'function'...
165 nextTok : function() {
166 return this.nextNonSpace();
168 nextNonSpace : function ()
176 if (tok.is('WHIT') || tok.is('COMM')) {
183 * @type JSDOC.Token[]
184 * @param start {String} token name or data (eg. '{'
185 * @param stop {String} (Optional) token name or data (eg. '}'
187 balance : function(/**String*/start, /**String*/stop) {
190 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
192 if (!stop) stop = Lang.matching(start);
197 //Seed.print("START:" + start);
198 //Seed.print("STOP:" + stop);
199 while ((token = this.look())) {
200 if (token.is(start)) {
201 // Seed.print("balance: START : " + depth + " " + token.data);
210 if (token.is(stop)) {
212 // Seed.print("balance: STOP: " + depth + " " + token.data);
213 if (depth < 1) return got;
215 if (!this.next()) break;
220 getMatchingToken : function(/**String*/start, /**String*/stop) {
222 var cursor = this.cursor;
225 start = Lang.matching(stop);
228 if (!stop) stop = Lang.matching(start);
230 while ((token = this.tokens[cursor])) {
231 if (token.is(start)) {
235 if (token.is(stop) && cursor) {
237 if (depth == 0) return this.tokens[cursor];
244 insertAhead : function(/**JSDOC.Token*/token) {
245 this.tokens.splice(this.cursor+1, 0, token);
248 remaining : function() {
251 var tok = this.look(1,true);
252 if (!tok || !tok.is || tok.is('VOID')) {
255 ret.push(this.next(1));
260 arrayToString : function(ar) {
261 console.log(typeof(ar));
263 ar.forEach(function(e) {
268 dump: function(start, end)
270 start = Math.max(start || 0, 0);
271 end = Math.min(end || this.tokens.length, this.tokens.length);
273 for (var i =start;i < end; i++) {
275 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;