let's add a "throw" method. it's for throwing an error when a term doesn't match.
/**
* @typedef {object} Term
* ...
* @property {(text: string) => never} throw
*/
set the default throw method.
Term.Default = {
//...
throw(text) {
throw new Error(`Expected ${this.name} but found '${text}'!`)
}
}
use the throw method in the default translate method.
Term.Default = {
//...
translate(text) {
const bites = this.bite(text)
if (bites.length === 0) {
return this.throw(text)
}
return bites.join("")
}
}
here it is in action.
back to the dream