Skip to content

Commit

Permalink
Print line/column in parse excceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Sep 16, 2023
1 parent 1e0b056 commit bfbf9ed
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion parseRFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ class Parser {
arrow += '^';
for (; i < to; ++i)
ctx += this.getChar(i);
throw new Error('[position ' + pos + '] ' + s + '\n' + ctx.replace(/\s/g, ' ') + '\n' + arrow);
// calculate line/column
let line = 1;
let lastLF = 0;
for (let i = 0; i < pos; ++i)
if (this.enc.charAt(i) == '\n') {
++line;
lastLF = i;
}
let column = pos - lastLF;
throw new Error('[position ' + pos + ', line ' + line + ':' + column + '] ' + s + '\n' + ctx.replace(/\s/g, ' ') + '\n' + arrow);
}
peek() {
return (typeof this.enc == 'string') ? this.enc.charCodeAt(this.pos) : this.enc[this.pos];
Expand Down

0 comments on commit bfbf9ed

Please sign in to comment.