diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index 53ae219..f8cd957 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -41,7 +41,7 @@ type errNonPortableInt = "number is outside the range of portable values" JsonLexer* = object - stream: AsciiInputStream + stream: InputStream mode*: JsonMode line*: int @@ -61,6 +61,12 @@ const 1e20, 1e21, 1e22] # TODO: this table should be much larger # The largest JSON number value is 1E308 +template peek(s: InputStream): char = + char input_stream.peek(s) + +template read(s: InputStream): char = + char input_stream.read(s) + proc hexCharValue(c: char): int {.inline.} = case c of '0'..'9': ord(c) - ord('0') @@ -77,7 +83,7 @@ proc col*(lexer: JsonLexer): int = proc tokenStartCol*(lexer: JsonLexer): int = 1 + lexer.tokenStart - lexer.lineStartPos -proc init*(T: type JsonLexer, stream: AsciiInputStream, mode = defaultJsonMode): T = +proc init*(T: type JsonLexer, stream: InputStream, mode = defaultJsonMode): T = T(stream: stream, mode: mode, line: 1, @@ -89,9 +95,6 @@ proc init*(T: type JsonLexer, stream: AsciiInputStream, mode = defaultJsonMode): floatVal: 0'f, strVal: "") -proc init*(T: type JsonLexer, stream: InputStream, mode = defaultJsonMode): T = - init(JsonLexer, AsciiInputStream(stream), mode) - template error(error: JsonErrorKind) {.dirty.} = lexer.err = error lexer.tok = tkError diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index 4e804c4..0c51086 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -61,9 +61,6 @@ method formatMsg*(err: ref GenericJsonReaderError, filename: string): string = method formatMsg*(err: ref IntOverflowError, filename: string): string = fmt"{filename}({err.line}, {err.col}) The value '{err.valueStr}' is outside of the allowed range" -template init*(T: type JsonReader, stream: InputStream, mode = defaultJsonMode): auto = - init JsonReader, AsciiInputStream(stream), mode - proc assignLineNumber*(ex: ref JsonReaderError, r: JsonReader) = ex.line = r.lexer.line ex.col = r.lexer.tokenStartCol @@ -100,7 +97,7 @@ proc handleReadException*(r: JsonReader, ex.innerException = err raise ex -proc init*(T: type JsonReader, stream: AsciiInputStream, mode = defaultJsonMode): T = +proc init*(T: type JsonReader, stream: InputStream, mode = defaultJsonMode): T = result.lexer = JsonLexer.init(stream, mode) result.lexer.next()