expression-evaluation
Version:
Expression Evaluation
39 lines (38 loc) • 1.12 kB
JavaScript
export class ParserFrame {
_expr;
_start;
_end;
constructor(_expr, _start = 0, _end = 0) {
this._expr = _expr;
this._start = _start;
this._end = _end;
}
get expr() {
return this._expr;
}
get start() {
return this._start;
}
get end() {
return this._end;
}
starts(start) {
this._start = start;
return this;
}
ends(end) {
this._end = end;
return this;
}
frame(token) {
return token
? new ParserFrame(this._expr, this._expr.indexOf(token), this._expr.indexOf(token) + token.length)
: new ParserFrame(this._expr, this._start, this._end);
}
throwError(message) {
const offset = this._start < 32 ? 0 : this._start - 32;
const length = this._end < this._start ? 0 : this._end - this._start - 1;
throw new Error(`error: ${message} at position ${this._start}:\n${this._expr.substring(offset, offset + 60)}\n` +
`${' '.repeat(this._expr.substring(offset, this._start).length)}^${'\''.repeat(length)}`);
}
}