summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorNeelesh Chandola <neelesh.chandola@outlook.com>2018-12-28 03:08:24 +0530
committerAndreas Rumpf <rumpf_a@web.de>2018-12-27 22:38:24 +0100
commit05b8085a873ad0d8e5e823d35a0b043997e873df (patch)
treec30020f651e8ea61675e7cacc614c4e17f2390ff
parent89488947137277efec2d6bc576843de84cf29e7b (diff)
downloadNim-05b8085a873ad0d8e5e823d35a0b043997e873df.tar.gz
Pragma syntax is now consistent (#9993)
* Give deprecation warning when type pragmas do not follow the type name
* pragma before generic parameter list in type definition is now deprecated
* Update changelog
* Fix bug where deprecated warning was being shown even though no generic param list was present
* Fix bug
* Use errGenerated
* Best attempt at writing the grammar
-rw-r--r--changelog.md6
-rw-r--r--compiler/parser.nim37
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)