From f017a57d867c0a741f58953e472502644afff7ae Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Wed, 29 Mar 2017 17:14:50 +0200 Subject: [PATCH] Made NimYAML compile for JS --- yaml/parser.nim | 31 ++++++++++++----------- yaml/private/lex.nim | 60 +++++++++++++++++++++++++------------------- yaml/tojson.nim | 35 +++++++++++++------------- 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/yaml/parser.nim b/yaml/parser.nim index 0604341..8c61773 100644 --- a/yaml/parser.nim +++ b/yaml/parser.nim @@ -1065,21 +1065,22 @@ proc init(c: ParserContext, p: YamlParser) {.raises: [YamlParserError].} = c.explicitFlowKey = false c.advance() -proc parse*(p: YamlParser, s: Stream): YamlStream - {.raises: [YamlParserError].} = - ## Parse the given stream as YAML character stream. - let c = new(ParserContext) - try: c.lex = newYamlLexer(s) - except: - let e = newException(YamlParserError, - "Error while opening stream: " & getCurrentExceptionMsg()) - e.parent = getCurrentException() - e.line = 1 - e.column = 1 - e.lineContent = "" - raise e - c.init(p) - result = c +when not defined(JS): + proc parse*(p: YamlParser, s: Stream): YamlStream + {.raises: [YamlParserError].} = + ## Parse the given stream as YAML character stream. + let c = new(ParserContext) + try: c.lex = newYamlLexer(s) + except: + let e = newException(YamlParserError, + "Error while opening stream: " & getCurrentExceptionMsg()) + e.parent = getCurrentException() + e.line = 1 + e.column = 1 + e.lineContent = "" + raise e + c.init(p) + result = c proc parse*(p: YamlParser, str: string): YamlStream {.raises: [YamlParserError].} = diff --git a/yaml/private/lex.nim b/yaml/private/lex.nim index d1c1431..848d82e 100644 --- a/yaml/private/lex.nim +++ b/yaml/private/lex.nim @@ -42,7 +42,8 @@ type newlines*: int # internals - source: pointer + when defined(JS): sSource: StringSource + else: source: pointer inFlow: bool literalEndIndent: int nextState, lineStartState, inlineState, insideLineImpl, insideDocImpl, @@ -1140,34 +1141,41 @@ proc init*[T](lex: YamlLexer) = lex.tokenLineGetter = tokenLine[T] lex.searchColonImpl = searchColon[T] -proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} = - let blSource = new(BaseLexer) - try: blSource[].open(source) - except: - 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) +when not defined(JS): + proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} = + let blSource = new(BaseLexer) + try: blSource[].open(source) + except: + 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: [].} = - 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) + when defined(JS): + let sSource = StringSource(pos: startAt, lineStart: startAt, line: 1, + src: source) + result = YamlLexer(buf: "", sSource: sSource, + inFlow: false, c: sSource.src[startAt], newlines: 0, folded: true) + else: + 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) = diff --git a/yaml/tojson.nim b/yaml/tojson.nim index 8a4617e..d53cdbd 100644 --- a/yaml/tojson.nim +++ b/yaml/tojson.nim @@ -176,23 +176,24 @@ proc constructJson*(s: var YamlStream): seq[JsonNode] else: internalError("Unexpected node kind: " & $levels[levels.high].node.kind) -proc loadToJson*(s: Stream): seq[JsonNode] - {.raises: [YamlParserError, YamlConstructionError, IOError].} = - ## Uses `YamlParser <#YamlParser>`_ and - ## `constructJson <#constructJson>`_ to construct an in-memory JSON tree - ## from a YAML character stream. - var - parser = newYamlParser(initCoreTagLibrary()) - events = parser.parse(s) - try: - return constructJson(events) - except YamlStreamError: - let e = getCurrentException() - if e.parent of IOError: - raise (ref IOError)(e.parent) - elif e.parent of YamlParserError: - raise (ref YamlParserError)(e.parent) - else: internalError("Unexpected exception: " & e.parent.repr) +when not defined(JS): + proc loadToJson*(s: Stream): seq[JsonNode] + {.raises: [YamlParserError, YamlConstructionError, IOError].} = + ## Uses `YamlParser <#YamlParser>`_ and + ## `constructJson <#constructJson>`_ to construct an in-memory JSON tree + ## from a YAML character stream. + var + parser = newYamlParser(initCoreTagLibrary()) + events = parser.parse(s) + try: + return constructJson(events) + except YamlStreamError: + let e = getCurrentException() + if e.parent of IOError: + raise (ref IOError)(e.parent) + elif e.parent of YamlParserError: + raise (ref YamlParserError)(e.parent) + else: internalError("Unexpected exception: " & e.parent.repr) proc loadToJson*(str: string): seq[JsonNode] {.raises: [YamlParserError, YamlConstructionError].} =