This commit is contained in:
Clyybber 2021-06-28 17:41:37 +02:00 • committed by GitHub
commit 72d6a9c885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View file

@ -537,13 +537,16 @@ proc parseGStrLit(p: var Parser, a: PNode): PNode =
proc complexOrSimpleStmt(p: var Parser): PNode proc complexOrSimpleStmt(p: var Parser): PNode
proc simpleExpr(p: var Parser, mode = pmNormal): PNode proc simpleExpr(p: var Parser, mode = pmNormal): PNode
proc parseIfExpr(p: var Parser, kind: TNodeKind): PNode proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode
proc semiStmtList(p: var Parser, result: PNode) = proc semiStmtList(p: var Parser, result: PNode) =
inc p.inSemiStmtList inc p.inSemiStmtList
withInd(p): withInd(p):
# Be lenient with the first stmt/expr # Be lenient with the first stmt/expr
let a = if p.tok.tokType == tkIf: parseIfExpr(p, nkIfStmt) else: complexOrSimpleStmt(p) let a = case p.tok.tokType
of tkIf: parseIfOrWhenExpr(p, nkIfStmt)
of tkWhen: parseIfOrWhenExpr(p, nkWhenStmt)
else: complexOrSimpleStmt(p)
result.add a result.add a
while p.tok.tokType != tkEof: while p.tok.tokType != tkEof:
@ -1203,13 +1206,13 @@ proc parseExpr(p: var Parser): PNode =
result = parseBlock(p) result = parseBlock(p)
of tkIf: of tkIf:
nimprettyDontTouch: nimprettyDontTouch:
result = parseIfExpr(p, nkIfExpr) result = parseIfOrWhenExpr(p, nkIfExpr)
of tkFor: of tkFor:
nimprettyDontTouch: nimprettyDontTouch:
result = parseFor(p) result = parseFor(p)
of tkWhen: of tkWhen:
nimprettyDontTouch: nimprettyDontTouch:
result = parseIfExpr(p, nkWhenExpr) result = parseIfOrWhenExpr(p, nkWhenExpr)
of tkCase: of tkCase:
# Currently we think nimpretty is good enough with case expressions, # Currently we think nimpretty is good enough with case expressions,
# so it is allowed to touch them: # so it is allowed to touch them:
@ -1559,7 +1562,7 @@ proc parseIfOrWhen(p: var Parser, kind: TNodeKind): PNode =
branch.add(parseStmt(p)) branch.add(parseStmt(p))
result.add(branch) result.add(branch)
proc parseIfExpr(p: var Parser, kind: TNodeKind): PNode = proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode =
#| condExpr = expr colcom expr optInd #| condExpr = expr colcom expr optInd
#| ('elif' expr colcom expr optInd)* #| ('elif' expr colcom expr optInd)*
#| 'else' colcom expr #| 'else' colcom expr

View file

@ -50,6 +50,8 @@ hello
9 9
hello hello
10 10
lucky
lucky
''' '''
""" """
@ -163,3 +165,16 @@ template dim: int =
else: else:
int.high) int.high)
# lenient indentation:
echo (if 0 == 1:
"0 == 1"
else:
"lucky")
# bug #16426
echo (when 0 == 1:
"0 == 1"
else:
"lucky")