From f4a715c67f1e7d9ad391cd506923dff20d77304d Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Fri, 30 Sep 2016 20:12:17 +0200 Subject: [PATCH] More improvements to lexer --- private/lex.nim | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/private/lex.nim b/private/lex.nim index cb5c23e..048e7e1 100644 --- a/private/lex.nim +++ b/private/lex.nim @@ -1127,35 +1127,34 @@ proc init*[T](lex: YamlLexer) = lex.tokenLineGetter = tokenLine[T] lex.searchColonImpl = searchColon[T] -proc newYamlLexer*(source: Stream): YamlLexer {.raises: [].} = - try: - let blSource = new(BaseLexer) - blSource[].open(source) - GC_ref(blSource) - new(result, proc(x: ref YamlLexerObj) {.nimcall.} = - GC_unref(cast[ref BaseLexer](x.source)) - ) - result[] = YamlLexerObj(source: cast[pointer](blSource), inFlow: false, - buf: "", c: blSource[].buf[blSource[].bufpos], newlines: 0, - folded: true) +proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} = + let blSource = new(BaseLexer) + try: blSource[].open(source) except: - discard # TODO + var e = newException(YamlLexerError, + "Could not open stream for reading:\n" & getCurrentExceptionMsg()) + e.parent = getCurrentException() + raise e + GC_ref(blSource) + new(result, proc(x: ref YamlLexerObj) {.nimcall.} = + GC_unref(cast[ref BaseLexer](x.source)) + ) + result[] = YamlLexerObj(source: cast[pointer](blSource), inFlow: false, + buf: "", c: blSource[].buf[blSource[].bufpos], newlines: 0, + folded: true) init[BaseLexer](result) proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer - {.raises: [].}= - try: - let sSource = new(StringSource) - sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1, - src: source) - GC_ref(sSource) - new(result, proc(x: ref YamlLexerObj) {.nimcall.} = - GC_unref(cast[ref StringSource](x.source)) - ) - result[] = YamlLexerObj(buf: "", source: cast[pointer](sSource), inFlow: false, - c: sSource.src[startAt], newlines: 0, folded: true) - except: - discard # TODO + {.raises: [].} = + let sSource = new(StringSource) + sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1, + src: source) + GC_ref(sSource) + new(result, proc(x: ref YamlLexerObj) {.nimcall.} = + GC_unref(cast[ref StringSource](x.source)) + ) + result[] = YamlLexerObj(buf: "", source: cast[pointer](sSource), + inFlow: false, c: sSource.src[startAt], newlines: 0, folded: true) init[StringSource](result) proc next*(lex: YamlLexer) =