This commit is contained in:
Andreas Rumpf 2020-08-27 11:25:20 +02:00 • committed by GitHub
commit ccccd30cf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 47 deletions

View file

@ -367,7 +367,7 @@ func contains*(pattern: CaptureBounds, name: string): bool =
func contains*(pattern: Captures, name: string): bool = func contains*(pattern: Captures, name: string): bool =
name in CaptureBounds(pattern) name in CaptureBounds(pattern)
func checkNamedCaptured(pattern: RegexMatch, name: string): void = func checkNamedCaptured(pattern: RegexMatch, name: string) =
if not (name in pattern.captureBounds): if not (name in pattern.captureBounds):
raise newException(KeyError, "Group '" & name & "' was not captured") raise newException(KeyError, "Group '" & name & "' was not captured")

View file

@ -115,54 +115,52 @@ macro genericParamsImpl(T: typedesc): untyped =
impl = impl[1] impl = impl[1]
while true: while true:
case impl.kind case impl.kind
of nnkSym: of nnkSym:
impl = impl.getImpl impl = impl.getImpl
continue of nnkTypeDef:
of nnkTypeDef: impl = impl[2]
impl = impl[2] of nnkTypeOfExpr:
continue impl = getTypeInst(impl[0])
of nnkTypeOfExpr: of nnkBracketExpr:
impl = getTypeInst(impl[0]) for i in 1..<impl.len:
continue let ai = impl[i]
of nnkBracketExpr: var ret: NimNode = nil
for i in 1..<impl.len: case ai.typeKind
let ai = impl[i] of ntyTypeDesc:
var ret: NimNode = nil ret = ai
case ai.typeKind of ntyStatic: doAssert false
of ntyTypeDesc: else:
# getType from a resolved symbol might return a typedesc symbol.
# If so, use it directly instead of wrapping it in StaticParam.
if (ai.kind == nnkSym and ai.symKind == nskType) or
(ai.kind == nnkBracketExpr and ai[0].kind == nnkSym and
ai[0].symKind == nskType) or ai.kind in {nnkRefTy, nnkVarTy, nnkPtrTy, nnkProcTy}:
ret = ai ret = ai
of ntyStatic: doAssert false elif ai.kind == nnkInfix and ai[0].kind == nnkIdent and
ai[0].strVal == "..":
# For built-in array types, the "2" is translated to "0..1" then
# automagically translated to "range[0..1]". However this is not
# reflected in the AST, thus requiring manual transformation here.
#
# We will also be losing some context here:
# var a: array[10, int]
# will be translated to:
# var a: array[0..9, int]
# after typecheck. This means that we can't get the exact
# definition as typed by the user, which will cause confusion for
# users expecting:
# genericParams(typeof(a)) is (StaticParam(10), int)
# to be true while in fact the result will be:
# genericParams(typeof(a)) is (range[0..9], int)
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
else: else:
# getType from a resolved symbol might return a typedesc symbol. since (1, 1):
# If so, use it directly instead of wrapping it in StaticParam. echo ai.typeKind
if (ai.kind == nnkSym and ai.symKind == nskType) or ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
(ai.kind == nnkBracketExpr and ai[0].kind == nnkSym and result.add ret
ai[0].symKind == nskType): break
ret = ai else:
elif ai.kind == nnkInfix and ai[0].kind == nnkIdent and error "wrong kind: " & $impl.kind, impl
ai[0].strVal == "..":
# For built-in array types, the "2" is translated to "0..1" then
# automagically translated to "range[0..1]". However this is not
# reflected in the AST, thus requiring manual transformation here.
#
# We will also be losing some context here:
# var a: array[10, int]
# will be translated to:
# var a: array[0..9, int]
# after typecheck. This means that we can't get the exact
# definition as typed by the user, which will cause confusion for
# users expecting:
# genericParams(typeof(a)) is (StaticParam(10), int)
# to be true while in fact the result will be:
# genericParams(typeof(a)) is (range[0..9], int)
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
else:
since (1, 1):
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
result.add ret
break
else:
error "wrong kind: " & $impl.kind, impl
since (1, 1): since (1, 1):
template genericParams*(T: typedesc): untyped = template genericParams*(T: typedesc): untyped =