Sujeet Kr. Jaiswal

	class CorpusManager {
		private textNodeManger: TextNodeManger;
		private corpusArr: Corpus[];
		private static removePattern: RegExp = new RegExp("^\\^#\\d+#\\^$");
		private static waitPattern: RegExp = new RegExp("^\\^@\\d+@\\^$");

		private currCorpus: Corpus;

		constructor(wholeCorpus: string, element: HTMLElement) {
			this.textNodeManger = new TextNodeManger(element)
			this.corpusArr = this.stringToCorpusArr(wholeCorpus).reverse();
		}
		private stringToCorpusArr(wholeCorpus: string): Corpus[] {
			return wholeCorpus.split(/(\^#\d+#\^|\^@\d+@\^)/g).map((corpus) => this.stringToCorpus(corpus));
		}
		private stringToCorpus(corpusStr: string): Corpus {
			if (CorpusManager.removePattern.test(corpusStr)) {
				return new RemoveCorpus(this.textNodeManger, parseInt(corpusStr.slice(2, -2)) || 0)
			} else if (CorpusManager.waitPattern.test(corpusStr)) {
				return new WaitCorpus(parseInt(corpusStr.slice(2, -2)) || 0);
			} else {
				return new AddCorpus(this.textNodeManger, corpusStr);
			}
		}

		private hasNext(): boolean {
			while ((this.currCorpus == null || this.currCorpus.hasNext() == false) && this.corpusArr.length > 0) {
				this.currCorpus = this.corpusArr.pop();
			}
			return this.currCorpus != null && (this.currCorpus.hasNext() || this.corpusArr.length > 0);
		}
		public doNext(): boolean {
			if (this.hasNext()) {
				this.currCorpus.doNext();
				return this.hasNext();
			} else {
				return false;
			}
		}
	}