15 public errordomain TokenStreamError {
18 public class TokenStream : Object
21 protected Gee.ArrayList<Token> tokens;
22 public int cursor; // where are we in the stream.
25 public TokenStream(Gee.ArrayList<Token> tokens) {
34 public void rewind() {
41 public Token? look (int n, bool considerWhitespace)
45 if (considerWhitespace == true) {
47 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.size -1)) {
48 return new Token("", "VOID", "START_OF_STREAM");
50 return this.tokens.get(this.cursor+n);
59 return new Token("", "VOID", "START_OF_STREAM");
61 if (i > this.tokens.size) {
62 return new Token("", "VOID", "END_OF_STREAM");
65 if (i != this.cursor && this.tokens.get(i).is("WHIT")) {
66 i += (n < 0) ? -1 : 1;
71 return this.tokens.get(i);
74 i += (n < 0) ? -1 : 1;
77 // return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
81 public int lookFor (string data)
84 var i = this.cursor < 0 ? 0 : this.cursor ;
87 if (i >= this.tokens.size) {
90 if (this.tokens.get(i).data == data) {
96 // should not get here!
103 * look ahead (or back) x number of tokens (which are not comment or whitespace)
106 public Token lookTok (int n) {
122 return new Token("", "VOID", "END_OF_STREAM");
124 if (i > this.tokens.size) {
125 return new Token("", "VOID", "END_OF_STREAM");
128 if (i != this.cursor && ( this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
129 i += (n < 0) ? -1 : 1;
134 return this.tokens.get(i);
137 i += (n < 0) ? -1 : 1;
139 // should never get here..
140 // return new Token("", "VOID", "END_OF_STREAM");; // because null isn't an object and caller always expects an object;
145 * @return {Token|null}
146 * next token (with white space)
150 public Token? next() {
153 //if (typeof howMany == "undefined") howMany = 1;
154 // if (howMany < 1) { return null; }
156 if (this.cursor+1 >= this.tokens.size) {
160 return this.tokens.get(this.cursor);
164 public Gee.ArrayList<Token>? nextM(int howMany) throws TokenStreamError {
166 //if (typeof howMany == "undefined") howMany = 1;
168 throw new TokenStreamError.ArgumentError("nextM called with wrong number : %d", howMany);
170 var got = new Gee.ArrayList<Token>();
172 for (var i = 1; i <= howMany; i++) {
173 if (this.cursor+i >= this.tokens.size) {
176 got.add(this.tokens.get(this.cursor+i));
178 this.cursor += howMany;
186 // what about comments after 'function'...
188 public Token? nextTok() {
189 return this.nextNonSpace();
192 public Token? nextNonSpace ()
196 var tok = this.next();
200 if (tok.is("WHIT") || tok.is("COMM")) {
209 * -- returns all the tokens betweeen and including stop token eg.. from {... to }
210 * @param start {String} token name or data (eg. '{'
211 * @param stop {String} (Optional) token name or data (eg. '}'
213 public Gee.ArrayList<Token> balance (string start, string stop = "") throws TokenStreamError
216 // accepts names or "{" etc..
218 start = Lang.punc(start) == null ? start : Lang.punc(start);
221 var newstop = Lang.matching(start);
225 throw new TokenStreamError.ArgumentError("balance called with invalid start/stop : %s",start);
229 var got = new Gee.ArrayList<Token>();
231 //Seed.print("START:" + start);
232 //Seed.print("STOP:" + stop);
235 while (null != (token = this.look(1,false))) {
236 if (token.is(start)) {
237 // Seed.print("balance: START : " + depth + " " + token.data);
246 if (token.is(stop)) {
248 // Seed.print("balance: STOP: " + depth + " " + token.data);
253 if (null == this.next()) {
257 return new Gee.ArrayList<Token>();
260 public Token? getMatchingToken(string start, string stop)
263 var cursor = this.cursor;
265 if (start.length < 1) {
266 var ns = Lang.matching(stop);
270 if (stop.length < 1) {
271 var ns = Lang.matching(start);
276 while (null != (token = this.tokens[cursor])) {
277 if (token.is(start)) {
281 if (token.is(stop) && cursor != 0) {
284 return this.tokens[cursor];
292 public Gee.ArrayList<Token> insertAhead(Token token)
294 this.tokens.splice(this.cursor+1, 0, token); // fixme...
298 public Gee.ArrayList<Token> remaining() {
299 var ret = new Gee.ArrayList<Token>();
301 var tok = this.look(1,true);
302 if (tok.is("VOID")) {
305 var nt = this.next();
313 public void printRange(int start, int end) {
315 for(var i = start; i < end +1; i++) {
316 print(this.tokens.get(i).asString());
321 arrayToString : function(ar) {
322 console.log(typeof(ar));
324 ar.forEach(function(e) {
330 public void dump(int start, int end)
332 start = int.max(start , 0);
333 end = int.min(end, this.tokens.size);
335 for (var i =start;i < end; i++) {
337 outs += (this.tokens[i].outData == "") ? this.tokens[i].data : this.tokens[i].outData;