diff --git a/index.html b/index.html index a366918..2a12225 100644 --- a/index.html +++ b/index.html @@ -164,11 +164,11 @@ right: *b var s = newFileStream("out.yaml", fmWrite) dump(mob, s, options = defineOptions(tagStyle = tsAll)) -s.close()
%YAML 1.2 +s.close()
YAML 1.2 --- !Mob -!!str level: !nim:system:int32 42 -!!str experience: !nim:system:int32 1800 -!!str drops: !Drops [!!str Sword of Mob Slaying]
code.nim | out.yaml |
---|---|
import yaml type Person = object @@ -214,13 +214,76 @@ right: *b |
code.nim | +in.yaml |
---|---|
import yaml +type Person = object + name: string + +setTagUriForType(Person, "!nim:demo:Person", + yTagPerson) + +var + s = newFileStream("in.yaml", fmRead) + context = newConstructionContext() + parser = newYamlParser(serializationTagLibrary) + events = parser.parse(s) + +assert events.next().kind == yamlStartDoc +assert events.next().kind == yamlStartSeq +var nextEvent = events.peek() +while nextEvent.kind != yamlEndSeq: + var curTag = nextEvent.tag() + if curTag == yTagQuestionMark: + # we only support implicitly tagged scalar events + assert nextEvent.kind == yamlScalar + case guessType(nextEvent.scalarContent) + of yTypeInteger: curTag = yTagInteger + of yTypeBoolTrue, yTypeBoolFalse: + curTag = yTagBoolean + of yTypeUnknown: curTag = yTagString + else: assert false, "Type not supported!" + elif curTag == yTagExclamationMark: + curTag = yTagString + case curTag + of yTagString: + var s: string + events.constructChild(context, s) + echo "got string: ", s + of yTagInteger: + var i: int32 + events.constructChild(context, i) + echo "got integer: ", i + of yTagBoolean: + var b: bool + events.constructChild(context, b) + echo "got boolean: ", b + else: + # non-standard tag ids are not available + # at compile time + if curTag == yTagPerson: + var p: Person + events.constructChild(context, p) + echo "got Person with name: ", p.name + else: assert false, "unsupported tag: " & $curTag + nextEvent = events.peek() +assert events.next().kind == yamlEndSeq +assert events.next().kind == yamlEndDoc +assert events.finished() +s.close() |
+%YAML 1.2 +--- !!seq +- this is a string +- 42 +- false +- !!str 23 +- !nim:demo:Person {name: Trillian} |
YamlStreamEventKind = enum @@ -515,7 +534,7 @@ class="link-seesrc" target="_blank">Source
TagId = distinct int
URI strings are mapped to TagId s for efficiency reasons (you do not need to compare strings every time) and to be able to discover unknown tag URIs early in the parsing process.
SourceAnchorId = distinct int
YamlStreamEvent = object @@ -563,7 +582,7 @@ class="link-seesrc" target="_blank">SourceA non-existing tag in the YAML character stream will be resolved to the non-specific tags ? or ! according to the YAML specification. These are by convention mapped to the TagId s yTagQuestionMark and yTagExclamationMark respectively. Mapping is done by a TagLibrary.
Source
YamlStream = object @@ -577,7 +596,7 @@ A YamlStream is an itThe creator of a YamlStream is responsible for it being well-formed. A user of the stream may assume that it is well-formed and is not required to check for it. The procs in this module will always yield a well-formed YamlStream and expect it to be well-formed if they take it as input parameter.
Source
TagLibrary = ref object @@ -591,7 +610,7 @@ class="link-seesrc" target="_blank">SourceYou can base your tag library on common tag libraries by initializing them with initFailsafeTagLibrary, initCoreTagLibrary or initExtendedTagLibrary.
Source
WarningCallback = proc (line, column: int; lineContent: string; message: string)
YamlParser = ref object @@ -614,7 +633,7 @@ class="link-seesrc" target="_blank">Source
PresentationStyle = enum @@ -628,7 +647,7 @@ Different styles for YAML character stream output.
TagStyle = enum @@ -640,7 +659,7 @@ Whether object should be serialized with explicit tags.
AnchorStyle = enum @@ -652,7 +671,7 @@ How ref object should be serialized.
NewLineStyle = enum @@ -664,7 +683,7 @@ What kind of newline sequence is used when presenting.
OutputYamlVersion = enum @@ -674,7 +693,7 @@ class="link-seesrc" target="_blank">SourceIt is also possible to specify that the presenter shall not emit any YAML version. The generated content is then guaranteed to be valid YAML 1.1 and 1.2 (but not 1.0 or any newer YAML version).
Source
PresentationOptions = object @@ -686,7 +705,7 @@ class="link-seesrc" target="_blank">Source
ConstructionContext = ref object @@ -695,7 +714,7 @@ class="link-seesrc" target="_blank">Source
SerializationContext = ref object @@ -706,7 +725,7 @@ class="link-seesrc" target="_blank">Source
RawYamlStream = iterator (): YamlStreamEvent {.raises: [].}
YamlNodeKind = enum @@ -722,14 +741,14 @@ class="link-seesrc" target="_blank">Source
YamlNode = ref YamlNodeObj not nil
YamlNodeObj = object @@ -742,7 +761,7 @@ class="link-seesrc" target="_blank">Source
YamlDocument = object @@ -751,7 +770,7 @@ class="link-seesrc" target="_blank">Source
YamlLoadingError = object of Exception @@ -766,7 +785,7 @@ class="link-seesrc" target="_blank">Source
YamlParserError = object of YamlLoadingError @@ -785,7 +804,7 @@ class="link-seesrc" target="_blank">SourceSome elements in this list are vague. For a detailed description of a valid YAML character stream, see the YAML specification.
Source
YamlPresenterJsonError = object of Exception @@ -796,7 +815,7 @@ Exception that may be raised by the YAML presenter when it is instructed to outp
YamlPresenterOutputError = object of Exception @@ -804,7 +823,7 @@ class="link-seesrc" target="_blank">Source
YamlStreamError = object of Exception @@ -812,7 +831,7 @@ class="link-seesrc" target="_blank">Source
YamlConstructionError = object of YamlLoadingError @@ -820,7 +839,7 @@ class="link-seesrc" target="_blank">Source
Should not be modified manually. Will be extended by serializable.
Source @@ -847,77 +866,77 @@ class="link-seesrc" target="_blank">SourceyTagNimInt8 = registerUri(serializationTagLibrary, "!nim:system:int8")
yTagNimInt16 = registerUri(serializationTagLibrary, "!nim:system:int16")
yTagNimInt32 = registerUri(serializationTagLibrary, "!nim:system:int32")
yTagNimInt64 = registerUri(serializationTagLibrary, "!nim:system:int64")
yTagNimUInt8 = registerUri(serializationTagLibrary, "!nim:system:uint8")
yTagNimUInt16 = registerUri(serializationTagLibrary, "!nim:system:uint16")
yTagNimUInt32 = registerUri(serializationTagLibrary, "!nim:system:uint32")
yTagNimUInt64 = registerUri(serializationTagLibrary, "!nim:system:uint64")
yTagNimFloat32 = registerUri(serializationTagLibrary, "!nim:system:float32")
yTagNimFloat64 = registerUri(serializationTagLibrary, "!nim:system:float64")
yTagQuestionMark: TagId = 1
yTagString: TagId = 2
yTagSequence: TagId = 3
yTagMapping: TagId = 4
yTagNull: TagId = 5
yTagBoolean: TagId = 6
yTagInteger: TagId = 7
yTagFloat: TagId = 8
yTagOrderedMap: TagId = 9
yTagPairs: TagId = 10
yTagSet: TagId = 11
yTagBinary: TagId = 12
yTagMerge: TagId = 13
yTagTimestamp: TagId = 14
yTagValue: TagId = 15
yTagYaml: TagId = 16
yTagNimField: TagId = 100
yFirstCustomTagId: TagId = 1000
yAnchorNone: AnchorId = -1
yamlTagRepositoryPrefix = "tag:yaml.org,2002:"
defaultPresentationOptions = PresentationOptions(style: psDefault, @@ -1087,7 +1114,7 @@ class="link-seesrc" target="_blank">Source
proc hash(id: TagId): Hash {.borrow.}
proc `==`(left, right: AnchorId): bool {.borrow.}
proc `$`(id: AnchorId): string {.borrow.}
proc hash(id: AnchorId): Hash {.borrow.}
proc yamlTag[](T: typedesc[char]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[int8]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[int16]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[int32]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[int64]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[uint8]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[uint16]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[uint32]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[uint64]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[float32]): TagId {.inline, raises: [].}
proc yamlTag[](T: typedesc[float64]): TagId {.inline, raises: [].}
proc `$`(id: TagId): string {.raises: [], tags: [].}
proc initTagLibrary(): TagLibrary {.raises: [], tags: [].}
proc registerUri(tagLib: TagLibrary; uri: string): TagId {.raises: [], tags: [].}
proc uri(tagLib: TagLibrary; id: TagId): string {.raises: [KeyError], tags: [].}
proc initFailsafeTagLibrary(): TagLibrary {.raises: [], tags: [].}
proc initCoreTagLibrary(): TagLibrary {.raises: [], tags: [].}
proc initExtendedTagLibrary(): TagLibrary {.raises: [], tags: [].}
proc `==`(left: YamlStreamEvent; right: YamlStreamEvent): bool {.raises: [], tags: [].}
proc `$`(event: YamlStreamEvent): string {.raises: [], tags: [].}
proc tag(event: YamlStreamEvent): TagId {.raises: [FieldError], tags: [].}
proc startDocEvent(): YamlStreamEvent {.inline, raises: [], tags: [].}
proc endDocEvent(): YamlStreamEvent {.inline, raises: [], tags: [].}
proc startMapEvent(tag: TagId = yTagQuestionMark; anchor: AnchorId = yAnchorNone): YamlStreamEvent {. @@ -1309,14 +1343,14 @@ class="link-seesrc" target="_blank">Source
proc endMapEvent(): YamlStreamEvent {.inline, raises: [], tags: [].}
proc startSeqEvent(tag: TagId = yTagQuestionMark; anchor: AnchorId = yAnchorNone): YamlStreamEvent {. @@ -1324,14 +1358,14 @@ class="link-seesrc" target="_blank">Source
proc endSeqEvent(): YamlStreamEvent {.inline, raises: [], tags: [].}
proc scalarEvent(content: string = ""; tag: TagId = yTagQuestionMark; @@ -1340,14 +1374,14 @@ class="link-seesrc" target="_blank">Source
proc aliasEvent(anchor: AnchorId): YamlStreamEvent {.inline, raises: [], tags: [].}
proc constructJson(s: var YamlStream): seq[JsonNode] {. @@ -1357,14 +1391,14 @@ class="link-seesrc" target="_blank">SourceWarning: The special float values [+-]Inf and NaN will be parsed into Nim's JSON structure without error. However, they cannot be rendered to a JSON character stream, because these values are not part of the JSON specification. Nim's JSON implementation currently does not check for these values and will output invalid JSON when rendering one of these values into a JSON character stream.
Source
proc loadToJson(s: Stream): seq[JsonNode] {.raises: [], tags: [RootEffect].}
proc defineOptions(style: PresentationStyle = psDefault; indentationStep: int = 2; @@ -1374,7 +1408,7 @@ class="link-seesrc" target="_blank">Source
proc present(s: var YamlStream; target: Stream; tagLib: TagLibrary; @@ -1384,7 +1418,7 @@ class="link-seesrc" target="_blank">Source
proc transform(input: Stream; output: Stream; @@ -1394,14 +1428,14 @@ class="link-seesrc" target="_blank">Source
proc guessType(scalar: string): TypeHint {.raises: [], tags: [].}
proc newYamlParser(tagLib: TagLibrary = initExtendedTagLibrary(); @@ -1409,28 +1443,28 @@ class="link-seesrc" target="_blank">Source
proc getLineNumber(p: YamlParser): int {.raises: [], tags: [].}
proc getColNumber(p: YamlParser): int {.raises: [], tags: [].}
proc getLineContent(p: YamlParser; marker: bool = true): string {.raises: [], tags: [].}
proc parse(p: YamlParser; s: Stream): YamlStream {.raises: [], @@ -1438,7 +1472,7 @@ class="link-seesrc" target="_blank">Source
proc initYamlStream(backend: iterator (): YamlStreamEvent): YamlStream {.raises: [], @@ -1446,7 +1480,7 @@ class="link-seesrc" target="_blank">Source
proc next(s: var YamlStream): YamlStreamEvent {.raises: [YamlStreamError], @@ -1454,7 +1488,7 @@ class="link-seesrc" target="_blank">Source
proc peek(s: var YamlStream): YamlStreamEvent {.raises: [YamlStreamError], @@ -1462,28 +1496,43 @@ class="link-seesrc" target="_blank">Source
proc peek=(s: var YamlStream; value: YamlStreamEvent) {.raises: [], tags: [].}
proc finished(s: var YamlStream): bool {.raises: [YamlStreamError], tags: [RootEffect].}
proc newConstructionContext(): ConstructionContext {.raises: [], tags: [].}
proc newSerializationContext(s: AnchorStyle): SerializationContext {.raises: [], + tags: [].}
proc yamlTag[](T: typedesc[string]): TagId {.inline, noSideEffect, raises: [].}
proc constructObject(s: var YamlStream; c: ConstructionContext; result: var string) {. @@ -1491,15 +1540,15 @@ class="link-seesrc" target="_blank">Source
proc representObject(value: string; ts: TagStyle = tsNone; c: SerializationContext): RawYamlStream {. +
proc representObject(value: string; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [], tags: [].}
proc constructObject[T](s: var YamlStream; c: ConstructionContext; result: var T) {. @@ -1507,15 +1556,15 @@ class="link-seesrc" target="_blank">Source
proc representObject[T](value: T; ts: TagStyle = tsNone; c: SerializationContext): RawYamlStream {. +
proc representObject[T](value: T; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [].}
proc constructObject[T](s: var YamlStream; c: ConstructionContext; result: var T) {. @@ -1523,15 +1572,15 @@ class="link-seesrc" target="_blank">Source
proc representObject[T](value: T; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject[T](value: T; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [].}
proc constructObject[T](s: var YamlStream; c: ConstructionContext; result: var T) {. @@ -1539,22 +1588,22 @@ class="link-seesrc" target="_blank">Source
proc representObject[T](value: T; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject[T](value: T; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [].}
proc yamlTag[](T: typedesc[bool]): TagId {.inline, raises: [].}
proc constructObject(s: var YamlStream; c: ConstructionContext; result: var bool) {. @@ -1562,15 +1611,15 @@ class="link-seesrc" target="_blank">Source
proc representObject(value: bool; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject(value: bool; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [], tags: [].}
proc constructObject(s: var YamlStream; c: ConstructionContext; result: var char) {. @@ -1578,22 +1627,22 @@ class="link-seesrc" target="_blank">Source
proc representObject(value: char; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject(value: char; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [], tags: [].}
proc yamlTag[I, ](T: typedesc[seq[I]]): TagId {.inline, raises: [].}
proc constructObject[T](s: var YamlStream; c: ConstructionContext; result: var seq[T]) {. @@ -1601,22 +1650,22 @@ class="link-seesrc" target="_blank">Source
proc representObject[T](value: seq[T]; ts: TagStyle; c: SerializationContext): RawYamlStream {. - raises: [].}
proc representObject[T](value: seq[T]; ts: TagStyle; c: SerializationContext; + tag: TagId): RawYamlStream {.raises: [].}
proc yamlTag[K, V, ](T: typedesc[Table[K, V]]): TagId {.inline, raises: [].}
proc constructObject[K, V](s: var YamlStream; c: ConstructionContext; @@ -1625,15 +1674,40 @@ class="link-seesrc" target="_blank">Source
proc representObject[K, V](value: Table[K, V]; ts: TagStyle; c: SerializationContext): RawYamlStream {. - raises: [].}
proc representObject[K, V](value: Table[K, V]; ts: TagStyle; c: SerializationContext; + tag: TagId): RawYamlStream {.raises: [].}
proc yamlTag[K, V, ](T: typedesc[OrderedTable[K, V]]): TagId {.inline, raises: [].}
proc constructObject[K, V](s: var YamlStream; c: ConstructionContext; + result: var OrderedTable[K, V]) {. + raises: [YamlConstructionError, YamlStreamError].}
proc representObject[K, V](value: OrderedTable[K, V]; ts: TagStyle; + c: SerializationContext; tag: TagId): RawYamlStream {. + raises: [].}
proc constructObject[O](s: var YamlStream; c: ConstructionContext; result: var O) {. @@ -1641,15 +1715,15 @@ class="link-seesrc" target="_blank">Source
proc representObject[O](value: O; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject[O](value: O; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [].}
proc constructObject[O](s: var YamlStream; c: ConstructionContext; result: var O) {. @@ -1657,22 +1731,22 @@ class="link-seesrc" target="_blank">Source
proc representObject[O](value: O; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representObject[O](value: O; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream {. raises: [].}
proc yamlTag[O, ](T: typedesc[ref O]): TagId {.inline, raises: [].}
proc constructChild[T](s: var YamlStream; c: ConstructionContext; result: var T) {. @@ -1680,7 +1754,7 @@ class="link-seesrc" target="_blank">Source
proc constructChild[O](s: var YamlStream; c: ConstructionContext; result: var ref O) {. @@ -1688,15 +1762,23 @@ class="link-seesrc" target="_blank">Source
proc representObject[O](value: ref O; ts: TagStyle; c: SerializationContext): RawYamlStream {. +
proc representChild[O](value: O; ts: TagStyle; c: SerializationContext): RawYamlStream {. + inline.}
proc representChild[O](value: ref O; ts: TagStyle; c: SerializationContext): RawYamlStream {. raises: [].}
proc construct[T](s: var YamlStream; target: var T) {. @@ -1704,7 +1786,7 @@ class="link-seesrc" target="_blank">Source
proc load[K](input: Stream; target: var K) {.raises: [YamlConstructionError, IOError, @@ -1712,7 +1794,7 @@ class="link-seesrc" target="_blank">Source
proc represent[T](value: T; ts: TagStyle = tsRootOnly; a: AnchorStyle = asTidy): YamlStream {. @@ -1720,7 +1802,7 @@ class="link-seesrc" target="_blank">Source
proc dump[K](value: K; target: Stream; tagStyle: TagStyle = tsRootOnly; @@ -1730,14 +1812,14 @@ class="link-seesrc" target="_blank">Source
proc newYamlNode(content: string; tag: string = "?"): YamlNode {.raises: [], tags: [].}
proc newYamlNode(children: openArray[YamlNode]; tag: string = "?"): YamlNode {. @@ -1745,7 +1827,7 @@ class="link-seesrc" target="_blank">Source
proc newYamlNode(pairs: openArray[tuple[key, value: YamlNode]]; tag: string = "?"): YamlNode {. @@ -1753,14 +1835,14 @@ class="link-seesrc" target="_blank">Source
proc initYamlDoc(root: YamlNode): YamlDocument {.raises: [], tags: [].}
proc compose(s: var YamlStream; tagLib: TagLibrary): YamlDocument {. @@ -1768,7 +1850,7 @@ class="link-seesrc" target="_blank">Source
proc loadDOM(s: Stream): YamlDocument {.raises: [IOError, YamlParserError, @@ -1776,7 +1858,7 @@ class="link-seesrc" target="_blank">Source
proc serialize(doc: YamlDocument; tagLib: TagLibrary; a: AnchorStyle = asTidy): YamlStream {. @@ -1784,7 +1866,7 @@ class="link-seesrc" target="_blank">Source
proc dumpDOM(doc: YamlDocument; target: Stream; anchorStyle: AnchorStyle = asTidy; @@ -1794,7 +1876,7 @@ class="link-seesrc" target="_blank">Source
template setTagUriForType[](t: typedesc; uri: string; idName: expr): stmt
template presentTag[](t: typedesc; ts: TagStyle): TagId
template constructScalarItem[](s: var YamlStream; i: expr; t: typedesc; content: untyped)
template constructObject(s: var YamlStream; c: ConstructionContext; result: var int)
template representObject(value: int; tagStyle: TagStyle; c: SerializationContext): RawYamlStream
template representObject(value: int; tagStyle: TagStyle; c: SerializationContext; + tag: TagId): RawYamlStream
template constructObject(s: var YamlStream; c: ConstructionContext; result: var uint)
template representObject(value: uint; ts: TagStyle; c: SerializationContext): RawYamlStream
template representObject(value: uint; ts: TagStyle; c: SerializationContext; tag: TagId): RawYamlStream
template constructObject(s: var YamlStream; c: ConstructionContext; result: var float)
template representObject(value: float; tagStyle: TagStyle; c: SerializationContext): RawYamlStream
template representObject(value: float; tagStyle: TagStyle; c: SerializationContext; + tag: TagId): RawYamlStream
template yamlTag[](T: typedesc[object | enum]): expr
template yamlTag[](T: typedesc[tuple]): expr