Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix comment after if/while/case (fixes #71) #78

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -473,3 +473,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
Loading