we're going to add a new method to the list term. it's called "digest". it collects up bites into an array.
this language bites of "pasta" then "gang" then a number and emits them as an array. bear in mind, you need to type it all out. otherwise, it returns null. it's all or nothing.
TRANSLATED:
to get the digest, eat your way through. add each bite to an array. if any bite fails, give up.
class ListTerm extends Term {
//...
digest(text) {
const bites = []
for (const term of this.terms) {
const [nextBite, nextTail] = term.eat(text)
if (nextBite === null) {
return null
}
bites.push(nextBite)
text = nextTail
}
return bites
}
}
then use it.
const listTerm = new ListTerm([
new StringTerm("pasta"),
new StringTerm("gang"),
new NumberTerm(),
])
function translate(text) {
return listTerm.digest(text)
}
back to the dream