2016-02-27 12:09:50 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2015 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
import tables, typetraits, strutils, macros, streams
|
|
|
|
import parser, common, taglib, presenter, stream, ../private/internal, hints
|
|
|
|
|
|
|
|
type
|
|
|
|
SerializationContext* = ref object
|
|
|
|
## Context information for the process of serializing YAML from Nim values.
|
|
|
|
refs*: Table[pointer, AnchorId]
|
|
|
|
style: AnchorStyle
|
|
|
|
nextAnchorId*: AnchorId
|
|
|
|
put*: proc(e: YamlStreamEvent) {.raises: [], closure.}
|
|
|
|
|
|
|
|
ConstructionContext* = ref object
|
|
|
|
## Context information for the process of constructing Nim values from YAML.
|
|
|
|
refs*: Table[AnchorId, pointer]
|
|
|
|
|
|
|
|
YamlConstructionError* = object of YamlLoadingError
|
|
|
|
## Exception that may be raised when constructing data objects from a
|
|
|
|
## `YamlStream <#YamlStream>`_. The fields ``line``, ``column`` and
|
|
|
|
## ``lineContent`` are only available if the costructing proc also does
|
|
|
|
## parsing, because otherwise this information is not available to the
|
|
|
|
## costruction proc.
|
|
|
|
|
|
|
|
# forward declares
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
|
|
|
## The stream will advance until after the finishing token that was used
|
|
|
|
## for constructing the value. The ``ConstructionContext`` is needed for
|
|
|
|
## potential child objects which may be refs.
|
|
|
|
|
|
|
|
proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs a Nim value that is a string from a part of a YAML stream.
|
|
|
|
## This specialization takes care of possible nil strings.
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var seq[T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs a Nim value that is a string from a part of a YAML stream.
|
|
|
|
## This specialization takes care of possible nil seqs.
|
|
|
|
|
|
|
|
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var ref O)
|
|
|
|
{.raises: [YamlStreamError].}
|
|
|
|
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
|
|
|
## The stream will advance until after the finishing token that was used
|
|
|
|
## for constructing the value. The object may be constructed from an alias
|
|
|
|
## node which will be resolved using the ``ConstructionContext``.
|
|
|
|
|
|
|
|
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext)
|
|
|
|
{.raises: [YamlStreamError].}
|
|
|
|
## Represents an arbitrary Nim reference value as YAML object. The object
|
|
|
|
## may be represented as alias node if it is already present in the
|
|
|
|
## ``SerializationContext``.
|
|
|
|
|
|
|
|
proc representChild*(value: string, ts: TagStyle, c: SerializationContext)
|
|
|
|
{.inline, raises: [].}
|
|
|
|
## Represents a Nim string. Supports nil strings.
|
|
|
|
|
|
|
|
proc representChild*[O](value: O, ts: TagStyle,
|
|
|
|
c: SerializationContext) {.raises: [YamlStreamError].}
|
|
|
|
## Represents an arbitrary Nim object as YAML object.
|
|
|
|
|
2016-03-25 22:12:00 +00:00
|
|
|
proc newConstructionContext*(): ConstructionContext =
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
|
|
|
result.refs = initTable[AnchorId, pointer]()
|
2016-01-28 21:29:26 +00:00
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
proc newSerializationContext*(s: AnchorStyle,
|
|
|
|
putImpl: proc(e: YamlStreamEvent) {.raises: [], closure.}):
|
|
|
|
SerializationContext =
|
|
|
|
SerializationContext(refs: initTable[pointer, AnchorId](), style: s,
|
|
|
|
nextAnchorId: 0.AnchorId, put: putImpl)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
template presentTag*(t: typedesc, ts: TagStyle): TagId =
|
2016-04-02 15:48:22 +00:00
|
|
|
## Get the TagId that represents the given type in the given style
|
|
|
|
if ts == tsNone: yTagQuestionMark else: yamlTag(t)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-26 20:13:40 +00:00
|
|
|
proc lazyLoadTag(uri: string): TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try: result = serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: result = serializationTagLibrary.registerUri(uri)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-26 20:13:40 +00:00
|
|
|
proc safeTagUri(id: TagId): string {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
|
|
|
let uri = serializationTagLibrary.uri(id)
|
|
|
|
if uri.len > 0 and uri[0] == '!': return uri[1..uri.len - 1]
|
|
|
|
else: return uri
|
2016-08-09 18:47:22 +00:00
|
|
|
except KeyError: internalError("Unexpected KeyError for TagId " & $id)
|
2016-01-05 15:54:14 +00:00
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
template constructScalarItem*(s: var YamlStream, i: untyped,
|
2016-02-26 20:13:40 +00:00
|
|
|
t: typedesc, content: untyped) =
|
2016-04-02 15:48:22 +00:00
|
|
|
## Helper template for implementing ``constructObject`` for types that
|
|
|
|
## are constructed from a scalar. ``i`` is the identifier that holds
|
|
|
|
## the scalar as ``YamlStreamEvent`` in the content. Exceptions raised in
|
|
|
|
## the content will be automatically catched and wrapped in
|
|
|
|
## ``YamlConstructionError``, which will then be raised.
|
2016-07-08 09:35:31 +00:00
|
|
|
let i = s.next()
|
2016-04-02 15:48:22 +00:00
|
|
|
if i.kind != yamlScalar:
|
|
|
|
raise newException(YamlConstructionError, "Expected scalar")
|
|
|
|
try: content
|
|
|
|
except YamlConstructionError: raise
|
|
|
|
except Exception:
|
|
|
|
var e = newException(YamlConstructionError,
|
|
|
|
"Cannot construct to " & name(t) & ": " & item.scalarContent)
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-02-15 18:46:21 +00:00
|
|
|
proc yamlTag*(T: typedesc[string]): TagId {.inline, noSideEffect, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
yTagString
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## costructs a string from a YAML scalar
|
|
|
|
constructScalarItem(s, item, string):
|
|
|
|
result = item.scalarContent
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: string, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a string as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent(value, tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-09-19 18:51:50 +00:00
|
|
|
proc parseHex[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](s: string): T =
|
|
|
|
result = 0
|
2016-09-19 20:51:33 +00:00
|
|
|
for i in 2..<s.len:
|
2016-09-19 18:51:50 +00:00
|
|
|
case s[i]
|
|
|
|
of '_': discard
|
2016-09-19 20:51:33 +00:00
|
|
|
of '0'..'9': result = result shl 4 or T(ord(s[i]) - ord('0'))
|
|
|
|
of 'a'..'f': result = result shl 4 or T(ord(s[i]) - ord('a') + 10)
|
|
|
|
of 'A'..'F': result = result shl 4 or T(ord(s[i]) - ord('A') + 10)
|
|
|
|
else: raise newException(ValueError, "Invalid character in hex: " & escape("" & s[i]))
|
2016-09-19 18:51:50 +00:00
|
|
|
|
|
|
|
proc parseOctal[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](s: string): T =
|
2016-09-19 20:51:33 +00:00
|
|
|
for i in 2..<s.len:
|
2016-09-19 18:51:50 +00:00
|
|
|
case s[i]
|
|
|
|
of '_': discard
|
2016-09-19 20:51:33 +00:00
|
|
|
of '0'..'7': result = result shl 3 + T((ord(s[i]) - ord('0')))
|
|
|
|
else: raise newException(ValueError, "Invalid character in hex: " & escape("" & s[i]))
|
2016-09-19 18:51:50 +00:00
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: int8|int16|int32|int64](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs an integer value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
2016-09-19 20:51:33 +00:00
|
|
|
if item.scalarContent[0] == '0' and item.scalarContent[1] in {'x', 'X' }:
|
2016-09-19 18:51:50 +00:00
|
|
|
result = parseHex[T](item.scalarContent)
|
2016-09-19 20:51:33 +00:00
|
|
|
elif item.scalarContent[0] == '0' and item.scalarContent[1] in {'o', 'O'}:
|
2016-09-19 18:51:50 +00:00
|
|
|
result = parseOctal[T](item.scalarContent)
|
|
|
|
else:
|
|
|
|
result = T(parseBiggestInt(item.scalarContent))
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var int)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## constructs an integer of architecture-defined length by loading it into
|
|
|
|
## int32 and then converting it.
|
|
|
|
var i32Result: int32
|
|
|
|
constructObject(s, c, i32Result)
|
|
|
|
result = int(i32Result)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*[T: int8|int16|int32|int64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents an integer value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc representObject*(value: int, tagStyle: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId)
|
|
|
|
{.raises: [YamlStreamError], inline.}=
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an integer of architecture-defined length by casting it to int32.
|
2016-08-09 18:47:22 +00:00
|
|
|
## on 64-bit systems, this may cause a RangeError.
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
# currently, sizeof(int) is at least sizeof(int32).
|
2016-09-01 18:56:34 +00:00
|
|
|
try: c.put(scalarEvent($int32(value), tag, yAnchorNone))
|
|
|
|
except RangeError:
|
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: uint8|uint16|uint32|uint64](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## construct an unsigned integer value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
2016-09-20 19:53:38 +00:00
|
|
|
if item.scalarContent[0] == '0' and item.scalarContent[1] in {'x', 'X'}:
|
|
|
|
result = parseHex[T](item.scalarContent)
|
|
|
|
elif item.scalarContent[0] == '0' and item.scalarContent[1] in {'o', 'O'}:
|
|
|
|
result = parseOctal[T](item.scalarContent)
|
|
|
|
else: result = T(parseBiggestUInt(item.scalarContent))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var uint)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an unsigned integer of architecture-defined length by loading it
|
|
|
|
## into uint32 and then converting it.
|
|
|
|
var u32Result: uint32
|
|
|
|
constructObject(s, c, u32Result)
|
|
|
|
result= uint(u32Result)
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-09-01 19:08:27 +00:00
|
|
|
when defined(JS):
|
|
|
|
# TODO: this is a dirty hack and may lead to overflows!
|
|
|
|
proc `$`(x: uint8|uint16|uint32|uint64|uint): string =
|
|
|
|
result = $BiggestInt(x)
|
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*[T: uint8|uint16|uint32|uint64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents an unsigned integer value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc representObject*(value: uint, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an unsigned integer of architecture-defined length by casting it
|
2016-08-09 18:47:22 +00:00
|
|
|
## to int32. on 64-bit systems, this may cause a RangeError.
|
2016-09-01 18:56:34 +00:00
|
|
|
try: c.put(scalarEvent($uint32(value), tag, yAnchorNone))
|
|
|
|
except RangeError:
|
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
proc constructObject*[T: float|float32|float64](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## construct a float value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
|
|
|
let hint = guessType(item.scalarContent)
|
|
|
|
case hint
|
2016-06-26 10:41:06 +00:00
|
|
|
of yTypeFloat:
|
|
|
|
discard parseBiggestFloat(item.scalarContent, result)
|
2016-07-08 09:35:31 +00:00
|
|
|
of yTypeInteger:
|
|
|
|
discard parseBiggestFloat(item.scalarContent, result)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeFloatInf:
|
|
|
|
if item.scalarContent[0] == '-': result = NegInf
|
|
|
|
else: result = Inf
|
|
|
|
of yTypeFloatNaN: result = NaN
|
|
|
|
else:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to float: " & item.scalarContent)
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
proc representObject*[T: float|float32|float64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a float value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
case value
|
|
|
|
of Inf: c.put(scalarEvent(".inf", tag))
|
|
|
|
of NegInf: c.put(scalarEvent("-.inf", tag))
|
|
|
|
of NaN: c.put(scalarEvent(".nan", tag))
|
|
|
|
else: c.put(scalarEvent($value, tag))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*(T: typedesc[bool]): TagId {.inline, raises: [].} = yTagBoolean
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var bool)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a bool value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, bool):
|
|
|
|
case guessType(item.scalarContent)
|
|
|
|
of yTypeBoolTrue: result = true
|
|
|
|
of yTypeBoolFalse: result = false
|
|
|
|
else:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to bool: " & item.scalarContent)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: bool, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a bool value as a YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent(if value: "y" else: "n", tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var char)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a char value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, char):
|
|
|
|
if item.scalarContent.len != 1:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to char (length != 1): " & item.scalarContent)
|
|
|
|
else: result = item.scalarContent[0]
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: char, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a char value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent("" & value, tag, yAnchorNone))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*[I](T: typedesc[seq[I]]): TagId {.inline, raises: [].} =
|
2016-04-02 16:29:26 +00:00
|
|
|
let uri = "!nim:system:seq(" & safeTagUri(yamlTag(I)) & ')'
|
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
|
|
|
|
proc yamlTag*[I](T: typedesc[set[I]]): TagId {.inline, raises: [].} =
|
|
|
|
let uri = "!nim:system:set(" & safeTagUri(yamlTag(I)) & ')'
|
2016-04-02 15:48:22 +00:00
|
|
|
result = lazyLoadTag(uri)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[T](s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var seq[T])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim seq from a YAML sequence
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
|
|
|
raise newException(YamlConstructionError, "Expected sequence start")
|
|
|
|
result = newSeq[T]()
|
|
|
|
while s.peek().kind != yamlEndSeq:
|
|
|
|
var item: T
|
|
|
|
constructChild(s, c, item)
|
|
|
|
result.add(item)
|
|
|
|
discard s.next()
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-04-02 16:29:26 +00:00
|
|
|
proc constructObject*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var set[T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim seq from a YAML sequence
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
|
|
|
raise newException(YamlConstructionError, "Expected sequence start")
|
|
|
|
result = {}
|
|
|
|
while s.peek().kind != yamlEndSeq:
|
|
|
|
var item: T
|
|
|
|
constructChild(s, c, item)
|
|
|
|
result.incl(item)
|
|
|
|
discard s.next()
|
|
|
|
|
|
|
|
proc representObject*[T](value: seq[T]|set[T], ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [YamlStreamError].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim seq as YAML sequence
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for item in value:
|
|
|
|
representChild(item, childTagStyle, c)
|
|
|
|
c.put(endSeqEvent())
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-04-03 09:29:49 +00:00
|
|
|
proc yamlTag*[I, V](T: typedesc[array[I, V]]): TagId {.inline, raises: [].} =
|
|
|
|
const rangeName = name(I)
|
|
|
|
let uri = "!nim:system:array(" & rangeName[6..rangeName.high()] & "," &
|
|
|
|
safeTagUri(yamlTag(V)) & ')'
|
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
|
|
|
|
proc constructObject*[I, T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var array[I, T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim array from a YAML sequence
|
|
|
|
var event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
|
|
|
raise newException(YamlConstructionError, "Expected sequence start")
|
|
|
|
for index in low(I)..high(I):
|
|
|
|
event = s.peek()
|
|
|
|
if event.kind == yamlEndSeq:
|
|
|
|
raise newException(YamlConstructionError, "Too few array values")
|
|
|
|
constructChild(s, c, result[index])
|
|
|
|
event = s.next()
|
|
|
|
if event.kind != yamlEndSeq:
|
|
|
|
raise newException(YamlConstructionError, "Too much array values")
|
|
|
|
|
|
|
|
proc representObject*[I, T](value: array[I, T], ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [YamlStreamError].} =
|
2016-04-03 09:29:49 +00:00
|
|
|
## represents a Nim array as YAML sequence
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for item in value:
|
|
|
|
representChild(item, childTagStyle, c)
|
|
|
|
c.put(endSeqEvent())
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*[K, V](T: typedesc[Table[K, V]]): TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
|
|
|
let uri = "!nim:tables:Table(" & safeTagUri(yamlTag(K)) & "," &
|
|
|
|
safeTagUri(yamlTag(V)) & ")"
|
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you know)
|
2016-08-09 18:47:22 +00:00
|
|
|
internalError("Unexpected KeyError")
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[K, V](s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var Table[K, V])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim Table from a YAML mapping
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartMap:
|
|
|
|
raise newException(YamlConstructionError, "Expected map start, got " &
|
|
|
|
$event.kind)
|
|
|
|
result = initTable[K, V]()
|
|
|
|
while s.peek.kind != yamlEndMap:
|
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
|
|
|
constructChild(s, c, key)
|
|
|
|
constructChild(s, c, value)
|
|
|
|
if result.contains(key):
|
|
|
|
raise newException(YamlConstructionError, "Duplicate table key!")
|
|
|
|
result[key] = value
|
|
|
|
discard s.next()
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[K, V](value: Table[K, V], ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises:[YamlStreamError].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim Table as YAML mapping
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startMapEvent(tag))
|
|
|
|
for key, value in value.pairs:
|
|
|
|
representChild(key, childTagStyle, c)
|
|
|
|
representChild(value, childTagStyle, c)
|
|
|
|
c.put(endMapEvent())
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc yamlTag*[K, V](T: typedesc[OrderedTable[K, V]]): TagId
|
2016-04-02 15:48:22 +00:00
|
|
|
{.inline, raises: [].} =
|
|
|
|
try:
|
|
|
|
let uri = "!nim:tables:OrderedTable(" & safeTagUri(yamlTag(K)) & "," &
|
|
|
|
safeTagUri(yamlTag(V)) & ")"
|
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you know)
|
2016-08-09 18:47:22 +00:00
|
|
|
internalError("Unexpected KeyError")
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc constructObject*[K, V](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var OrderedTable[K, V])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim OrderedTable from a YAML mapping
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
|
|
|
raise newException(YamlConstructionError, "Expected seq start, got " &
|
|
|
|
$event.kind)
|
|
|
|
result = initOrderedTable[K, V]()
|
|
|
|
while s.peek.kind != yamlEndSeq:
|
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
|
|
|
if s.next().kind != yamlStartMap:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected map start, got " & $event.kind)
|
|
|
|
constructChild(s, c, key)
|
|
|
|
constructChild(s, c, value)
|
|
|
|
if s.next().kind != yamlEndMap:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected map end, got " & $event.kind)
|
|
|
|
if result.contains(key):
|
|
|
|
raise newException(YamlConstructionError, "Duplicate table key!")
|
|
|
|
result.add(key, value)
|
|
|
|
discard s.next()
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc representObject*[K, V](value: OrderedTable[K, V], ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [YamlStreamError].} =
|
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for key, value in value.pairs:
|
|
|
|
c.put(startMapEvent())
|
|
|
|
representChild(key, childTagStyle, c)
|
|
|
|
representChild(value, childTagStyle, c)
|
|
|
|
c.put(endMapEvent())
|
|
|
|
c.put(endSeqEvent())
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc yamlTag*(T: typedesc[object|enum]):
|
2016-06-05 13:43:39 +00:00
|
|
|
TagId {.inline, raises: [].} =
|
|
|
|
var uri = "!nim:custom:" & (typetraits.name(type(T)))
|
|
|
|
try: serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: serializationTagLibrary.registerUri(uri)
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc yamlTag*(T: typedesc[tuple]):
|
2016-06-05 13:43:39 +00:00
|
|
|
TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
i: T
|
|
|
|
uri = "!nim:tuple("
|
|
|
|
first = true
|
|
|
|
for name, value in fieldPairs(i):
|
|
|
|
if first: first = false
|
|
|
|
else: uri.add(",")
|
|
|
|
uri.add(safeTagUri(yamlTag(type(value))))
|
|
|
|
uri.add(")")
|
|
|
|
try: serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: serializationTagLibrary.registerUri(uri)
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
macro constructFieldValue(t: typedesc, stream: untyped, context: untyped,
|
|
|
|
name: untyped, o: untyped): typed =
|
2016-06-05 17:29:16 +00:00
|
|
|
let tDesc = getType(getType(t)[1])
|
|
|
|
result = newNimNode(nnkCaseStmt).add(name)
|
2016-06-05 18:52:43 +00:00
|
|
|
for child in tDesc[2].children:
|
2016-06-05 17:29:16 +00:00
|
|
|
if child.kind == nnkRecCase:
|
|
|
|
let
|
|
|
|
discriminant = newDotExpr(o, newIdentNode($child[0]))
|
|
|
|
discType = newCall("type", discriminant)
|
|
|
|
var disOb = newNimNode(nnkOfBranch).add(newStrLitNode($child[0]))
|
|
|
|
disOb.add(newStmtList(
|
|
|
|
newNimNode(nnkVarSection).add(
|
|
|
|
newNimNode(nnkIdentDefs).add(
|
|
|
|
newIdentNode("value"), discType, newEmptyNode())),
|
|
|
|
newCall("constructChild", stream, context, newIdentNode("value")),
|
|
|
|
newAssignment(discriminant, newIdentNode("value"))))
|
|
|
|
result.add(disOb)
|
|
|
|
for bIndex in 1 .. len(child) - 1:
|
|
|
|
let discTest = infix(discriminant, "==", child[bIndex][0])
|
|
|
|
for item in child[bIndex][1].children:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert item.kind == nnkSym
|
2016-06-05 17:29:16 +00:00
|
|
|
var ob = newNimNode(nnkOfBranch).add(newStrLitNode($item))
|
|
|
|
let field = newDotExpr(o, newIdentNode($item))
|
|
|
|
var ifStmt = newIfStmt((cond: discTest, body: newStmtList(
|
|
|
|
newCall("constructChild", stream, context, field))))
|
2016-06-08 17:15:50 +00:00
|
|
|
ifStmt.add(newNimNode(nnkElse).add(newNimNode(nnkRaiseStmt).add(
|
|
|
|
newCall("newException", newIdentNode("YamlConstructionError"),
|
|
|
|
infix(newStrLitNode("Field " & $item & " not allowed for " &
|
|
|
|
$child[0] & " == "), "&", prefix(discriminant, "$"))))))
|
2016-06-05 17:29:16 +00:00
|
|
|
ob.add(newStmtList(ifStmt))
|
|
|
|
result.add(ob)
|
|
|
|
else:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert child.kind == nnkSym
|
2016-06-05 17:29:16 +00:00
|
|
|
var ob = newNimNode(nnkOfBranch).add(newStrLitNode($child))
|
|
|
|
let field = newDotExpr(o, newIdentNode($child))
|
|
|
|
ob.add(newStmtList(newCall("constructChild", stream, context, field)))
|
|
|
|
result.add(ob)
|
2016-08-17 20:50:37 +00:00
|
|
|
# TODO: is this correct?
|
|
|
|
result.add(newNimNode(nnkElse).add(newNimNode(nnkDiscardStmt).add(
|
|
|
|
newEmptyNode())))
|
2016-06-05 17:29:16 +00:00
|
|
|
|
2016-06-08 17:15:50 +00:00
|
|
|
proc isVariantObject(t: typedesc): bool {.compileTime.} =
|
|
|
|
let tDesc = getType(t)
|
|
|
|
if tDesc.kind != nnkObjectTy: return false
|
|
|
|
for child in tDesc[2].children:
|
|
|
|
if child.kind == nnkRecCase: return true
|
|
|
|
return false
|
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
proc constructObject*[O: object|tuple](
|
|
|
|
s: var YamlStream, c: ConstructionContext, result: var O)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim object or tuple from a YAML mapping
|
|
|
|
let e = s.next()
|
2016-06-08 17:15:50 +00:00
|
|
|
const
|
|
|
|
startKind = when isVariantObject(O): yamlStartSeq else: yamlStartMap
|
|
|
|
endKind = when isVariantObject(O): yamlEndSeq else: yamlEndMap
|
|
|
|
if e.kind != startKind:
|
2016-07-08 09:35:31 +00:00
|
|
|
raise newException(YamlConstructionError, "While constructing " &
|
2016-06-05 17:29:16 +00:00
|
|
|
typetraits.name(O) & ": Expected map start, got " & $e.kind)
|
2016-06-08 17:15:50 +00:00
|
|
|
when isVariantObject(O): reset(result) # make discriminants writeable
|
|
|
|
while s.peek.kind != endKind:
|
2016-06-05 17:29:16 +00:00
|
|
|
# todo: check for duplicates in input and raise appropriate exception
|
|
|
|
# also todo: check for missing items and raise appropriate exception
|
2016-06-08 17:15:50 +00:00
|
|
|
var e = s.next()
|
|
|
|
when isVariantObject(O):
|
|
|
|
if e.kind != yamlStartMap:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected single-pair map, got " & $e.kind)
|
|
|
|
e = s.next()
|
2016-04-02 15:48:22 +00:00
|
|
|
if e.kind != yamlScalar:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected field name, got " & $e.kind)
|
|
|
|
let name = e.scalarContent
|
2016-06-05 18:52:43 +00:00
|
|
|
when result is tuple:
|
|
|
|
for fname, value in fieldPairs(result):
|
|
|
|
if fname == name:
|
|
|
|
constructChild(s, c, value)
|
|
|
|
break
|
2016-06-08 17:15:50 +00:00
|
|
|
else:
|
|
|
|
constructFieldValue(O, s, c, name, result)
|
|
|
|
when isVariantObject(O):
|
|
|
|
e = s.next()
|
|
|
|
if e.kind != yamlEndMap:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected end of single-pair map, got " & $e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
discard s.next()
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[O: object|tuple](value: O, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [YamlStreamError].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim object or tuple as YAML mapping
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
when isVariantObject(O): c.put(startSeqEvent(tag, yAnchorNone))
|
|
|
|
else: c.put(startMapEvent(tag, yAnchorNone))
|
|
|
|
for name, value in fieldPairs(value):
|
|
|
|
when isVariantObject(O): c.put(startMapEvent(yTagQuestionMark, yAnchorNone))
|
|
|
|
c.put(scalarEvent(name, if childTagStyle == tsNone: yTagQuestionMark else:
|
|
|
|
yTagNimField, yAnchorNone))
|
|
|
|
representChild(value, childTagStyle, c)
|
|
|
|
when isVariantObject(O): c.put(endMapEvent())
|
|
|
|
when isVariantObject(O): c.put(endSeqEvent())
|
|
|
|
else: c.put(endMapEvent())
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[O: enum](s: var YamlStream, c: ConstructionContext,
|
2016-02-01 19:16:35 +00:00
|
|
|
result: var O)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim enum from a YAML scalar
|
|
|
|
let e = s.next()
|
|
|
|
if e.kind != yamlScalar:
|
|
|
|
raise newException(YamlConstructionError, "Expected scalar, got " &
|
|
|
|
$e.kind)
|
|
|
|
try: result = parseEnum[O](e.scalarContent)
|
|
|
|
except ValueError:
|
|
|
|
var ex = newException(YamlConstructionError, "Cannot parse '" &
|
|
|
|
e.scalarContent & "' as " & type(O).name)
|
|
|
|
ex.parent = getCurrentException()
|
|
|
|
raise ex
|
2016-02-01 19:16:35 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[O: enum](value: O, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim enum as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-02-01 19:16:35 +00:00
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped,
|
|
|
|
t: typedesc): typed =
|
2016-06-05 17:29:16 +00:00
|
|
|
let tDesc = getType(getType(t)[1])
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert tDesc.kind == nnkObjectTy
|
2016-06-05 18:52:43 +00:00
|
|
|
let recCase = tDesc[2][0]
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert recCase.kind == nnkRecCase
|
2016-08-17 20:50:37 +00:00
|
|
|
let discriminant = newDotExpr(r, newIdentNode($recCase[0]))
|
2016-06-05 17:29:16 +00:00
|
|
|
var ifStmt = newNimNode(nnkIfStmt)
|
|
|
|
for i in 1 .. recCase.len - 1:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert recCase[i].kind == nnkOfBranch
|
2016-06-05 17:29:16 +00:00
|
|
|
var branch = newNimNode(nnkElifBranch)
|
|
|
|
var branchContent = newStmtList(newAssignment(discriminant, recCase[i][0]))
|
|
|
|
case recCase[i][1].len
|
|
|
|
of 0:
|
|
|
|
branch.add(infix(newIdentNode("yTagNull"), "in", possibleTagIds))
|
|
|
|
branchContent.add(newNimNode(nnkDiscardStmt).add(newCall("next", s)))
|
|
|
|
of 1:
|
|
|
|
let field = newDotExpr(r, newIdentNode($recCase[i][1][0]))
|
|
|
|
branch.add(infix(
|
|
|
|
newCall("yamlTag", newCall("type", field)), "in", possibleTagIds))
|
|
|
|
branchContent.add(newCall("constructChild", s, c, field))
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Too many children: " & $recCase[i][1].len)
|
2016-06-05 17:29:16 +00:00
|
|
|
branch.add(branchContent)
|
|
|
|
ifStmt.add(branch)
|
|
|
|
let raiseStmt = newNimNode(nnkRaiseStmt).add(
|
|
|
|
newCall("newException", newIdentNode("YamlConstructionError"),
|
|
|
|
infix(newStrLitNode("This value type does not map to any field in " &
|
|
|
|
typetraits.name(t) & ": "), "&",
|
|
|
|
newCall("uri", newIdentNode("serializationTagLibrary"),
|
|
|
|
newNimNode(nnkBracketExpr).add(possibleTagIds, newIntLitNode(0)))
|
|
|
|
)
|
|
|
|
))
|
|
|
|
ifStmt.add(newNimNode(nnkElse).add(newNimNode(nnkTryStmt).add(
|
|
|
|
newStmtList(raiseStmt), newNimNode(nnkExceptBranch).add(
|
2016-08-09 18:47:22 +00:00
|
|
|
newIdentNode("KeyError"), newStmtList(newCall("internalError",
|
|
|
|
newStrLitNode("Unexcpected KeyError")))
|
2016-06-05 17:29:16 +00:00
|
|
|
))))
|
|
|
|
result = newStmtList(newCall("reset", r), ifStmt)
|
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var T) =
|
2016-04-02 15:48:22 +00:00
|
|
|
let item = s.peek()
|
2016-06-05 17:29:16 +00:00
|
|
|
when compiles(implicitVariantObject(result)):
|
|
|
|
var possibleTagIds = newSeq[TagId]()
|
|
|
|
case item.kind
|
|
|
|
of yamlScalar:
|
|
|
|
case item.scalarTag
|
|
|
|
of yTagQuestionMark:
|
|
|
|
case guessType(item.scalarContent)
|
|
|
|
of yTypeInteger:
|
|
|
|
possibleTagIds.add([yamlTag(int), yamlTag(int8), yamlTag(int16),
|
|
|
|
yamlTag(int32), yamlTag(int64)])
|
|
|
|
if item.scalarContent[0] != '-':
|
|
|
|
possibleTagIds.add([yamlTag(uint), yamlTag(uint8), yamlTag(uint16),
|
|
|
|
yamlTag(uint32), yamlTag(uint64)])
|
|
|
|
of yTypeFloat, yTypeFloatInf, yTypeFloatNaN:
|
|
|
|
possibleTagIds.add([yamlTag(float), yamlTag(float32),
|
|
|
|
yamlTag(float64)])
|
|
|
|
of yTypeBoolTrue, yTypeBoolFalse:
|
|
|
|
possibleTagIds.add(yamlTag(bool))
|
|
|
|
of yTypeNull:
|
|
|
|
raise newException(YamlConstructionError, "not implemented!")
|
|
|
|
of yTypeUnknown:
|
|
|
|
possibleTagIds.add(yamlTag(string))
|
|
|
|
of yTagExclamationMark:
|
|
|
|
possibleTagIds.add(yamlTag(string))
|
|
|
|
else:
|
|
|
|
possibleTagIds.add(item.scalarTag)
|
|
|
|
of yamlStartMap:
|
|
|
|
if item.mapTag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Complex value of implicit variant object type must have a tag.")
|
|
|
|
possibleTagIds.add(item.mapTag)
|
|
|
|
of yamlStartSeq:
|
|
|
|
if item.seqTag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Complex value of implicit variant object type must have a tag.")
|
|
|
|
possibleTagIds.add(item.seqTag)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected item kind: " & $item.kind)
|
2016-06-05 17:29:16 +00:00
|
|
|
constructImplicitVariantObject(s, c, result, possibleTagIds, T)
|
|
|
|
else:
|
|
|
|
case item.kind
|
|
|
|
of yamlScalar:
|
|
|
|
if item.scalarTag notin [yTagQuestionMark, yTagExclamationMark,
|
|
|
|
yamlTag(T)]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for " &
|
|
|
|
typetraits.name(T))
|
|
|
|
elif item.scalarAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on non-ref type")
|
|
|
|
of yamlStartMap:
|
|
|
|
if item.mapTag notin [yTagQuestionMark, yamlTag(T)]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for " &
|
|
|
|
typetraits.name(T))
|
|
|
|
elif item.mapAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on non-ref type")
|
|
|
|
of yamlStartSeq:
|
|
|
|
if item.seqTag notin [yTagQuestionMark, yamlTag(T)]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for " &
|
|
|
|
typetraits.name(T))
|
|
|
|
elif item.seqAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on non-ref type")
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected item kind: " & $item.kind)
|
2016-06-05 17:29:16 +00:00
|
|
|
constructObject(s, c, result)
|
2016-02-16 18:24:55 +00:00
|
|
|
|
2016-04-04 19:21:24 +00:00
|
|
|
proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string) =
|
|
|
|
let item = s.peek()
|
|
|
|
if item.kind == yamlScalar:
|
|
|
|
if item.scalarTag == yTagNimNilString:
|
|
|
|
discard s.next()
|
|
|
|
result = nil
|
|
|
|
return
|
|
|
|
elif item.scalarTag notin
|
|
|
|
[yTagQuestionMark, yTagExclamationMark, yamlTag(string)]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for string")
|
|
|
|
elif item.scalarAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on non-ref type")
|
|
|
|
constructObject(s, c, result)
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var seq[T]) =
|
|
|
|
let item = s.peek()
|
|
|
|
if item.kind == yamlScalar:
|
|
|
|
if item.scalarTag == yTagNimNilSeq:
|
|
|
|
discard s.next()
|
|
|
|
result = nil
|
|
|
|
return
|
|
|
|
elif item.kind == yamlStartSeq:
|
|
|
|
if item.seqTag notin [yTagQuestionMark, yamlTag(seq[T])]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for " &
|
|
|
|
typetraits.name(seq[T]))
|
|
|
|
elif item.seqAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on non-ref type")
|
|
|
|
constructObject(s, c, result)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
2016-04-02 15:48:22 +00:00
|
|
|
result: var ref O) =
|
|
|
|
var e = s.peek()
|
|
|
|
if e.kind == yamlScalar:
|
|
|
|
if e.scalarTag == yTagNull or (e.scalarTag == yTagQuestionMark and
|
|
|
|
guessType(e.scalarContent) == yTypeNull):
|
|
|
|
result = nil
|
|
|
|
discard s.next()
|
|
|
|
return
|
|
|
|
elif e.kind == yamlAlias:
|
2016-08-09 18:47:22 +00:00
|
|
|
result = cast[ref O](c.refs.getOrDefault(e.aliasTarget))
|
|
|
|
discard s.next()
|
|
|
|
return
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
|
|
|
template removeAnchor(anchor: var AnchorId) {.dirty.} =
|
|
|
|
if anchor != yAnchorNone:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert(not c.refs.hasKey(anchor))
|
2016-04-02 15:48:22 +00:00
|
|
|
c.refs[anchor] = cast[pointer](result)
|
|
|
|
anchor = yAnchorNone
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
case e.kind
|
|
|
|
of yamlScalar: removeAnchor(e.scalarAnchor)
|
|
|
|
of yamlStartMap: removeAnchor(e.mapAnchor)
|
|
|
|
of yamlStartSeq: removeAnchor(e.seqAnchor)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected event kind: " & $e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
s.peek = e
|
|
|
|
try: constructChild(s, c, result[])
|
2016-08-09 18:47:22 +00:00
|
|
|
except YamlConstructionError, YamlStreamError: raise
|
2016-04-02 15:48:22 +00:00
|
|
|
except Exception:
|
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
proc representChild*(value: string, ts: TagStyle, c: SerializationContext) =
|
|
|
|
if isNil(value): c.put(scalarEvent("", yTagNimNilString))
|
|
|
|
else: representObject(value, ts, c, presentTag(string, ts))
|
|
|
|
|
|
|
|
proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) =
|
|
|
|
if isNil(value): c.put(scalarEvent("", yTagNimNilSeq))
|
|
|
|
else: representObject(value, ts, c, presentTag(seq[T], ts))
|
|
|
|
|
|
|
|
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext) =
|
|
|
|
if isNil(value): c.put(scalarEvent("~", yTagNull))
|
|
|
|
elif c.style == asNone: representChild(value[], ts, c)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
|
|
|
let p = cast[pointer](value)
|
|
|
|
if c.refs.hasKey(p):
|
2016-08-09 18:47:22 +00:00
|
|
|
if c.refs.getOrDefault(p) == yAnchorNone:
|
2016-04-02 15:48:22 +00:00
|
|
|
c.refs[p] = c.nextAnchorId
|
|
|
|
c.nextAnchorId = AnchorId(int(c.nextAnchorId) + 1)
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(aliasEvent(c.refs.getOrDefault(p)))
|
2016-08-09 18:47:22 +00:00
|
|
|
return
|
|
|
|
if c.style == asAlways:
|
|
|
|
c.refs[p] = c.nextAnchorId
|
|
|
|
c.nextAnchorId = AnchorId(int(c.nextAnchorId) + 1)
|
|
|
|
else: c.refs[p] = yAnchorNone
|
|
|
|
let
|
|
|
|
a = if c.style == asAlways: c.refs.getOrDefault(p) else: cast[AnchorId](p)
|
|
|
|
childTagStyle = if ts == tsAll: tsAll else: tsRootOnly
|
2016-09-01 18:56:34 +00:00
|
|
|
let origPut = c.put
|
|
|
|
c.put = proc(e: YamlStreamEvent) =
|
|
|
|
var ex = e
|
|
|
|
case ex.kind
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlStartMap:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.mapAnchor = a
|
|
|
|
if ts == tsNone: ex.mapTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlStartSeq:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.seqAnchor = a
|
|
|
|
if ts == tsNone: ex.seqTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlScalar:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.scalarAnchor = a
|
|
|
|
if ts == tsNone and guessType(ex.scalarContent) != yTypeNull:
|
|
|
|
ex.scalarTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
else: discard
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put = origPut
|
|
|
|
c.put(ex)
|
|
|
|
representChild(value[], childTagStyle, c)
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-06-05 13:43:39 +00:00
|
|
|
proc representChild*[O](value: O, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext) =
|
2016-06-05 17:29:16 +00:00
|
|
|
when compiles(implicitVariantObject(value)):
|
|
|
|
# todo: this would probably be nicer if constructed with a macro
|
|
|
|
var count = 0
|
|
|
|
for name, field in fieldPairs(value):
|
|
|
|
if count > 0:
|
2016-09-01 18:56:34 +00:00
|
|
|
representChild(field, if ts == tsAll: tsAll else: tsRootOnly, c)
|
2016-06-05 17:29:16 +00:00
|
|
|
inc(count)
|
2016-09-01 18:56:34 +00:00
|
|
|
if count == 1: c.put(scalarEvent("~", yTagNull))
|
2016-06-05 17:29:16 +00:00
|
|
|
else:
|
2016-09-01 18:56:34 +00:00
|
|
|
representObject(value, ts, c,
|
|
|
|
if ts == tsNone: yTagQuestionMark else: yamlTag(O))
|
2016-06-05 13:43:39 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc construct*[T](s: var YamlStream, target: var T)
|
|
|
|
{.raises: [YamlStreamError].} =
|
|
|
|
## Constructs a Nim value from a YAML stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
var context = newConstructionContext()
|
|
|
|
try:
|
|
|
|
var e = s.next()
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert(e.kind == yamlStartDoc)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
constructChild(s, context, target)
|
|
|
|
e = s.next()
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert(e.kind == yamlEndDoc)
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlConstructionError:
|
|
|
|
raise (ref YamlConstructionError)(getCurrentException())
|
|
|
|
except YamlStreamError:
|
2016-09-13 10:01:21 +00:00
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur.parent
|
|
|
|
raise e
|
2016-04-02 15:48:22 +00:00
|
|
|
except Exception:
|
|
|
|
# may occur while calling s()
|
|
|
|
var ex = newException(YamlStreamError, "")
|
|
|
|
ex.parent = getCurrentException()
|
|
|
|
raise ex
|
2016-01-26 19:51:21 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc load*[K](input: Stream | string, target: var K)
|
|
|
|
{.raises: [YamlConstructionError, IOError, YamlParserError].} =
|
|
|
|
## Loads a Nim value from a YAML character stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
parser = newYamlParser(serializationTagLibrary)
|
|
|
|
events = parser.parse(input)
|
|
|
|
try: construct(events, target)
|
|
|
|
except YamlConstructionError:
|
|
|
|
var e = (ref YamlConstructionError)(getCurrentException())
|
2016-09-12 19:38:56 +00:00
|
|
|
discard events.getLastTokenContext(e.line, e.column, e.lineContent)
|
2016-04-02 15:48:22 +00:00
|
|
|
raise e
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlStreamError)(getCurrentException())
|
2016-09-19 18:51:50 +00:00
|
|
|
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)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
proc loadMultiDoc*[K](input: Stream | string, target: var seq[K]) =
|
2016-09-19 18:51:50 +00:00
|
|
|
if target.isNil:
|
|
|
|
target = newSeq[K]()
|
|
|
|
var
|
|
|
|
parser = newYamlParser(serializationTagLibrary)
|
|
|
|
events = parser.parse(input)
|
|
|
|
try:
|
|
|
|
while not events.finished():
|
|
|
|
var item: K
|
|
|
|
construct(events, item)
|
|
|
|
target.add(item)
|
|
|
|
except YamlConstructionError:
|
|
|
|
var e = (ref YamlConstructionError)(getCurrentException())
|
|
|
|
discard events.getLastTokenContext(e.line, e.column, e.lineContent)
|
|
|
|
raise e
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlStreamError)(getCurrentException())
|
2016-04-02 15:48:22 +00:00
|
|
|
if e.parent of IOError: raise (ref IOError)(e.parent)
|
|
|
|
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected exception: " & e.parent.repr)
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2016-02-22 20:56:30 +00:00
|
|
|
proc setAnchor(a: var AnchorId, q: var Table[pointer, AnchorId])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.inline.} =
|
2016-08-09 18:47:22 +00:00
|
|
|
if a != yAnchorNone: a = q.getOrDefault(cast[pointer](a))
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc represent*[T](value: T, ts: TagStyle = tsRootOnly,
|
2016-09-20 19:53:38 +00:00
|
|
|
a: AnchorStyle = asTidy): YamlStream
|
|
|
|
{.raises: [YamlStreamError].} =
|
|
|
|
## Represents a Nim value as ``YamlStream``
|
2016-09-01 18:56:34 +00:00
|
|
|
var bys = newBufferYamlStream()
|
|
|
|
var context = newSerializationContext(a, proc(e: YamlStreamEvent) =
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(e)
|
2016-09-01 18:56:34 +00:00
|
|
|
)
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(startDocEvent())
|
2016-09-01 18:56:34 +00:00
|
|
|
representChild(value, ts, context)
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(endDocEvent())
|
2016-04-02 15:48:22 +00:00
|
|
|
if a == asTidy:
|
2016-09-20 19:53:38 +00:00
|
|
|
for item in bys.mitems():
|
2016-09-01 18:56:34 +00:00
|
|
|
case item.kind
|
|
|
|
of yamlStartMap: item.mapAnchor.setAnchor(context.refs)
|
|
|
|
of yamlStartSeq: item.seqAnchor.setAnchor(context.refs)
|
|
|
|
of yamlScalar: item.scalarAnchor.setAnchor(context.refs)
|
|
|
|
else: discard
|
|
|
|
result = bys
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-02-26 20:55:59 +00:00
|
|
|
proc dump*[K](value: K, target: Stream, tagStyle: TagStyle = tsRootOnly,
|
|
|
|
anchorStyle: AnchorStyle = asTidy,
|
2016-09-20 19:53:38 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions)
|
|
|
|
{.raises: [YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError].} =
|
|
|
|
## Dump a Nim value as YAML character stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
var events = represent(value,
|
|
|
|
if options.style == psCanonical: tsAll else: tagStyle,
|
|
|
|
if options.style == psJson: asNone else: anchorStyle)
|
|
|
|
try: present(events, target, serializationTagLibrary, options)
|
|
|
|
except YamlStreamError:
|
2016-08-09 18:47:22 +00:00
|
|
|
internalError("Unexpected exception: " & getCurrentException().repr)
|
2016-09-14 12:35:41 +00:00
|
|
|
|
|
|
|
proc dump*[K](value: K, tagStyle: TagStyle = tsRootOnly,
|
|
|
|
anchorStyle: AnchorStyle = asTidy,
|
|
|
|
options: PresentationOptions = defaultPresentationOptions):
|
2016-09-20 19:53:38 +00:00
|
|
|
string {.raises: [YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError].} =
|
|
|
|
## Dump a Nim value as YAML into a string
|
2016-09-19 17:33:29 +00:00
|
|
|
var events = represent(value,
|
|
|
|
if options.style == psCanonical: tsAll else: tagStyle,
|
|
|
|
if options.style == psJson: asNone else: anchorStyle)
|
|
|
|
try: result = present(events, serializationTagLibrary, options)
|
|
|
|
except YamlStreamError:
|
|
|
|
internalError("Unexpected exception: " & getCurrentException().repr)
|