2016-09-20 19:53:38 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2016-09-21 19:20:57 +00:00
|
|
|
# (c) Copyright 2016 Felix Krause
|
2016-09-20 19:53:38 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## ==================
|
|
|
|
## Module yaml.taglib
|
|
|
|
## ==================
|
|
|
|
##
|
|
|
|
## The taglib API enables you to query real names of tags emitted by the parser
|
|
|
|
## and create own tags. It also enables you to define tags for types used with
|
|
|
|
## the serialization API.
|
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
import tables, macros, hashes
|
2020-11-03 20:17:31 +00:00
|
|
|
import data
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TagLibrary* = ref object
|
|
|
|
## A ``TagLibrary`` maps tag URIs to ``TagId`` s.
|
|
|
|
##
|
|
|
|
## When `YamlParser <#YamlParser>`_ encounters tags not existing in the
|
|
|
|
## tag library, it will use
|
|
|
|
## `registerUri <#registerUri,TagLibrary,string>`_ to add
|
|
|
|
## the tag to the library.
|
|
|
|
##
|
|
|
|
## You can base your tag library on common tag libraries by initializing
|
|
|
|
## them with `initFailsafeTagLibrary <#initFailsafeTagLibrary>`_,
|
|
|
|
## `initCoreTagLibrary <#initCoreTagLibrary>`_ or
|
|
|
|
## `initExtendedTagLibrary <#initExtendedTagLibrary>`_.
|
2020-11-10 12:55:22 +00:00
|
|
|
tags*: Table[string, Tag]
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
proc initTagLibrary*(): TagLibrary {.raises: [].} =
|
|
|
|
## initializes the ``tags`` table and sets ``nextCustomTagId`` to
|
|
|
|
## ``yFirstCustomTagId``.
|
|
|
|
new(result)
|
2020-11-10 12:55:22 +00:00
|
|
|
result.tags = initTable[string, Tag]()
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
proc registerUri*(tagLib: TagLibrary, uri: string): Tag {.raises: [].} =
|
2016-09-20 19:53:38 +00:00
|
|
|
## registers a custom tag URI with a ``TagLibrary``. The URI will get
|
|
|
|
## the ``TagId`` ``nextCustomTagId``, which will be incremented.
|
2020-11-10 12:55:22 +00:00
|
|
|
result = uri.Tag
|
|
|
|
tagLib.tags[uri] = result
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-10-10 18:16:54 +00:00
|
|
|
template y(suffix: string): string = yamlTagRepositoryPrefix & suffix
|
|
|
|
template n(suffix: string): string = nimyamlTagRepositoryPrefix & suffix
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc initFailsafeTagLibrary*(): TagLibrary {.raises: [].} =
|
|
|
|
## Contains only:
|
|
|
|
## - ``!``
|
|
|
|
## - ``?``
|
|
|
|
## - ``!!str``
|
|
|
|
## - ``!!map``
|
|
|
|
## - ``!!seq``
|
|
|
|
result = initTagLibrary()
|
|
|
|
result.tags["!"] = yTagExclamationMark
|
|
|
|
result.tags["?"] = yTagQuestionMark
|
2016-10-10 18:16:54 +00:00
|
|
|
result.tags[y"str"] = yTagString
|
|
|
|
result.tags[y"seq"] = yTagSequence
|
|
|
|
result.tags[y"map"] = yTagMapping
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
proc initCoreTagLibrary*(): TagLibrary {.raises: [].} =
|
|
|
|
## Contains everything in ``initFailsafeTagLibrary`` plus:
|
|
|
|
## - ``!!null``
|
|
|
|
## - ``!!bool``
|
|
|
|
## - ``!!int``
|
|
|
|
## - ``!!float``
|
|
|
|
result = initFailsafeTagLibrary()
|
2016-10-10 18:16:54 +00:00
|
|
|
result.tags[y"null"] = yTagNull
|
|
|
|
result.tags[y"bool"] = yTagBoolean
|
|
|
|
result.tags[y"int"] = yTagInteger
|
|
|
|
result.tags[y"float"] = yTagFloat
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
proc initExtendedTagLibrary*(): TagLibrary {.raises: [].} =
|
|
|
|
## Contains everything from ``initCoreTagLibrary`` plus:
|
|
|
|
## - ``!!omap``
|
|
|
|
## - ``!!pairs``
|
|
|
|
## - ``!!set``
|
|
|
|
## - ``!!binary``
|
|
|
|
## - ``!!merge``
|
|
|
|
## - ``!!timestamp``
|
|
|
|
## - ``!!value``
|
|
|
|
## - ``!!yaml``
|
|
|
|
result = initCoreTagLibrary()
|
2016-10-10 18:16:54 +00:00
|
|
|
result.tags[y"omap"] = yTagOrderedMap
|
|
|
|
result.tags[y"pairs"] = yTagPairs
|
|
|
|
result.tags[y"binary"] = yTagBinary
|
|
|
|
result.tags[y"merge"] = yTagMerge
|
|
|
|
result.tags[y"timestamp"] = yTagTimestamp
|
|
|
|
result.tags[y"value"] = yTagValue
|
|
|
|
result.tags[y"yaml"] = yTagYaml
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
proc initSerializationTagLibrary*(): TagLibrary =
|
|
|
|
result = initTagLibrary()
|
|
|
|
result.tags["!"] = yTagExclamationMark
|
|
|
|
result.tags["?"] = yTagQuestionMark
|
2016-10-10 18:16:54 +00:00
|
|
|
result.tags[y"str"] = yTagString
|
|
|
|
result.tags[y"null"] = yTagNull
|
|
|
|
result.tags[y"bool"] = yTagBoolean
|
|
|
|
result.tags[y"float"] = yTagFloat
|
|
|
|
result.tags[y"timestamp"] = yTagTimestamp
|
|
|
|
result.tags[y"value"] = yTagValue
|
|
|
|
result.tags[y"binary"] = yTagBinary
|
|
|
|
result.tags[n"field"] = yTagNimField
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
var
|
2020-11-10 12:55:22 +00:00
|
|
|
serializationTagLibrary* {.compileTime.} = initSerializationTagLibrary() ## \
|
2016-09-20 19:53:38 +00:00
|
|
|
## 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
|
|
|
|
## `serializable <#serializable,stmt,stmt>`_.
|
|
|
|
|
|
|
|
var
|
|
|
|
registeredUris {.compileTime.} = newSeq[string]() ## \
|
|
|
|
## Since Table doesn't really work at compile time, we also store
|
|
|
|
## registered URIs here to be able to generate a static compiler error
|
|
|
|
## when the user tries to register an URI more than once.
|
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
template setTagUri*(t: typedesc, uri: string) =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Associate the given uri with a certain type. This uri is used as YAML tag
|
|
|
|
## when loading and dumping values of this type.
|
|
|
|
when uri in registeredUris:
|
|
|
|
{. fatal: "[NimYAML] URI \"" & uri & "\" registered twice!" .}
|
2020-11-10 12:55:22 +00:00
|
|
|
const tag {.genSym.} = uri.Tag
|
2016-09-20 19:53:38 +00:00
|
|
|
static:
|
|
|
|
registeredUris.add(uri)
|
2020-11-10 12:55:22 +00:00
|
|
|
serializationTagLibrary.tags[uri] = tag
|
|
|
|
proc yamlTag*(T: typedesc[t]): Tag {.inline, raises: [].} = tag
|
2016-09-20 19:53:38 +00:00
|
|
|
## autogenerated
|
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
template setTagUri*(t: typedesc, uri: string, idName: untyped) =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Like `setTagUri <#setTagUri.t,typedesc,string>`_, but lets
|
|
|
|
## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only
|
|
|
|
## necessary if you want to implement serialization / construction yourself.
|
|
|
|
when uri in registeredUris:
|
|
|
|
{. fatal: "[NimYAML] URI \"" & uri & "\" registered twice!" .}
|
2020-11-10 12:55:22 +00:00
|
|
|
const idName* = uri.Tag
|
2016-09-20 19:53:38 +00:00
|
|
|
static:
|
|
|
|
registeredUris.add(uri)
|
|
|
|
serializationTagLibrary.tags[uri] = idName
|
2020-11-10 12:55:22 +00:00
|
|
|
proc yamlTag*(T: typedesc[t]): Tag {.inline, raises: [].} = idName
|
2016-09-20 19:53:38 +00:00
|
|
|
## autogenerated
|
|
|
|
|
|
|
|
static:
|
|
|
|
# standard YAML tags used by serialization
|
|
|
|
registeredUris.add("!")
|
|
|
|
registeredUris.add("?")
|
2016-10-10 18:16:54 +00:00
|
|
|
registeredUris.add(y"str")
|
|
|
|
registeredUris.add(y"null")
|
|
|
|
registeredUris.add(y"bool")
|
|
|
|
registeredUris.add(y"float")
|
|
|
|
registeredUris.add(y"timestamp")
|
|
|
|
registeredUris.add(y"value")
|
|
|
|
registeredUris.add(y"binary")
|
2016-09-20 19:53:38 +00:00
|
|
|
# special tags used by serialization
|
2016-10-10 18:16:54 +00:00
|
|
|
registeredUris.add(n"field")
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
# tags for Nim's standard types
|
2016-10-10 18:16:54 +00:00
|
|
|
setTagUri(char, n"system:char", yTagNimChar)
|
|
|
|
setTagUri(int8, n"system:int8", yTagNimInt8)
|
|
|
|
setTagUri(int16, n"system:int16", yTagNimInt16)
|
|
|
|
setTagUri(int32, n"system:int32", yTagNimInt32)
|
|
|
|
setTagUri(int64, n"system:int64", yTagNimInt64)
|
|
|
|
setTagUri(uint8, n"system:uint8", yTagNimUInt8)
|
|
|
|
setTagUri(uint16, n"system:uint16", yTagNimUInt16)
|
|
|
|
setTagUri(uint32, n"system:uint32", yTagNimUInt32)
|
|
|
|
setTagUri(uint64, n"system:uint64", yTagNimUInt64)
|
|
|
|
setTagUri(float32, n"system:float32", yTagNimFloat32)
|
|
|
|
setTagUri(float64, n"system:float64", yTagNimFloat64)
|
|
|
|
|
|
|
|
proc nimTag*(suffix: string): string =
|
|
|
|
## prepends NimYAML's tag repository prefix to the given suffix. For example,
|
|
|
|
## ``nimTag("system:char")`` yields ``"tag:nimyaml.org,2016:system:char"``.
|
2018-10-03 17:49:05 +00:00
|
|
|
nimyamlTagRepositoryPrefix & suffix
|