File tree Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -156,8 +156,35 @@ proc parsePyStmt*(mparser; statement: NimNode): NimNode =
156
156
expectKind statement[0 ], nnkStmtList
157
157
nStmt.add mparser.parsePyBody statement[0 ]
158
158
var excBranch = statement[1 ]
159
- excBranch[^ 1 ] = mparser.parsePyBody excBranch[^ 1 ]
160
- nStmt.add excBranch
159
+ let nExcBranchStmts = mparser.parsePyBody excBranch[^ 1 ]
160
+ let first = excBranch[0 ]
161
+ proc errNotParenExcs(where: NimNode) =
162
+ error " SyntaxError: multiple exception types must be parenthesized" , where
163
+ func newExceptBranch(excs, body: NimNode): NimNode =
164
+ result = newNimNode nnkExceptBranch
165
+ for e in excs:
166
+ result .add e
167
+ result .add body
168
+ if first.kind == nnkTupleConstr:
169
+ # except (exc, ...) -> except exc, ... as Nim disallows the former.
170
+ let excs = first
171
+ if excBranch.len != 2 :
172
+ errNotParenExcs excs
173
+ excBranch = newExceptBranch(excs, nExcBranchStmts)
174
+ nStmt.add excBranch
175
+ elif first.kind == nnkInfix and first[0 ].eqIdent" as" :
176
+ if first[1 ].kind != nnkTupleConstr:
177
+ errNotParenExcs first[2 ]
178
+
179
+ let exc = first[2 ]
180
+ var nBody = newStmtList(newLetStmt(exc, newCall(bindSym" getCurrentException" )))
181
+ for i in nExcBranchStmts: nBody.add i
182
+
183
+ var nExcBranch = newExceptBranch(first[1 ], nBody)
184
+ nStmt.add nExcBranch
185
+ else :
186
+ excBranch[^ 1 ] = nExcBranchStmts
187
+ nStmt.add excBranch
161
188
result .add nStmt
162
189
of nnkInfix:
163
190
result .add statement
You can’t perform that action at this time.
0 commit comments