diff options
-rw-r--r-- | compiler/parser.nim | 6 | ||||
-rw-r--r-- | doc/grammar.txt | 4 | ||||
-rw-r--r-- | tests/parser/tletcolon.nim | 11 |
3 files changed, 18 insertions, 3 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim index e3bb68da4..41303ead9 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -72,6 +72,7 @@ proc parseStmtPragma(p: var TParser): PNode proc parsePragma(p: var TParser): PNode proc postExprBlocks(p: var TParser, x: PNode): PNode proc parseExprStmt(p: var TParser): PNode +proc parseBlock(p: var TParser): PNode # implementation proc getTok(p: var TParser) = @@ -1078,12 +1079,14 @@ proc parseTypeDescKAux(p: var TParser, kind: TNodeKind, parseSymbolList(p, list) proc parseExpr(p: var TParser): PNode = - #| expr = (ifExpr + #| expr = (blockExpr + #| | ifExpr #| | whenExpr #| | caseExpr #| | tryExpr) #| / simpleExpr case p.tok.tokType: + of tkBlock: result = parseBlock(p) of tkIf: result = parseIfExpr(p, nkIfExpr) of tkWhen: result = parseIfExpr(p, nkWhenExpr) of tkCase: result = parseCase(p) @@ -1517,6 +1520,7 @@ proc parseFor(p: var TParser): PNode = proc parseBlock(p: var TParser): PNode = #| blockStmt = 'block' symbol? colcom stmt + #| blockExpr = 'block' symbol? colcom stmt result = newNodeP(nkBlockStmt, p) getTokNoInd(p) if p.tok.tokType == tkColon: addSon(result, ast.emptyNode) diff --git a/doc/grammar.txt b/doc/grammar.txt index 58b119331..eae3694a0 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -83,7 +83,8 @@ paramListColon = paramList? (':' optInd typeDesc)? doBlock = 'do' paramListArrow pragmas? colcom stmt procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)? distinct = 'distinct' optInd typeDesc -expr = (ifExpr +expr = (blockExpr + | ifExpr | whenExpr | caseExpr | tryExpr) @@ -141,6 +142,7 @@ tryExpr = 'try' colcom stmt &(optInd 'except'|'finally') exceptBlock = 'except' colcom stmt forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt blockStmt = 'block' symbol? colcom stmt +blockExpr = 'block' symbol? colcom stmt staticStmt = 'static' colcom stmt deferStmt = 'defer' colcom stmt asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLE_STR_LIT) diff --git a/tests/parser/tletcolon.nim b/tests/parser/tletcolon.nim index eab7a8edd..7eaa5e3e5 100644 --- a/tests/parser/tletcolon.nim +++ b/tests/parser/tletcolon.nim @@ -4,7 +4,8 @@ discard """ 44 3 more body code yes -yes''' +yes +block expression works''' """ template x(body): untyped = @@ -50,3 +51,11 @@ let format = else: echo "Texture Format Unknown, assuming RGB" #This echo causes an error TextureInternalFormat.RGB + +# Block as expressions #3827 +block: + let x = block: + var y = 2 + echo "block expression works" + y*y + doAssert x == 4 \ No newline at end of file |