fixes #1799 properly

This commit is contained in:
Araq
2015-10-25 02:35:18 +02:00
parent c1c76a20e6
commit cd7b5ae1eb
2 changed files with 86 additions and 8 deletions

View File

@@ -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)

View File

@@ -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