name

we can make our error messages a bit better by giving our terms a name.

this language bites off "pasta" or "pizza". it gives you a helpful error message if you don't.

TRANSLATED:



give the term class a name method. by default, a term is anonymous.

class Term {
	//...
	name() {
		return "anonymous term"
	}
}

then override it in the string term.

class StringTerm extends Term {
	//...
	name() {
		return `"${this.string}"`
	}
}

and in the "or" term too!

class OrTerm extends Term {
	//...
	name() {
		return `${this.term1.name()} or ${this.term2.name()}`
	}
}

now you can use it to make your error message better.

class OrTerm extends Term {
	//...
	error(text) {
		if (this.check(text)) {
			return null
		}
		return `FRIGHTENING error: expected ${this.name()} but found '${text}'!`
	}
}

back to the dream