NimYAML/private/events.nim

73 lines
3.0 KiB
Nim
Raw Normal View History

2015-12-29 17:38:22 +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.
proc `==`*(left: YamlStreamEvent, right: YamlStreamEvent): bool =
2016-04-02 15:48:22 +00:00
if left.kind != right.kind: return false
case left.kind
of yamlStartDoc, yamlEndDoc, yamlEndMap, yamlEndSeq: result = true
of yamlStartMap:
result = left.mapAnchor == right.mapAnchor and left.mapTag == right.mapTag
of yamlStartSeq:
result = left.seqAnchor == right.seqAnchor and left.seqTag == right.seqTag
of yamlScalar:
result = left.scalarAnchor == right.scalarAnchor and
left.scalarTag == right.scalarTag and
left.scalarContent == right.scalarContent
of yamlAlias: result = left.aliasTarget == right.aliasTarget
2015-12-29 17:38:22 +00:00
proc `$`*(event: YamlStreamEvent): string =
2016-04-02 15:48:22 +00:00
result = $event.kind & '('
case event.kind
of yamlEndMap, yamlEndSeq, yamlStartDoc, yamlEndDoc: discard
of yamlStartMap:
result &= "tag=" & $event.mapTag
if event.mapAnchor != yAnchorNone: result &= ", anchor=" & $event.mapAnchor
of yamlStartSeq:
result &= "tag=" & $event.seqTag
if event.seqAnchor != yAnchorNone: result &= ", anchor=" & $event.seqAnchor
of yamlScalar:
result &= "tag=" & $event.scalarTag
if event.scalarAnchor != yAnchorNone:
result &= ", anchor=" & $event.scalarAnchor
result &= ", content=\"" & event.scalarContent & '\"'
of yamlAlias:
result &= "aliasTarget=" & $event.aliasTarget
result &= ")"
2015-12-29 17:38:22 +00:00
2016-03-25 22:12:00 +00:00
proc tag*(event: YamlStreamEvent): TagId =
2016-04-02 15:48:22 +00:00
case event.kind
of yamlStartMap: result = event.mapTag
of yamlStartSeq: result = event.seqTag
of yamlScalar: result = event.scalarTag
else: raise newException(FieldError, "Event " & $event.kind & " has no tag")
2016-03-25 22:12:00 +00:00
2015-12-29 17:38:22 +00:00
proc startDocEvent*(): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlStartDoc)
2015-12-29 17:38:22 +00:00
proc endDocEvent*(): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlEndDoc)
2015-12-29 17:38:22 +00:00
proc startMapEvent*(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlStartMap, mapTag: tag, mapAnchor: anchor)
2015-12-29 17:38:22 +00:00
proc endMapEvent*(): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlEndMap)
2015-12-29 17:38:22 +00:00
proc startSeqEvent*(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlStartSeq, seqTag: tag, seqAnchor: anchor)
2015-12-29 17:38:22 +00:00
proc endSeqEvent*(): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlEndSeq)
2015-12-29 17:38:22 +00:00
proc scalarEvent*(content: string = "", tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone): YamlStreamEvent =
2016-04-02 15:48:22 +00:00
result = YamlStreamEvent(kind: yamlScalar, scalarTag: tag,
scalarAnchor: anchor, scalarContent: content)
proc aliasEvent*(anchor: AnchorId): YamlStreamEvent =
result = YamlStreamEvent(kind: yamlAlias, aliasTarget: anchor)