UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

59 lines (57 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors = require("./../errors"); class AdvancedIterator { constructor(iterator) { this.iterator = iterator; this.buffer = Array.from(Array(3)); // previous, current, next this.bufferIndex = 0; this.isDone = false; this.nextCount = 0; this.advance(); } get done() { return this.isDone; } get current() { if (this.nextCount === 0) throw new errors.InvalidOperationError("Cannot get the current when the iterator has not been advanced."); return this.buffer[this.bufferIndex]; } get previous() { if (this.nextCount <= 1) throw new errors.InvalidOperationError("Cannot get the previous when the iterator has not advanced enough."); return this.buffer[(this.bufferIndex + this.buffer.length - 1) % this.buffer.length]; } get peek() { if (this.isDone) throw new errors.InvalidOperationError("Cannot peek at the end of the iterator."); return this.buffer[(this.bufferIndex + 1) % this.buffer.length]; } next() { if (this.done) throw new errors.InvalidOperationError("Cannot get the next when at the end of the iterator."); const next = this.buffer[this.getNextBufferIndex()]; this.advance(); this.nextCount++; return next; } *rest() { while (!this.done) yield this.next(); } advance() { const next = this.iterator.next(); this.bufferIndex = this.getNextBufferIndex(); if (next.done) { this.isDone = true; return; } this.buffer[this.getNextBufferIndex()] = next.value; } getNextBufferIndex() { return (this.bufferIndex + 1) % this.buffer.length; } } exports.AdvancedIterator = AdvancedIterator; //# sourceMappingURL=AdvancedIterator.js.map