15 class TokenStream : Object
17 Gee.ArrayList<Token> tokens;
21 TokenStream(Gee.ArrayList<Token> tokens) {
28 int cursor; // where are we in the stream.
37 look : function(int n, bool considerWhitespace)
39 if (typeof n == "undefined") n = 0;
41 if (considerWhitespace == true) {
42 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
43 return new Token("", "VOID", "START_OF_STREAM");
45 return this.tokens[this.cursor+n];
52 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
53 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
55 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
56 if (n < 0) i--; else i++;
60 if (count == Math.abs(n)) {
61 return this.tokens[i];
67 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
71 lookFor : function (data)
74 var i = this.cursor < 0 ? 0 : this.cursor ;
77 if (i >= this.tokens.length) return -1;
78 if (this.tokens[i].data == data) {
84 // should not get here!
91 * look ahead (or back) x number of tokens (which are not comment or whitespace)
94 lookTok : function(/**Number*/n) {
95 if (typeof n == "undefined") n = 1;
110 return new Token("", "VOID", "END_OF_STREAM");
112 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
114 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
115 if (n < 0) i--; else i++;
119 if (count == Math.abs(n)) {
120 return this.tokens[i];
125 // should never get here..
126 return false; // because null isn't an object and caller always expects an object;
131 * @return {Token|null}
132 * next token (with white space)
136 next : function(/**Number*/howMany) {
137 if (typeof howMany == "undefined") howMany = 1;
138 if (howMany < 1) return null;
141 for (var i = 1; i <= howMany; i++) {
142 if (this.cursor+i >= this.tokens.length) {
145 got.push(this.tokens[this.cursor+i]);
147 this.cursor += howMany;
154 // what about comments after 'function'...
156 nextTok : function() {
157 return this.nextNonSpace();
159 nextNonSpace : function ()
167 if (tok.is('WHIT') || tok.is('COMM')) {
174 * @type JSDOC.Token[]
175 * @param start {String} token name or data (eg. '{'
176 * @param stop {String} (Optional) token name or data (eg. '}'
178 balance : function(/**String*/start, /**String*/stop) {
181 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
183 if (!stop) stop = Lang.matching(start);
188 //Seed.print("START:" + start);
189 //Seed.print("STOP:" + stop);
190 while ((token = this.look())) {
191 if (token.is(start)) {
192 // Seed.print("balance: START : " + depth + " " + token.data);
201 if (token.is(stop)) {
203 // Seed.print("balance: STOP: " + depth + " " + token.data);
204 if (depth < 1) return got;
206 if (!this.next()) break;
211 getMatchingToken : function(/**String*/start, /**String*/stop) {
213 var cursor = this.cursor;
216 start = Lang.matching(stop);
219 if (!stop) stop = Lang.matching(start);
221 while ((token = this.tokens[cursor])) {
222 if (token.is(start)) {
226 if (token.is(stop) && cursor) {
228 if (depth == 0) return this.tokens[cursor];
235 insertAhead : function(/**JSDOC.Token*/token) {
236 this.tokens.splice(this.cursor+1, 0, token);
239 remaining : function() {
242 var tok = this.look(1,true);
243 if (!tok || !tok.is || tok.is('VOID')) {
246 ret.push(this.next(1));
251 arrayToString : function(ar) {
252 console.log(typeof(ar));
254 ar.forEach(function(e) {
259 dump: function(start, end)
261 start = Math.max(start || 0, 0);
262 end = Math.min(end || this.tokens.length, this.tokens.length);
264 for (var i =start;i < end; i++) {
266 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;