2015-12-28 21:24:05 +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-01-24 10:44:10 +00:00
|
|
|
proc `$`*(id: TagId): string =
|
2016-04-02 15:48:22 +00:00
|
|
|
case id
|
|
|
|
of yTagQuestionMark: "?"
|
|
|
|
of yTagExclamationMark: "!"
|
|
|
|
of yTagString: "!!str"
|
|
|
|
of yTagSequence: "!!seq"
|
|
|
|
of yTagMapping: "!!map"
|
|
|
|
of yTagNull: "!!null"
|
|
|
|
of yTagBoolean: "!!bool"
|
|
|
|
of yTagInteger: "!!int"
|
|
|
|
of yTagFloat: "!!float"
|
|
|
|
of yTagOrderedMap: "!!omap"
|
|
|
|
of yTagPairs: "!!pairs"
|
|
|
|
of yTagSet: "!!set"
|
|
|
|
of yTagBinary: "!!binary"
|
|
|
|
of yTagMerge: "!!merge"
|
|
|
|
of yTagTimestamp: "!!timestamp"
|
|
|
|
of yTagValue: "!!value"
|
|
|
|
of yTagYaml: "!!yaml"
|
|
|
|
of yTagNimField: "!nim:field"
|
|
|
|
else: "<" & $cast[int](id) & ">"
|
2016-01-24 10:44:10 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc initTagLibrary*(): TagLibrary =
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
|
|
|
result.tags = initTable[string, TagId]()
|
|
|
|
result.secondaryPrefix = yamlTagRepositoryPrefix
|
|
|
|
result.nextCustomTagId = yFirstCustomTagId
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-03-17 17:44:35 +00:00
|
|
|
proc registerUri*(tagLib: TagLibrary, uri: string): TagId =
|
2016-04-02 15:48:22 +00:00
|
|
|
tagLib.tags[uri] = tagLib.nextCustomTagId
|
|
|
|
result = tagLib.nextCustomTagId
|
|
|
|
tagLib.nextCustomTagId = cast[TagId](cast[int](tagLib.nextCustomTagId) + 1)
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc uri*(tagLib: TagLibrary, id: TagId): string =
|
2016-04-02 15:48:22 +00:00
|
|
|
for iUri, iId in tagLib.tags.pairs:
|
|
|
|
if iId == id: return iUri
|
|
|
|
raise newException(KeyError, "Unknown tag id: " & $id)
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc initFailsafeTagLibrary(): TagLibrary =
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initTagLibrary()
|
|
|
|
result.tags["!"] = yTagExclamationMark
|
|
|
|
result.tags["?"] = yTagQuestionMark
|
|
|
|
result.tags["tag:yaml.org,2002:str"] = yTagString
|
|
|
|
result.tags["tag:yaml.org,2002:seq"] = yTagSequence
|
|
|
|
result.tags["tag:yaml.org,2002:map"] = yTagMapping
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc initCoreTagLibrary(): TagLibrary =
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initFailsafeTagLibrary()
|
|
|
|
result.tags["tag:yaml.org,2002:null"] = yTagNull
|
|
|
|
result.tags["tag:yaml.org,2002:bool"] = yTagBoolean
|
|
|
|
result.tags["tag:yaml.org,2002:int"] = yTagInteger
|
|
|
|
result.tags["tag:yaml.org,2002:float"] = yTagFloat
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc initExtendedTagLibrary(): TagLibrary =
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initCoreTagLibrary()
|
|
|
|
result.tags["tag:yaml.org,2002:omap"] = yTagOrderedMap
|
|
|
|
result.tags["tag:yaml.org,2002:pairs"] = yTagPairs
|
|
|
|
result.tags["tag:yaml.org,2002:binary"] = yTagBinary
|
|
|
|
result.tags["tag:yaml.org,2002:merge"] = yTagMerge
|
|
|
|
result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp
|
|
|
|
result.tags["tag:yaml.org,2002:value"] = yTagValue
|
|
|
|
result.tags["tag:yaml.org,2002:yaml"] = yTagYaml
|
2016-03-20 18:55:23 +00:00
|
|
|
|
|
|
|
proc initSerializationTagLibrary(): TagLibrary =
|
2016-04-02 15:48:22 +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-04-04 19:21:24 +00:00
|
|
|
result.tags["!nim:field"] = yTagNimField
|
|
|
|
result.tags["!nim:nil:string"] = yTagNimNilString
|
|
|
|
result.tags["!nim:nil:seq"] = yTagNimNilSeq
|