From c20c246e4c195c553b63369c5102522a5a25cf8c Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Sun, 11 Aug 2024 21:11:34 -0400 Subject: [PATCH] [lib/util] Add debugging utilities to TextReader --- lib/util/TextReader.v3 | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/util/TextReader.v3 b/lib/util/TextReader.v3 index 7ec33d3e4..a19d3bad1 100644 --- a/lib/util/TextReader.v3 +++ b/lib/util/TextReader.v3 @@ -242,6 +242,38 @@ class TextReader { } } private def tabColumn() { - column = 1 + (((column + 8) >>> 3) << 3); + column = nextTabStop(column); + } + def nextTabStop(column: int) -> int { + return 1 + (((column + 8) >>> 3) << 3); + } + def renderCurrentLineWithCaret(buf: StringBuilder, pos: int) -> StringBuilder { + var start = pos - 1; + while (start > 0) { + if (data[start] == '\n') { + start++; + break; + } + start--; + } + var end = start; + while (end < limit) { + if (data[end] == '\n') break; + end++; + } + buf.putr(data[start ... end]); + var column = 1; + for (i = start; i < end; i++) { + if (i == pos) break; + var ch = data[i]; + if (ch == '\t') column = nextTabStop(column); + else column++; + } + if (start <= pos && pos < end) { + buf.ln(); + for (i < column - 1) buf.sp(); + buf.putc('^'); + } + return buf; } }