mirror of https://github.com/status-im/NimYAML.git
added marks and styles to YamlNode
This commit is contained in:
parent
be18033461
commit
947207dd02
59
yaml/dom.nim
59
yaml/dom.nim
|
@ -26,6 +26,10 @@ import data, stream, taglib, serialization, private/internal, parser,
|
||||||
when defined(gcArc) and not defined(gcOrc):
|
when defined(gcArc) and not defined(gcOrc):
|
||||||
{.error: "NimYAML's DOM API only supports ORC because ARC can't deal with cycles".}
|
{.error: "NimYAML's DOM API only supports ORC because ARC can't deal with cycles".}
|
||||||
|
|
||||||
|
const
|
||||||
|
defaultMark: Mark = (1.Positive, 1.Positive) ## \
|
||||||
|
## used for events that are not generated from input.
|
||||||
|
|
||||||
when defined(nimNoNil):
|
when defined(nimNoNil):
|
||||||
{.experimental: "notnil".}
|
{.experimental: "notnil".}
|
||||||
type
|
type
|
||||||
|
@ -37,10 +41,17 @@ type
|
||||||
|
|
||||||
YamlNodeObj* = object
|
YamlNodeObj* = object
|
||||||
tag*: Tag
|
tag*: Tag
|
||||||
|
startPos*, endPos*: Mark
|
||||||
case kind*: YamlNodeKind
|
case kind*: YamlNodeKind
|
||||||
of yScalar: content*: string
|
of yScalar:
|
||||||
of ySequence: elems*: seq[YamlNode]
|
content* : string
|
||||||
of yMapping: fields*: TableRef[YamlNode, YamlNode]
|
scalarStyle*: ScalarStyle
|
||||||
|
of ySequence:
|
||||||
|
elems* : seq[YamlNode]
|
||||||
|
seqStyle*: CollectionStyle
|
||||||
|
of yMapping:
|
||||||
|
fields* : TableRef[YamlNode, YamlNode]
|
||||||
|
mapStyle*: CollectionStyle
|
||||||
# compiler does not like Table[YamlNode, YamlNode]
|
# compiler does not like Table[YamlNode, YamlNode]
|
||||||
|
|
||||||
YamlDocument* = object
|
YamlDocument* = object
|
||||||
|
@ -122,16 +133,23 @@ proc `$`*(n: YamlNode): string =
|
||||||
result.setLen(result.len - 1)
|
result.setLen(result.len - 1)
|
||||||
result[^1] = '}'
|
result[^1] = '}'
|
||||||
|
|
||||||
proc newYamlNode*(content: string, tag: Tag = yTagQuestionMark): YamlNode =
|
proc newYamlNode*(content: string, tag: Tag = yTagQuestionMark,
|
||||||
YamlNode(kind: yScalar, content: content, tag: tag)
|
style: ScalarStyle = ssAny,
|
||||||
|
startPos, endPos: Mark = defaultMark): YamlNode =
|
||||||
|
YamlNode(kind: yScalar, content: content, tag: tag,
|
||||||
|
startPos: startPos, endPos: endPos)
|
||||||
|
|
||||||
proc newYamlNode*(elems: openarray[YamlNode], tag: Tag = yTagQuestionMark):
|
proc newYamlNode*(elems: openarray[YamlNode], tag: Tag = yTagQuestionMark,
|
||||||
YamlNode =
|
style: CollectionStyle = csAny,
|
||||||
YamlNode(kind: ySequence, elems: @elems, tag: tag)
|
startPos, endPos: Mark = defaultMark): YamlNode =
|
||||||
|
YamlNode(kind: ySequence, elems: @elems, tag: tag,
|
||||||
|
startPos: startPos, endPos: endPos)
|
||||||
|
|
||||||
proc newYamlNode*(fields: openarray[(YamlNode, YamlNode)],
|
proc newYamlNode*(fields: openarray[(YamlNode, YamlNode)],
|
||||||
tag: Tag = yTagQuestionMark): YamlNode =
|
tag: Tag = yTagQuestionMark, style: CollectionStyle = csAny,
|
||||||
YamlNode(kind: yMapping, fields: newTable(fields), tag: tag)
|
startPos, endPos: Mark = defaultMark): YamlNode =
|
||||||
|
YamlNode(kind: yMapping, fields: newTable(fields), tag: tag,
|
||||||
|
startPos: startPos, endPos: endPos)
|
||||||
|
|
||||||
proc initYamlDoc*(root: YamlNode): YamlDocument =
|
proc initYamlDoc*(root: YamlNode): YamlDocument =
|
||||||
result = YamlDocument(root: root)
|
result = YamlDocument(root: root)
|
||||||
|
@ -151,7 +169,9 @@ proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
||||||
of yamlStartMap:
|
of yamlStartMap:
|
||||||
result = YamlNode(tag: start.mapProperties.tag,
|
result = YamlNode(tag: start.mapProperties.tag,
|
||||||
kind: yMapping,
|
kind: yMapping,
|
||||||
fields: newTable[YamlNode, YamlNode]())
|
fields: newTable[YamlNode, YamlNode](),
|
||||||
|
mapStyle: start.mapStyle,
|
||||||
|
startPos: start.startPos, endPos: start.endPos)
|
||||||
while s.peek().kind != yamlEndMap:
|
while s.peek().kind != yamlEndMap:
|
||||||
var
|
var
|
||||||
key: YamlNode = nil
|
key: YamlNode = nil
|
||||||
|
@ -166,7 +186,9 @@ proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
||||||
of yamlStartSeq:
|
of yamlStartSeq:
|
||||||
result = YamlNode(tag: start.seqProperties.tag,
|
result = YamlNode(tag: start.seqProperties.tag,
|
||||||
kind: ySequence,
|
kind: ySequence,
|
||||||
elems: newSeq[YamlNode]())
|
elems: newSeq[YamlNode](),
|
||||||
|
seqStyle: start.seqStyle,
|
||||||
|
startPos: start.startPos, endPos: start.endPos)
|
||||||
while s.peek().kind != yamlEndSeq:
|
while s.peek().kind != yamlEndSeq:
|
||||||
var item: YamlNode = nil
|
var item: YamlNode = nil
|
||||||
constructChild(s, c, item)
|
constructChild(s, c, item)
|
||||||
|
@ -175,7 +197,8 @@ proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
||||||
discard s.next()
|
discard s.next()
|
||||||
of yamlScalar:
|
of yamlScalar:
|
||||||
result = YamlNode(tag: start.scalarProperties.tag,
|
result = YamlNode(tag: start.scalarProperties.tag,
|
||||||
kind: yScalar)
|
kind: yScalar, scalarStyle: start.scalarStyle,
|
||||||
|
startPos: start.startPos, endPos: start.endPos)
|
||||||
shallowCopy(result.content, start.scalarContent)
|
shallowCopy(result.content, start.scalarContent)
|
||||||
addAnchor(c, start.scalarProperties.anchor)
|
addAnchor(c, start.scalarProperties.anchor)
|
||||||
of yamlAlias:
|
of yamlAlias:
|
||||||
|
@ -220,13 +243,17 @@ proc representChild*(value: YamlNodeObj, ts: TagStyle,
|
||||||
c: SerializationContext) =
|
c: SerializationContext) =
|
||||||
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
||||||
case value.kind
|
case value.kind
|
||||||
of yScalar: c.put(scalarEvent(value.content, value.tag))
|
of yScalar:
|
||||||
|
c.put(scalarEvent(value.content, value.tag, style = value.scalarStyle,
|
||||||
|
startPos = value.startPos, endPos = value.endPos))
|
||||||
of ySequence:
|
of ySequence:
|
||||||
c.put(startSeqEvent(tag = value.tag))
|
c.put(startSeqEvent(tag = value.tag, style = value.seqStyle,
|
||||||
|
startPos = value.startPos, endPos = value.endPos))
|
||||||
for item in value.elems: representChild(item, childTagStyle, c)
|
for item in value.elems: representChild(item, childTagStyle, c)
|
||||||
c.put(endSeqEvent())
|
c.put(endSeqEvent())
|
||||||
of yMapping:
|
of yMapping:
|
||||||
c.put(startMapEvent(tag = value.tag))
|
c.put(startMapEvent(tag = value.tag, style = value.mapStyle,
|
||||||
|
startPos = value.startPos, endPos = value.endPos))
|
||||||
for key, value in value.fields.pairs:
|
for key, value in value.fields.pairs:
|
||||||
representChild(key, childTagStyle, c)
|
representChild(key, childTagStyle, c)
|
||||||
representChild(value, childTagStyle, c)
|
representChild(value, childTagStyle, c)
|
||||||
|
|
Loading…
Reference in New Issue