2016-01-28 20:59:26 +00:00
|
|
|
proc initRefNodeData(p: pointer): RefNodeData =
|
|
|
|
result.p = p
|
|
|
|
result.count = 1
|
|
|
|
result.anchor = yAnchorNone
|
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc newConstructionContext(): ConstructionContext =
|
|
|
|
new(result)
|
|
|
|
result.refs = initTable[AnchorId, pointer]()
|
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
proc newSerializationContext(s: AnchorStyle): SerializationContext =
|
|
|
|
new(result)
|
|
|
|
result.refsList = newSeq[RefNodeData]()
|
|
|
|
result.style = s
|
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc initSerializationTagLibrary(): TagLibrary {.raises: [].} =
|
2016-01-04 20:46:33 +00:00
|
|
|
result = initTagLibrary()
|
|
|
|
result.tags["!"] = yTagExclamationMark
|
|
|
|
result.tags["?"] = yTagQuestionMark
|
|
|
|
result.tags["tag:yaml.org,2002:str"] = yTagString
|
|
|
|
result.tags["tag:yaml.org,2002:null"] = yTagNull
|
|
|
|
result.tags["tag:yaml.org,2002:bool"] = yTagBoolean
|
|
|
|
result.tags["tag:yaml.org,2002:float"] = yTagFloat
|
|
|
|
result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp
|
|
|
|
result.tags["tag:yaml.org,2002:value"] = yTagValue
|
|
|
|
result.tags["tag:yaml.org,2002:binary"] = yTagBinary
|
2016-02-01 18:48:42 +00:00
|
|
|
result.tags["!nim:system:int8"] = yTagNimInt8
|
|
|
|
result.tags["!nim:system:int16"] = yTagNimInt16
|
|
|
|
result.tags["!nim:system:int32"] = yTagNimInt32
|
|
|
|
result.tags["!nim:system:int64"] = yTagNimInt64
|
|
|
|
result.tags["!nim:system:uint8"] = yTagNimUInt8
|
|
|
|
result.tags["!nim:system:uint16"] = yTagNimUInt16
|
|
|
|
result.tags["!nim:system:uint32"] = yTagNimUInt32
|
|
|
|
result.tags["!nim:system:uint64"] = yTagNimUInt64
|
|
|
|
result.tags["!nim:system:float32"] = yTagNimFloat32
|
|
|
|
result.tags["!nim:system:float64"] = yTagNimFloat64
|
|
|
|
result.tags["!nim:system:char"] = yTagNimChar
|
2016-01-04 20:46:33 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
serializationTagLibrary* = initSerializationTagLibrary() ## \
|
|
|
|
## contains all local tags that are used for type serialization. Does
|
|
|
|
## not contain any of the specific default tags for sequences or maps,
|
|
|
|
## as those are not suited for Nim's static type system.
|
|
|
|
##
|
|
|
|
## Should not be modified manually. Will be extended by
|
2016-01-24 19:38:30 +00:00
|
|
|
## `serializable <#serializable,stmt,stmt>`_.
|
2016-01-04 20:46:33 +00:00
|
|
|
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
static:
|
2016-01-24 19:38:30 +00:00
|
|
|
iterator objectFields(n: NimNode): tuple[name: NimNode, t: NimNode]
|
|
|
|
{.raises: [].} =
|
2016-01-05 15:54:14 +00:00
|
|
|
assert n.kind in [nnkRecList, nnkTupleTy]
|
2015-12-29 14:09:37 +00:00
|
|
|
for identDefs in n.children:
|
|
|
|
let numFields = identDefs.len - 2
|
|
|
|
for i in 0..numFields - 1:
|
|
|
|
yield (name: identDefs[i], t: identDefs[^2])
|
2016-01-05 15:54:14 +00:00
|
|
|
|
|
|
|
var existingTuples = newSeq[NimNode]()
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
template presentTag(t: typedesc, ts: TagStyle): TagId =
|
|
|
|
if ts == tsNone: yTagQuestionMark else: yamlTag(t)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc lazyLoadTag*(uri: string): TagId {.inline, raises: [].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Internal function. Do not call explicitly.
|
2016-01-04 20:46:33 +00:00
|
|
|
try:
|
|
|
|
result = serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError:
|
|
|
|
result = serializationTagLibrary.registerUri(uri)
|
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
macro serializable*(types: stmt): stmt =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Macro for customizing serialization of user-defined types.
|
|
|
|
## Currently does not provide more features than just using the standard
|
|
|
|
## serialization procs. This will change in the future.
|
2015-12-29 14:09:37 +00:00
|
|
|
assert types.kind == nnkTypeSection
|
|
|
|
result = newStmtList(types)
|
|
|
|
for typedef in types.children:
|
|
|
|
assert typedef.kind == nnkTypeDef
|
|
|
|
let
|
|
|
|
tName = $typedef[0].symbol
|
|
|
|
tIdent = newIdentNode(tName)
|
2016-01-05 15:54:14 +00:00
|
|
|
var
|
|
|
|
tUri: NimNode
|
|
|
|
recList: NimNode
|
2015-12-29 14:09:37 +00:00
|
|
|
assert typedef[1].kind == nnkEmpty
|
|
|
|
let objectTy = typedef[2]
|
2016-01-05 15:54:14 +00:00
|
|
|
case objectTy.kind
|
|
|
|
of nnkObjectTy:
|
|
|
|
assert objectTy[0].kind == nnkEmpty
|
|
|
|
assert objectTy[1].kind == nnkEmpty
|
2016-02-01 18:48:42 +00:00
|
|
|
tUri = newStrLitNode("!nim:custom:" & tName)
|
2016-01-05 15:54:14 +00:00
|
|
|
recList = objectTy[2]
|
|
|
|
of nnkTupleTy:
|
|
|
|
if objectTy in existingTuples:
|
|
|
|
continue
|
|
|
|
existingTuples.add(objectTy)
|
|
|
|
|
|
|
|
recList = objectTy
|
|
|
|
tUri = newStmtList()
|
|
|
|
var
|
|
|
|
first = true
|
|
|
|
curStrLit = "!nim:tuple("
|
|
|
|
curInfix = tUri
|
|
|
|
for field in objectFields(recList):
|
|
|
|
if first:
|
|
|
|
first = false
|
|
|
|
else:
|
|
|
|
curStrLit &= ","
|
|
|
|
curStrLit &= $field.name & "="
|
|
|
|
var tmp = newNimNode(nnkInfix).add(newIdentNode("&"),
|
|
|
|
newStrLitNode(curStrLit))
|
|
|
|
curInfix.add(tmp)
|
|
|
|
curInfix = tmp
|
|
|
|
tmp = newNimNode(nnkInfix).add(newIdentNode("&"),
|
|
|
|
newCall("safeTagUri", newCall("yamlTag",
|
|
|
|
newCall("type", field.t))))
|
|
|
|
curInfix.add(tmp)
|
|
|
|
curInfix = tmp
|
|
|
|
curStrLit = ""
|
|
|
|
curInfix.add(newStrLitNode(curStrLit & ")"))
|
|
|
|
tUri = tUri[0]
|
|
|
|
else:
|
|
|
|
assert false
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
# yamlTag()
|
|
|
|
|
|
|
|
var yamlTagProc = newProc(newIdentNode("yamlTag"), [
|
|
|
|
newIdentNode("TagId"),
|
|
|
|
newIdentDefs(newIdentNode("T"), newNimNode(nnkBracketExpr).add(
|
|
|
|
newIdentNode("typedesc"), tIdent))])
|
2016-01-05 15:54:14 +00:00
|
|
|
var impl = newStmtList(newCall("lazyLoadTag", tUri))
|
2016-01-04 20:46:33 +00:00
|
|
|
yamlTagProc[6] = impl
|
|
|
|
result.add(yamlTagProc)
|
|
|
|
|
2016-01-26 19:51:21 +00:00
|
|
|
# constructObject()
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-26 19:51:21 +00:00
|
|
|
var constructProc = newProc(newIdentNode("constructObject"), [
|
2015-12-29 14:09:37 +00:00
|
|
|
newEmptyNode(),
|
2015-12-29 15:10:47 +00:00
|
|
|
newIdentDefs(newIdentNode("s"), newIdentNode("YamlStream")),
|
2016-01-28 21:29:26 +00:00
|
|
|
newIdentDefs(newIdentNode("c"),
|
|
|
|
newIdentNode("ConstructionContext")),
|
2015-12-29 14:09:37 +00:00
|
|
|
newIdentDefs(newIdentNode("result"),
|
|
|
|
newNimNode(nnkVarTy).add(tIdent))])
|
2016-01-24 19:38:30 +00:00
|
|
|
constructProc[4] = newNimNode(nnkPragma).add(
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("raises"),
|
|
|
|
newNimNode(nnkBracket).add(
|
|
|
|
newIdentNode("YamlConstructionError"),
|
2016-02-12 18:53:25 +00:00
|
|
|
newIdentNode("YamlStreamError"))))
|
2016-01-04 20:46:33 +00:00
|
|
|
impl = quote do:
|
2015-12-29 14:09:37 +00:00
|
|
|
var event = s()
|
|
|
|
if finished(s) or event.kind != yamlStartMap:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError, "Expected map start")
|
2016-01-04 20:46:33 +00:00
|
|
|
if event.mapTag != yTagQuestionMark and
|
|
|
|
event.mapTag != yamlTag(type(`tIdent`)):
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Wrong tag for " & `tName`)
|
2015-12-29 14:09:37 +00:00
|
|
|
event = s()
|
2016-01-24 19:38:30 +00:00
|
|
|
assert(not finished(s))
|
2015-12-29 14:09:37 +00:00
|
|
|
while event.kind != yamlEndMap:
|
|
|
|
assert event.kind == yamlScalar
|
2016-01-04 20:46:33 +00:00
|
|
|
assert event.scalarTag in [yTagQuestionMark, yTagString]
|
2016-01-24 20:21:37 +00:00
|
|
|
case event.scalarContent
|
2015-12-29 14:09:37 +00:00
|
|
|
else:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Unknown key for " & `tName` & ": " &
|
|
|
|
event.scalarContent)
|
2015-12-29 14:09:37 +00:00
|
|
|
event = s()
|
2016-01-24 19:38:30 +00:00
|
|
|
assert(not finished(s))
|
2016-01-05 17:14:11 +00:00
|
|
|
var keyCase = impl[5][1][2]
|
2015-12-29 14:09:37 +00:00
|
|
|
assert keyCase.kind == nnkCaseStmt
|
|
|
|
for field in objectFields(recList):
|
|
|
|
keyCase.insert(1, newNimNode(nnkOfBranch).add(
|
2016-01-24 20:21:37 +00:00
|
|
|
newStrLitNode($field.name.ident)).add(newStmtList(
|
2016-01-26 19:51:21 +00:00
|
|
|
newCall("constructObject", [newIdentNode("s"),
|
2016-01-28 21:29:26 +00:00
|
|
|
newIdentNode("c"),
|
2016-01-26 19:51:21 +00:00
|
|
|
newDotExpr(newIdentNode("result"), field.name)])
|
2015-12-29 14:09:37 +00:00
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
constructProc[6] = impl
|
|
|
|
result.add(constructProc)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
# representObject()
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
var representProc = newProc(newIdentNode("representObject"), [
|
2016-02-12 18:53:25 +00:00
|
|
|
newIdentNode("RawYamlStream"),
|
2015-12-29 15:10:47 +00:00
|
|
|
newIdentDefs(newIdentNode("value"), tIdent),
|
2016-01-28 20:59:26 +00:00
|
|
|
newIdentDefs(newIdentNode("ts"),
|
|
|
|
newIdentNode("TagStyle")),
|
|
|
|
newIdentDefs(newIdentNode("c"),
|
|
|
|
newIdentNode("SerializationContext"))])
|
2016-02-02 17:19:40 +00:00
|
|
|
representProc[4] = newNimNode(nnkPragma).add(
|
2016-01-24 19:38:30 +00:00
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("raises"),
|
|
|
|
newNimNode(nnkBracket)))
|
2016-01-04 21:18:55 +00:00
|
|
|
var iterBody = newStmtList(
|
|
|
|
newLetStmt(newIdentNode("childTagStyle"), newNimNode(nnkIfExpr).add(
|
|
|
|
newNimNode(nnkElifExpr).add(
|
|
|
|
newNimNode(nnkInfix).add(newIdentNode("=="),
|
2016-01-28 20:59:26 +00:00
|
|
|
newIdentNode("ts"), newIdentNode("tsRootOnly")),
|
2016-01-14 21:51:30 +00:00
|
|
|
newIdentNode("tsNone")
|
2016-01-28 20:59:26 +00:00
|
|
|
), newNimNode(nnkElseExpr).add(newIdentNode("ts")))),
|
2016-01-04 21:18:55 +00:00
|
|
|
newNimNode(nnkYieldStmt).add(
|
|
|
|
newNimNode(nnkObjConstr).add(newIdentNode("YamlStreamEvent"),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("kind"),
|
|
|
|
newIdentNode("yamlStartMap")),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("mapTag"),
|
|
|
|
newNimNode(nnkIfExpr).add(newNimNode(nnkElifExpr).add(
|
|
|
|
newNimNode(nnkInfix).add(newIdentNode("=="),
|
2016-01-28 20:59:26 +00:00
|
|
|
newIdentNode("ts"),
|
2016-01-14 21:51:30 +00:00
|
|
|
newIdentNode("tsNone")),
|
2016-01-04 21:18:55 +00:00
|
|
|
newIdentNode("yTagQuestionMark")
|
|
|
|
), newNimNode(nnkElseExpr).add(
|
|
|
|
newCall("yamlTag", newCall("type", tIdent))
|
|
|
|
))),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("mapAnchor"),
|
|
|
|
newIdentNode("yAnchorNone"))
|
|
|
|
)
|
2016-01-04 20:46:33 +00:00
|
|
|
), newNimNode(nnkYieldStmt).add(newNimNode(nnkObjConstr).add(
|
|
|
|
newIdentNode("YamlStreamEvent"), newNimNode(nnkExprColonExpr).add(
|
|
|
|
newIdentNode("kind"), newIdentNode("yamlEndMap")
|
|
|
|
)
|
|
|
|
)))
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-04 21:18:55 +00:00
|
|
|
var i = 2
|
2015-12-29 15:10:47 +00:00
|
|
|
for field in objectFields(recList):
|
|
|
|
let
|
|
|
|
fieldIterIdent = newIdentNode($field.name & "Events")
|
|
|
|
fieldNameString = newStrLitNode($field.name)
|
|
|
|
iterbody.insert(i, quote do:
|
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
2016-01-04 21:18:55 +00:00
|
|
|
scalarTag: presentTag(string,
|
|
|
|
childTagStyle),
|
2015-12-29 15:10:47 +00:00
|
|
|
scalarAnchor: yAnchorNone,
|
|
|
|
scalarContent: `fieldNameString`)
|
|
|
|
)
|
|
|
|
iterbody.insert(i + 1, newVarStmt(fieldIterIdent,
|
2016-02-02 17:19:40 +00:00
|
|
|
newCall("representObject", newDotExpr(newIdentNode("value"),
|
2016-01-28 20:59:26 +00:00
|
|
|
field.name), newIdentNode("childTagStyle"),
|
|
|
|
newIdentNode("c"))))
|
2015-12-29 15:10:47 +00:00
|
|
|
iterbody.insert(i + 2, quote do:
|
|
|
|
for event in `fieldIterIdent`():
|
|
|
|
yield event
|
|
|
|
)
|
|
|
|
i += 3
|
|
|
|
impl = newStmtList(newAssignment(newIdentNode("result"), newProc(
|
|
|
|
newEmptyNode(), [newIdentNode("YamlStreamEvent")], iterBody,
|
|
|
|
nnkIteratorDef)))
|
2016-02-02 17:19:40 +00:00
|
|
|
representProc[6] = impl
|
|
|
|
result.add(representProc)
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc safeTagUri*(id: TagId): string {.raises: [].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Internal function. Do not call explicitly.
|
2016-01-24 19:38:30 +00:00
|
|
|
try:
|
|
|
|
let uri = serializationTagLibrary.uri(id)
|
|
|
|
if uri.len > 0 and uri[0] == '!':
|
|
|
|
return uri[1..uri.len - 1]
|
|
|
|
else:
|
|
|
|
return uri
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you known)
|
|
|
|
assert(false)
|
2016-01-05 15:54:14 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
template constructScalarItem(bs: var YamlStream, item: YamlStreamEvent,
|
|
|
|
name: string, t: TagId, content: stmt) =
|
|
|
|
item = bs.next()
|
|
|
|
if item.kind != yamlScalar:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError, "Expected scalar")
|
|
|
|
if item.scalarTag notin [yTagQuestionMark, yTagExclamationMark, t]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for " & name)
|
|
|
|
try:
|
|
|
|
content
|
|
|
|
except YamlConstructionError:
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
var e = newException(YamlConstructionError,
|
|
|
|
"Cannot construct to " & name & ": " & item.scalarContent)
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
|
|
|
|
proc yamlTag*(T: typedesc[string]): TagId {.inline, raises: [].} = yTagString
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-24 19:38:30 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, "string", yTagString):
|
2016-01-24 19:38:30 +00:00
|
|
|
result = item.scalarContent
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*(value: string, ts: TagStyle = tsNone,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent(value, presentTag(string, ts), yAnchorNone)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-01-26 19:00:28 +00:00
|
|
|
proc yamlTag*(T: typedesc[int8]): TagId {.inline, raises: [].} = yTagNimInt8
|
|
|
|
proc yamlTag*(T: typedesc[int16]): TagId {.inline, raises: [].} = yTagNimInt16
|
|
|
|
proc yamlTag*(T: typedesc[int32]): TagId {.inline, raises: [].} = yTagNimInt32
|
|
|
|
proc yamlTag*(T: typedesc[int64]): TagId {.inline, raises: [].} = yTagNimInt64
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: int8|int16|int32|int64](
|
2016-02-12 18:53:25 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-24 19:38:30 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, name(T), yamlTag(T)):
|
2016-01-26 19:00:28 +00:00
|
|
|
result = T(parseBiggestInt(item.scalarContent))
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
template constructObject*(s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var int) =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".}
|
|
|
|
discard
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[T: int8|int16|int32|int64](
|
2016-01-28 20:59:26 +00:00
|
|
|
value: T, ts: TagStyle = tsNone, c: SerializationContext):
|
2016-02-12 18:53:25 +00:00
|
|
|
RawYamlStream {.raises: [].} =
|
2016-01-26 19:00:28 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent($value, presentTag(T, ts), yAnchorNone)
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
template representObject*(value: int, tagStyle: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".}
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc yamlTag*(T: typedesc[uint8]): TagId {.inline, raises: [].} = yTagNimUInt8
|
|
|
|
proc yamlTag*(T: typedesc[uint16]): TagId {.inline, raises: [].} = yTagNimUInt16
|
|
|
|
proc yamlTag*(T: typedesc[uint32]): TagId {.inline, raises: [].} = yTagNimUInt32
|
|
|
|
proc yamlTag*(T: typedesc[uint64]): TagId {.inline, raises: [].} = yTagNimUInt64
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-26 19:00:28 +00:00
|
|
|
{.push overflowChecks: on.}
|
|
|
|
proc parseBiggestUInt(s: string): uint64 =
|
|
|
|
result = 0
|
|
|
|
for c in s:
|
|
|
|
if c in {'0'..'9'}:
|
|
|
|
result *= 10.uint64 + (uint64(c) - uint64('0'))
|
|
|
|
elif c == '_':
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Invalid char in uint: " & c)
|
|
|
|
{.pop.}
|
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: uint8|uint16|uint32|uint64](
|
2016-02-12 18:53:25 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-24 19:38:30 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, name[T], yamlTag(T)):
|
2016-01-26 19:00:28 +00:00
|
|
|
result = T(parseBiggestUInt(item.scalarContent))
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
template constructObject*(s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var uint) =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal:
|
|
|
|
"The length of `uint` is platform dependent. Use uint[8|16|32|64].".}
|
|
|
|
discard
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[T: uint8|uint16|uint32|uint64](
|
2016-01-28 20:59:26 +00:00
|
|
|
value: T, ts: TagStyle, c: SerializationContext):
|
2016-02-12 18:53:25 +00:00
|
|
|
RawYamlStream {.raises: [].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent($value, presentTag(T, ts), yAnchorNone)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
template representObject*(value: uint, ts: TagStyle, c: SerializationContext):
|
|
|
|
RawYamlStream =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal:
|
|
|
|
"The length of `uint` is platform dependent. Use uint[8|16|32|64].".}
|
|
|
|
discard
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-26 19:00:28 +00:00
|
|
|
proc yamlTag*(T: typedesc[float32]): TagId {.inline, raises: [].} =
|
|
|
|
yTagNimFloat32
|
|
|
|
proc yamlTag*(T: typedesc[float64]): TagId {.inline, raises: [].} =
|
|
|
|
yTagNimFloat64
|
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: float32|float64](
|
2016-02-12 18:53:25 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-24 19:38:30 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, name(T), yamlTag(T)):
|
2016-01-24 19:38:30 +00:00
|
|
|
let hint = guessType(item.scalarContent)
|
|
|
|
case hint
|
|
|
|
of yTypeFloat:
|
2016-01-26 19:00:28 +00:00
|
|
|
result = T(parseBiggestFloat(item.scalarContent))
|
2016-01-24 19:38:30 +00:00
|
|
|
of yTypeFloatInf:
|
|
|
|
if item.scalarContent[0] == '-':
|
|
|
|
result = NegInf
|
|
|
|
else:
|
|
|
|
result = Inf
|
|
|
|
of yTypeFloatNaN:
|
|
|
|
result = NaN
|
2015-12-29 14:09:37 +00:00
|
|
|
else:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to float: " & item.scalarContent)
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
template constructObject*(s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var float) =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal: "The length of `float` is platform dependent. Use float[32|64].".}
|
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[T: float32|float64](value: T, ts: TagStyle,
|
2016-01-28 20:59:26 +00:00
|
|
|
c: SerializationContext):
|
2016-02-12 18:53:25 +00:00
|
|
|
RawYamlStream {.raises: [].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-05 18:06:55 +00:00
|
|
|
var
|
|
|
|
asString: string
|
|
|
|
case value
|
|
|
|
of Inf:
|
|
|
|
asString = ".inf"
|
|
|
|
of NegInf:
|
|
|
|
asString = "-.inf"
|
|
|
|
of NaN:
|
|
|
|
asString = ".nan"
|
|
|
|
else:
|
|
|
|
asString = $value
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent(asString, presentTag(T, ts), yAnchorNone)
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
template representObject*(value: float, tagStyle: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream =
|
2016-01-26 19:00:28 +00:00
|
|
|
{.fatal: "The length of `float` is platform dependent. Use float[32|64].".}
|
2015-12-29 15:10:47 +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)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-24 19:38:30 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, "bool", yTagBoolean):
|
2016-01-24 19:38:30 +00:00
|
|
|
case guessType(item.scalarContent)
|
2016-01-05 15:54:14 +00:00
|
|
|
of yTypeBoolTrue:
|
|
|
|
result = true
|
|
|
|
of yTypeBoolFalse:
|
|
|
|
result = false
|
|
|
|
else:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to bool: " & item.scalarContent)
|
2016-01-05 15:54:14 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*(value: bool, ts: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent(if value: "y" else: "n", presentTag(bool, ts),
|
2016-01-14 18:58:38 +00:00
|
|
|
yAnchorNone)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-01-26 19:00:28 +00:00
|
|
|
proc yamlTag*(T: typedesc[char]): TagId {.inline, raises: [].} = yTagNimChar
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var char)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-01-26 19:00:28 +00:00
|
|
|
var item: YamlStreamEvent
|
2016-02-12 18:53:25 +00:00
|
|
|
constructScalarItem(s, item, "char", yTagNimChar):
|
2016-01-26 19:00:28 +00:00
|
|
|
if item.scalarContent.len != 1:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Cannot construct to char (length != 1): " &
|
|
|
|
item.scalarContent)
|
|
|
|
else:
|
|
|
|
result = item.scalarContent[0]
|
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*(value: char, ts: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2016-01-26 19:00:28 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
yield scalarEvent("" & value, presentTag(char, ts), 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-02-01 18:48:42 +00:00
|
|
|
let uri = "!nim:system:seq(" & safeTagUri(yamlTag(I)) & ")"
|
2016-01-04 20:46:33 +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-02-12 18:53:25 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSequence:
|
2016-01-24 19:38:30 +00:00
|
|
|
raise newException(YamlConstructionError, "Expected sequence start")
|
|
|
|
if event.seqTag notin [yTagQuestionMark, yamlTag(seq[T])]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for seq[T]")
|
2015-12-29 14:09:37 +00:00
|
|
|
result = newSeq[T]()
|
2016-02-12 18:53:25 +00:00
|
|
|
while s.peek().kind != yamlEndSequence:
|
|
|
|
var item: T
|
|
|
|
try: constructObject(s, c, item)
|
2016-02-01 18:48:42 +00:00
|
|
|
except AssertionError, YamlConstructionError,
|
2016-02-12 18:53:25 +00:00
|
|
|
YamlStreamError: raise
|
2016-01-24 19:38:30 +00:00
|
|
|
except:
|
|
|
|
# compiler bug: https://github.com/nim-lang/Nim/issues/3772
|
|
|
|
assert(false)
|
2015-12-29 14:09:37 +00:00
|
|
|
result.add(item)
|
2016-02-12 18:53:25 +00:00
|
|
|
discard s.next()
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[T](value: seq[T], ts: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
2016-01-04 20:46:33 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlStartSequence,
|
2016-01-26 19:51:21 +00:00
|
|
|
seqTag: presentTag(seq[T], ts),
|
2015-12-29 15:10:47 +00:00
|
|
|
seqAnchor: yAnchorNone)
|
|
|
|
for item in value:
|
2016-02-02 17:19:40 +00:00
|
|
|
var events = representObject(item, childTagStyle, c)
|
2016-02-12 18:53:25 +00:00
|
|
|
while true:
|
|
|
|
let event = events()
|
|
|
|
if finished(events): break
|
2015-12-29 15:10:47 +00:00
|
|
|
yield event
|
|
|
|
yield YamlStreamEvent(kind: yamlEndSequence)
|
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*[K, V](T: typedesc[Table[K, V]]): TagId {.inline, raises: [].} =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
keyUri = serializationTagLibrary.uri(yamlTag(K))
|
|
|
|
valueUri = serializationTagLibrary.uri(yamlTag(V))
|
|
|
|
keyIdent = if keyUri[0] == '!': keyUri[1..keyUri.len - 1] else:
|
|
|
|
keyUri
|
|
|
|
valueIdent = if valueUri[0] == '!':
|
|
|
|
valueUri[1..valueUri.len - 1] else: valueUri
|
2016-02-01 18:48:42 +00:00
|
|
|
uri = "!nim:tables:Table(" & keyUri & "," & valueUri & ")"
|
2016-01-24 19:38:30 +00:00
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you known)
|
|
|
|
assert(false)
|
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-02-12 18:53:25 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
let event = s.next()
|
2016-02-01 18:48:42 +00:00
|
|
|
if event.kind != yamlStartMap:
|
|
|
|
raise newException(YamlConstructionError, "Expected map start, got " &
|
|
|
|
$event.kind)
|
2016-01-24 19:38:30 +00:00
|
|
|
if event.mapTag notin [yTagQuestionMark, yamlTag(Table[K, V])]:
|
|
|
|
raise newException(YamlConstructionError, "Wrong tag for Table[K, V]")
|
2015-12-29 14:09:37 +00:00
|
|
|
result = initTable[K, V]()
|
2016-02-12 18:53:25 +00:00
|
|
|
while s.peek.kind != yamlEndMap:
|
2015-12-29 14:09:37 +00:00
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
2016-01-24 19:38:30 +00:00
|
|
|
try:
|
2016-02-12 18:53:25 +00:00
|
|
|
constructObject(s, c, key)
|
2016-01-28 21:29:26 +00:00
|
|
|
constructObject(s, c, value)
|
2016-01-26 19:51:21 +00:00
|
|
|
except AssertionError: raise
|
2016-01-24 19:38:30 +00:00
|
|
|
except Exception:
|
|
|
|
# compiler bug: https://github.com/nim-lang/Nim/issues/3772
|
|
|
|
assert(false)
|
2015-12-29 14:09:37 +00:00
|
|
|
result[key] = value
|
2016-02-12 18:53:25 +00:00
|
|
|
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-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises:[].} =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-26 19:51:21 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
2016-01-04 21:18:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlStartMap,
|
2016-01-26 19:51:21 +00:00
|
|
|
mapTag: presentTag(Table[K, V], ts),
|
2015-12-29 15:10:47 +00:00
|
|
|
mapAnchor: yAnchorNone)
|
|
|
|
for key, value in value.pairs:
|
2016-02-02 17:19:40 +00:00
|
|
|
var events = representObject(key, childTagStyle, c)
|
2016-02-12 18:53:25 +00:00
|
|
|
while true:
|
|
|
|
let event = events()
|
|
|
|
if finished(events): break
|
2015-12-29 15:10:47 +00:00
|
|
|
yield event
|
2016-02-02 17:19:40 +00:00
|
|
|
events = representObject(value, childTagStyle, c)
|
2016-02-12 18:53:25 +00:00
|
|
|
while true:
|
|
|
|
let event = events()
|
|
|
|
if finished(events): break
|
2015-12-29 15:10:47 +00:00
|
|
|
yield event
|
2015-12-29 17:22:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlEndMap)
|
|
|
|
|
2016-02-01 19:16:35 +00:00
|
|
|
template yamlTag*(T: typedesc[object|enum]): expr =
|
2016-02-01 18:48:42 +00:00
|
|
|
var uri = when compiles(yamlTagId(T)): yamlTagId(T) else:
|
2016-02-12 18:53:25 +00:00
|
|
|
"!nim:custom:" & (typetraits.name(type(T)))
|
2016-02-01 18:48:42 +00:00
|
|
|
try:
|
|
|
|
serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError:
|
|
|
|
serializationTagLibrary.registerUri(uri)
|
|
|
|
|
|
|
|
template yamlTag*(T: typedesc[tuple]): expr =
|
|
|
|
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-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[O](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var ref O)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
|
|
|
|
proc constructObject*[O: object|tuple](s: var YamlStream,
|
|
|
|
c: ConstructionContext,
|
|
|
|
result: var O)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
let e = s.next()
|
2016-02-01 18:48:42 +00:00
|
|
|
if e.kind != yamlStartMap:
|
|
|
|
raise newException(YamlConstructionError, "Expected map start, got " &
|
|
|
|
$e.kind)
|
|
|
|
if e.mapAnchor != yAnchorNone:
|
|
|
|
raise newException(YamlConstructionError, "Anchor on a non-ref type")
|
2016-02-12 18:53:25 +00:00
|
|
|
while s.peek.kind != yamlEndMap:
|
|
|
|
let e = s.next()
|
2016-02-01 18:48:42 +00:00
|
|
|
if e.kind != yamlScalar:
|
2016-02-12 18:53:25 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Expected field name, got " & $e.kind)
|
2016-02-01 18:48:42 +00:00
|
|
|
let name = e.scalarContent
|
|
|
|
for fname, value in fieldPairs(result):
|
|
|
|
if fname == name:
|
|
|
|
constructObject(s, c, value)
|
|
|
|
break
|
2016-02-12 18:53:25 +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-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2016-02-01 18:48:42 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
yield startMapEvent(presentTag(O, ts), yAnchorNone)
|
|
|
|
for name, value in fieldPairs(value):
|
|
|
|
yield scalarEvent(name, presentTag(string, childTagStyle),
|
|
|
|
yAnchorNone)
|
2016-02-02 17:19:40 +00:00
|
|
|
var events = representObject(value, childTagStyle, c)
|
2016-02-12 18:53:25 +00:00
|
|
|
while true:
|
|
|
|
let event = events()
|
|
|
|
if finished(events): break
|
2016-02-01 18:48:42 +00:00
|
|
|
yield event
|
|
|
|
yield endMapEvent()
|
|
|
|
|
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-02-12 18:53:25 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
let e = s.next()
|
2016-02-01 19:16:35 +00:00
|
|
|
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-02 17:19:40 +00:00
|
|
|
proc representObject*[O: enum](value: O, ts: TagStyle,
|
2016-02-12 18:53:25 +00:00
|
|
|
c: SerializationContext): RawYamlStream {.raises: [].} =
|
2016-02-01 19:16:35 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
yield scalarEvent($value, presentTag(O, ts), yAnchorNone)
|
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[O](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var ref O) =
|
|
|
|
var e = s.peek()
|
2016-01-28 20:59:26 +00:00
|
|
|
if e.kind == yamlScalar:
|
|
|
|
if e.scalarTag == yTagNull or (
|
|
|
|
e.scalarTag in [yTagQuestionMark, yTagExclamationMark] and
|
|
|
|
guessType(e.scalarContent) == yTypeNull):
|
|
|
|
result = nil
|
2016-02-12 18:53:25 +00:00
|
|
|
discard s.next()
|
2016-01-28 20:59:26 +00:00
|
|
|
return
|
2016-01-28 21:29:26 +00:00
|
|
|
elif e.kind == yamlAlias:
|
|
|
|
try:
|
|
|
|
result = cast[ref O](c.refs[e.aliasTarget])
|
2016-02-12 18:53:25 +00:00
|
|
|
discard s.next()
|
2016-01-28 21:29:26 +00:00
|
|
|
return
|
|
|
|
except KeyError:
|
|
|
|
assert(false)
|
2016-01-28 20:59:26 +00:00
|
|
|
new(result)
|
2016-02-12 18:53:25 +00:00
|
|
|
template removeAnchor(anchor: var AnchorId) {.dirty.} =
|
|
|
|
if anchor != yAnchorNone:
|
|
|
|
assert(not c.refs.hasKey(anchor))
|
|
|
|
c.refs[anchor] = cast[pointer](result)
|
|
|
|
anchor = yAnchorNone
|
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
case e.kind
|
2016-02-12 18:53:25 +00:00
|
|
|
of yamlScalar: removeAnchor(e.scalarAnchor)
|
|
|
|
of yamlStartMap: removeAnchor(e.mapAnchor)
|
|
|
|
of yamlStartSequence: removeAnchor(e.seqAnchor)
|
2016-01-28 21:29:26 +00:00
|
|
|
else: assert(false)
|
2016-02-12 18:53:25 +00:00
|
|
|
s.peek = e
|
2016-01-28 20:59:26 +00:00
|
|
|
try:
|
2016-02-12 18:53:25 +00:00
|
|
|
constructObject(s, c, result[])
|
|
|
|
except YamlConstructionError, YamlStreamError, AssertionError:
|
2016-01-28 20:59:26 +00:00
|
|
|
raise
|
|
|
|
except Exception:
|
2016-02-12 18:53:25 +00:00
|
|
|
var e = newException(YamlStreamError,
|
2016-01-28 20:59:26 +00:00
|
|
|
getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[O](value: ref O, ts: TagStyle, c: SerializationContext):
|
2016-02-12 18:53:25 +00:00
|
|
|
RawYamlStream {.raises: [].} =
|
2016-01-28 20:59:26 +00:00
|
|
|
if value == nil:
|
2016-02-12 18:53:25 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
yield scalarEvent("~", yTagNull)
|
2016-01-28 20:59:26 +00:00
|
|
|
elif c.style == asNone:
|
2016-02-02 17:19:40 +00:00
|
|
|
result = representObject(value[], ts, c)
|
2016-01-28 20:59:26 +00:00
|
|
|
else:
|
|
|
|
let
|
|
|
|
p = cast[pointer](value)
|
|
|
|
for i in countup(0, c.refsList.high):
|
|
|
|
if p == c.refsList[i].p:
|
|
|
|
c.refsList[i].count.inc()
|
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
yield aliasEvent(if c.style == asAlways: AnchorId(i) else:
|
|
|
|
cast[AnchorId](p))
|
|
|
|
return
|
|
|
|
c.refsList.add(initRefNodeData(p))
|
|
|
|
let a = if c.style == asAlways: AnchorId(c.refsList.high) else:
|
|
|
|
cast[AnchorId](p)
|
2016-02-12 18:53:25 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
var child = representObject(value[], ts, c)
|
|
|
|
var first = child()
|
|
|
|
assert(not finished(child))
|
|
|
|
case first.kind
|
2016-01-28 20:59:26 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
first.mapAnchor = a
|
|
|
|
of yamlStartSequence:
|
|
|
|
first.seqAnchor = a
|
|
|
|
of yamlScalar:
|
|
|
|
first.scalarAnchor = a
|
2016-02-12 18:53:25 +00:00
|
|
|
else: discard
|
|
|
|
yield first
|
|
|
|
while true:
|
|
|
|
let event = child()
|
|
|
|
if finished(child): break
|
|
|
|
yield event
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc construct*[T](s: var YamlStream, target: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Construct a Nim value from a YAML stream.
|
2016-02-12 18:53:25 +00:00
|
|
|
var
|
|
|
|
context = newConstructionContext()
|
2016-01-26 19:51:21 +00:00
|
|
|
try:
|
2016-02-12 18:53:25 +00:00
|
|
|
var e = s.next()
|
|
|
|
assert(e.kind == yamlStartDocument)
|
2016-01-28 21:29:26 +00:00
|
|
|
|
|
|
|
constructObject(s, context, target)
|
2016-02-12 18:53:25 +00:00
|
|
|
e = s.next()
|
|
|
|
assert(e.kind == yamlEndDocument)
|
|
|
|
except YamlConstructionError, YamlStreamError, AssertionError:
|
2016-01-26 19:51:21 +00:00
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
# may occur while calling s()
|
2016-02-12 18:53:25 +00:00
|
|
|
var ex = newException(YamlStreamError, "")
|
2016-01-26 19:51:21 +00:00
|
|
|
ex.parent = getCurrentException()
|
|
|
|
raise ex
|
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc load*[K](input: Stream, target: var K)
|
2016-01-24 19:53:51 +00:00
|
|
|
{.raises: [YamlConstructionError, IOError, YamlParserError].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Load a Nim value from a YAML character stream.
|
2016-01-26 19:51:21 +00:00
|
|
|
var
|
|
|
|
parser = newYamlParser(serializationTagLibrary)
|
|
|
|
events = parser.parse(input)
|
2016-01-24 19:53:51 +00:00
|
|
|
try:
|
|
|
|
construct(events, target)
|
2016-01-26 19:51:21 +00:00
|
|
|
except YamlConstructionError, AssertionError:
|
2016-01-24 19:53:51 +00:00
|
|
|
raise
|
2016-02-12 18:53:25 +00:00
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlStreamError)(getCurrentException())
|
2016-01-24 20:38:29 +00:00
|
|
|
if e.parent of IOError:
|
2016-01-26 19:51:21 +00:00
|
|
|
raise (ref IOError)(e.parent)
|
2016-01-24 20:38:29 +00:00
|
|
|
elif e.parent of YamlParserError:
|
2016-01-26 19:51:21 +00:00
|
|
|
raise (ref YamlParserError)(e.parent)
|
2016-01-24 19:53:51 +00:00
|
|
|
else:
|
|
|
|
assert(false)
|
|
|
|
except Exception:
|
|
|
|
# compiler bug: https://github.com/nim-lang/Nim/issues/3772
|
|
|
|
assert(false)
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
proc setAnchor(a: var AnchorId, q: var seq[RefNodeData], n: var AnchorId)
|
|
|
|
{.inline.} =
|
|
|
|
if a != yAnchorNone:
|
|
|
|
let p = cast[pointer](a)
|
|
|
|
for i in countup(0, q.len - 1):
|
|
|
|
if p == q[i].p:
|
|
|
|
if q[i].count > 1:
|
|
|
|
assert(q[i].anchor == yAnchorNone)
|
|
|
|
q[i].anchor = n
|
|
|
|
a = n
|
|
|
|
n = AnchorId(int(n) + 1)
|
|
|
|
else:
|
|
|
|
a = yAnchorNone
|
|
|
|
break
|
|
|
|
|
|
|
|
proc setAliasAnchor(a: var AnchorId, q: var seq[RefNodeData]) {.inline.} =
|
|
|
|
let p = cast[pointer](a)
|
|
|
|
for i in countup(0, q.len - 1):
|
|
|
|
if p == q[i].p:
|
|
|
|
assert q[i].count > 1
|
|
|
|
assert q[i].anchor != yAnchorNone
|
|
|
|
a = q[i].anchor
|
|
|
|
return
|
|
|
|
assert(false)
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc represent*[T](value: T, ts: TagStyle = tsRootOnly,
|
2016-01-28 20:59:26 +00:00
|
|
|
a: AnchorStyle = asTidy): YamlStream {.raises: [].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Represent a Nim value as ``YamlStream``.
|
2016-01-28 20:59:26 +00:00
|
|
|
var
|
|
|
|
context = newSerializationContext(a)
|
2016-02-12 18:53:25 +00:00
|
|
|
objStream = iterator(): YamlStreamEvent =
|
|
|
|
yield YamlStreamEvent(kind: yamlStartDocument)
|
|
|
|
var events = representObject(value, ts, context)
|
|
|
|
while true:
|
|
|
|
let e = events()
|
|
|
|
if finished(events): break
|
|
|
|
yield e
|
|
|
|
yield YamlStreamEvent(kind: yamlEndDocument)
|
2016-01-28 20:59:26 +00:00
|
|
|
if a == asTidy:
|
|
|
|
var objQueue = newSeq[YamlStreamEvent]()
|
|
|
|
try:
|
|
|
|
for event in objStream():
|
|
|
|
objQueue.add(event)
|
|
|
|
except Exception:
|
|
|
|
assert(false)
|
|
|
|
var next = 0.AnchorId
|
2016-02-12 18:53:25 +00:00
|
|
|
var backend = iterator(): YamlStreamEvent =
|
2016-01-28 20:59:26 +00:00
|
|
|
for i in countup(0, objQueue.len - 1):
|
|
|
|
var event = objQueue[i]
|
|
|
|
case event.kind
|
|
|
|
of yamlStartMap:
|
|
|
|
event.mapAnchor.setAnchor(context.refsList, next)
|
|
|
|
of yamlStartSequence:
|
|
|
|
event.seqAnchor.setAnchor(context.refsList, next)
|
|
|
|
of yamlScalar:
|
|
|
|
event.scalarAnchor.setAnchor(context.refsList, next)
|
|
|
|
of yamlAlias:
|
|
|
|
event.aliasTarget.setAliasAnchor(context.refsList)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
yield event
|
2016-02-12 18:53:25 +00:00
|
|
|
result = initYamlStream(backend)
|
2016-01-28 20:59:26 +00:00
|
|
|
else:
|
2016-02-12 18:53:25 +00:00
|
|
|
result = initYamlStream(objStream)
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-01-26 19:51:21 +00:00
|
|
|
proc dump*[K](value: K, target: Stream, style: PresentationStyle = psDefault,
|
2016-01-28 20:59:26 +00:00
|
|
|
tagStyle: TagStyle = tsRootOnly,
|
|
|
|
anchorStyle: AnchorStyle = asTidy, indentationStep: int = 2)
|
2016-01-26 19:51:21 +00:00
|
|
|
{.raises: [YamlPresenterJsonError, YamlPresenterOutputError].} =
|
2016-02-12 19:44:38 +00:00
|
|
|
## Dump a Nim value as YAML character stream.
|
2016-02-02 17:19:40 +00:00
|
|
|
var events = represent(value, if style == psCanonical: tsAll else: tagStyle,
|
2016-01-28 20:59:26 +00:00
|
|
|
if style == psJson: asNone else: anchorStyle)
|
2016-01-24 19:38:30 +00:00
|
|
|
try:
|
|
|
|
present(events, target, serializationTagLibrary, style, indentationStep)
|
2016-02-12 18:53:25 +00:00
|
|
|
except YamlStreamError:
|
2016-01-24 19:38:30 +00:00
|
|
|
# serializing object does not raise any errors, so we can ignore this
|
2016-01-28 20:59:26 +00:00
|
|
|
var e = getCurrentException()
|
2016-01-26 19:51:21 +00:00
|
|
|
assert(false)
|
2016-02-12 18:53:25 +00:00
|
|
|
except YamlPresenterJsonError, YamlPresenterOutputError, AssertionError, FieldError:
|
2016-01-26 19:51:21 +00:00
|
|
|
raise
|
|
|
|
except Exception:
|
2016-02-02 17:19:40 +00:00
|
|
|
# cannot occur as represent() doesn't raise any errors
|
2016-01-24 19:38:30 +00:00
|
|
|
assert(false)
|