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 Token? look (int n, bool considerWhitespace)
42 if (considerWhitespace == true) {
44 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
45 return new Token("", "VOID", "START_OF_STREAM");
47 return this.tokens.get(this.cursor+n);
54 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
55 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
57 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
58 if (n < 0) i--; else i++;
62 if (count == Math.abs(n)) {
63 return this.tokens[i];
69 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
73 lookFor : function (data)
76 var i = this.cursor < 0 ? 0 : this.cursor ;
79 if (i >= this.tokens.length) return -1;
80 if (this.tokens[i].data == data) {
86 // should not get here!
93 * look ahead (or back) x number of tokens (which are not comment or whitespace)
96 lookTok : function(/**Number*/n) {
97 if (typeof n == "undefined") n = 1;
112 return new Token("", "VOID", "END_OF_STREAM");
114 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
116 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
117 if (n < 0) i--; else i++;
121 if (count == Math.abs(n)) {
122 return this.tokens[i];
127 // should never get here..
128 return false; // because null isn't an object and caller always expects an object;
133 * @return {Token|null}
134 * next token (with white space)
138 next : function(/**Number*/howMany) {
139 if (typeof howMany == "undefined") howMany = 1;
140 if (howMany < 1) return null;
143 for (var i = 1; i <= howMany; i++) {
144 if (this.cursor+i >= this.tokens.length) {
147 got.push(this.tokens[this.cursor+i]);
149 this.cursor += howMany;
156 // what about comments after 'function'...
158 nextTok : function() {
159 return this.nextNonSpace();
161 nextNonSpace : function ()
169 if (tok.is('WHIT') || tok.is('COMM')) {
176 * @type JSDOC.Token[]
177 * @param start {String} token name or data (eg. '{'
178 * @param stop {String} (Optional) token name or data (eg. '}'
180 balance : function(/**String*/start, /**String*/stop) {
183 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
185 if (!stop) stop = Lang.matching(start);
190 //Seed.print("START:" + start);
191 //Seed.print("STOP:" + stop);
192 while ((token = this.look())) {
193 if (token.is(start)) {
194 // Seed.print("balance: START : " + depth + " " + token.data);
203 if (token.is(stop)) {
205 // Seed.print("balance: STOP: " + depth + " " + token.data);
206 if (depth < 1) return got;
208 if (!this.next()) break;
213 getMatchingToken : function(/**String*/start, /**String*/stop) {
215 var cursor = this.cursor;
218 start = Lang.matching(stop);
221 if (!stop) stop = Lang.matching(start);
223 while ((token = this.tokens[cursor])) {
224 if (token.is(start)) {
228 if (token.is(stop) && cursor) {
230 if (depth == 0) return this.tokens[cursor];
237 insertAhead : function(/**JSDOC.Token*/token) {
238 this.tokens.splice(this.cursor+1, 0, token);
241 remaining : function() {
244 var tok = this.look(1,true);
245 if (!tok || !tok.is || tok.is('VOID')) {
248 ret.push(this.next(1));
253 arrayToString : function(ar) {
254 console.log(typeof(ar));
256 ar.forEach(function(e) {
261 dump: function(start, end)
263 start = Math.max(start || 0, 0);
264 end = Math.min(end || this.tokens.length, this.tokens.length);
266 for (var i =start;i < end; i++) {
268 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;