Made NimYAML compile for JS

This commit is contained in:
Felix Krause 2017-03-29 17:14:50 +02:00
parent 83efb88978
commit f017a57d86
3 changed files with 68 additions and 58 deletions

View File

@ -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].} =

View File

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

View File

@ -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].} =