From 34cbe61479cdbf0798314b975d9e41feef1b140a Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Wed, 17 Aug 2016 22:50:37 +0200 Subject: [PATCH] Got rid of lots of compiler warnings --- private/dom.nim | 2 +- private/hints.nim | 4 ++-- private/parse.nim | 16 ++++++++++------ private/serialization.nim | 19 ++++++++++--------- test/serializing.nim | 7 +++---- yaml.nim | 10 +++++----- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/private/dom.nim b/private/dom.nim index c68e2d2..3a28795 100644 --- a/private/dom.nim +++ b/private/dom.nim @@ -135,7 +135,7 @@ proc serializeNode(n: YamlNode, c: SerializationContext, a: AnchorStyle, yield event yield endMapEvent() -template processAnchoredEvent(target: expr, c: SerializationContext): stmt = +template processAnchoredEvent(target: untyped, c: SerializationContext): typed = let anchorId = c.refs.getOrDefault(cast[pointer](target)) if anchorId != yAnchorNone: target = anchorId else: target = yAnchorNone diff --git a/private/hints.nim b/private/hints.nim index a9a99e3..d4e928e 100644 --- a/private/hints.nim +++ b/private/hints.nim @@ -28,7 +28,7 @@ type 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 result = newNimNode(nnkCaseStmt, content).add(copyNimNode(c)) for branch in content.children: @@ -43,7 +43,7 @@ macro typeHintStateMachine(c: untyped, content: untyped): stmt = inc(i) for rule in branch[i].children: yAssert rule.kind == nnkInfix - yAssert ($rule[0].ident == "=>") + yAssert $rule[0].ident == "=>" var stateBranch = newNimNode(nnkOfBranch, rule) case rule[1].kind of nnkBracket: diff --git a/private/parse.nim b/private/parse.nim index 049b7fd..1ef2bb5 100644 --- a/private/parse.nim +++ b/private/parse.nim @@ -886,14 +886,18 @@ proc handleFlowPlainScalar(c: ParserContext, e: var YamlStreamEvent) = # --- 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: ## ## proc name(s: YamlStream, e: var YamlStreamEvent): ## bool {.raises: [YamlParserError].} result = newStmtList() 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"), ident("YamlStream")), newIdentDefs(ident("e"), newNimNode(nnkVarTy).add( ident("YamlStreamEvent")))], newEmptyNode())) @@ -912,14 +916,14 @@ proc processStateAsgns(source, target: NimNode) {.compileTime.} = newNameId = newDotExpr(ident("c"), ident("storedState")) else: newNameId = - newIdentNode("state" & strutils.capitalize($child[1].ident)) + newIdentNode("state" & capitalize($child[1].ident)) target.add(newAssignment(newDotExpr( newIdentNode("s"), newIdentNode("nextImpl")), newNameId)) continue elif $child[0].ident == "stored": assert child[1].kind == nnkIdent let newNameId = - newIdentNode("state" & strutils.capitalize($child[1].ident)) + newIdentNode("state" & capitalize($child[1].ident)) target.add(newAssignment(newDotExpr(newIdentNode("c"), newIdentNode("storedState")), newNameId)) continue @@ -927,7 +931,7 @@ proc processStateAsgns(source, target: NimNode) {.compileTime.} = processStateAsgns(child, 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 ## ## 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. let nameStr = $name.ident - nameId = newIdentNode("state" & strutils.capitalize(nameStr)) + nameId = newIdentNode("state" & capitalize(nameStr)) var procImpl = quote do: debug("state: " & `nameStr`) procImpl.add(newLetStmt(ident("c"), newCall("ParserContext", ident("s")))) diff --git a/private/serialization.nim b/private/serialization.nim index 335d6c8..8cd891c 100644 --- a/private/serialization.nim +++ b/private/serialization.nim @@ -29,7 +29,7 @@ proc safeTagUri(id: TagId): string {.raises: [].} = else: return uri 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) = ## Helper template for implementing ``constructObject`` for types that ## are constructed from a scalar. ``i`` is the identifier that holds @@ -422,8 +422,8 @@ proc yamlTag*(T: typedesc[tuple]): try: serializationTagLibrary.tags[uri] except KeyError: serializationTagLibrary.registerUri(uri) -macro constructFieldValue(t: typedesc, stream: expr, context: expr, - name: expr, o: expr): stmt = +macro constructFieldValue(t: typedesc, stream: untyped, context: untyped, + name: untyped, o: untyped): typed = let tDesc = getType(getType(t)[1]) result = newNimNode(nnkCaseStmt).add(name) for child in tDesc[2].children: @@ -459,6 +459,9 @@ macro constructFieldValue(t: typedesc, stream: expr, context: expr, let field = newDotExpr(o, newIdentNode($child)) ob.add(newStmtList(newCall("constructChild", stream, context, field))) result.add(ob) + # TODO: is this correct? + result.add(newNimNode(nnkElse).add(newNimNode(nnkDiscardStmt).add( + newEmptyNode()))) proc isVariantObject(t: typedesc): bool {.compileTime.} = 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) -macro constructImplicitVariantObject(s, c, r, possibleTagIds: expr, - t: typedesc): stmt = +macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped, + t: typedesc): typed = let tDesc = getType(getType(t)[1]) yAssert tDesc.kind == nnkObjectTy let recCase = tDesc[2][0] yAssert recCase.kind == nnkRecCase - let - discriminant = newDotExpr(r, newIdentNode($recCase[0])) - discType = newCall("type", discriminant) + let discriminant = newDotExpr(r, newIdentNode($recCase[0])) var ifStmt = newNimNode(nnkIfStmt) for i in 1 .. recCase.len - 1: yAssert recCase[i].kind == nnkOfBranch @@ -856,7 +857,7 @@ proc represent*[T](value: T, ts: TagStyle = tsRootOnly, if a == asTidy: var objQueue = newSeq[YamlStreamEvent]() 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): var event = objQueue[i] case event.kind diff --git a/test/serializing.nim b/test/serializing.nim index 4e87d31..953075f 100644 --- a/test/serializing.nim +++ b/test/serializing.nim @@ -39,7 +39,7 @@ type barkometer: int proc `$`(v: BetterInt): string {.borrow.} -proc `==`(l, r: BetterInt): bool {.borrow.} +proc `==`(left, right: BetterInt): bool {.borrow.} setTagUri(TrafficLight, "!tl") setTagUri(Node, "!example.net:Node") @@ -75,10 +75,9 @@ proc newNode(v: string): ref Node = result.value = v result.next = nil -suite "Serialization": - setup: - let blockOnly = defineOptions(style=psBlockOnly) +let blockOnly = defineOptions(style=psBlockOnly) +suite "Serialization": test "Serialization: Load integer without fixed length": var input = newStringStream("-4247") var result: int diff --git a/yaml.nim b/yaml.nim index 5b2ff4a..0e36f84 100644 --- a/yaml.nim +++ b/yaml.nim @@ -603,7 +603,7 @@ proc constructChild*[T](s: var YamlStream, c: ConstructionContext, proc constructChild*[O](s: var YamlStream, c: ConstructionContext, result: var ref O) - {.raises: [YamlConstructionError, YamlStreamError].} + {.raises: [YamlStreamError].} ## Constructs an arbitrary Nim value from a part of a YAML stream. ## The stream will advance until after the finishing token that was used ## 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. proc construct*[T](s: var YamlStream, target: var T) - {.raises: [YamlConstructionError, YamlStreamError].} + {.raises: [YamlStreamError].} ## Constructs a Nim value from a YAML stream. 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 ## 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 ## when loading and dumping values of this type. when uri in registeredUris: @@ -675,7 +675,7 @@ template setTagUri*(t: typedesc, uri: string): stmt = proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = id ## 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 ## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only ## necessary if you want to implement serialization / construction yourself. @@ -704,7 +704,7 @@ proc canBeImplicit(t: typedesc): bool {.compileTime.} = else: return false 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 ## of nothing but a case expression and each branch of the case expression ## containing exactly one field - with the exception that one branch may