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 if (i > this.tokens.length) {
122 return new Token("", "VOID", "END_OF_STREAM");
125 if (i != this.cursor && ( this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
126 i += (n < 0) ? -1 : 1;
131 return this.tokens.get(i);
134 i += (n < 0) ? -1 : 1;
136 // should never get here..
137 return new Token("", "VOID", "END_OF_STREAM");; // because null isn't an object and caller always expects an object;
142 * @return {Token|null}
143 * next token (with white space)
147 public Token? next() {
150 //if (typeof howMany == "undefined") howMany = 1;
151 // if (howMany < 1) { return null; }
153 if (this.cursor+1 >= this.tokens.size) {
157 return this.tokens.get(this.cursor);
161 public Gee.ArrayList<Token> nextM(uint howMany) {
163 //if (typeof howMany == "undefined") howMany = 1;
165 throw new JSDOC.TokenStreamError("nextM called with wrong number : %d",howMany);
167 var got = new Gee.ArrayList<Token>();
169 for (var i = 1; i <= howMany; i++) {
170 if (this.cursor+i >= this.tokens.size) {
173 got.add(this.tokens.get(this.cursor+i));
175 this.cursor += howMany;
183 // what about comments after 'function'...
185 public Token? nextTok() {
186 return this.nextNonSpace();
189 public Token? nextNonSpace ()
197 if (tok.is("WHIT") || tok.is("COMM")) {
206 * -- returns all the tokens betweeen and including stop token eg.. from {... to }
207 * @param start {String} token name or data (eg. '{'
208 * @param stop {String} (Optional) token name or data (eg. '}'
210 public Gee.ArrayList<Token> balance (string start, string stop = "") {
212 // accepts names or "{" etc..
214 start = Lang.punc(start) == null ? start : Lang.punc(start);
217 stop = Lang.matching(start);
220 throw new JSDOC.TokenStreamError("balance called with invalid start/stop : %s",start);
224 var got = new Gee.ArrayList<Token>();
226 //Seed.print("START:" + start);
227 //Seed.print("STOP:" + stop);
229 while ((token = this.look())) {
230 if (token.is(start)) {
231 // Seed.print("balance: START : " + depth + " " + token.data);
240 if (token.is(stop)) {
242 // Seed.print("balance: STOP: " + depth + " " + token.data);
251 return new Gee.ArrayList<Token>();
254 public Token getMatchingToken(string start, string stop) {
256 var cursor = this.cursor;
259 start = Lang.matching(stop);
262 if (!stop) stop = Lang.matching(start);
264 while ((token = this.tokens[cursor])) {
265 if (token.is(start)) {
269 if (token.is(stop) && cursor) {
271 if (depth == 0) return this.tokens[cursor];
278 public Gee.ArrayList<Token> insertAhead(Token token) {
279 this.tokens.splice(this.cursor+1, 0, token); // fixme...
282 public Gee.ArrayList<Token> remaining() {
283 var ret = new Gee.ArrayList<Token>();
285 var tok = this.look(1,true);
286 if (tok.is('VOID')) {
289 ret.push(this.next(1));
294 arrayToString : function(ar) {
295 console.log(typeof(ar));
297 ar.forEach(function(e) {
302 dump: function(start, end)
304 start = Math.max(start || 0, 0);
305 end = Math.min(end || this.tokens.length, this.tokens.length);
307 for (var i =start;i < end; i++) {
309 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;