-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02953b9
commit 952d70b
Showing
29 changed files
with
1,024 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
use rustpython_ast::{Expr, ExprContext, ExprKind}; | ||
|
||
pub fn set_context(expr: Expr, ctx: ExprContext) -> Expr { | ||
match expr.node { | ||
ExprKind::Name { id, .. } => Expr { | ||
node: ExprKind::Name { id, ctx }, | ||
..expr | ||
}, | ||
ExprKind::Tuple { elts, .. } => Expr { | ||
node: ExprKind::Tuple { | ||
elts: elts | ||
.into_iter() | ||
.map(|elt| set_context(elt, ctx.clone())) | ||
.collect(), | ||
ctx, | ||
}, | ||
..expr | ||
}, | ||
ExprKind::List { elts, .. } => Expr { | ||
node: ExprKind::List { | ||
elts: elts | ||
.into_iter() | ||
.map(|elt| set_context(elt, ctx.clone())) | ||
.collect(), | ||
ctx, | ||
}, | ||
..expr | ||
}, | ||
ExprKind::Attribute { value, attr, .. } => Expr { | ||
node: ExprKind::Attribute { value, attr, ctx }, | ||
..expr | ||
}, | ||
ExprKind::Subscript { value, slice, .. } => Expr { | ||
node: ExprKind::Subscript { value, slice, ctx }, | ||
..expr | ||
}, | ||
ExprKind::Starred { value, .. } => Expr { | ||
node: ExprKind::Starred { | ||
value: Box::new(set_context(*value, ctx.clone())), | ||
ctx, | ||
}, | ||
..expr | ||
}, | ||
_ => expr, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::parser::parse_program; | ||
|
||
#[test] | ||
fn test_assign_name() { | ||
let source = String::from("x = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_tuple() { | ||
let source = String::from("(x, y) = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_list() { | ||
let source = String::from("[x, y] = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_attribute() { | ||
let source = String::from("x.y = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_subscript() { | ||
let source = String::from("x[y] = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_starred() { | ||
let source = String::from("(x, *y) = (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_for() { | ||
let source = String::from("for x in (1, 2, 3): pass"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_list_comp() { | ||
let source = String::from("x = [y for y in (1, 2, 3)]"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_set_comp() { | ||
let source = String::from("x = {y for y in (1, 2, 3)}"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_with() { | ||
let source = String::from("with 1 as x: pass"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_assign_named_expr() { | ||
let source = String::from("if x:= 1: pass"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_ann_assign_name() { | ||
let source = String::from("x: int = 1"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_aug_assign_name() { | ||
let source = String::from("x += 1"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_aug_assign_attribute() { | ||
let source = String::from("x.y += (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_aug_assign_subscript() { | ||
let source = String::from("x[y] += (1, 2, 3)"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_del_name() { | ||
let source = String::from("del x"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_del_attribute() { | ||
let source = String::from("del x.y"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
|
||
#[test] | ||
fn test_del_subscript() { | ||
let source = String::from("del x[y]"); | ||
let parse_ast = parse_program(&source, "<test>").unwrap(); | ||
insta::assert_debug_snapshot!(parse_ast); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,5 +30,6 @@ pub mod mode; | |
pub mod parser; | ||
#[rustfmt::skip] | ||
mod python; | ||
mod context; | ||
mod string; | ||
pub mod token; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
parser/src/snapshots/rustpython_parser__context__tests__assign.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
source: compiler/parser/src/context.rs | ||
expression: parse_ast | ||
--- | ||
[ | ||
Located { | ||
location: Location { | ||
row: 1, | ||
column: 1, | ||
}, | ||
custom: (), | ||
node: Assign { | ||
targets: [ | ||
Located { | ||
location: Location { | ||
row: 1, | ||
column: 1, | ||
}, | ||
custom: (), | ||
node: Name { | ||
id: "x", | ||
ctx: Store, | ||
}, | ||
}, | ||
], | ||
value: Located { | ||
location: Location { | ||
row: 1, | ||
column: 5, | ||
}, | ||
custom: (), | ||
node: Constant { | ||
value: Int( | ||
1, | ||
), | ||
kind: None, | ||
}, | ||
}, | ||
type_comment: None, | ||
}, | ||
}, | ||
] |
Oops, something went wrong.