mirror of https://github.com/status-im/NimYAML.git
Made NimYAML compile for JS
This commit is contained in:
parent
83efb88978
commit
f017a57d86
|
@ -1065,21 +1065,22 @@ proc init(c: ParserContext, p: YamlParser) {.raises: [YamlParserError].} =
|
||||||
c.explicitFlowKey = false
|
c.explicitFlowKey = false
|
||||||
c.advance()
|
c.advance()
|
||||||
|
|
||||||
proc parse*(p: YamlParser, s: Stream): YamlStream
|
when not defined(JS):
|
||||||
{.raises: [YamlParserError].} =
|
proc parse*(p: YamlParser, s: Stream): YamlStream
|
||||||
## Parse the given stream as YAML character stream.
|
{.raises: [YamlParserError].} =
|
||||||
let c = new(ParserContext)
|
## Parse the given stream as YAML character stream.
|
||||||
try: c.lex = newYamlLexer(s)
|
let c = new(ParserContext)
|
||||||
except:
|
try: c.lex = newYamlLexer(s)
|
||||||
let e = newException(YamlParserError,
|
except:
|
||||||
"Error while opening stream: " & getCurrentExceptionMsg())
|
let e = newException(YamlParserError,
|
||||||
e.parent = getCurrentException()
|
"Error while opening stream: " & getCurrentExceptionMsg())
|
||||||
e.line = 1
|
e.parent = getCurrentException()
|
||||||
e.column = 1
|
e.line = 1
|
||||||
e.lineContent = ""
|
e.column = 1
|
||||||
raise e
|
e.lineContent = ""
|
||||||
c.init(p)
|
raise e
|
||||||
result = c
|
c.init(p)
|
||||||
|
result = c
|
||||||
|
|
||||||
proc parse*(p: YamlParser, str: string): YamlStream
|
proc parse*(p: YamlParser, str: string): YamlStream
|
||||||
{.raises: [YamlParserError].} =
|
{.raises: [YamlParserError].} =
|
||||||
|
|
|
@ -42,7 +42,8 @@ type
|
||||||
newlines*: int
|
newlines*: int
|
||||||
|
|
||||||
# internals
|
# internals
|
||||||
source: pointer
|
when defined(JS): sSource: StringSource
|
||||||
|
else: source: pointer
|
||||||
inFlow: bool
|
inFlow: bool
|
||||||
literalEndIndent: int
|
literalEndIndent: int
|
||||||
nextState, lineStartState, inlineState, insideLineImpl, insideDocImpl,
|
nextState, lineStartState, inlineState, insideLineImpl, insideDocImpl,
|
||||||
|
@ -1140,34 +1141,41 @@ proc init*[T](lex: YamlLexer) =
|
||||||
lex.tokenLineGetter = tokenLine[T]
|
lex.tokenLineGetter = tokenLine[T]
|
||||||
lex.searchColonImpl = searchColon[T]
|
lex.searchColonImpl = searchColon[T]
|
||||||
|
|
||||||
proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} =
|
when not defined(JS):
|
||||||
let blSource = new(BaseLexer)
|
proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} =
|
||||||
try: blSource[].open(source)
|
let blSource = new(BaseLexer)
|
||||||
except:
|
try: blSource[].open(source)
|
||||||
var e = newException(YamlLexerError,
|
except:
|
||||||
"Could not open stream for reading:\n" & getCurrentExceptionMsg())
|
var e = newException(YamlLexerError,
|
||||||
e.parent = getCurrentException()
|
"Could not open stream for reading:\n" & getCurrentExceptionMsg())
|
||||||
raise e
|
e.parent = getCurrentException()
|
||||||
GC_ref(blSource)
|
raise e
|
||||||
new(result, proc(x: ref YamlLexerObj) {.nimcall.} =
|
GC_ref(blSource)
|
||||||
GC_unref(cast[ref BaseLexer](x.source))
|
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,
|
result[] = YamlLexerObj(source: cast[pointer](blSource), inFlow: false,
|
||||||
folded: true)
|
buf: "", c: blSource[].buf[blSource[].bufpos], newlines: 0,
|
||||||
init[BaseLexer](result)
|
folded: true)
|
||||||
|
init[BaseLexer](result)
|
||||||
|
|
||||||
proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer
|
proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer
|
||||||
{.raises: [].} =
|
{.raises: [].} =
|
||||||
let sSource = new(StringSource)
|
when defined(JS):
|
||||||
sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1,
|
let sSource = StringSource(pos: startAt, lineStart: startAt, line: 1,
|
||||||
src: source)
|
src: source)
|
||||||
GC_ref(sSource)
|
result = YamlLexer(buf: "", sSource: sSource,
|
||||||
new(result, proc(x: ref YamlLexerObj) {.nimcall.} =
|
inFlow: false, c: sSource.src[startAt], newlines: 0, folded: true)
|
||||||
GC_unref(cast[ref StringSource](x.source))
|
else:
|
||||||
)
|
let sSource = new(StringSource)
|
||||||
result[] = YamlLexerObj(buf: "", source: cast[pointer](sSource),
|
sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1,
|
||||||
inFlow: false, c: sSource.src[startAt], newlines: 0, folded: true)
|
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)
|
init[StringSource](result)
|
||||||
|
|
||||||
proc next*(lex: YamlLexer) =
|
proc next*(lex: YamlLexer) =
|
||||||
|
|
|
@ -176,23 +176,24 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
|
||||||
else:
|
else:
|
||||||
internalError("Unexpected node kind: " & $levels[levels.high].node.kind)
|
internalError("Unexpected node kind: " & $levels[levels.high].node.kind)
|
||||||
|
|
||||||
proc loadToJson*(s: Stream): seq[JsonNode]
|
when not defined(JS):
|
||||||
{.raises: [YamlParserError, YamlConstructionError, IOError].} =
|
proc loadToJson*(s: Stream): seq[JsonNode]
|
||||||
## Uses `YamlParser <#YamlParser>`_ and
|
{.raises: [YamlParserError, YamlConstructionError, IOError].} =
|
||||||
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
## Uses `YamlParser <#YamlParser>`_ and
|
||||||
## from a YAML character stream.
|
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
||||||
var
|
## from a YAML character stream.
|
||||||
parser = newYamlParser(initCoreTagLibrary())
|
var
|
||||||
events = parser.parse(s)
|
parser = newYamlParser(initCoreTagLibrary())
|
||||||
try:
|
events = parser.parse(s)
|
||||||
return constructJson(events)
|
try:
|
||||||
except YamlStreamError:
|
return constructJson(events)
|
||||||
let e = getCurrentException()
|
except YamlStreamError:
|
||||||
if e.parent of IOError:
|
let e = getCurrentException()
|
||||||
raise (ref IOError)(e.parent)
|
if e.parent of IOError:
|
||||||
elif e.parent of YamlParserError:
|
raise (ref IOError)(e.parent)
|
||||||
raise (ref YamlParserError)(e.parent)
|
elif e.parent of YamlParserError:
|
||||||
else: internalError("Unexpected exception: " & e.parent.repr)
|
raise (ref YamlParserError)(e.parent)
|
||||||
|
else: internalError("Unexpected exception: " & e.parent.repr)
|
||||||
|
|
||||||
proc loadToJson*(str: string): seq[JsonNode]
|
proc loadToJson*(str: string): seq[JsonNode]
|
||||||
{.raises: [YamlParserError, YamlConstructionError].} =
|
{.raises: [YamlParserError, YamlConstructionError].} =
|
||||||
|
|
Loading…
Reference in New Issue