Skip to content

Commit aa87c24

Browse files
authored
fix moved or eaten comments (fixes #102, #111) (#112)
1 parent 44bc0b4 commit aa87c24

6 files changed

Lines changed: 162 additions & 5 deletions

File tree

src/phparser.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2598,7 +2598,7 @@ proc parseStmtPragma(p: var Parser): PNode =
25982598
result = newNodeI(nkPragmaBlock, a.info)
25992599
result.add a
26002600
getTok(p)
2601-
splitLookahead(p, result, clPostfix) # TODO mid
2601+
splitLookahead(p, result, clMid)
26022602
result.add parseStmt(p)
26032603
splitLookahead(p, result, clPostfix)
26042604
setEndInfo()

src/phrenderer.nim

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,12 @@ proc infixArgument(g: var TOutput, n: PNode, i: int, flags: SubFlags) =
13591359
put(g, tkParRi, ")")
13601360

13611361
proc postStatements(
1362-
g: var TOutput, n: PNode, start: int, skipDo: bool, skipColon = false
1362+
g: var TOutput,
1363+
n: PNode,
1364+
start: int,
1365+
skipDo: bool,
1366+
skipColon = false,
1367+
skipMids = true,
13631368
) =
13641369
# Sometimes, `do` can be skipped but it is not entirely clear when - this
13651370
# feature rests on experiments with large codebases but should be researched
@@ -1378,6 +1383,9 @@ proc postStatements(
13781383
elif not skipColon:
13791384
putWithSpace(g, tkColon, ":")
13801385

1386+
if not skipMids:
1387+
gmids(g, n)
1388+
13811389
gsub(g, n[i])
13821390

13831391
i.inc
@@ -1483,7 +1491,8 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
14831491
g, n, tkParLe, start = 1, theEnd = i - 1 - n.len, flags = {lfLongSepAtEnd}
14841492
)
14851493

1486-
postStatements(g, n, i, sfSkipDo in flags)
1494+
postStatements(g, n, i, sfSkipDo in flags, skipMids = i > 1)
1495+
14871496
dedent(g, ind)
14881497

14891498
if n.lastSon.kind == nkDo and sfParDo in flags:
@@ -1561,7 +1570,9 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
15611570
# ":" is present so it looks like we can skip the `do` here :/ this needs
15621571
# deeper investigation - see also `nkPar` which sometimes removes the
15631572
# parenthesis from the AST
1564-
postStatements(g, n, i, sfSkipDo in flags, n[i].kind == nkStmtListExpr)
1573+
postStatements(
1574+
g, n, i, sfSkipDo in flags, n[i].kind == nkStmtListExpr, skipMids = false
1575+
)
15651576
else:
15661577
# The first argument must not be line-broken, or command syntax breaks!
15671578
if n.len > 1:

tests/after/comments.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,19 @@ block:
498498
discard
499499
else: #comment
500500
discard
501+
502+
checkUntilTimeout: # wait for nodes subscribe to finish
503+
discard
504+
505+
checkUntilTimeout( # wait for nodes subscribe to finish
506+
arg
507+
)
508+
509+
checkUntilTimeout: # wait for nodes subscribe to finish
510+
discard
511+
512+
command a: # comment
513+
discard
514+
515+
{.gcsafe.}: # comment
516+
discard

tests/after/comments.nim.nph.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,3 +2038,59 @@ sons:
20382038
- kind: "nkDiscardStmt"
20392039
sons:
20402040
- kind: "nkEmpty"
2041+
- kind: "nkCall"
2042+
mid:
2043+
- "# wait for nodes subscribe to finish"
2044+
sons:
2045+
- kind: "nkIdent"
2046+
ident: "checkUntilTimeout"
2047+
- kind: "nkStmtList"
2048+
sons:
2049+
- kind: "nkDiscardStmt"
2050+
sons:
2051+
- kind: "nkEmpty"
2052+
- kind: "nkCall"
2053+
mid:
2054+
- "# wait for nodes subscribe to finish"
2055+
sons:
2056+
- kind: "nkIdent"
2057+
ident: "checkUntilTimeout"
2058+
- kind: "nkIdent"
2059+
ident: "arg"
2060+
- kind: "nkCall"
2061+
mid:
2062+
- "# wait for nodes subscribe to finish"
2063+
sons:
2064+
- kind: "nkIdent"
2065+
ident: "checkUntilTimeout"
2066+
- kind: "nkStmtList"
2067+
sons:
2068+
- kind: "nkDiscardStmt"
2069+
sons:
2070+
- kind: "nkEmpty"
2071+
- kind: "nkCommand"
2072+
mid:
2073+
- "# comment"
2074+
sons:
2075+
- kind: "nkIdent"
2076+
ident: "command"
2077+
- kind: "nkIdent"
2078+
ident: "a"
2079+
- kind: "nkStmtList"
2080+
sons:
2081+
- kind: "nkDiscardStmt"
2082+
sons:
2083+
- kind: "nkEmpty"
2084+
- kind: "nkPragmaBlock"
2085+
mid:
2086+
- "# comment"
2087+
sons:
2088+
- kind: "nkPragma"
2089+
sons:
2090+
- kind: "nkIdent"
2091+
ident: "gcsafe"
2092+
- kind: "nkStmtList"
2093+
sons:
2094+
- kind: "nkDiscardStmt"
2095+
sons:
2096+
- kind: "nkEmpty"

tests/before/comments.nim

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,22 @@ block:
492492
true:
493493
discard
494494
else #comment
495-
: discard
495+
: discard
496+
497+
checkUntilTimeout: # wait for nodes subscribe to finish
498+
discard
499+
500+
checkUntilTimeout(# wait for nodes subscribe to finish
501+
arg
502+
)
503+
504+
checkUntilTimeout(# wait for nodes subscribe to finish
505+
):
506+
discard
507+
508+
command a:# comment
509+
discard
510+
511+
{.gcsafe.}: # comment
512+
discard
513+

tests/before/comments.nim.nph.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,3 +2034,59 @@ sons:
20342034
- kind: "nkDiscardStmt"
20352035
sons:
20362036
- kind: "nkEmpty"
2037+
- kind: "nkCall"
2038+
mid:
2039+
- "# wait for nodes subscribe to finish"
2040+
sons:
2041+
- kind: "nkIdent"
2042+
ident: "checkUntilTimeout"
2043+
- kind: "nkStmtList"
2044+
sons:
2045+
- kind: "nkDiscardStmt"
2046+
sons:
2047+
- kind: "nkEmpty"
2048+
- kind: "nkCall"
2049+
mid:
2050+
- "# wait for nodes subscribe to finish"
2051+
sons:
2052+
- kind: "nkIdent"
2053+
ident: "checkUntilTimeout"
2054+
- kind: "nkIdent"
2055+
ident: "arg"
2056+
- kind: "nkCall"
2057+
mid:
2058+
- "# wait for nodes subscribe to finish"
2059+
sons:
2060+
- kind: "nkIdent"
2061+
ident: "checkUntilTimeout"
2062+
- kind: "nkStmtList"
2063+
sons:
2064+
- kind: "nkDiscardStmt"
2065+
sons:
2066+
- kind: "nkEmpty"
2067+
- kind: "nkCommand"
2068+
mid:
2069+
- "# comment"
2070+
sons:
2071+
- kind: "nkIdent"
2072+
ident: "command"
2073+
- kind: "nkIdent"
2074+
ident: "a"
2075+
- kind: "nkStmtList"
2076+
sons:
2077+
- kind: "nkDiscardStmt"
2078+
sons:
2079+
- kind: "nkEmpty"
2080+
- kind: "nkPragmaBlock"
2081+
mid:
2082+
- "# comment"
2083+
sons:
2084+
- kind: "nkPragma"
2085+
sons:
2086+
- kind: "nkIdent"
2087+
ident: "gcsafe"
2088+
- kind: "nkStmtList"
2089+
sons:
2090+
- kind: "nkDiscardStmt"
2091+
sons:
2092+
- kind: "nkEmpty"

0 commit comments

Comments
 (0)