Set ExprContext::Store on parenthesized with expressions

This commit is contained in:
Charlie Marsh 2022-12-12 09:01:17 -05:00
parent bfd847d04c
commit 36f5f5affb
2 changed files with 384 additions and 1 deletions

View file

@ -2091,4 +2091,380 @@ expression: "parse_program(source, \"<test>\").unwrap()"
type_comment: None,
},
},
Located {
location: Location {
row: 23,
column: 0,
},
end_location: Some(
Location {
row: 24,
column: 0,
},
),
custom: (),
node: With {
items: [
Withitem {
context_expr: Located {
location: Location {
row: 23,
column: 6,
},
end_location: Some(
Location {
row: 23,
column: 7,
},
),
custom: (),
node: Constant {
value: Int(
0,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 23,
column: 11,
},
end_location: Some(
Location {
row: 23,
column: 12,
},
),
custom: (),
node: Name {
id: "a",
ctx: Store,
},
},
),
},
],
body: [
Located {
location: Location {
row: 23,
column: 15,
},
end_location: Some(
Location {
row: 23,
column: 19,
},
),
custom: (),
node: Pass,
},
],
type_comment: None,
},
},
Located {
location: Location {
row: 24,
column: 0,
},
end_location: Some(
Location {
row: 25,
column: 0,
},
),
custom: (),
node: With {
items: [
Withitem {
context_expr: Located {
location: Location {
row: 24,
column: 6,
},
end_location: Some(
Location {
row: 24,
column: 7,
},
),
custom: (),
node: Constant {
value: Int(
0,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 24,
column: 11,
},
end_location: Some(
Location {
row: 24,
column: 12,
},
),
custom: (),
node: Name {
id: "a",
ctx: Store,
},
},
),
},
],
body: [
Located {
location: Location {
row: 24,
column: 16,
},
end_location: Some(
Location {
row: 24,
column: 20,
},
),
custom: (),
node: Pass,
},
],
type_comment: None,
},
},
Located {
location: Location {
row: 25,
column: 0,
},
end_location: Some(
Location {
row: 26,
column: 0,
},
),
custom: (),
node: With {
items: [
Withitem {
context_expr: Located {
location: Location {
row: 25,
column: 6,
},
end_location: Some(
Location {
row: 25,
column: 7,
},
),
custom: (),
node: Constant {
value: Int(
0,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 25,
column: 11,
},
end_location: Some(
Location {
row: 25,
column: 12,
},
),
custom: (),
node: Name {
id: "a",
ctx: Store,
},
},
),
},
Withitem {
context_expr: Located {
location: Location {
row: 25,
column: 14,
},
end_location: Some(
Location {
row: 25,
column: 15,
},
),
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 25,
column: 19,
},
end_location: Some(
Location {
row: 25,
column: 20,
},
),
custom: (),
node: Name {
id: "b",
ctx: Store,
},
},
),
},
],
body: [
Located {
location: Location {
row: 25,
column: 23,
},
end_location: Some(
Location {
row: 25,
column: 27,
},
),
custom: (),
node: Pass,
},
],
type_comment: None,
},
},
Located {
location: Location {
row: 26,
column: 0,
},
end_location: Some(
Location {
row: 27,
column: 0,
},
),
custom: (),
node: With {
items: [
Withitem {
context_expr: Located {
location: Location {
row: 26,
column: 6,
},
end_location: Some(
Location {
row: 26,
column: 7,
},
),
custom: (),
node: Constant {
value: Int(
0,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 26,
column: 11,
},
end_location: Some(
Location {
row: 26,
column: 12,
},
),
custom: (),
node: Name {
id: "a",
ctx: Store,
},
},
),
},
Withitem {
context_expr: Located {
location: Location {
row: 26,
column: 14,
},
end_location: Some(
Location {
row: 26,
column: 15,
},
),
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
optional_vars: Some(
Located {
location: Location {
row: 26,
column: 19,
},
end_location: Some(
Location {
row: 26,
column: 20,
},
),
custom: (),
node: Name {
id: "b",
ctx: Store,
},
},
),
},
],
body: [
Located {
location: Location {
row: 26,
column: 24,
},
end_location: Some(
Location {
row: 26,
column: 28,
},
),
custom: (),
node: Pass,
},
],
type_comment: None,
},
},
]

View file

@ -15,6 +15,7 @@
//! intermediate data types.
use crate::ast::{self, Location};
use crate::context;
use crate::error::{LexicalError, LexicalErrorType};
use crate::token::Tok;
use lalrpop_util::ParseError as LalrpopError;
@ -117,7 +118,9 @@ impl TryFrom<ExprOrWithitems> for Vec<ast::Withitem> {
.into_iter()
.map(|(context_expr, optional_vars)| ast::Withitem {
context_expr: Box::new(context_expr),
optional_vars,
optional_vars: optional_vars.map(|expr| {
Box::new(context::set_context(*expr, ast::ExprContext::Store))
}),
})
.collect())
}
@ -158,6 +161,10 @@ with (a := 0): pass
with (a := 0) as x: pass
with (a := 0, b := 1): pass
with (a := 0, b := 1) as x: pass
with (0 as a): pass
with (0 as a,): pass
with (0 as a, 1 as b): pass
with (0 as a, 1 as b,): pass
";
insta::assert_debug_snapshot!(parse_program(source, "<test>").unwrap());
}