summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-10-25 02:35:18 +0200
committerAraq <rumpf_a@web.de>2015-10-25 02:35:18 +0200
commitcd7b5ae1eb999434aebdf81aeecae754457cacb5 (patch)
tree10d469e46cb876e63f78241c67fec86c36e9bbe8
parentc1c76a20e6e988377cc4d757ca0eedcfcbbaa537 (diff)
downloadNim-cd7b5ae1eb999434aebdf81aeecae754457cacb5.tar.gz
fixes #1799 properly
-rw-r--r--compiler/parser.nim23
-rw-r--r--tests/parser/tdoc_comments.nim71
2 files changed, 86 insertions, 8 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 074e13a93..dbf9706ea 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -123,6 +123,9 @@ proc rawSkipComment(p: var TParser, node: PNode) =
     getTok(p)
 
 proc skipComment(p: var TParser, node: PNode) =
+  if p.tok.indent < 0: rawSkipComment(p, node)
+
+proc flexComment(p: var TParser, node: PNode) =
   if p.tok.indent < 0 or realInd(p): rawSkipComment(p, node)
 
 proc skipInd(p: var TParser) =
@@ -892,7 +895,8 @@ proc parseTuple(p: var TParser, indentAllowed = false): PNode =
           case p.tok.tokType
           of tkSymbol, tkAccent:
             var a = parseIdentColonEquals(p, {})
-            skipComment(p, a)
+            if p.tok.indent < 0 or p.tok.indent >= p.currInd:
+              rawSkipComment(p, a)
             addSon(result, a)
           of tkEof: break
           else:
@@ -1608,7 +1612,7 @@ proc parseEnum(p: var TParser): PNode =
   getTok(p)
   addSon(result, ast.emptyNode)
   optInd(p, result)
-  rawSkipComment(p, result)
+  flexComment(p, result)
   while true:
     var a = parseSymbol(p)
     if a.kind == nkEmpty: return
@@ -1622,12 +1626,14 @@ proc parseEnum(p: var TParser): PNode =
       a = newNodeP(nkEnumFieldDef, p)
       addSon(a, b)
       addSon(a, parseExpr(p))
-      skipComment(p, a)
+      if p.tok.indent < 0 or p.tok.indent >= p.currInd:
+        rawSkipComment(p, a)
     if p.tok.tokType == tkComma and p.tok.indent < 0:
       getTok(p)
       rawSkipComment(p, a)
     else:
-      skipComment(p, a)
+      if p.tok.indent < 0 or p.tok.indent >= p.currInd:
+        rawSkipComment(p, a)
     addSon(result, a)
     if p.tok.indent >= 0 and p.tok.indent <= p.currInd or
         p.tok.tokType == tkEof:
@@ -1648,7 +1654,7 @@ proc parseObjectWhen(p: var TParser): PNode =
     addSon(branch, parseExpr(p))
     colcom(p, branch)
     addSon(branch, parseObjectPart(p))
-    skipComment(p, branch)
+    flexComment(p, branch)
     addSon(result, branch)
     if p.tok.tokType != tkElif: break
   if p.tok.tokType == tkElse and sameInd(p):
@@ -1656,7 +1662,7 @@ proc parseObjectWhen(p: var TParser): PNode =
     eat(p, tkElse)
     colcom(p, branch)
     addSon(branch, parseObjectPart(p))
-    skipComment(p, branch)
+    flexComment(p, branch)
     addSon(result, branch)
 
 proc parseObjectCase(p: var TParser): PNode =
@@ -1676,7 +1682,7 @@ proc parseObjectCase(p: var TParser): PNode =
   addSon(a, ast.emptyNode)
   addSon(result, a)
   if p.tok.tokType == tkColon: getTok(p)
-  skipComment(p, result)
+  flexComment(p, result)
   var wasIndented = false
   let oldInd = p.currInd
   if realInd(p):
@@ -1725,7 +1731,8 @@ proc parseObjectPart(p: var TParser): PNode =
       result = parseObjectCase(p)
     of tkSymbol, tkAccent:
       result = parseIdentColonEquals(p, {withPragma})
-      skipComment(p, result)
+      if p.tok.indent < 0 or p.tok.indent >= p.currInd:
+        rawSkipComment(p, result)
     of tkNil, tkDiscard:
       result = newNodeP(nkNilLit, p)
       getTok(p)
diff --git a/tests/parser/tdoc_comments.nim b/tests/parser/tdoc_comments.nim
new file mode 100644
index 000000000..fa1374b45
--- /dev/null
+++ b/tests/parser/tdoc_comments.nim
@@ -0,0 +1,71 @@
+
+# bug #1799
+
+proc MyProc1*() = ## Comment behind procedure
+  discard
+
+proc MyProc2*() =
+  ## Comment below procedure
+  discard
+
+
+template MyTemplate1*() = discard ## Comment behind template
+
+template MyTemplate2*() = discard
+  ## Comment below template
+
+
+const
+  MyConst1* = 1 ## Comment behind constant
+  MyConst2* = 2
+    ## Comment below constant
+
+
+var
+  MyVar1* = 1 ## Comment behind variable
+  MyVar2* = 2
+    ## Comment below variable
+
+
+type
+  MyObject1* = object
+    ## Comment below declaration
+    field1*: int ## Comment behind field
+    field2*: int ## Comment behind field
+    field3*: int
+      ## Comment below field
+    field4*: int
+      ## Comment below field
+
+  MyObject2* = object ## Comment behind declaration
+    field1*: int
+
+
+type
+  MyTuple1* = tuple
+    ## Comment below declaration
+    field1: int ## Comment behind field
+    field2: int ## Comment behind field
+    field3: int
+      ## Comment below field
+    field4: int
+      ## Comment below field
+
+  MyTuple2* = tuple ## Comment behind declaration
+    field1: int
+
+
+type
+  MyEnum1* = enum
+    ## Comment below declaration
+    value1, ## Comment behind value
+    value2,
+      ## Comment below value with comma
+    value3
+      ## Comment below value without comma
+
+  MyEnum2* = enum ## Comment behind declaration
+    value4
+
+  MyEnum3* = enum
+    value5  ## only document the enum value