UNPKG

849 BJavaScriptView Raw
1var TokenIterator = module.exports = function(tokens, startIndex) {
2 this._tokens = tokens;
3 this._startIndex = startIndex || 0;
4};
5
6TokenIterator.prototype.head = function() {
7 return this._tokens[this._startIndex];
8};
9
10TokenIterator.prototype.tail = function(startIndex) {
11 return new TokenIterator(this._tokens, this._startIndex + 1);
12};
13
14TokenIterator.prototype.toArray = function() {
15 return this._tokens.slice(this._startIndex);
16};
17
18TokenIterator.prototype.end = function() {
19 return this._tokens[this._tokens.length - 1];
20};
21
22// TODO: doesn't need to be a method, can be a separate function,
23// which simplifies implementation of the TokenIterator interface
24TokenIterator.prototype.to = function(end) {
25 var start = this.head().source;
26 var endToken = end.head() || end.end();
27 return start.to(endToken.source);
28};