maybe

sometimes you don't care if you bite something or not. if it's there, you bite it. OTHERWISE, you carry on biting other things.

in this language, you can bite off "pasta", then "gang", then a number. but the "gang" is OPTIONAL. you can leave it off if you want.

TRANSLATED:



it's easy. make a term that accepts a term.

class MaybeTerm extends Term {
	constructor(term) {
		super()
		this.term = term
	}
}

give it a name. square brackets mean it's OPTIONAL.

class MaybeTerm extends Term {
	//...
	name() {
		return `[${this.term.name()}]`
	}
}

check is even EASIER than usual. it's optional so it's always true.

class MaybeTerm extends Term {
	//...
	check(text) {
		return true
	}
}

bite is easy. if we can bite, then bite. if we can't bite, then bite nothing.

class MaybeTerm extends Term {
	//...
	bite(text) {
		const bite = this.term.bite(text)
		if (bite !== null) {
			return bite
		}
		return ""
	}
}

then make your OPTIONAL term.

const maybeGangTerm = new MaybeTerm(new StringTerm("gang"))

then use it somewhere.

const listTerm = new ListTerm([
	new StringTerm("pasta"),
	maybeGangTerm,
	new NumberTerm(),
])

function translate(text) {
	return listTerm.translate(text)
}

back to the dream