Add parenthesized flag to ExprTuple and ExprGenerator (#9614)

This commit is contained in:
Micha Reiser 2024-02-26 16:35:20 +01:00 committed by GitHub
parent ab4bd71755
commit 77c5561646
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
65 changed files with 391 additions and 139 deletions

View file

@ -3,10 +3,16 @@ use ruff_python_ast::{self as ast, Expr, ExprContext};
pub(crate) fn set_context(expr: Expr, ctx: ExprContext) -> Expr {
match expr {
Expr::Name(ast::ExprName { id, range, .. }) => ast::ExprName { range, id, ctx }.into(),
Expr::Tuple(ast::ExprTuple { elts, range, .. }) => ast::ExprTuple {
Expr::Tuple(ast::ExprTuple {
elts,
range,
parenthesized: is_parenthesized,
ctx: _,
}) => ast::ExprTuple {
elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(),
range,
ctx,
parenthesized: is_parenthesized,
}
.into(),

View file

@ -1505,4 +1505,20 @@ u"foo" f"bar {baz} really" u"bar" "no"
let parse_ast = parse_suite(r#"x = "\N{BACKSPACE}another cool trick""#).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_tuple() {
let parse_ast = parse_suite(
r#"
a,b
(a,b)
()
(a,)
((a,b))
"#
.trim(),
)
.unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
}

View file

@ -483,7 +483,8 @@ MatchStatement: ast::Stmt = {
ast::ExprTuple {
elts: vec![subject.into()],
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
range: (tuple_location..tuple_end_location).into(),
parenthesized: false
},
)),
cases,
@ -506,7 +507,8 @@ MatchStatement: ast::Stmt = {
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
range: (tuple_location..tuple_end_location).into(),
parenthesized: false
},
)),
cases,
@ -1573,6 +1575,7 @@ SubscriptList: crate::parser::ParenthesizedExpr = {
elts: vec![s1.into()],
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
},
<location:@L> <elts:TwoOrMoreSep<Subscript, ",">> ","? <end_location:@R> => {
@ -1581,6 +1584,7 @@ SubscriptList: crate::parser::ParenthesizedExpr = {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
};
@ -1726,7 +1730,12 @@ Atom<Goal>: crate::parser::ParenthesizedExpr = {
}
} else {
let elts = elts.into_iter().map(ast::Expr::from).collect();
ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into()
}
},
<location:@L> "(" <left:(<OneOrMore<Test<"all">>> ",")?> <mid:NamedOrStarExpr> <right:("," <TestOrStarNamedExpr>)*> <trailing_comma:","?> ")" <end_location:@R> =>? {
@ -1743,13 +1752,19 @@ Atom<Goal>: crate::parser::ParenthesizedExpr = {
})
} else {
let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect();
Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into())
Ok(ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into())
}
},
<location:@L> "(" ")" <end_location:@R> => ast::ExprTuple {
elts: Vec::new(),
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into(),
<location:@L> "(" <e:YieldExpr> ")" <end_location:@R> => crate::parser::ParenthesizedExpr {
expr: e.into(),
@ -1759,6 +1774,7 @@ Atom<Goal>: crate::parser::ParenthesizedExpr = {
elt: Box::new(elt.into()),
generators,
range: (location..end_location).into(),
parenthesized: true
}.into(),
"(" <location:@L> "**" <e:Expression<"all">> ")" <end_location:@R> =>? {
Err(LexicalError::new(
@ -1852,7 +1868,12 @@ GenericList<Element>: crate::parser::ParenthesizedExpr = {
}
} else {
let elts = elts.into_iter().map(ast::Expr::from).collect();
ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
}
}
@ -1904,7 +1925,8 @@ FunctionArgument: (Option<(TextSize, TextSize, Option<ast::Identifier>)>, ast::E
ast::ExprGeneratorExp {
elt: Box::new(elt.into()),
generators,
range: (location..end_location).into()
range: (location..end_location).into(),
parenthesized: false
}
),
None => elt.into(),

View file

@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: 8c85e4bbac54760ed8be03b56a428d76e14d18e6dbde62b424d0b2b5e8e65dbe
// sha3: d64ca7ff27121baee9d7a1b4d0f341932391a365fe75f115987b05bf2aaf538e
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_python_ast::{self as ast, Int, IpyEscapeKind};
use crate::{
@ -33938,7 +33938,8 @@ fn __action86<
ast::ExprTuple {
elts: vec![subject.into()],
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
range: (tuple_location..tuple_end_location).into(),
parenthesized: false
},
)),
cases,
@ -33982,7 +33983,8 @@ fn __action87<
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
range: (tuple_location..tuple_end_location).into(),
parenthesized: false
},
)),
cases,
@ -36227,6 +36229,7 @@ fn __action208<
elts: vec![s1.into()],
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
}
@ -36249,6 +36252,7 @@ fn __action209<
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
}
@ -36796,7 +36800,8 @@ fn __action242<
ast::ExprGeneratorExp {
elt: Box::new(elt.into()),
generators,
range: (location..end_location).into()
range: (location..end_location).into(),
parenthesized: false
}
),
None => elt.into(),
@ -37049,7 +37054,12 @@ fn __action259<
}
} else {
let elts = elts.into_iter().map(ast::Expr::from).collect();
ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
}
}
@ -37103,7 +37113,12 @@ fn __action262<
}
} else {
let elts = elts.into_iter().map(ast::Expr::from).collect();
ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: false
}.into()
}
}
}
@ -41292,7 +41307,12 @@ fn __action553<
}
} else {
let elts = elts.into_iter().map(ast::Expr::from).collect();
ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into()
}
}
}
@ -41327,7 +41347,12 @@ fn __action554<
})
} else {
let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect();
Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into())
Ok(ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into())
}
}
}
@ -41348,6 +41373,7 @@ fn __action555<
elts: Vec::new(),
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into()
}
@ -41388,6 +41414,7 @@ fn __action557<
elt: Box::new(elt.into()),
generators,
range: (location..end_location).into(),
parenthesized: true
}.into()
}
@ -42025,7 +42052,12 @@ fn __action596<
})
} else {
let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect();
Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into())
Ok(ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into())
}
}
}
@ -42046,6 +42078,7 @@ fn __action597<
elts: Vec::new(),
ctx: ast::ExprContext::Load,
range: (location..end_location).into(),
parenthesized: true
}.into()
}
@ -42086,6 +42119,7 @@ fn __action599<
elt: Box::new(elt.into()),
generators,
range: (location..end_location).into(),
parenthesized: true
}.into()
}

View file

@ -55,6 +55,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -44,6 +44,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
body: [

View file

@ -60,6 +60,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -65,6 +65,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
ifs: [],

View file

@ -45,6 +45,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -65,6 +65,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
ifs: [],

View file

@ -33,6 +33,7 @@ expression: parse_ast
),
],
ctx: Store,
parenthesized: true,
},
),
],
@ -66,6 +67,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -58,6 +58,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -27,6 +27,7 @@ expression: parse_ast
),
],
ctx: Store,
parenthesized: true,
},
),
],
@ -60,6 +61,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -54,6 +54,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -57,6 +57,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -35,6 +35,7 @@ Ok(
),
],
ctx: Store,
parenthesized: true,
},
),
],
@ -68,6 +69,7 @@ Ok(
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -111,6 +111,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
debug_text: None,
@ -501,6 +502,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
debug_text: Some(

View file

@ -146,12 +146,14 @@ Call(
),
],
ctx: Load,
parenthesized: true,
},
),
ifs: [],
is_async: false,
},
],
parenthesized: false,
},
),
],

View file

@ -469,6 +469,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [
@ -521,6 +522,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [
@ -573,6 +575,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
Expr(
@ -52,6 +52,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
},
@ -105,6 +106,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
},
@ -385,6 +387,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 298..300,
elts: [],
ctx: Load,
parenthesized: true,
},
),
],
@ -425,6 +428,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 329..331,
elts: [],
ctx: Load,
parenthesized: true,
},
),
],
@ -505,6 +509,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Load,
@ -548,6 +553,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
ctx: Load,

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
With(
@ -171,6 +171,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -220,6 +221,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
ClassDef(
@ -143,6 +143,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
),

View file

@ -35,6 +35,7 @@ ListComp(
),
],
ctx: Store,
parenthesized: false,
},
),
iter: Name(

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
FunctionDef(
@ -254,6 +254,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
),

View file

@ -33,5 +33,6 @@ GeneratorExp(
is_async: false,
},
],
parenthesized: true,
},
)

View file

@ -52,5 +52,6 @@ GeneratorExp(
is_async: false,
},
],
parenthesized: true,
},
)

View file

@ -59,5 +59,6 @@ GeneratorExp(
is_async: false,
},
],
parenthesized: true,
},
)

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
Assign(
@ -27,6 +27,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Store,
parenthesized: false,
},
),
],
@ -52,6 +53,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
},

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
TypeAlias(
@ -354,6 +354,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
},
@ -438,6 +439,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
},
@ -484,6 +486,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
),
@ -537,6 +540,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
},

View file

@ -1659,6 +1659,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
cases: [
@ -2148,6 +2149,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
cases: [
@ -3287,6 +3289,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
cases: [
@ -3385,6 +3388,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: true,
},
),
cases: [
@ -3466,6 +3470,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [
@ -3542,6 +3547,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [
@ -3635,6 +3641,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
cases: [

View file

@ -66,6 +66,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Load,
@ -128,6 +129,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Store,
@ -188,6 +190,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Load,
@ -253,6 +256,7 @@ expression: parse_ast
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Load,

View file

@ -0,0 +1,124 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: parse_ast
---
[
Expr(
StmtExpr {
range: 0..3,
value: Tuple(
ExprTuple {
range: 0..3,
elts: [
Name(
ExprName {
range: 0..1,
id: "a",
ctx: Load,
},
),
Name(
ExprName {
range: 2..3,
id: "b",
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
},
),
Expr(
StmtExpr {
range: 4..9,
value: Tuple(
ExprTuple {
range: 4..9,
elts: [
Name(
ExprName {
range: 5..6,
id: "a",
ctx: Load,
},
),
Name(
ExprName {
range: 7..8,
id: "b",
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
Expr(
StmtExpr {
range: 10..12,
value: Tuple(
ExprTuple {
range: 10..12,
elts: [],
ctx: Load,
parenthesized: true,
},
),
},
),
Expr(
StmtExpr {
range: 13..17,
value: Tuple(
ExprTuple {
range: 13..17,
elts: [
Name(
ExprName {
range: 14..15,
id: "a",
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
Expr(
StmtExpr {
range: 18..25,
value: Tuple(
ExprTuple {
range: 19..24,
elts: [
Name(
ExprName {
range: 20..21,
id: "a",
ctx: Load,
},
),
Name(
ExprName {
range: 22..23,
id: "b",
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
]

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
Expr(
@ -52,6 +52,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
},
@ -105,6 +106,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
},
@ -385,6 +387,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 283..285,
elts: [],
ctx: Load,
parenthesized: true,
},
),
],
@ -425,6 +428,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 312..314,
elts: [],
ctx: Load,
parenthesized: true,
},
),
],
@ -505,6 +509,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: false,
},
),
ctx: Load,
@ -548,6 +553,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
ctx: Load,

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "parse_suite(source, \"<test>\").unwrap()"
expression: parse_suite(source).unwrap()
---
[
With(
@ -275,6 +275,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 133..135,
elts: [],
ctx: Load,
parenthesized: true,
},
),
optional_vars: None,
@ -301,6 +302,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
range: 147..149,
elts: [],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -433,6 +435,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -523,6 +526,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -571,6 +575,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: None,
@ -611,6 +616,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -667,6 +673,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: None,
@ -715,6 +722,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(
@ -876,6 +884,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: None,
@ -943,6 +952,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
),
],
ctx: Load,
parenthesized: true,
},
),
optional_vars: Some(