From 72190536cb9718fe6336f20f0c4366a3856d822a Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 11 Apr 2025 04:29:20 +0300 Subject: [PATCH] skip semicolon in stmtlist expr parsing (#24855) Previously it would try to parse the semicolon as its own statement and produce an `nkEmpty` node Also more than 1 semicolon in an expression list i.e. `(a;; b)` gives an "expression expected" error in `semiStmtList` when multiple semicolons are allowed in normal statements, this could be fixed by changing the `if tok.kind == tokSemicolon` check to a `while` but it does not match the grammar so not done here. (cherry picked from commit 918f972369e62d6aa07e173aa1e70aa5c4714fc8) --- compiler/parser.nim | 2 ++ tests/parser/tstmtlistexprempty.nim | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/parser/tstmtlistexprempty.nim diff --git a/compiler/parser.nim b/compiler/parser.nim index 7475050974..7f438f4208 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -699,10 +699,12 @@ proc parsePar(p: var Parser): PNode = asgn.add b result.add(asgn) if p.tok.tokType == tkSemiColon: + getTok(p) semiStmtList(p, result) elif p.tok.tokType == tkSemiColon: # stmt context: result.add(a) + getTok(p) semiStmtList(p, result) else: a = colonOrEquals(p, a) diff --git a/tests/parser/tstmtlistexprempty.nim b/tests/parser/tstmtlistexprempty.nim new file mode 100644 index 0000000000..1d71c1aeb6 --- /dev/null +++ b/tests/parser/tstmtlistexprempty.nim @@ -0,0 +1,23 @@ +discard """ + nimout: ''' +StmtList + ReturnStmt + StmtListExpr + Call + DotExpr + Ident "x" + Ident "add" + StrLit "123" + Call + DotExpr + Ident "x" + Ident "add" + StrLit "123" + Ident "x" +''' +""" + +import std/macros + +dumpTree: + return (x.add("123"); x.add("123"); x)