In error messages, display the start position of strings instead of the end pos

This commit is contained in:
Zahary Karadjov 2019-07-21 17:12:48 +03:00
parent 943501f941
commit 0c12b0f42d
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 8 additions and 5 deletions

View File

@ -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,

View File

@ -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