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);
56 return new Token("", "VOID", "START_OF_STREAM");
58 if (i > this.tokens.length) {
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 lookFor : function (data)
81 var i = this.cursor < 0 ? 0 : this.cursor ;
84 if (i >= this.tokens.length) return -1;
85 if (this.tokens[i].data == data) {
91 // should not get here!
98 * look ahead (or back) x number of tokens (which are not comment or whitespace)
101 lookTok : function(/**Number*/n) {
102 if (typeof n == "undefined") n = 1;
117 return new Token("", "VOID", "END_OF_STREAM");
119 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
121 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
122 if (n < 0) i--; else i++;
126 if (count == Math.abs(n)) {
127 return this.tokens[i];
132 // should never get here..
133 return false; // because null isn't an object and caller always expects an object;
138 * @return {Token|null}
139 * next token (with white space)
143 next : function(/**Number*/howMany) {
144 if (typeof howMany == "undefined") howMany = 1;
145 if (howMany < 1) return null;
148 for (var i = 1; i <= howMany; i++) {
149 if (this.cursor+i >= this.tokens.length) {
152 got.push(this.tokens[this.cursor+i]);
154 this.cursor += howMany;
161 // what about comments after 'function'...
163 nextTok : function() {
164 return this.nextNonSpace();
166 nextNonSpace : function ()
174 if (tok.is('WHIT') || tok.is('COMM')) {
181 * @type JSDOC.Token[]
182 * @param start {String} token name or data (eg. '{'
183 * @param stop {String} (Optional) token name or data (eg. '}'
185 balance : function(/**String*/start, /**String*/stop) {
188 start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
190 if (!stop) stop = Lang.matching(start);
195 //Seed.print("START:" + start);
196 //Seed.print("STOP:" + stop);
197 while ((token = this.look())) {
198 if (token.is(start)) {
199 // Seed.print("balance: START : " + depth + " " + token.data);
208 if (token.is(stop)) {
210 // Seed.print("balance: STOP: " + depth + " " + token.data);
211 if (depth < 1) return got;
213 if (!this.next()) break;
218 getMatchingToken : function(/**String*/start, /**String*/stop) {
220 var cursor = this.cursor;
223 start = Lang.matching(stop);
226 if (!stop) stop = Lang.matching(start);
228 while ((token = this.tokens[cursor])) {
229 if (token.is(start)) {
233 if (token.is(stop) && cursor) {
235 if (depth == 0) return this.tokens[cursor];
242 insertAhead : function(/**JSDOC.Token*/token) {
243 this.tokens.splice(this.cursor+1, 0, token);
246 remaining : function() {
249 var tok = this.look(1,true);
250 if (!tok || !tok.is || tok.is('VOID')) {
253 ret.push(this.next(1));
258 arrayToString : function(ar) {
259 console.log(typeof(ar));
261 ar.forEach(function(e) {
266 dump: function(start, end)
268 start = Math.max(start || 0, 0);
269 end = Math.min(end || this.tokens.length, this.tokens.length);
271 for (var i =start;i < end; i++) {
273 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;