diff options
-rw-r--r-- | changelog.md | 6 | ||||
-rw-r--r-- | compiler/parser.nim | 37 |
2 files changed, 40 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index d25b6101d..47642cab5 100644 --- a/changelog.md +++ b/changelog.md @@ -141,6 +141,12 @@ proc enumToString*(enums: openArray[enum]): string = it's more recognizable and allows tools like github to recognize it as Nim, see [#9647](https://github.com/nim-lang/Nim/issues/9647). The previous extension will continue to work. +- Pragma syntax is now consistent. Previous syntax where type pragmas did not + follow the type name is now deprecated. Also pragma before generic parameter + list is deprecated to be consistent with how pragmas are used with a proc. See + [#8514](https://github.com/nim-lang/Nim/issues/8514) and + [#1872](https://github.com/nim-lang/Nim/issues/1872) for further details. + ### Tool changes - `jsondoc` now include a `moduleDescription` field with the module diff --git a/compiler/parser.nim b/compiler/parser.nim index e26ea5ee2..c2c8427c8 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1905,6 +1905,8 @@ proc parseObject(p: var TParser): PNode = result = newNodeP(nkObjectTy, p) getTok(p) if p.tok.tokType == tkCurlyDotLe and p.validInd: + # Deprecated since v0.20.0 + parMessage(p, warnDeprecated, "type pragmas follow the type name; this form of writing pragmas") addSon(result, parsePragma(p)) else: addSon(result, p.emptyNode) @@ -1977,13 +1979,42 @@ proc parseTypeClass(p: var TParser): PNode = proc parseTypeDef(p: var TParser): PNode = #| #| typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux + #| indAndComment? / identVisDot genericParamList? pragma '=' optInd typeDefAux #| indAndComment? result = newNodeP(nkTypeDef, p) - addSon(result, identWithPragma(p, allowDot=true)) + var identifier = identVis(p, allowDot=true) + var identPragma = identifier + var pragma: PNode + var genericParam: PNode + var noPragmaYet = true + + if p.tok.tokType == tkCurlyDotLe: + pragma = optPragmas(p) + identPragma = newNodeP(nkPragmaExpr, p) + addSon(identPragma, identifier) + addSon(identPragma, pragma) + noPragmaYet = false + if p.tok.tokType == tkBracketLe and p.validInd: - addSon(result, parseGenericParamList(p)) + if not noPragmaYet: + # Deprecated since v0.20.0 + parMessage(p, warnDeprecated, "pragma before generic parameter list") + genericParam = parseGenericParamList(p) else: - addSon(result, p.emptyNode) + genericParam = p.emptyNode + + if noPragmaYet: + pragma = optPragmas(p) + if pragma.kind != nkEmpty: + identPragma = newNodeP(nkPragmaExpr, p) + addSon(identPragma, identifier) + addSon(identPragma, pragma) + elif p.tok.tokType == tkCurlyDotLe: + parMessage(p, errGenerated, "pragma already present") + + addSon(result, identPragma) + addSon(result, genericParam) + if p.tok.tokType == tkEquals: result.info = parLineInfo(p) getTok(p) |