eat

you often want to get the bite and tail at the same time. we do this with the eat method.

this language bites off "pasta" and emits both the bite and the tail.

TRANSLATED:



add the eat method to the term class. by default, it can use the bite and tail methods.

class Term {
	//...
	eat(text) {
		return [this.bite(text), this.tail(text)]
	}
}

but you can also OVERRIDE it to avoid doing REPEATED work.

class StringTerm extends Term {
	//...
	eat(text) {
		if (!this.check(text)) {
			return [null, null]
		}
		return [text.slice(0, this.string.length), text.slice(this.string.length)]
	}
}

either way, it works.

const pastaTerm = new StringTerm("pasta")

function translate(text) {
	return pastaTerm.eat(text)
}

back to the dream