2020-11-03 20:17:31 +00:00
|
|
|
import hashes
|
2020-11-10 18:07:46 +00:00
|
|
|
import private/escaping
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Anchor* = distinct string ## \
|
|
|
|
## An ``Anchor`` identifies an anchor in the current document.
|
|
|
|
## It is not necessarily unique and references to an anchor must be
|
|
|
|
## resolved immediately on occurrence.
|
|
|
|
##
|
|
|
|
## Anchor provides the operator `$` for converting to string, `==` for
|
|
|
|
## comparison, and `hash` for usage in a hashmap.
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
Tag* = distinct string ## \
|
|
|
|
## A ``Tag`` contains an URI, like for example ``"tag:yaml.org,2002:str"``.
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
ScalarStyle* = enum
|
|
|
|
## Original style of the scalar (for input),
|
|
|
|
## or desired style of the scalar (for output).
|
|
|
|
ssAny, ssPlain, ssSingleQuoted, ssDoubleQuoted, ssLiteral, ssFolded
|
|
|
|
|
|
|
|
CollectionStyle* = enum
|
2020-11-06 15:21:58 +00:00
|
|
|
csAny, csBlock, csFlow, csPair
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
EventKind* = enum
|
|
|
|
## Kinds of YAML events that may occur in an ``YamlStream``. Event kinds
|
|
|
|
## are discussed in `YamlStreamEvent <#YamlStreamEvent>`_.
|
|
|
|
yamlStartStream, yamlEndStream,
|
|
|
|
yamlStartDoc, yamlEndDoc, yamlStartMap, yamlEndMap,
|
|
|
|
yamlStartSeq, yamlEndSeq, yamlScalar, yamlAlias
|
|
|
|
|
|
|
|
Event* = object
|
|
|
|
## An element from a `YamlStream <#YamlStream>`_. Events that start an
|
|
|
|
## object (``yamlStartMap``, ``yamlStartSeq``, ``yamlScalar``) have
|
|
|
|
## an optional anchor and a tag associated with them. The anchor will be
|
|
|
|
## set to ``yAnchorNone`` if it doesn't exist.
|
|
|
|
##
|
|
|
|
## A missing tag in the YAML character stream generates
|
|
|
|
## 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 <#TagLibrary>`_.
|
|
|
|
##
|
|
|
|
## ``startPos`` and ``endPos`` are only relevant for events from an input
|
|
|
|
## stream - they are generally ignored if used with events that generate
|
|
|
|
## output.
|
|
|
|
startPos*, endPos*: Mark
|
|
|
|
case kind*: EventKind
|
|
|
|
of yamlStartStream, yamlEndStream: discard
|
|
|
|
of yamlStartMap:
|
|
|
|
mapProperties*: Properties
|
|
|
|
mapStyle*: CollectionStyle
|
|
|
|
of yamlStartSeq:
|
|
|
|
seqProperties*: Properties
|
|
|
|
seqStyle*: CollectionStyle
|
|
|
|
of yamlScalar:
|
|
|
|
scalarProperties*: Properties
|
|
|
|
scalarStyle* : ScalarStyle
|
|
|
|
scalarContent*: string
|
|
|
|
of yamlStartDoc:
|
|
|
|
explicitDirectivesEnd*: bool
|
|
|
|
version*: string
|
2020-11-06 20:39:50 +00:00
|
|
|
handles*: seq[tuple[handle, uriPrefix: string]]
|
2020-11-03 20:17:31 +00:00
|
|
|
of yamlEndDoc:
|
|
|
|
explicitDocumentEnd*: bool
|
|
|
|
of yamlEndMap, yamlEndSeq: discard
|
|
|
|
of yamlAlias:
|
|
|
|
aliasTarget* : Anchor
|
|
|
|
|
|
|
|
Mark* = tuple[line, column: Positive]
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
Properties* = tuple[anchor: Anchor, tag: Tag]
|
|
|
|
|
|
|
|
const
|
|
|
|
yamlTagRepositoryPrefix* = "tag:yaml.org,2002:"
|
|
|
|
nimyamlTagRepositoryPrefix* = "tag:nimyaml.org,2016:"
|
|
|
|
|
|
|
|
proc defineTag*(uri: string): Tag =
|
|
|
|
## defines a tag. Use this to optimize away copies of globally defined
|
|
|
|
## Tags.
|
|
|
|
result = uri.Tag
|
|
|
|
#shallow(result.string) # doesn't work at compile-time
|
|
|
|
|
|
|
|
proc defineCoreTag*(name: string): Tag =
|
|
|
|
## defines a tag in YAML's core namespace, ``tag:yaml.org,2002:``
|
|
|
|
result = defineTag(yamlTagRepositoryPrefix & name)
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
yAnchorNone*: Anchor = "".Anchor ## \
|
|
|
|
## yielded when no anchor was defined for a YAML node
|
|
|
|
|
|
|
|
defaultMark: Mark = (1.Positive, 1.Positive) ## \
|
|
|
|
## used for events that are not generated from input.
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
yTagExclamationMark*: Tag = defineTag("!")
|
|
|
|
yTagQuestionMark* : Tag = defineTag("?")
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
# failsafe schema
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
yTagString* = defineCoreTag("str")
|
|
|
|
yTagSequence* = defineCoreTag("seq")
|
|
|
|
yTagMapping* = defineCoreTag("map")
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
# json & core schema
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
yTagNull* = defineCoreTag("null")
|
|
|
|
yTagBoolean* = defineCoreTag("bool")
|
|
|
|
yTagInteger* = defineCoreTag("int")
|
|
|
|
yTagFloat* = defineCoreTag("float")
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
# other language-independent YAML types (from http://yaml.org/type/ )
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
yTagOrderedMap* = defineCoreTag("omap")
|
|
|
|
yTagPairs* = defineCoreTag("pairs")
|
|
|
|
yTagSet* = defineCoreTag("set")
|
|
|
|
yTagBinary* = defineCoreTag("binary")
|
|
|
|
yTagMerge* = defineCoreTag("merge")
|
|
|
|
yTagTimestamp* = defineCoreTag("timestamp")
|
|
|
|
yTagValue* = defineCoreTag("value")
|
|
|
|
yTagYaml* = defineCoreTag("yaml")
|
2020-11-03 20:17:31 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
# NimYAML specific tags
|
|
|
|
|
|
|
|
yTagNimField* = defineTag(nimyamlTagRepositoryPrefix & "field")
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
proc properties*(event: Event): Properties =
|
|
|
|
## returns the tag of the given event
|
|
|
|
case event.kind
|
|
|
|
of yamlStartMap: result = event.mapProperties
|
|
|
|
of yamlStartSeq: result = event.seqProperties
|
|
|
|
of yamlScalar: result = event.scalarProperties
|
|
|
|
else: raise newException(FieldDefect, "Event " & $event.kind & " has no properties")
|
|
|
|
|
|
|
|
proc collectionStyle*(event: Event): CollectionStyle =
|
|
|
|
## returns the style of the given collection start event
|
|
|
|
case event.kind
|
|
|
|
of yamlStartMap: result = event.mapStyle
|
|
|
|
of yamlStartSeq: result = event.seqStyle
|
|
|
|
else: raise (ref FieldDefect)(msg: "Event " & $event.kind & " has no collectionStyle")
|
|
|
|
|
2020-11-06 15:21:58 +00:00
|
|
|
proc startStreamEvent*(): Event =
|
|
|
|
return Event(startPos: defaultMark, endPos: defaultMark, kind: yamlStartStream)
|
|
|
|
|
|
|
|
proc endStreamEvent*(): Event =
|
|
|
|
return Event(startPos: defaultMark, endPos: defaultMark, kind: yamlEndStream)
|
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc startDocEvent*(explicit: bool = false, version: string = "",
|
|
|
|
handles: seq[tuple[handle, uriPrefix: string]] = @[],
|
|
|
|
startPos, endPos: Mark = defaultMark): Event
|
2020-11-03 20:17:31 +00:00
|
|
|
{.inline, raises: [].} =
|
|
|
|
## creates a new event that marks the start of a YAML document
|
|
|
|
result = Event(startPos: startPos, endPos: endPos,
|
2020-11-06 20:39:50 +00:00
|
|
|
kind: yamlStartDoc, version: version, handles: handles,
|
2020-11-03 20:17:31 +00:00
|
|
|
explicitDirectivesEnd: explicit)
|
|
|
|
|
|
|
|
proc endDocEvent*(explicit: bool = false, startPos, endPos: Mark = defaultMark): Event
|
|
|
|
{.inline, raises: [].} =
|
|
|
|
## creates a new event that marks the end of a YAML document
|
|
|
|
result = Event(startPos: startPos, endPos: endPos,
|
|
|
|
kind: yamlEndDoc, explicitDocumentEnd: explicit)
|
|
|
|
|
|
|
|
proc startMapEvent*(style: CollectionStyle, props: Properties,
|
2020-11-03 21:08:21 +00:00
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
2020-11-03 20:17:31 +00:00
|
|
|
## creates a new event that marks the start of a YAML mapping
|
|
|
|
result = Event(startPos: startPos, endPos: endPos,
|
|
|
|
kind: yamlStartMap, mapProperties: props,
|
|
|
|
mapStyle: style)
|
|
|
|
|
2020-11-06 15:21:58 +00:00
|
|
|
proc startMapEvent*(style: CollectionStyle = csAny,
|
2020-11-10 12:55:22 +00:00
|
|
|
tag: Tag = yTagQuestionMark,
|
2020-11-03 20:17:31 +00:00
|
|
|
anchor: Anchor = yAnchorNone,
|
2020-11-06 15:21:58 +00:00
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline.} =
|
2020-11-03 20:17:31 +00:00
|
|
|
return startMapEvent(style, (anchor, tag), startPos, endPos)
|
|
|
|
|
|
|
|
proc endMapEvent*(startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
|
|
|
## creates a new event that marks the end of a YAML mapping
|
|
|
|
result = Event(startPos: startPos, endPos: endPos, kind: yamlEndMap)
|
|
|
|
|
|
|
|
proc startSeqEvent*(style: CollectionStyle,
|
|
|
|
props: Properties,
|
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
|
|
|
## creates a new event that marks the beginning of a YAML sequence
|
|
|
|
result = Event(startPos: startPos, endPos: endPos,
|
|
|
|
kind: yamlStartSeq, seqProperties: props,
|
|
|
|
seqStyle: style)
|
|
|
|
|
2020-11-06 15:21:58 +00:00
|
|
|
proc startSeqEvent*(style: CollectionStyle = csAny,
|
2020-11-10 12:55:22 +00:00
|
|
|
tag: Tag = yTagQuestionMark,
|
2020-11-03 20:17:31 +00:00
|
|
|
anchor: Anchor = yAnchorNone,
|
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline.} =
|
|
|
|
return startSeqEvent(style, (anchor, tag), startPos, endPos)
|
|
|
|
|
|
|
|
proc endSeqEvent*(startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
|
|
|
## creates a new event that marks the end of a YAML sequence
|
|
|
|
result = Event(startPos: startPos, endPos: endPos, kind: yamlEndSeq)
|
|
|
|
|
|
|
|
proc scalarEvent*(content: string, props: Properties,
|
|
|
|
style: ScalarStyle = ssAny,
|
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
|
|
|
## creates a new event that represents a YAML scalar
|
|
|
|
result = Event(startPos: startPos, endPos: endPos,
|
|
|
|
kind: yamlScalar, scalarProperties: props,
|
|
|
|
scalarContent: content, scalarStyle: style)
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
proc scalarEvent*(content: string = "", tag: Tag = yTagQuestionMark,
|
2020-11-03 20:17:31 +00:00
|
|
|
anchor: Anchor = yAnchorNone,
|
|
|
|
style: ScalarStyle = ssAny,
|
|
|
|
startPos, endPos: Mark = defaultMark): Event {.inline.} =
|
|
|
|
return scalarEvent(content, (anchor, tag), style, startPos, endPos)
|
|
|
|
|
|
|
|
proc aliasEvent*(target: Anchor, startPos, endPos: Mark = defaultMark): Event {.inline, raises: [].} =
|
|
|
|
## creates a new event that represents a YAML alias
|
|
|
|
result = Event(startPos: startPos, endPos: endPos, kind: yamlAlias, aliasTarget: target)
|
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
proc `==`*(left, right: Anchor): bool {.borrow.}
|
|
|
|
proc `$`*(id: Anchor): string {.borrow.}
|
|
|
|
proc hash*(id: Anchor): Hash {.borrow.}
|
|
|
|
|
|
|
|
proc `==`*(left, right: Tag): bool {.borrow.}
|
|
|
|
proc `$`*(tag: Tag): string {.borrow.}
|
|
|
|
proc hash*(tag: Tag): Hash {.borrow.}
|
2020-11-03 20:17:31 +00:00
|
|
|
|
|
|
|
proc `==`*(left: Event, right: Event): bool {.raises: [].} =
|
|
|
|
## compares all existing fields of the given items
|
|
|
|
if left.kind != right.kind: return false
|
|
|
|
case left.kind
|
|
|
|
of yamlStartStream, yamlEndStream, yamlStartDoc, yamlEndDoc, yamlEndMap, yamlEndSeq:
|
|
|
|
result = true
|
|
|
|
of yamlStartMap:
|
|
|
|
result = left.mapProperties == right.mapProperties
|
|
|
|
of yamlStartSeq:
|
|
|
|
result = left.seqProperties == right.seqProperties
|
|
|
|
of yamlScalar:
|
|
|
|
result = left.scalarProperties == right.scalarProperties and
|
|
|
|
left.scalarContent == right.scalarContent
|
|
|
|
of yamlAlias: result = left.aliasTarget == right.aliasTarget
|
|
|
|
|
|
|
|
proc renderAttrs*(props: Properties, isPlain: bool = true): string =
|
|
|
|
result = ""
|
|
|
|
if props.anchor != yAnchorNone: result &= " &" & $props.anchor
|
|
|
|
case props.tag
|
2020-11-10 12:55:22 +00:00
|
|
|
of yTagQuestionMark: discard
|
|
|
|
of yTagExclamationMark:
|
2020-11-03 20:17:31 +00:00
|
|
|
if isPlain: result &= " <!>"
|
|
|
|
else:
|
|
|
|
result &= " <" & $props.tag & ">"
|
|
|
|
|
|
|
|
proc `$`*(event: Event): string {.raises: [].} =
|
|
|
|
## outputs a human-readable string describing the given event.
|
|
|
|
## This string is compatible to the format used in the yaml test suite.
|
|
|
|
case event.kind
|
|
|
|
of yamlStartStream: result = "+STR"
|
|
|
|
of yamlEndStream: result = "-STR"
|
|
|
|
of yamlEndMap: result = "-MAP"
|
|
|
|
of yamlEndSeq: result = "-SEQ"
|
|
|
|
of yamlStartDoc:
|
|
|
|
result = "+DOC"
|
|
|
|
if event.explicitDirectivesEnd: result &= " ---"
|
|
|
|
of yamlEndDoc:
|
|
|
|
result = "-DOC"
|
|
|
|
if event.explicitDocumentEnd: result &= " ..."
|
|
|
|
of yamlStartMap: result = "+MAP" & renderAttrs(event.mapProperties)
|
2020-11-04 21:47:52 +00:00
|
|
|
of yamlStartSeq: result = "+SEQ" & renderAttrs(event.seqProperties)
|
2020-11-03 20:17:31 +00:00
|
|
|
of yamlScalar:
|
|
|
|
result = "=VAL" & renderAttrs(event.scalarProperties,
|
|
|
|
event.scalarStyle == ssPlain or
|
|
|
|
event.scalarStyle == ssAny)
|
|
|
|
case event.scalarStyle
|
|
|
|
of ssPlain, ssAny: result &= " :"
|
|
|
|
of ssSingleQuoted: result &= " \'"
|
|
|
|
of ssDoubleQuoted: result &= " \""
|
|
|
|
of ssLiteral: result &= " |"
|
|
|
|
of ssFolded: result &= " >"
|
|
|
|
result &= yamlTestSuiteEscape(event.scalarContent)
|
2020-11-10 13:48:19 +00:00
|
|
|
of yamlAlias: result = "=ALI *" & $event.aliasTarget
|