diff --git a/src/parser/parser_types.nim b/src/parser/parser_types.nim index df02c9a..7b1dd09 100644 --- a/src/parser/parser_types.nim +++ b/src/parser/parser_types.nim @@ -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)}",