2015-12-27 15:36:32 +00:00
|
|
|
import streams, unicode, lexbase, tables, strutils, json, hashes, queues
|
2015-12-23 11:35:07 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
YamlTypeHint* = enum
|
|
|
|
yTypeInteger, yTypeFloat, yTypeBoolean, yTypeNull, yTypeString,
|
|
|
|
yTypeUnknown
|
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
YamlStreamEventKind* = enum
|
2015-12-23 11:35:07 +00:00
|
|
|
yamlStartDocument, yamlEndDocument, yamlStartMap, yamlEndMap,
|
|
|
|
yamlStartSequence, yamlEndSequence, yamlScalar, yamlAlias,
|
|
|
|
yamlError, yamlWarning
|
|
|
|
|
|
|
|
TagId* = distinct int
|
|
|
|
AnchorId* = distinct int
|
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
YamlStreamEvent* = object
|
|
|
|
case kind*: YamlStreamEventKind
|
2015-12-27 15:36:32 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
mapAnchor* : AnchorId
|
|
|
|
mapTag* : TagId
|
|
|
|
mapMayHaveKeyObjects* : bool
|
|
|
|
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)
|
2015-12-23 17:12:51 +00:00
|
|
|
scalarType* : YamlTypeHint
|
2015-12-23 11:35:07 +00:00
|
|
|
of yamlEndMap, yamlEndSequence, yamlStartDocument, yamlEndDocument:
|
|
|
|
discard
|
|
|
|
of yamlAlias:
|
|
|
|
aliasTarget* : AnchorId
|
|
|
|
of yamlError, yamlWarning:
|
|
|
|
description* : string
|
|
|
|
line* : int
|
|
|
|
column* : int
|
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
YamlStream* = iterator(): YamlStreamEvent
|
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
YamlTagLibrary* = object
|
|
|
|
tags: Table[string, TagId]
|
|
|
|
nextCustomTagId*: TagId
|
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
YamlSequentialParser* = ref object
|
2015-12-26 17:40:23 +00:00
|
|
|
tagLib: YamlTagLibrary
|
2015-12-23 11:35:07 +00:00
|
|
|
anchors: OrderedTable[string, AnchorId]
|
2015-12-27 15:36:32 +00:00
|
|
|
|
|
|
|
YamlDumpStyle* = enum
|
|
|
|
yDumpMinimal, yDumpCanonical, yDumpDefault, yDumpJson, yDumpBlockOnly
|
2015-12-26 12:16:57 +00:00
|
|
|
const
|
2015-12-26 17:40:23 +00:00
|
|
|
# failsafe schema
|
|
|
|
|
2015-12-27 15:36:32 +00:00
|
|
|
tagExclamationMark*: TagId = 0.TagId ## ``!`` non-specific tag
|
|
|
|
tagQuestionMark* : TagId = 1.TagId ## ``?`` non-specific tag
|
|
|
|
tagString* : TagId = 2.TagId ## \
|
|
|
|
## `!!str <http://yaml.org/type/str.html >`_ tag
|
|
|
|
tagSequence* : TagId = 3.TagId ## \
|
|
|
|
## `!!seq <http://yaml.org/type/seq.html>`_ tag
|
|
|
|
tagMap* : TagId = 4.TagId ## \
|
|
|
|
## `!!map <http://yaml.org/type/map.html>`_ tag
|
2015-12-26 17:40:23 +00:00
|
|
|
|
|
|
|
# json & core schema
|
|
|
|
|
2015-12-27 15:36:32 +00:00
|
|
|
tagNull* : TagId = 5.TagId ## \
|
|
|
|
## `!!null <http://yaml.org/type/null.html>`_ tag
|
|
|
|
tagBoolean* : TagId = 6.TagId ## \
|
|
|
|
## `!!bool <http://yaml.org/type/bool.html>`_ tag
|
|
|
|
tagInteger* : TagId = 7.TagId ## \
|
|
|
|
## `!!int <http://yaml.org/type/int.html>`_ tag
|
|
|
|
tagFloat* : TagId = 8.TagId ## \
|
|
|
|
## `!!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 15:36:32 +00:00
|
|
|
tagOrderedMap* : TagId = 9.TagId ## \
|
|
|
|
## `!!omap <http://yaml.org/type/omap.html>`_ tag
|
|
|
|
tagPairs* : TagId = 10.TagId ## \
|
|
|
|
## `!!pairs <http://yaml.org/type/pairs.html>`_ tag
|
|
|
|
tagSet* : TagId = 11.TagId ## \
|
|
|
|
## `!!set <http://yaml.org/type/set.html>`_ tag
|
|
|
|
tagBinary* : TagId = 12.TagId ## \
|
|
|
|
## `!!binary <http://yaml.org/type/binary.html>`_ tag
|
|
|
|
tagMerge* : TagId = 13.TagId ## \
|
|
|
|
## `!!merge <http://yaml.org/type/merge.html>`_ tag
|
|
|
|
tagTimestamp* : TagId = 14.TagId ## \
|
|
|
|
## `!!timestamp <http://yaml.org/type/timestamp.html>`_ tag
|
|
|
|
tagValue* : TagId = 15.TagId ## \
|
|
|
|
## `!!value <http://yaml.org/type/value.html>`_ tag
|
|
|
|
tagYaml* : TagId = 16.TagId ## \
|
|
|
|
## `!!yaml <http://yaml.org/type/yaml.html>`_ tag
|
2015-12-26 17:40:23 +00:00
|
|
|
|
|
|
|
anchorNone*: AnchorId = (-1).AnchorId # no anchor defined
|
2015-12-26 12:16:57 +00:00
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
# interface
|
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
proc `==`*(left: YamlStreamEvent, right: YamlStreamEvent): bool
|
2015-12-27 17:30:20 +00:00
|
|
|
proc `$`*(event: YamlStreamEvent): string
|
2015-12-26 12:16:57 +00:00
|
|
|
|
|
|
|
proc `==`*(left, right: TagId): bool {.borrow.}
|
|
|
|
proc `$`*(id: TagId): string {.borrow.}
|
|
|
|
proc hash*(id: TagId): Hash {.borrow.}
|
|
|
|
|
|
|
|
proc `==`*(left, right: AnchorId): bool {.borrow.}
|
|
|
|
proc `$`*(id: AnchorId): string {.borrow.}
|
|
|
|
proc hash*(id: AnchorId): Hash {.borrow.}
|
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
proc initTagLibrary*(): YamlTagLibrary
|
|
|
|
proc registerUri*(tagLib: var YamlTagLibrary, uri: string): TagId
|
|
|
|
proc uri*(tagLib: YamlTagLibrary, id: TagId): string
|
|
|
|
|
|
|
|
# these should be consts, but the Nim VM still has problems handling tables
|
|
|
|
# properly, so we use constructor procs instead.
|
2015-12-26 12:16:57 +00:00
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
proc failsafeTagLibrary*(): YamlTagLibrary
|
|
|
|
proc coreTagLibrary*(): YamlTagLibrary
|
|
|
|
proc extendedTagLibrary*(): YamlTagLibrary
|
2015-12-26 12:16:57 +00:00
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
proc newParser*(tagLib: YamlTagLibrary): YamlSequentialParser
|
2015-12-26 12:16:57 +00:00
|
|
|
|
|
|
|
proc anchor*(parser: YamlSequentialParser, id: AnchorId): string
|
|
|
|
|
2015-12-26 12:39:43 +00:00
|
|
|
proc parse*(parser: YamlSequentialParser, s: Stream): YamlStream
|
2015-12-23 11:35:07 +00:00
|
|
|
|
2015-12-24 14:21:49 +00:00
|
|
|
proc parseToJson*(s: Stream): seq[JsonNode]
|
|
|
|
proc parseToJson*(s: string): seq[JsonNode]
|
|
|
|
|
2015-12-27 15:36:32 +00:00
|
|
|
proc dump*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
|
|
|
|
style: YamlDumpStyle = yDumpDefault, indentationStep: int = 2)
|
|
|
|
|
|
|
|
proc transform*(input: Stream, output: Stream, style: YamlDumpStyle,
|
|
|
|
indentationStep: int = 2)
|
|
|
|
|
2015-12-23 11:35:07 +00:00
|
|
|
# implementation
|
|
|
|
|
|
|
|
include private.lexer
|
2015-12-26 17:40:23 +00:00
|
|
|
include private.tagLibrary
|
2015-12-24 14:21:49 +00:00
|
|
|
include private.sequential
|
2015-12-26 17:40:23 +00:00
|
|
|
include private.json
|
2015-12-27 15:36:32 +00:00
|
|
|
include private.dumper
|