Skip to content

Commit

Permalink
fix comment after if/while/case (fixes #71) (#78)
Browse files Browse the repository at this point in the history
When a comment appears after keywords with conditionals, it's an open
question how to format them: they could stay there but this causes
trouble with indent of long/non-trivial conditions - instead of coming
up with a clever solution, this PR simply moves the comment out of
there.
  • Loading branch information
arnetheduck committed Jul 19, 2024
1 parent 8b261be commit 77457f1
Show file tree
Hide file tree
Showing 5 changed files with 557 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/phparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ proc exprEqExpr(p: var Parser): PNode =
proc exprList(p: var Parser, endTok: TokType, result: PNode) =
#| exprList = expr ^+ comma
getTok(p)
splitLookahead(p, result, clMid)
optInd(p, result)
# progress guaranteed
var a = parseExpr(p)
Expand All @@ -498,6 +499,7 @@ proc exprList(p: var Parser, endTok: TokType, result: PNode) =
proc optionalExprList(p: var Parser, endTok: TokType, result: PNode) =
#| optionalExprList = expr ^* comma
getTok(p)
splitLookahead(p, result, clMid)
optInd(p, result)
# progress guaranteed
while (p.tok.tokType != endTok) and (p.tok.tokType != tkEof):
Expand Down Expand Up @@ -1894,9 +1896,10 @@ proc parseIfOrWhen(p: var Parser, kind: TNodeKind): PNode =
#| ifStmt = 'if' condStmt
#| whenStmt = 'when' condStmt
result = newNodeP(kind, p)

while true:
getTok(p) # skip `if`, `when`, `elif`
var branch = newNodeP(nkElifBranch, p)
getTok(p) # skip `if`, `when`, `elif`
splitLookahead(p, branch, clMid)
optInd(p, branch)
branch.add(parseExpr(p))
Expand All @@ -1920,8 +1923,8 @@ proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode =
#| whenExpr = 'when' condExpr
result = newNodeP(kind, p)
while true:
getTok(p) # skip `if`, `when`, `elif`
var branch = newNodeP(nkElifExpr, p)
getTok(p) # skip `if`, `when`, `elif`
splitLookahead(p, branch, clMid)
optInd(p, branch)
branch.add(parseExpr(p))
Expand Down Expand Up @@ -1983,11 +1986,13 @@ proc parseCase(p: var Parser): PNode =
inElif = true
b = newNodeP(nkElifBranch, p)
getTok(p)
splitLookahead(p, b, clMid)
optInd(p, b)
b.add(parseExpr(p))
of tkElse:
b = newNodeP(nkElse, p)
getTok(p)
splitLookahead(p, b, clMid)
else:
break
b.add(parseColComStmt(p, b, clMid))
Expand Down
26 changes: 26 additions & 0 deletions tests/after/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,29 @@ block:
block:
discard
# dedented comment post discard

block:
if 2 >= 1 and 2 >= 1 and 2 >= 1: # some conditions:
discard
elif 2 >= 1 and 2 >= 1 and 2 >= 1: # some elif conds
discard

if 2 >= 1 and 2 >= 1 and 2 >= 1:
# some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc
discard

if 2 >= 1 and
# some conditions
2 >= 1 and 2 >= 1:
discard

while 2 >= 1 and 2 >= 1 and 2 >= 1: # some conditions:
discard

case
# comment
true
of true: # comment
discard
else: #comment
discard
242 changes: 242 additions & 0 deletions tests/after/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1796,3 +1796,245 @@ sons:
- kind: "nkEmpty"
- kind: "nkCommentStmt"
"comment": "# dedented comment post discard"
- kind: "nkBlockStmt"
sons:
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkIfStmt"
sons:
- kind: "nkElifBranch"
mid:
- "# some conditions:"
sons:
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkElifBranch"
mid:
- "# some elif conds"
sons:
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkIfStmt"
sons:
- kind: "nkElifBranch"
sons:
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkStmtList"
sons:
- kind: "nkCommentStmt"
"comment": "# some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc"
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkIfStmt"
sons:
- kind: "nkElifBranch"
sons:
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
prefix:
- "# some conditions"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkWhileStmt"
mid:
- "# some conditions:"
sons:
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: "and"
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkInfix"
sons:
- kind: "nkIdent"
ident: ">="
- kind: "nkIntLit"
intVal: 2
- kind: "nkIntLit"
intVal: 1
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkCaseStmt"
sons:
- kind: "nkIdent"
prefix:
- "# comment"
ident: "true"
- kind: "nkOfBranch"
mid:
- "# comment"
sons:
- kind: "nkIdent"
ident: "true"
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkElse"
mid:
- "#comment"
sons:
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
41 changes: 40 additions & 1 deletion tests/before/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,43 @@ discard
block:
block:
discard
# dedented comment post discard
# dedented comment post discard

block:
if # some conditions:
2 >= 1 and
2 >= 1 and
2 >= 1:
discard
elif # some elif conds
2 >= 1 and
2 >= 1 and
2 >= 1:
discard

if # some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc
2 >= 1 and
2 >= 1 and
2 >= 1:
discard

if
2 >= 1 and
# some conditions
2 >= 1 and
2 >= 1:
discard

while # some conditions:
2 >= 1 and
2 >= 1 and
2 >= 1:
discard

case # comment
true
of # comment
true:
discard
else #comment
: discard
Loading

0 comments on commit 77457f1

Please sign in to comment.