From 36f5f5affb1c172e64fb8d13c4bb10af7825d7ee Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 12 Dec 2022 09:01:17 -0500 Subject: [PATCH] Set ExprContext::Store on parenthesized with expressions --- ...n_parser__with__tests__with_statement.snap | 376 ++++++++++++++++++ parser/src/with.rs | 9 +- 2 files changed, 384 insertions(+), 1 deletion(-) diff --git a/parser/src/snapshots/rustpython_parser__with__tests__with_statement.snap b/parser/src/snapshots/rustpython_parser__with__tests__with_statement.snap index e82ee2d..39001dc 100644 --- a/parser/src/snapshots/rustpython_parser__with__tests__with_statement.snap +++ b/parser/src/snapshots/rustpython_parser__with__tests__with_statement.snap @@ -2091,4 +2091,380 @@ expression: "parse_program(source, \"\").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, + }, + }, ] diff --git a/parser/src/with.rs b/parser/src/with.rs index afc4823..57ee1a6 100644 --- a/parser/src/with.rs +++ b/parser/src/with.rs @@ -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 for Vec { .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, "").unwrap()); }