Skip to content

Commit 31c69b4

Browse files
authored
Parse async with multliple statements (#2018)
* Parse async with multliple statements * Re-enable grammar test
1 parent f2ec7df commit 31c69b4

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/core/IronPython.StdLib/lib/test/test_grammar.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,13 +1474,12 @@ async def foo():
14741474
pass
14751475
async with manager() as (x, y):
14761476
pass
1477-
# ironpython: todo implement this
1478-
#async with manager(), manager():
1479-
# pass
1480-
#async with manager() as x, manager() as y:
1481-
# pass
1482-
#async with manager() as x, manager():
1483-
# pass
1477+
async with manager(), manager():
1478+
pass
1479+
async with manager() as x, manager() as y:
1480+
pass
1481+
async with manager() as x, manager():
1482+
pass
14841483
raise Done
14851484

14861485
with self.assertRaises(Done):

src/core/IronPython/Compiler/Parser.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ private WithStatement ParseWithStmt() {
14171417
var withItem = ParseWithItem();
14181418
List<WithItem> items = null;
14191419
while (MaybeEat(TokenKind.Comma)) {
1420-
if (items == null) {
1420+
if (items is null) {
14211421
items = new List<WithItem>();
14221422
}
14231423

@@ -1427,7 +1427,7 @@ private WithStatement ParseWithStmt() {
14271427

14281428
var header = GetEnd();
14291429
Statement body = ParseSuite();
1430-
if (items != null) {
1430+
if (items is not null) {
14311431
for (int i = items.Count - 1; i >= 0; i--) {
14321432
var curItem = items[i];
14331433
var innerWith = new WithStatement(curItem.ContextManager, curItem.Variable, body);
@@ -1480,12 +1480,34 @@ private AsyncWithStatement ParseAsyncWithStmt(int asyncStart) {
14801480
}
14811481

14821482
Eat(TokenKind.KeywordWith);
1483+
14831484
var withItem = ParseWithItem();
1485+
List<WithItem> items = null;
1486+
while (MaybeEat(TokenKind.Comma)) {
1487+
if (items is null) {
1488+
items = new List<WithItem>();
1489+
}
1490+
1491+
items.Add(ParseWithItem());
1492+
}
1493+
1494+
14841495
var header = GetEnd();
14851496
Statement body = ParseSuite();
1497+
if (items is not null) {
1498+
for (int i = items.Count - 1; i >= 0; i--) {
1499+
var curItem = items[i];
1500+
var innerWith = new AsyncWithStatement(curItem.ContextManager, curItem.Variable, body);
1501+
innerWith.HeaderIndex = header;
1502+
innerWith.SetLoc(_globalParent, withItem.Start, GetEnd());
1503+
body = innerWith;
1504+
header = GetEnd();
1505+
}
1506+
}
1507+
14861508
AsyncWithStatement ret = new AsyncWithStatement(withItem.ContextManager, withItem.Variable, body);
14871509
ret.HeaderIndex = header;
1488-
ret.SetLoc(_globalParent, asyncStart, GetEnd());
1510+
ret.SetLoc(_globalParent, withItem.Start, GetEnd());
14891511
return ret;
14901512
}
14911513

0 commit comments

Comments
 (0)