2015-12-28 21:24:05 +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-24 14:21:49 +00:00
|
|
|
type
|
|
|
|
Level = tuple[node: JsonNode, key: string]
|
|
|
|
|
2015-12-26 12:16:57 +00:00
|
|
|
proc initLevel(node: JsonNode): Level = (node: node, key: cast[string](nil))
|
2015-12-24 14:21:49 +00:00
|
|
|
|
2015-12-26 17:40:23 +00:00
|
|
|
proc jsonFromScalar(content: string, tag: TagId,
|
|
|
|
typeHint: YamlTypeHint): JsonNode =
|
2015-12-24 14:21:49 +00:00
|
|
|
new(result)
|
2015-12-26 17:40:23 +00:00
|
|
|
var mappedType: YamlTypeHint
|
|
|
|
|
|
|
|
case tag
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagQuestionMark:
|
2015-12-26 17:40:23 +00:00
|
|
|
mappedType = typeHint
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagExclamationMark, yTagString:
|
2015-12-26 17:40:23 +00:00
|
|
|
mappedType = yTypeString
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagBoolean:
|
2015-12-27 20:36:47 +00:00
|
|
|
case typeHint
|
|
|
|
of yTypeBoolTrue:
|
|
|
|
mappedType = yTypeBoolTrue
|
|
|
|
of yTypeBoolFalse:
|
|
|
|
mappedType = yTypeBoolFalse
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Invalid boolean value: " & content)
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagInteger:
|
2015-12-26 17:40:23 +00:00
|
|
|
mappedType = yTypeInteger
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagNull:
|
2015-12-26 17:40:23 +00:00
|
|
|
mappedType = yTypeNull
|
2015-12-27 22:40:27 +00:00
|
|
|
of yTagFloat:
|
2015-12-26 17:40:23 +00:00
|
|
|
mappedType = yTypeFloat
|
2015-12-27 20:36:47 +00:00
|
|
|
## TODO: NaN, inf
|
2015-12-26 17:40:23 +00:00
|
|
|
else:
|
|
|
|
mappedType = yTypeUnknown
|
|
|
|
|
|
|
|
case mappedType
|
2015-12-24 14:21:49 +00:00
|
|
|
of yTypeInteger:
|
|
|
|
result.kind = JInt
|
|
|
|
result.num = parseBiggestInt(content)
|
|
|
|
of yTypeFloat:
|
|
|
|
result.kind = JFloat
|
|
|
|
result.fnum = parseFloat(content)
|
2015-12-28 18:25:53 +00:00
|
|
|
of yTypeFloatInf:
|
|
|
|
result.kind = JFloat
|
|
|
|
result.fnum = if content[0] == '-': NegInf else: Inf
|
|
|
|
of yTypeFloatNaN:
|
|
|
|
result.kind = JFloat
|
|
|
|
result.fnum = NaN
|
2015-12-27 20:36:47 +00:00
|
|
|
of yTypeBoolTrue:
|
2015-12-24 14:21:49 +00:00
|
|
|
result.kind = JBool
|
2015-12-27 20:36:47 +00:00
|
|
|
result.bval = true
|
|
|
|
of yTypeBoolFalse:
|
|
|
|
result.kind = JBool
|
|
|
|
result.bval = false
|
2015-12-24 14:21:49 +00:00
|
|
|
of yTypeNull:
|
|
|
|
result.kind = JNull
|
|
|
|
else:
|
|
|
|
result.kind = JString
|
|
|
|
result.str = content
|
|
|
|
|
2015-12-29 11:33:41 +00:00
|
|
|
proc constructJson*(s: YamlStream): seq[JsonNode] =
|
2015-12-24 14:21:49 +00:00
|
|
|
newSeq(result, 0)
|
|
|
|
|
|
|
|
var
|
2015-12-26 17:40:23 +00:00
|
|
|
levels = newSeq[Level]()
|
|
|
|
anchors = initTable[AnchorId, JsonNode]()
|
2015-12-24 14:21:49 +00:00
|
|
|
|
2015-12-29 11:33:41 +00:00
|
|
|
for event in s():
|
2015-12-24 14:21:49 +00:00
|
|
|
case event.kind
|
|
|
|
of yamlStartDocument:
|
|
|
|
# we don't need to do anything here; root node will be created
|
|
|
|
# by first scalar, sequence or map event
|
|
|
|
discard
|
|
|
|
of yamlEndDocument:
|
|
|
|
# we can savely assume that levels has e length of exactly 1.
|
|
|
|
result.add(levels.pop().node)
|
|
|
|
of yamlStartSequence:
|
2015-12-26 12:16:57 +00:00
|
|
|
levels.add(initLevel(newJArray()))
|
2015-12-27 22:40:27 +00:00
|
|
|
if event.seqAnchor != yAnchorNone:
|
2015-12-27 15:36:32 +00:00
|
|
|
anchors[event.seqAnchor] = levels[levels.high].node
|
2015-12-24 14:21:49 +00:00
|
|
|
of yamlStartMap:
|
2015-12-26 12:16:57 +00:00
|
|
|
levels.add(initLevel(newJObject()))
|
2015-12-27 22:40:27 +00:00
|
|
|
if event.mapAnchor != yAnchorNone:
|
2015-12-27 15:36:32 +00:00
|
|
|
anchors[event.mapAnchor] = levels[levels.high].node
|
2015-12-24 14:21:49 +00:00
|
|
|
of yamlScalar:
|
2015-12-26 17:40:23 +00:00
|
|
|
if levels.len == 0:
|
|
|
|
# parser ensures that next event will be yamlEndDocument
|
|
|
|
levels.add((node: jsonFromScalar(event.scalarContent,
|
|
|
|
event.scalarTag,
|
|
|
|
event.scalarType), key: nil))
|
|
|
|
continue
|
|
|
|
|
2015-12-24 14:21:49 +00:00
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray:
|
2015-12-26 12:16:57 +00:00
|
|
|
let jsonScalar = jsonFromScalar(event.scalarContent,
|
2015-12-26 17:40:23 +00:00
|
|
|
event.scalarTag,
|
2015-12-26 12:16:57 +00:00
|
|
|
event.scalarType)
|
|
|
|
levels[levels.high].node.elems.add(jsonScalar)
|
2015-12-27 22:40:27 +00:00
|
|
|
if event.scalarAnchor != yAnchorNone:
|
2015-12-26 12:16:57 +00:00
|
|
|
anchors[event.scalarAnchor] = jsonScalar
|
2015-12-24 14:21:49 +00:00
|
|
|
of JObject:
|
|
|
|
if isNil(levels[levels.high].key):
|
|
|
|
# JSON only allows strings as keys
|
|
|
|
levels[levels.high].key = event.scalarContent
|
2015-12-27 22:40:27 +00:00
|
|
|
if event.scalarAnchor != yAnchorNone:
|
2015-12-26 12:16:57 +00:00
|
|
|
raise newException(ValueError,
|
|
|
|
"scalar keys may not have anchors in JSON")
|
2015-12-24 14:21:49 +00:00
|
|
|
else:
|
2015-12-26 12:16:57 +00:00
|
|
|
let jsonScalar = jsonFromScalar(event.scalarContent,
|
2015-12-26 17:40:23 +00:00
|
|
|
event.scalarTag,
|
2015-12-26 12:16:57 +00:00
|
|
|
event.scalarType)
|
2015-12-24 14:21:49 +00:00
|
|
|
levels[levels.high].node.fields.add(
|
2015-12-26 12:16:57 +00:00
|
|
|
(key: levels[levels.high].key, val: jsonScalar))
|
2015-12-24 14:21:49 +00:00
|
|
|
levels[levels.high].key = nil
|
2015-12-27 22:40:27 +00:00
|
|
|
if event.scalarAnchor != yAnchorNone:
|
2015-12-26 12:16:57 +00:00
|
|
|
anchors[event.scalarAnchor] = jsonScalar
|
2015-12-24 14:21:49 +00:00
|
|
|
else:
|
|
|
|
discard # will never happen
|
|
|
|
of yamlEndSequence, yamlEndMap:
|
|
|
|
if levels.len > 1:
|
|
|
|
let level = levels.pop()
|
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray:
|
|
|
|
levels[levels.high].node.elems.add(level.node)
|
|
|
|
of JObject:
|
|
|
|
if isNil(levels[levels.high].key):
|
|
|
|
raise newException(ValueError,
|
|
|
|
"non-scalar as key not allowed in JSON")
|
|
|
|
else:
|
|
|
|
levels[levels.high].node.fields.add(
|
|
|
|
(key: levels[levels.high].key, val: level.node))
|
|
|
|
levels[levels.high].key = nil
|
|
|
|
else:
|
|
|
|
discard # will never happen
|
|
|
|
else:
|
|
|
|
discard # wait for yamlEndDocument
|
|
|
|
of yamlAlias:
|
2015-12-26 12:16:57 +00:00
|
|
|
# we can savely assume that the alias exists in anchors
|
|
|
|
# (else the parser would have already thrown an exception)
|
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray:
|
|
|
|
levels[levels.high].node.elems.add(anchors[event.aliasTarget])
|
|
|
|
of JObject:
|
|
|
|
if isNil(levels[levels.high].key):
|
|
|
|
raise newException(ValueError,
|
|
|
|
"cannot use alias node as key in JSON")
|
|
|
|
else:
|
|
|
|
levels[levels.high].node.fields.add(
|
|
|
|
(key: levels[levels.high].key,
|
|
|
|
val: anchors[event.aliasTarget]))
|
|
|
|
levels[levels.high].key = nil
|
|
|
|
else:
|
2016-01-05 18:58:46 +00:00
|
|
|
discard # will never happen
|
|
|
|
|
|
|
|
proc loadToJson*(s: Stream): seq[JsonNode] =
|
|
|
|
var
|
|
|
|
parser = newParser(coreTagLibrary())
|
|
|
|
events = parser.parse(s)
|
|
|
|
return constructJson(events)
|