Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extra_tests/snippets/syntax_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def make_numbers():
r = list(x for x in [1, 2, 3])
assert r == [1, 2, 3]

r = list(y := x + 1 for x in [1, 2, 3])
assert r == [2, 3, 4]

def g2(x):
x = yield x
yield x + 5
Expand Down
2 changes: 1 addition & 1 deletion parser/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ Atom: ast::Expr = {
}
},
"(" <e:YieldExpr> ")" => e,
<location:@L> "(" <elt:Test> <generators:CompFor> ")" => {
<location:@L> "(" <elt:NamedExpressionTest> <generators:CompFor> ")" => {
ast::Expr {
location,
custom: (),
Expand Down
21 changes: 21 additions & 0 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,25 @@ class Foo(A, B):
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_generator_comprehension() {
let source = String::from("(x for y in z)");
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_named_expression_generator_comprehension() {
let source = String::from("(x := y + 1 for y in z)");
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_if_else_generator_comprehension() {
let source = String::from("(x if y else y for y in z)");
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: parser/src/parser.rs
expression: parse_ast
---
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: GeneratorExp {
elt: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "x",
ctx: Load,
},
},
generators: [
Comprehension {
target: Located {
location: Location {
row: 1,
column: 8,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 1,
column: 13,
},
custom: (),
node: Name {
id: "z",
ctx: Load,
},
},
ifs: [],
is_async: 0,
},
],
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
source: parser/src/parser.rs
expression: parse_ast
---
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: GeneratorExp {
elt: Located {
location: Location {
row: 1,
column: 4,
},
custom: (),
node: IfExp {
test: Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
body: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "x",
ctx: Load,
},
},
orelse: Located {
location: Location {
row: 1,
column: 14,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
},
},
generators: [
Comprehension {
target: Located {
location: Location {
row: 1,
column: 20,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 1,
column: 25,
},
custom: (),
node: Name {
id: "z",
ctx: Load,
},
},
ifs: [],
is_async: 0,
},
],
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
source: parser/src/parser.rs
expression: parse_ast
---
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: GeneratorExp {
elt: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: NamedExpr {
target: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "x",
ctx: Store,
},
},
value: Located {
location: Location {
row: 1,
column: 9,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
op: Add,
right: Located {
location: Location {
row: 1,
column: 11,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
},
},
},
},
generators: [
Comprehension {
target: Located {
location: Location {
row: 1,
column: 17,
},
custom: (),
node: Name {
id: "y",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 1,
column: 22,
},
custom: (),
node: Name {
id: "z",
ctx: Load,
},
},
ifs: [],
is_async: 0,
},
],
},
}