let's make a default term that we can re-use when making other terms.
by default, terms bite off nothing.
Term.Default = {
bite() {
return [""]
}
}
by default, terms emit their bites.
Term.Default = {
//...
translate(text) {
const bites = this.bite(text)
if (bites.length === 0) {
throw new Error(`Expected something but found '${text}'!`)
}
return bites.join("")
}
}
then we can use the default term to make our string term shorter.
Term.string = (string) => ({
...Term.Default,
bite(text) {
return text.startsWith(string) ? [string] : []
}
})
here it is in action.
back to the dream