summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-12-22 04:46:14 +0800
committerGitHub <noreply@github.com>2022-12-21 21:46:14 +0100
commitd0721eadf8cd6bf9436b5e5c9113c4c7bcfcc770 (patch)
tree77f1039491ceeaf71d59b8df8d379661d9852591
parent81d8ea95af0cfaaedca2fd1881199e113e6f5b41 (diff)
downloadNim-d0721eadf8cd6bf9436b5e5c9113c4c7bcfcc770.tar.gz
fixes #21144; try expression will not match the less indentation except (#21152)
fixes #21144; try expression will not match the less indent except
-rw-r--r--compiler/parser.nim4
-rw-r--r--tests/parser/ttry.nim27
2 files changed, 30 insertions, 1 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 6b3cebb7f..00013c218 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -1770,11 +1770,13 @@ proc parseTry(p: var Parser; isExpr: bool): PNode =
   #|            (optInd 'except' optionalExprList colcom stmt)*
   #|            (optInd 'finally' colcom stmt)?
   result = newNodeP(nkTryStmt, p)
+  let parentIndent = p.currInd # isExpr
   getTok(p)
   colcom(p, result)
   result.add(parseStmt(p))
   var b: PNode = nil
-  while sameOrNoInd(p) or isExpr:
+
+  while sameOrNoInd(p) or (isExpr and parentIndent <= p.tok.indent):
     case p.tok.tokType
     of tkExcept:
       b = newNodeP(nkExceptBranch, p)
diff --git a/tests/parser/ttry.nim b/tests/parser/ttry.nim
new file mode 100644
index 000000000..190b0b8dc
--- /dev/null
+++ b/tests/parser/ttry.nim
@@ -0,0 +1,27 @@
+# bug #21144
+block:
+  try:
+    let c = try:
+        10
+      except ValueError as exc:
+        10
+  except ValueError as exc:
+    discard
+
+if true:
+  block:
+    let c = try:
+          10
+        except ValueError as exc:
+          10
+    except OSError:
+      99
+
+
+try:
+  let c = try:
+    10
+  except ValueError as exc:
+    10
+except ValueError as exc:
+  discard
\ No newline at end of file