Add new stream highlighting function

This commit is contained in:
Florian Schroedl
2022-06-18 11:27:45 +02:00
parent e8e464ef00
commit 748a9763f2

View File

@@ -353,6 +353,40 @@ func highlightStreamPosition(stream: string, position: int): string =
return a & LEFT_HIGHLIGHT_CHAR & ch & RIGHT_HIGHLIGHT_CHAR & b
func highlightStreamPosition2(stream: string, position: int): string =
if position < 0: return stream
if position > stream.len - 1: return stream
let startIndex = stream.rfind("\n", 0, position)
let lineStartIndex =
case startIndex:
of -1: 0
else: startIndex
let endIndex = stream.find("\n", position)
let lineEndIndex =
case endIndex:
of -1: stream.len - 1
else: endIndex
let spaceChars = " ".repeat(position - lineStartIndex)
let lineChars = "_".repeat((lineEndIndex - position) + 10)
let ch = stream[position]
let escapedChar =
case ch:
of '\n': "\\n"
of ' ': "\\s"
else: $ch
let insertMessageAtIndex =
case ch:
# Print indicator for newlines on the previous line, which looks better for the reader
of '\n': (lineEndIndex - 1).max(0)
else: lineEndIndex
stream.dup(insert(&"\n{spaceChars}^{lineChars} Char at \"{escapedChar}\"\n", insertMessageAtIndex))
proc `$`*(x: ParserState): string =
&"""ParserState(
stream: "{x.stream.highlightStreamPosition(x.position)}",