2015-12-28 21:22:51 +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.
|
|
|
|
|
2015-12-27 20:36:47 +00:00
|
|
|
## This module provides facilities to generate and interpret
|
|
|
|
## `YAML <http://yaml.org>`_ character streams. All primitive operations on
|
2015-12-27 22:40:27 +00:00
|
|
|
## data objects use a `YamlStream <#YamlStream>`_ either as source or as
|
|
|
|
## output. Because this stream is implemented as iterator, it is possible to
|
|
|
|
## process YAML input and output sequentially, i.e. without loading the
|
|
|
|
## processed data structure completely into RAM. This supports the processing of
|
|
|
|
## large data structures.
|
2015-12-27 20:36:47 +00:00
|
|
|
##
|
|
|
|
## As YAML is a strict superset of `JSON <http://json.org>`_, JSON input is
|
2016-02-02 17:19:40 +00:00
|
|
|
## automatically supported. While JSON is less readable than YAML,
|
2015-12-27 20:36:47 +00:00
|
|
|
## this enhances interoperability with other languages.
|
|
|
|
|
2016-02-12 19:44:38 +00:00
|
|
|
import streams, unicode, lexbase, tables, strutils, json, hashes, queues,
|
|
|
|
macros, typetraits
|
2015-12-28 21:22:51 +00:00
|
|
|
export streams, tables, json
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2016-01-20 20:15:33 +00:00
|
|
|
when defined(yamlDebug):
|
|
|
|
import terminal
|
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
type
|
2016-01-14 18:58:38 +00:00
|
|
|
TypeHint* = enum
|
2016-01-24 17:24:09 +00:00
|
|
|
## A type hint can be computed from scalar content and tells you what
|
|
|
|
## NimYAML thinks the scalar's type is. It is generated by
|
2016-01-24 18:15:50 +00:00
|
|
|
## `guessType <#guessType,string>`_ The first matching RegEx
|
2016-01-24 17:24:09 +00:00
|
|
|
## in the following table will be the type hint of a scalar string.
|
|
|
|
##
|
|
|
|
## You can use it to determine the type of YAML scalars that have a '?'
|
|
|
|
## non-specific tag, but using this feature is completely optional.
|
2015-12-27 20:36:47 +00:00
|
|
|
##
|
|
|
|
## ================== =========================
|
|
|
|
## Name RegEx
|
|
|
|
## ================== =========================
|
|
|
|
## ``yTypeInteger`` ``0 | -? [1-9] [0-9]*``
|
|
|
|
## ``yTypeFloat`` ``-? [1-9] ( \. [0-9]* [1-9] )? ( e [-+] [1-9] [0-9]* )?``
|
|
|
|
## ``yTypeFloatInf`` ``-? \. (inf | Inf | INF)``
|
|
|
|
## ``yTypeFloatNaN`` ``-? \. (nan | NaN | NAN)``
|
|
|
|
## ``yTypeBoolTrue`` ``y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON``
|
|
|
|
## ``yTypeBoolFalse`` ``n|N|no|No|NO|false|False|FALSE|off|Off|OFF``
|
|
|
|
## ``yTypeNull`` ``~ | null | Null | NULL``
|
|
|
|
## ``yTypeUnknown`` ``*``
|
2015-12-27 22:40:27 +00:00
|
|
|
## ================== =========================
|
2015-12-27 20:36:47 +00:00
|
|
|
yTypeInteger, yTypeFloat, yTypeFloatInf, yTypeFloatNaN, yTypeBoolTrue,
|
2016-01-24 17:24:09 +00:00
|
|
|
yTypeBoolFalse, yTypeNull, yTypeUnknown
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
YamlStreamEventKind* = enum
|
2015-12-27 22:40:27 +00:00
|
|
|
## Kinds of YAML events that may occur in an ``YamlStream``. Event kinds
|
|
|
|
## are discussed in ``YamlStreamEvent``.
|
2015-12-23 11:35:07 +00:00
|
|
|
yamlStartDocument, yamlEndDocument, yamlStartMap, yamlEndMap,
|
2016-01-05 18:06:55 +00:00
|
|
|
yamlStartSequence, yamlEndSequence, yamlScalar, yamlAlias
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
TagId* = distinct int ## \
|
|
|
|
## A ``TagId`` identifies a tag URI, like for example
|
|
|
|
## ``"tag:yaml.org,2002:str"``. The URI corresponding to a ``TagId`` can
|
2016-01-20 20:15:33 +00:00
|
|
|
## be queried from the `TagLibrary <#TagLibrary>`_ which was
|
2016-01-24 17:24:09 +00:00
|
|
|
## used to create this ``TagId``; e.g. when you parse a YAML character
|
|
|
|
## stream, the ``TagLibrary`` of the parser is the one which generates
|
|
|
|
## the resulting ``TagId`` s.
|
|
|
|
##
|
|
|
|
## 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.
|
2016-01-05 17:14:11 +00:00
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
AnchorId* = distinct int ## \
|
|
|
|
## An ``AnchorId`` identifies an anchor in the current document. It
|
|
|
|
## becomes invalid as soon as the current document scope is invalidated
|
|
|
|
## (for example, because the parser yielded a ``yamlEndDocument``
|
|
|
|
## event). ``AnchorId`` s exists because of efficiency, much like
|
2016-01-24 10:44:10 +00:00
|
|
|
## ``TagId`` s. The actual anchor name is a presentation detail and
|
|
|
|
## cannot be queried by the user.
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
YamlStreamEvent* = object
|
2015-12-27 22:40:27 +00:00
|
|
|
## An element from a `YamlStream <#YamlStream>`_. Events that start an
|
|
|
|
## object (``yamlStartMap``, ``yamlStartSequence``, ``yamlScalar``) have
|
|
|
|
## an optional anchor and a tag associated with them. The anchor will be
|
|
|
|
## set to ``yAnchorNone`` if it doesn't exist.
|
|
|
|
##
|
|
|
|
## A 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.
|
2016-01-20 20:15:33 +00:00
|
|
|
## Mapping is done by a `TagLibrary <#TagLibrary>`_.
|
2015-12-26 12:39:43 +00:00
|
|
|
case kind*: YamlStreamEventKind
|
2015-12-27 15:36:32 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
mapAnchor* : AnchorId
|
|
|
|
mapTag* : TagId
|
|
|
|
of yamlStartSequence:
|
|
|
|
seqAnchor* : AnchorId
|
|
|
|
seqTag* : TagId
|
2015-12-23 11:35:07 +00:00
|
|
|
of yamlScalar:
|
|
|
|
scalarAnchor* : AnchorId
|
|
|
|
scalarTag* : TagId
|
|
|
|
scalarContent*: string # may not be nil (but empty)
|
|
|
|
of yamlEndMap, yamlEndSequence, yamlStartDocument, yamlEndDocument:
|
|
|
|
discard
|
|
|
|
of yamlAlias:
|
|
|
|
aliasTarget* : AnchorId
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
YamlStream* = object ## \
|
|
|
|
## A ``YamlStream`` is an iterator-like object that yields a
|
|
|
|
## well-formed stream of
|
2015-12-27 22:40:27 +00:00
|
|
|
## ``YamlStreamEvents``. Well-formed means that every ``yamlStartMap``
|
|
|
|
## is terminated by a ``yamlEndMap``, every ``yamlStartSequence`` is
|
|
|
|
## terminated by a ``yamlEndSequence`` and every ``yamlStartDocument``
|
2016-01-24 17:24:09 +00:00
|
|
|
## is terminated by a ``yamlEndDocument``. Moreover, every emitted map
|
|
|
|
## has an even number of children.
|
2015-12-27 22:40:27 +00:00
|
|
|
##
|
|
|
|
## The 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 it's an input.
|
2016-02-12 18:53:25 +00:00
|
|
|
##
|
|
|
|
##
|
|
|
|
backend: iterator(): YamlStreamEvent
|
|
|
|
peeked: bool
|
|
|
|
cached: YamlStreamEvent
|
|
|
|
|
2016-01-20 20:15:33 +00:00
|
|
|
TagLibrary* = ref object
|
2016-01-24 10:44:10 +00:00
|
|
|
## A ``TagLibrary`` maps tag URIs to ``TagId`` s.
|
2015-12-27 22:40:27 +00:00
|
|
|
##
|
2016-01-24 10:44:10 +00:00
|
|
|
## When `YamlParser <#YamlParser>`_ encounters tags not existing in the
|
2016-01-24 17:24:09 +00:00
|
|
|
## tag library, it will use
|
2016-02-15 18:46:21 +00:00
|
|
|
## `registerUri <#registerUri,TagLibrary,string>`_ to add
|
2016-01-24 17:24:09 +00:00
|
|
|
## 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>`_.
|
2015-12-27 22:40:27 +00:00
|
|
|
tags*: Table[string, TagId]
|
2016-01-04 20:46:33 +00:00
|
|
|
nextCustomTagId*: TagId
|
|
|
|
secondaryPrefix*: string
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
WarningCallback* = proc(line, column: int, lineContent: string,
|
2016-01-05 18:06:55 +00:00
|
|
|
message: string)
|
|
|
|
## Callback for parser warnings. Currently, this callback may be called
|
|
|
|
## on two occasions while parsing a YAML document stream:
|
|
|
|
##
|
|
|
|
## - If the version number in the ``%YAML`` directive does not match
|
|
|
|
## ``1.2``.
|
|
|
|
## - If there is an unknown directive encountered.
|
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
YamlParser* = ref object
|
2016-01-24 10:44:10 +00:00
|
|
|
## A parser object. Retains its ``TagLibrary`` across calls to
|
2016-01-24 18:15:50 +00:00
|
|
|
## `parse <#parse,YamlParser,Stream>`_. Can be used
|
2015-12-27 22:40:27 +00:00
|
|
|
## to access anchor names while parsing a YAML character stream, but
|
|
|
|
## only until the document goes out of scope (i.e. until
|
|
|
|
## ``yamlEndDocument`` is yielded).
|
2016-01-14 21:51:30 +00:00
|
|
|
tagLib: TagLibrary
|
2015-12-23 11:35:07 +00:00
|
|
|
anchors: OrderedTable[string, AnchorId]
|
2016-01-14 21:51:30 +00:00
|
|
|
callback: WarningCallback
|
2016-01-24 10:44:10 +00:00
|
|
|
lexer: BaseLexer
|
|
|
|
tokenstart: int
|
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
PresentationStyle* = enum
|
2016-01-04 20:46:33 +00:00
|
|
|
## Different styles for YAML character stream output.
|
2015-12-27 22:40:27 +00:00
|
|
|
##
|
2016-01-04 20:46:33 +00:00
|
|
|
## - ``ypsMinimal``: Single-line flow-only output which tries to
|
2015-12-27 22:40:27 +00:00
|
|
|
## use as few characters as possible.
|
2016-01-04 20:46:33 +00:00
|
|
|
## - ``ypsCanonical``: Canonical YAML output. Writes all tags except
|
2015-12-27 22:40:27 +00:00
|
|
|
## for the non-specific tags ``?`` and ``!``, uses flow style, quotes
|
|
|
|
## all string scalars.
|
2016-01-04 20:46:33 +00:00
|
|
|
## - ``ypsDefault``: Tries to be as human-readable as possible. Uses
|
2015-12-27 22:40:27 +00:00
|
|
|
## block style by default, but tries to condense maps and sequences
|
|
|
|
## which only contain scalar nodes into a single line using flow
|
|
|
|
## style.
|
2016-01-04 20:46:33 +00:00
|
|
|
## - ``ypsJson``: Omits the ``%YAML`` directive and the ``---``
|
2015-12-27 22:40:27 +00:00
|
|
|
## marker. Uses flow style. Flattens anchors and aliases, omits tags.
|
|
|
|
## Output will be parseable as JSON. ``YamlStream`` to dump may only
|
|
|
|
## contain one document.
|
2016-01-04 20:46:33 +00:00
|
|
|
## - ``ypsBlockOnly``: Formats all output in block style, does not use
|
|
|
|
## flow style at all.
|
2016-01-14 21:51:30 +00:00
|
|
|
psMinimal, psCanonical, psDefault, psJson, psBlockOnly
|
2016-01-05 17:14:11 +00:00
|
|
|
|
2016-02-12 19:44:38 +00:00
|
|
|
TagStyle* = enum
|
|
|
|
## Whether object should be serialized with explicit tags.
|
|
|
|
##
|
|
|
|
## - ``tsNone``: No tags will be outputted unless necessary.
|
|
|
|
## - ``tsRootOnly``: A tag will only be outputted for the root tag and
|
|
|
|
## where necessary.
|
|
|
|
## - ``tsAll``: Tags will be outputted for every object.
|
|
|
|
tsNone, tsRootOnly, tsAll
|
|
|
|
|
|
|
|
AnchorStyle* = enum
|
|
|
|
## How ref object should be serialized.
|
|
|
|
##
|
|
|
|
## - ``asNone``: No anchors will be outputted. Values present at
|
|
|
|
## multiple places in the content that should be
|
|
|
|
## serialized will be fully serialized at every occurence.
|
|
|
|
## If the content is cyclic, this will lead to an
|
|
|
|
## endless loop!
|
|
|
|
## - ``asTidy``: Anchors will only be generated for objects that
|
|
|
|
## actually occur more than once in the content to be
|
|
|
|
## serialized. This is a bit slower and needs more memory
|
|
|
|
## than ``asAlways``.
|
|
|
|
## - ``asAlways``: Achors will be generated for every ref object in the
|
|
|
|
## content to be serialized, regardless of whether the
|
|
|
|
## object is referenced again afterwards
|
|
|
|
asNone, asTidy, asAlways
|
|
|
|
|
|
|
|
RefNodeData = object
|
|
|
|
p: pointer
|
|
|
|
count: int
|
|
|
|
anchor: AnchorId
|
|
|
|
|
|
|
|
ConstructionContext* = ref object
|
|
|
|
## Context information for the process of constructing Nim values from
|
|
|
|
## YAML.
|
|
|
|
refs: Table[AnchorId, pointer]
|
|
|
|
|
|
|
|
SerializationContext* = ref object
|
|
|
|
## Context information for the process of serializing YAML from Nim
|
|
|
|
## values.
|
|
|
|
refsList: seq[RefNodeData]
|
|
|
|
style: AnchorStyle
|
|
|
|
|
|
|
|
RawYamlStream* = iterator(): YamlStreamEvent {.raises: [].} ## \
|
|
|
|
## Stream of ``YamlStreamEvent``s returned by ``representObject`` procs.
|
|
|
|
|
2016-01-05 18:06:55 +00:00
|
|
|
YamlLoadingError* = object of Exception
|
|
|
|
## Base class for all exceptions that may be raised during the process
|
|
|
|
## of loading a YAML character stream.
|
|
|
|
line*: int ## line number (1-based) where the error was encountered
|
|
|
|
column*: int ## \
|
|
|
|
## column number (1-based) where the error was encountered
|
|
|
|
lineContent*: string ## \
|
|
|
|
## content of the line where the error was encountered. Includes a
|
|
|
|
## second line with a marker ``^`` at the position where the error
|
2016-01-24 17:24:09 +00:00
|
|
|
## was encountered.
|
2016-01-05 18:06:55 +00:00
|
|
|
|
|
|
|
YamlParserError* = object of YamlLoadingError
|
2016-01-05 17:14:11 +00:00
|
|
|
## A parser error is raised if the character stream that is parsed is
|
|
|
|
## not a valid YAML character stream. This stream cannot and will not be
|
|
|
|
## parsed wholly nor partially and all events that have been emitted by
|
|
|
|
## the YamlStream the parser provides should be discarded.
|
|
|
|
##
|
|
|
|
## A character stream is invalid YAML if and only if at least one of the
|
|
|
|
## following conditions apply:
|
|
|
|
##
|
|
|
|
## - There are invalid characters in an element whose contents is
|
|
|
|
## restricted to a limited set of characters. For example, there are
|
|
|
|
## characters in a tag URI which are not valid URI characters.
|
|
|
|
## - An element has invalid indentation. This can happen for example if
|
|
|
|
## a block list element indicated by ``"- "`` is less indented than
|
|
|
|
## the element in the previous line, but there is no block sequence
|
|
|
|
## list open at the same indentation level.
|
|
|
|
## - The YAML structure is invalid. For example, an explicit block map
|
|
|
|
## indicated by ``"? "`` and ``": "`` may not suddenly have a block
|
|
|
|
## sequence item (``"- "``) at the same indentation level. Another
|
|
|
|
## possible violation is closing a flow style object with the wrong
|
|
|
|
## closing character (``}``, ``]``) or not closing it at all.
|
|
|
|
## - A custom tag shorthand is used that has not previously been
|
|
|
|
## declared with a ``%TAG`` directive.
|
|
|
|
## - Multiple tags or anchors are defined for the same node.
|
|
|
|
## - An alias is used which does not map to any anchor that has
|
|
|
|
## previously been declared in the same document.
|
|
|
|
## - An alias has a tag or anchor associated with it.
|
|
|
|
##
|
|
|
|
## Some elements in this list are vague. For a detailed description of a
|
|
|
|
## valid YAML character stream, see the YAML specification.
|
|
|
|
|
2016-01-05 18:58:46 +00:00
|
|
|
YamlPresenterJsonError* = object of Exception
|
|
|
|
## Exception that may be raised by the YAML presenter when it is
|
2016-01-05 18:06:55 +00:00
|
|
|
## instructed to output JSON, but is unable to do so. This may occur if:
|
|
|
|
##
|
|
|
|
## - The given `YamlStream <#YamlStream>`_ contains a map which has any
|
|
|
|
## non-scalar type as key.
|
|
|
|
## - Any float scalar bears a ``NaN`` or positive/negative infinity
|
2016-01-05 18:58:46 +00:00
|
|
|
## value
|
|
|
|
|
|
|
|
YamlPresenterOutputError* = object of Exception
|
|
|
|
## Exception that may be raised by the YAML presenter. This occurs if
|
2016-01-24 17:24:09 +00:00
|
|
|
## writing character data to the output stream raises any exception.
|
|
|
|
## The error that has occurred is available from ``parent``.
|
2016-01-05 18:58:46 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
YamlStreamError* = object of Exception
|
|
|
|
## Exception that may be raised by a ``YamlStream`` when the underlying
|
|
|
|
## backend raises an exception. The error that has occurred is
|
2016-01-24 17:24:09 +00:00
|
|
|
## available from ``parent``.
|
|
|
|
|
|
|
|
YamlConstructionError* = object of YamlLoadingError
|
|
|
|
## Exception that may be raised when constructing data objects from a
|
|
|
|
## `YamlStream <#YamlStream>`_. The fields ``line``, ``column`` and
|
|
|
|
## ``lineContent`` are only available if the costructing proc also does
|
|
|
|
## parsing, because otherwise this information is not available to the
|
|
|
|
## costruction proc.
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2015-12-26 12:16:57 +00:00
|
|
|
const
|
2015-12-26 17:40:23 +00:00
|
|
|
# failsafe schema
|
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagExclamationMark*: TagId = 0.TagId ## ``!`` non-specific tag
|
|
|
|
yTagQuestionMark* : TagId = 1.TagId ## ``?`` non-specific tag
|
|
|
|
yTagString* : TagId = 2.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!str <http://yaml.org/type/str.html >`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagSequence* : TagId = 3.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!seq <http://yaml.org/type/seq.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagMap* : TagId = 4.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!map <http://yaml.org/type/map.html>`_ tag
|
2015-12-26 17:40:23 +00:00
|
|
|
|
|
|
|
# json & core schema
|
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagNull* : TagId = 5.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!null <http://yaml.org/type/null.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagBoolean* : TagId = 6.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!bool <http://yaml.org/type/bool.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagInteger* : TagId = 7.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!int <http://yaml.org/type/int.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagFloat* : TagId = 8.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!float <http://yaml.org/type/float.html>`_ tag
|
2015-12-26 17:40:23 +00:00
|
|
|
|
|
|
|
# other language-independent YAML types (from http://yaml.org/type/ )
|
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagOrderedMap* : TagId = 9.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!omap <http://yaml.org/type/omap.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagPairs* : TagId = 10.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!pairs <http://yaml.org/type/pairs.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagSet* : TagId = 11.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!set <http://yaml.org/type/set.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagBinary* : TagId = 12.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!binary <http://yaml.org/type/binary.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagMerge* : TagId = 13.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!merge <http://yaml.org/type/merge.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagTimestamp* : TagId = 14.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!timestamp <http://yaml.org/type/timestamp.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagValue* : TagId = 15.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!value <http://yaml.org/type/value.html>`_ tag
|
2015-12-27 22:40:27 +00:00
|
|
|
yTagYaml* : TagId = 16.TagId ## \
|
2015-12-27 15:36:32 +00:00
|
|
|
## `!!yaml <http://yaml.org/type/yaml.html>`_ tag
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2015-12-27 22:40:27 +00:00
|
|
|
yFirstCustomTagId* : TagId = 1000.TagId ## \
|
|
|
|
## The first ``TagId`` which should be assigned to an URI that does not
|
|
|
|
## exist in the ``YamlTagLibrary`` which is used for parsing.
|
|
|
|
|
|
|
|
yAnchorNone*: AnchorId = (-1).AnchorId ## \
|
|
|
|
## yielded when no anchor was defined for a YAML node
|
2016-01-04 20:46:33 +00:00
|
|
|
|
|
|
|
yamlTagRepositoryPrefix* = "tag:yaml.org,2002:"
|
2016-02-12 19:44:38 +00:00
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
# interface
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc `==`*(left: YamlStreamEvent, right: YamlStreamEvent): bool {.raises: [].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## compares all existing fields of the given items
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc `$`*(event: YamlStreamEvent): string {.raises: [].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## outputs a human-readable string describing the given event
|
2015-12-26 12:16:57 +00:00
|
|
|
|
2016-01-14 18:58:38 +00:00
|
|
|
proc startDocEvent*(): YamlStreamEvent {.inline, raises: [].}
|
|
|
|
proc endDocEvent*(): YamlStreamEvent {.inline, raises: [].}
|
2015-12-29 17:22:55 +00:00
|
|
|
proc startMapEvent*(tag: TagId = yTagQuestionMark,
|
2016-01-14 18:58:38 +00:00
|
|
|
anchor: AnchorId = yAnchorNone):
|
|
|
|
YamlStreamEvent {.inline, raises: [].}
|
|
|
|
proc endMapEvent*(): YamlStreamEvent {.inline, raises: [].}
|
2015-12-29 17:22:55 +00:00
|
|
|
proc startSeqEvent*(tag: TagId = yTagQuestionMark,
|
2016-01-14 18:58:38 +00:00
|
|
|
anchor: AnchorId = yAnchorNone):
|
|
|
|
YamlStreamEvent {.inline, raises: [].}
|
|
|
|
proc endSeqEvent*(): YamlStreamEvent {.inline, raises: [].}
|
2015-12-29 17:22:55 +00:00
|
|
|
proc scalarEvent*(content: string = "", tag: TagId = yTagQuestionMark,
|
2016-01-14 18:58:38 +00:00
|
|
|
anchor: AnchorId = yAnchorNone):
|
|
|
|
YamlStreamEvent {.inline, raises: [].}
|
2016-01-20 20:15:33 +00:00
|
|
|
proc aliasEvent*(anchor: AnchorId): YamlStreamEvent {.inline, raises: [].}
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2015-12-26 12:16:57 +00:00
|
|
|
proc `==`*(left, right: TagId): bool {.borrow.}
|
2015-12-29 17:22:55 +00:00
|
|
|
proc `$`*(id: TagId): string
|
2015-12-26 12:16:57 +00:00
|
|
|
proc hash*(id: TagId): Hash {.borrow.}
|
|
|
|
|
|
|
|
proc `==`*(left, right: AnchorId): bool {.borrow.}
|
|
|
|
proc `$`*(id: AnchorId): string {.borrow.}
|
|
|
|
proc hash*(id: AnchorId): Hash {.borrow.}
|
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc initYamlStream*(backend: iterator(): YamlStreamEvent):
|
|
|
|
YamlStream {.raises: [].}
|
2016-02-12 19:44:38 +00:00
|
|
|
## Creates a new ``YamlStream`` that uses the given iterator as backend.
|
2016-02-12 18:53:25 +00:00
|
|
|
proc next*(s: var YamlStream): YamlStreamEvent {.raises: [YamlStreamError].}
|
2016-02-12 19:44:38 +00:00
|
|
|
## Get the next item of the stream. Requires ``finished(s) == true``.
|
|
|
|
## If the backend yields an exception, that exception will be encapsulated
|
|
|
|
## into a ``YamlStreamError``, which will be raised.
|
2016-02-12 18:53:25 +00:00
|
|
|
proc peek*(s: var YamlStream): YamlStreamEvent {.raises: [YamlStreamError].}
|
2016-02-12 19:44:38 +00:00
|
|
|
## Get the next item of the stream without advancing the stream.
|
|
|
|
## Requires ``finished(s) == true``. Handles exceptions of the backend like
|
|
|
|
## ``next()``.
|
2016-02-12 18:53:25 +00:00
|
|
|
proc `peek=`*(s: var YamlStream, value: YamlStreamEvent) {.raises: [].}
|
2016-02-12 19:44:38 +00:00
|
|
|
## Set the next item of the stream. Will replace a previously peeked item,
|
|
|
|
## if one exists.
|
2016-02-12 18:53:25 +00:00
|
|
|
proc finished*(s: var YamlStream): bool {.raises: [YamlStreamError].}
|
2016-02-12 19:44:38 +00:00
|
|
|
## ``true`` if no more items are available in the stream. Handles exceptions
|
|
|
|
## of the backend like ``next()``.
|
|
|
|
iterator items*(s: var YamlStream): YamlStreamEvent
|
|
|
|
{.raises: [YamlStreamError].} =
|
|
|
|
## Iterate over all items of the stream. You may not use ``peek()`` on the
|
|
|
|
## stream while iterating.
|
2016-02-12 18:53:25 +00:00
|
|
|
if s.peeked:
|
|
|
|
s.peeked = false
|
|
|
|
yield s.cached
|
|
|
|
while true:
|
|
|
|
var event: YamlStreamEvent
|
|
|
|
try:
|
|
|
|
event = s.backend()
|
|
|
|
if finished(s.backend): break
|
|
|
|
except AssertionError: raise
|
|
|
|
except YamlStreamError:
|
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur.parent
|
|
|
|
raise e
|
|
|
|
except Exception:
|
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
yield event
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc initTagLibrary*(): TagLibrary {.raises: [].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## initializes the ``tags`` table and sets ``nextCustomTagId`` to
|
|
|
|
## ``yFirstCustomTagId``.
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc registerUri*(tagLib: TagLibrary, uri: string): TagId {.raises: [].}
|
2016-01-24 10:44:10 +00:00
|
|
|
## registers a custom tag URI with a ``TagLibrary``. The URI will get
|
2015-12-27 22:40:27 +00:00
|
|
|
## the ``TagId`` ``nextCustomTagId``, which will be incremented.
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc uri*(tagLib: TagLibrary, id: TagId): string {.raises: [KeyError].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## retrieve the URI a ``TagId`` maps to.
|
2015-12-26 17:40:23 +00:00
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc initFailsafeTagLibrary*(): TagLibrary {.raises: [].}
|
2016-01-24 10:44:10 +00:00
|
|
|
## Contains only:
|
|
|
|
## - ``!``
|
|
|
|
## - ``?``
|
|
|
|
## - ``!!str``
|
|
|
|
## - ``!!map``
|
|
|
|
## - ``!!seq``
|
2016-01-24 17:24:09 +00:00
|
|
|
proc initCoreTagLibrary*(): TagLibrary {.raises: [].}
|
2016-01-24 10:44:10 +00:00
|
|
|
## Contains everything in ``initFailsafeTagLibrary`` plus:
|
|
|
|
## - ``!!null``
|
|
|
|
## - ``!!bool``
|
|
|
|
## - ``!!int``
|
|
|
|
## - ``!!float``
|
2016-01-24 17:24:09 +00:00
|
|
|
proc initExtendedTagLibrary*(): TagLibrary {.raises: [].}
|
2016-01-24 10:44:10 +00:00
|
|
|
## Contains everything from ``initCoreTagLibrary`` plus:
|
|
|
|
## - ``!!omap``
|
|
|
|
## - ``!!pairs``
|
|
|
|
## - ``!!set``
|
|
|
|
## - ``!!binary``
|
|
|
|
## - ``!!merge``
|
|
|
|
## - ``!!timestamp``
|
|
|
|
## - ``!!value``
|
|
|
|
## - ``!!yaml``
|
2015-12-26 12:16:57 +00:00
|
|
|
|
2016-01-14 18:58:38 +00:00
|
|
|
proc guessType*(scalar: string): TypeHint {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Parse scalar string according to the RegEx table documented at
|
|
|
|
## `TypeHint <#TypeHind>`_.
|
2016-01-14 18:58:38 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
proc newYamlParser*(tagLib: TagLibrary = initExtendedTagLibrary(),
|
2016-01-24 17:24:09 +00:00
|
|
|
callback: WarningCallback = nil): YamlParser {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Creates a YAML parser. if ``callback`` is not ``nil``, it will be called
|
|
|
|
## whenever the parser yields a warning.
|
2016-01-24 17:24:09 +00:00
|
|
|
|
|
|
|
proc getLineNumber*(p: YamlParser): int {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Get the line number (1-based) of the recently yielded parser token.
|
|
|
|
## Useful for error reporting at later loading stages.
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc getColNumber*(p: YamlParser): int {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Get the column number (1-based) of the recently yielded parser token.
|
|
|
|
## Useful for error reporting at later parsing stages.
|
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc getLineContent*(p: YamlParser, marker: bool = true): string {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Get the content of the input line containing the recently yielded parser
|
|
|
|
## token. Useful for error reporting at later parsing stages. The line will
|
|
|
|
## be terminated by ``"\n"``. If ``marker`` is ``true``, a second line will
|
|
|
|
## be returned containing a ``^`` at the position of the recent parser
|
|
|
|
## token.
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc parse*(p: YamlParser, s: Stream): YamlStream {.raises: [].}
|
2016-02-02 20:14:56 +00:00
|
|
|
## Parse the given stream as YAML character stream.
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructJson*(s: var YamlStream): seq[JsonNode]
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
2015-12-29 11:33:41 +00:00
|
|
|
## Construct an in-memory JSON tree from a YAML event stream. The stream may
|
2016-01-05 18:58:46 +00:00
|
|
|
## not contain any tags apart from those in ``coreTagLibrary``. Anchors and
|
|
|
|
## aliases will be resolved. Maps in the input must not contain
|
2015-12-29 11:33:41 +00:00
|
|
|
## non-scalars as keys. Each element of the result represents one document
|
|
|
|
## in the YAML stream.
|
2015-12-28 18:25:53 +00:00
|
|
|
##
|
|
|
|
## **Warning:** 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.
|
2016-01-05 18:58:46 +00:00
|
|
|
|
|
|
|
proc loadToJson*(s: Stream): seq[JsonNode]
|
2016-02-12 18:53:25 +00:00
|
|
|
{.raises: [IOError, YamlParserError, YamlConstructionError,
|
|
|
|
OutOfMemError].}
|
2016-01-24 17:24:09 +00:00
|
|
|
## Uses `YamlParser <#YamlParser>`_ and
|
2016-01-05 18:58:46 +00:00
|
|
|
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
|
|
|
## from a YAML character stream.
|
2015-12-27 22:40:27 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc present*(s: var YamlStream, target: Stream, tagLib: TagLibrary,
|
2016-01-14 21:51:30 +00:00
|
|
|
style: PresentationStyle = psDefault,
|
2016-01-05 18:58:46 +00:00
|
|
|
indentationStep: int = 2) {.raises: [YamlPresenterJsonError,
|
|
|
|
YamlPresenterOutputError,
|
2016-02-12 18:53:25 +00:00
|
|
|
YamlStreamError].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## Convert ``s`` to a YAML character stream and write it to ``target``.
|
|
|
|
|
2016-01-14 21:51:30 +00:00
|
|
|
proc transform*(input: Stream, output: Stream, style: PresentationStyle,
|
2016-01-24 17:24:09 +00:00
|
|
|
indentationStep: int = 2) {.raises: [IOError, YamlParserError,
|
|
|
|
YamlPresenterJsonError,
|
2016-02-12 18:53:25 +00:00
|
|
|
YamlPresenterOutputError,
|
|
|
|
OutOfMemError].}
|
2015-12-27 22:40:27 +00:00
|
|
|
## Parser ``input`` as YAML character stream and then dump it to ``output``
|
2016-01-24 17:24:09 +00:00
|
|
|
## while resolving non-specific tags to the ones in the YAML core tag
|
|
|
|
## library.
|
2015-12-27 15:36:32 +00:00
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
# implementation
|
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
include private.tagLibrary
|
2015-12-29 17:22:55 +00:00
|
|
|
include private.events
|
2015-12-26 17:40:23 +00:00
|
|
|
include private.json
|
2016-01-14 18:58:38 +00:00
|
|
|
include private.presenter
|
2016-01-20 20:15:33 +00:00
|
|
|
include private.hints
|
2016-02-12 18:53:25 +00:00
|
|
|
include private.fastparse
|
2016-02-12 19:44:38 +00:00
|
|
|
include private.streams
|
|
|
|
include private.serialization
|