« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xupd8.js25
1 files 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) + '^'}
+            `);
         }
     };
 }