TokenIterator.js 849 B

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