From 0c12b0f42d94948c7d504f4a8470032065fbe0f4 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Sun, 21 Jul 2019 17:12:48 +0300 Subject: [PATCH] In error messages, display the start position of strings instead of the end pos --- json_serialization/lexer.nim | 5 ++++- json_serialization/reader.nim | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index 25f90ee..3a79e69 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -75,10 +75,13 @@ proc isDigit(c: char): bool {.inline.} = proc col*(lexer: JsonLexer): int = lexer.stream[].pos - lexer.lineStartPos +proc tokenStartCol*(lexer: JsonLexer): int = + 1 + lexer.tokenStart - lexer.lineStartPos + proc init*(T: type JsonLexer, stream: ref AsciiStream, mode = defaultJsonMode): T = T(stream: stream, mode: mode, - line: 0, + line: 1, lineStartPos: 0, tokenStart: -1, tok: tkError, diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index 10ed698..3a2c9e1 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -52,18 +52,18 @@ method formatMsg*(err: ref GenericJsonReaderError, filename: string): string = template init*(T: type JsonReader, stream: ByteStreamVar, mode = defaultJsonMode): auto = init JsonReader, AsciiStreamVar(stream), mode -proc assignLineNumber(ex: ref JsonReaderError, r: JsonReader) = +proc assignLineNumber*(ex: ref JsonReaderError, r: JsonReader) = ex.line = r.lexer.line - ex.col = r.lexer.col + ex.col = r.lexer.tokenStartCol -proc raiseUnexpectedToken(r: JsonReader, expected: ExpectedTokenCategory) = +proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory) = var ex = new UnexpectedToken ex.assignLineNumber(r) ex.encountedToken = r.lexer.tok ex.expectedToken = expected raise ex -proc raiseUnexpectedField(r: JsonReader, fieldName, deserializedType: cstring) = +proc raiseUnexpectedField*(r: JsonReader, fieldName, deserializedType: cstring) = var ex = new UnexpectedField ex.assignLineNumber(r) ex.encounteredField = fieldName