mirror of https://github.com/status-im/NimYAML.git
Got rid of lots of compiler warnings
This commit is contained in:
parent
30365d2f17
commit
34cbe61479
|
@ -135,7 +135,7 @@ proc serializeNode(n: YamlNode, c: SerializationContext, a: AnchorStyle,
|
||||||
yield event
|
yield event
|
||||||
yield endMapEvent()
|
yield endMapEvent()
|
||||||
|
|
||||||
template processAnchoredEvent(target: expr, c: SerializationContext): stmt =
|
template processAnchoredEvent(target: untyped, c: SerializationContext): typed =
|
||||||
let anchorId = c.refs.getOrDefault(cast[pointer](target))
|
let anchorId = c.refs.getOrDefault(cast[pointer](target))
|
||||||
if anchorId != yAnchorNone: target = anchorId
|
if anchorId != yAnchorNone: target = anchorId
|
||||||
else: target = yAnchorNone
|
else: target = yAnchorNone
|
||||||
|
|
|
@ -28,7 +28,7 @@ type
|
||||||
|
|
||||||
ythMinus, yth0, ythInt, ythDecimal, ythNumE, ythNumEPlusMinus, ythExponent
|
ythMinus, yth0, ythInt, ythDecimal, ythNumE, ythNumEPlusMinus, ythExponent
|
||||||
|
|
||||||
macro typeHintStateMachine(c: untyped, content: untyped): stmt =
|
macro typeHintStateMachine(c: untyped, content: untyped): typed =
|
||||||
yAssert content.kind == nnkStmtList
|
yAssert content.kind == nnkStmtList
|
||||||
result = newNimNode(nnkCaseStmt, content).add(copyNimNode(c))
|
result = newNimNode(nnkCaseStmt, content).add(copyNimNode(c))
|
||||||
for branch in content.children:
|
for branch in content.children:
|
||||||
|
@ -43,7 +43,7 @@ macro typeHintStateMachine(c: untyped, content: untyped): stmt =
|
||||||
inc(i)
|
inc(i)
|
||||||
for rule in branch[i].children:
|
for rule in branch[i].children:
|
||||||
yAssert rule.kind == nnkInfix
|
yAssert rule.kind == nnkInfix
|
||||||
yAssert ($rule[0].ident == "=>")
|
yAssert $rule[0].ident == "=>"
|
||||||
var stateBranch = newNimNode(nnkOfBranch, rule)
|
var stateBranch = newNimNode(nnkOfBranch, rule)
|
||||||
case rule[1].kind
|
case rule[1].kind
|
||||||
of nnkBracket:
|
of nnkBracket:
|
||||||
|
|
|
@ -886,14 +886,18 @@ proc handleFlowPlainScalar(c: ParserContext, e: var YamlStreamEvent) =
|
||||||
|
|
||||||
# --- macros for defining parser states ---
|
# --- macros for defining parser states ---
|
||||||
|
|
||||||
macro parserStates(names: varargs[untyped]): stmt =
|
template capitalize(s: string): string =
|
||||||
|
when declared(strutils.capitalizeAscii): strutils.capitalizeAscii(s)
|
||||||
|
else: strutils.capitalize(s)
|
||||||
|
|
||||||
|
macro parserStates(names: varargs[untyped]): typed =
|
||||||
## generates proc declaration for each state in list like this:
|
## generates proc declaration for each state in list like this:
|
||||||
##
|
##
|
||||||
## proc name(s: YamlStream, e: var YamlStreamEvent):
|
## proc name(s: YamlStream, e: var YamlStreamEvent):
|
||||||
## bool {.raises: [YamlParserError].}
|
## bool {.raises: [YamlParserError].}
|
||||||
result = newStmtList()
|
result = newStmtList()
|
||||||
for name in names:
|
for name in names:
|
||||||
let nameId = newIdentNode("state" & strutils.capitalize($name.ident))
|
let nameId = newIdentNode("state" & capitalize($name.ident))
|
||||||
result.add(newProc(nameId, [ident("bool"), newIdentDefs(ident("s"),
|
result.add(newProc(nameId, [ident("bool"), newIdentDefs(ident("s"),
|
||||||
ident("YamlStream")), newIdentDefs(ident("e"), newNimNode(nnkVarTy).add(
|
ident("YamlStream")), newIdentDefs(ident("e"), newNimNode(nnkVarTy).add(
|
||||||
ident("YamlStreamEvent")))], newEmptyNode()))
|
ident("YamlStreamEvent")))], newEmptyNode()))
|
||||||
|
@ -912,14 +916,14 @@ proc processStateAsgns(source, target: NimNode) {.compileTime.} =
|
||||||
newNameId = newDotExpr(ident("c"), ident("storedState"))
|
newNameId = newDotExpr(ident("c"), ident("storedState"))
|
||||||
else:
|
else:
|
||||||
newNameId =
|
newNameId =
|
||||||
newIdentNode("state" & strutils.capitalize($child[1].ident))
|
newIdentNode("state" & capitalize($child[1].ident))
|
||||||
target.add(newAssignment(newDotExpr(
|
target.add(newAssignment(newDotExpr(
|
||||||
newIdentNode("s"), newIdentNode("nextImpl")), newNameId))
|
newIdentNode("s"), newIdentNode("nextImpl")), newNameId))
|
||||||
continue
|
continue
|
||||||
elif $child[0].ident == "stored":
|
elif $child[0].ident == "stored":
|
||||||
assert child[1].kind == nnkIdent
|
assert child[1].kind == nnkIdent
|
||||||
let newNameId =
|
let newNameId =
|
||||||
newIdentNode("state" & strutils.capitalize($child[1].ident))
|
newIdentNode("state" & capitalize($child[1].ident))
|
||||||
target.add(newAssignment(newDotExpr(newIdentNode("c"),
|
target.add(newAssignment(newDotExpr(newIdentNode("c"),
|
||||||
newIdentNode("storedState")), newNameId))
|
newIdentNode("storedState")), newNameId))
|
||||||
continue
|
continue
|
||||||
|
@ -927,7 +931,7 @@ proc processStateAsgns(source, target: NimNode) {.compileTime.} =
|
||||||
processStateAsgns(child, processed)
|
processStateAsgns(child, processed)
|
||||||
target.add(processed)
|
target.add(processed)
|
||||||
|
|
||||||
macro parserState(name: untyped, impl: untyped): stmt =
|
macro parserState(name: untyped, impl: untyped): typed =
|
||||||
## Creates a parser state. Every parser state is a proc with the signature
|
## Creates a parser state. Every parser state is a proc with the signature
|
||||||
##
|
##
|
||||||
## proc(s: YamlStream, e: var YamlStreamEvent):
|
## proc(s: YamlStream, e: var YamlStreamEvent):
|
||||||
|
@ -941,7 +945,7 @@ macro parserState(name: untyped, impl: untyped): stmt =
|
||||||
## The [newState] must have been declared with states(...) previously.
|
## The [newState] must have been declared with states(...) previously.
|
||||||
let
|
let
|
||||||
nameStr = $name.ident
|
nameStr = $name.ident
|
||||||
nameId = newIdentNode("state" & strutils.capitalize(nameStr))
|
nameId = newIdentNode("state" & capitalize(nameStr))
|
||||||
var procImpl = quote do:
|
var procImpl = quote do:
|
||||||
debug("state: " & `nameStr`)
|
debug("state: " & `nameStr`)
|
||||||
procImpl.add(newLetStmt(ident("c"), newCall("ParserContext", ident("s"))))
|
procImpl.add(newLetStmt(ident("c"), newCall("ParserContext", ident("s"))))
|
||||||
|
|
|
@ -29,7 +29,7 @@ proc safeTagUri(id: TagId): string {.raises: [].} =
|
||||||
else: return uri
|
else: return uri
|
||||||
except KeyError: internalError("Unexpected KeyError for TagId " & $id)
|
except KeyError: internalError("Unexpected KeyError for TagId " & $id)
|
||||||
|
|
||||||
template constructScalarItem*(s: var YamlStream, i: expr,
|
template constructScalarItem*(s: var YamlStream, i: untyped,
|
||||||
t: typedesc, content: untyped) =
|
t: typedesc, content: untyped) =
|
||||||
## Helper template for implementing ``constructObject`` for types that
|
## Helper template for implementing ``constructObject`` for types that
|
||||||
## are constructed from a scalar. ``i`` is the identifier that holds
|
## are constructed from a scalar. ``i`` is the identifier that holds
|
||||||
|
@ -422,8 +422,8 @@ proc yamlTag*(T: typedesc[tuple]):
|
||||||
try: serializationTagLibrary.tags[uri]
|
try: serializationTagLibrary.tags[uri]
|
||||||
except KeyError: serializationTagLibrary.registerUri(uri)
|
except KeyError: serializationTagLibrary.registerUri(uri)
|
||||||
|
|
||||||
macro constructFieldValue(t: typedesc, stream: expr, context: expr,
|
macro constructFieldValue(t: typedesc, stream: untyped, context: untyped,
|
||||||
name: expr, o: expr): stmt =
|
name: untyped, o: untyped): typed =
|
||||||
let tDesc = getType(getType(t)[1])
|
let tDesc = getType(getType(t)[1])
|
||||||
result = newNimNode(nnkCaseStmt).add(name)
|
result = newNimNode(nnkCaseStmt).add(name)
|
||||||
for child in tDesc[2].children:
|
for child in tDesc[2].children:
|
||||||
|
@ -459,6 +459,9 @@ macro constructFieldValue(t: typedesc, stream: expr, context: expr,
|
||||||
let field = newDotExpr(o, newIdentNode($child))
|
let field = newDotExpr(o, newIdentNode($child))
|
||||||
ob.add(newStmtList(newCall("constructChild", stream, context, field)))
|
ob.add(newStmtList(newCall("constructChild", stream, context, field)))
|
||||||
result.add(ob)
|
result.add(ob)
|
||||||
|
# TODO: is this correct?
|
||||||
|
result.add(newNimNode(nnkElse).add(newNimNode(nnkDiscardStmt).add(
|
||||||
|
newEmptyNode())))
|
||||||
|
|
||||||
proc isVariantObject(t: typedesc): bool {.compileTime.} =
|
proc isVariantObject(t: typedesc): bool {.compileTime.} =
|
||||||
let tDesc = getType(t)
|
let tDesc = getType(t)
|
||||||
|
@ -556,15 +559,13 @@ proc representObject*[O: enum](value: O, ts: TagStyle,
|
||||||
|
|
||||||
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
||||||
|
|
||||||
macro constructImplicitVariantObject(s, c, r, possibleTagIds: expr,
|
macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped,
|
||||||
t: typedesc): stmt =
|
t: typedesc): typed =
|
||||||
let tDesc = getType(getType(t)[1])
|
let tDesc = getType(getType(t)[1])
|
||||||
yAssert tDesc.kind == nnkObjectTy
|
yAssert tDesc.kind == nnkObjectTy
|
||||||
let recCase = tDesc[2][0]
|
let recCase = tDesc[2][0]
|
||||||
yAssert recCase.kind == nnkRecCase
|
yAssert recCase.kind == nnkRecCase
|
||||||
let
|
let discriminant = newDotExpr(r, newIdentNode($recCase[0]))
|
||||||
discriminant = newDotExpr(r, newIdentNode($recCase[0]))
|
|
||||||
discType = newCall("type", discriminant)
|
|
||||||
var ifStmt = newNimNode(nnkIfStmt)
|
var ifStmt = newNimNode(nnkIfStmt)
|
||||||
for i in 1 .. recCase.len - 1:
|
for i in 1 .. recCase.len - 1:
|
||||||
yAssert recCase[i].kind == nnkOfBranch
|
yAssert recCase[i].kind == nnkOfBranch
|
||||||
|
@ -856,7 +857,7 @@ proc represent*[T](value: T, ts: TagStyle = tsRootOnly,
|
||||||
if a == asTidy:
|
if a == asTidy:
|
||||||
var objQueue = newSeq[YamlStreamEvent]()
|
var objQueue = newSeq[YamlStreamEvent]()
|
||||||
for event in objStream(): objQueue.add(event)
|
for event in objStream(): objQueue.add(event)
|
||||||
var backend = iterator(): YamlStreamEvent {.raises: [YamlStreamError].} =
|
var backend = iterator(): YamlStreamEvent {.raises: [].} =
|
||||||
for i in countup(0, objQueue.len - 1):
|
for i in countup(0, objQueue.len - 1):
|
||||||
var event = objQueue[i]
|
var event = objQueue[i]
|
||||||
case event.kind
|
case event.kind
|
||||||
|
|
|
@ -39,7 +39,7 @@ type
|
||||||
barkometer: int
|
barkometer: int
|
||||||
|
|
||||||
proc `$`(v: BetterInt): string {.borrow.}
|
proc `$`(v: BetterInt): string {.borrow.}
|
||||||
proc `==`(l, r: BetterInt): bool {.borrow.}
|
proc `==`(left, right: BetterInt): bool {.borrow.}
|
||||||
|
|
||||||
setTagUri(TrafficLight, "!tl")
|
setTagUri(TrafficLight, "!tl")
|
||||||
setTagUri(Node, "!example.net:Node")
|
setTagUri(Node, "!example.net:Node")
|
||||||
|
@ -75,10 +75,9 @@ proc newNode(v: string): ref Node =
|
||||||
result.value = v
|
result.value = v
|
||||||
result.next = nil
|
result.next = nil
|
||||||
|
|
||||||
suite "Serialization":
|
let blockOnly = defineOptions(style=psBlockOnly)
|
||||||
setup:
|
|
||||||
let blockOnly = defineOptions(style=psBlockOnly)
|
|
||||||
|
|
||||||
|
suite "Serialization":
|
||||||
test "Serialization: Load integer without fixed length":
|
test "Serialization: Load integer without fixed length":
|
||||||
var input = newStringStream("-4247")
|
var input = newStringStream("-4247")
|
||||||
var result: int
|
var result: int
|
||||||
|
|
10
yaml.nim
10
yaml.nim
|
@ -603,7 +603,7 @@ proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
||||||
|
|
||||||
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
||||||
result: var ref O)
|
result: var ref O)
|
||||||
{.raises: [YamlConstructionError, YamlStreamError].}
|
{.raises: [YamlStreamError].}
|
||||||
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
||||||
## The stream will advance until after the finishing token that was used
|
## The stream will advance until after the finishing token that was used
|
||||||
## for constructing the value. The object may be constructed from an alias
|
## for constructing the value. The object may be constructed from an alias
|
||||||
|
@ -624,7 +624,7 @@ proc representChild*[O](value: O, ts: TagStyle,
|
||||||
## Represents an arbitrary Nim object as YAML object.
|
## Represents an arbitrary Nim object as YAML object.
|
||||||
|
|
||||||
proc construct*[T](s: var YamlStream, target: var T)
|
proc construct*[T](s: var YamlStream, target: var T)
|
||||||
{.raises: [YamlConstructionError, YamlStreamError].}
|
{.raises: [YamlStreamError].}
|
||||||
## Constructs a Nim value from a YAML stream.
|
## Constructs a Nim value from a YAML stream.
|
||||||
|
|
||||||
proc load*[K](input: Stream, target: var K)
|
proc load*[K](input: Stream, target: var K)
|
||||||
|
@ -660,7 +660,7 @@ var
|
||||||
## registered URIs here to be able to generate a static compiler error
|
## registered URIs here to be able to generate a static compiler error
|
||||||
## when the user tries to register an URI more than once.
|
## when the user tries to register an URI more than once.
|
||||||
|
|
||||||
template setTagUri*(t: typedesc, uri: string): stmt =
|
template setTagUri*(t: typedesc, uri: string): typed =
|
||||||
## Associate the given uri with a certain type. This uri is used as YAML tag
|
## Associate the given uri with a certain type. This uri is used as YAML tag
|
||||||
## when loading and dumping values of this type.
|
## when loading and dumping values of this type.
|
||||||
when uri in registeredUris:
|
when uri in registeredUris:
|
||||||
|
@ -675,7 +675,7 @@ template setTagUri*(t: typedesc, uri: string): stmt =
|
||||||
proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = id
|
proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = id
|
||||||
## autogenerated
|
## autogenerated
|
||||||
|
|
||||||
template setTagUri*(t: typedesc, uri: string, idName: expr): stmt =
|
template setTagUri*(t: typedesc, uri: string, idName: untyped): typed =
|
||||||
## Like `setTagUri <#setTagUri.t,typedesc,string>`_, but lets
|
## Like `setTagUri <#setTagUri.t,typedesc,string>`_, but lets
|
||||||
## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only
|
## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only
|
||||||
## necessary if you want to implement serialization / construction yourself.
|
## necessary if you want to implement serialization / construction yourself.
|
||||||
|
@ -704,7 +704,7 @@ proc canBeImplicit(t: typedesc): bool {.compileTime.} =
|
||||||
else: return false
|
else: return false
|
||||||
return true
|
return true
|
||||||
|
|
||||||
template markAsImplicit*(t: typedesc): stmt =
|
template markAsImplicit*(t: typedesc): typed =
|
||||||
## Mark a variant object type as implicit. This requires the type to consist
|
## Mark a variant object type as implicit. This requires the type to consist
|
||||||
## of nothing but a case expression and each branch of the case expression
|
## of nothing but a case expression and each branch of the case expression
|
||||||
## containing exactly one field - with the exception that one branch may
|
## containing exactly one field - with the exception that one branch may
|
||||||
|
|
Loading…
Reference in New Issue