From 14eb1ea1e2aa92831794e599121663f4113361ed Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 16 Apr 2021 13:55:51 -0300 Subject: show line and column position in parse errors --- upd8.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/upd8.js b/upd8.js index d2983e7..17b3f28 100755 --- a/upd8.js +++ b/upd8.js @@ -1210,8 +1210,29 @@ const replacerSpec = { const { i, data: { message } } = errorNode; - // TODO: Visual line/surrounding characters presentation! - throw new SyntaxError(`Parse error (at pos ${i}): ${message}`); + let lineStart = input.slice(0, i).lastIndexOf('\n'); + if (lineStart >= 0) { + lineStart += 1; + } else { + lineStart = 0; + } + + let lineEnd = input.slice(i).indexOf('\n'); + if (lineEnd >= 0) { + lineEnd += i; + } else { + lineEnd = input.length; + } + + const line = input.slice(lineStart, lineEnd); + + const cursor = i - lineStart; + + throw new SyntaxError(fixWS` + Parse error (at pos ${i}): ${message} + ${line} + ${'-'.repeat(cursor) + '^'} + `); } }; } -- cgit 1.3.0-6-gf8a5