@@ -164,6 +164,11 @@ TestOrStarExpr: ast::Expr = {
164
164
StarExpr,
165
165
};
166
166
167
+ NamedOrStarExpr: ast::Expr = {
168
+ NamedExpression,
169
+ StarExpr,
170
+ };
171
+
167
172
TestOrStarNamedExpr: ast::Expr = {
168
173
NamedExpressionTest,
169
174
StarExpr,
@@ -518,7 +523,7 @@ WithItems: Vec<ast::Withitem> = {
518
523
519
524
#[inline]
520
525
WithItemsNoAs: Vec<ast::Withitem> = {
521
- <OneOrMore<NamedExpressionTest >> => {
526
+ <OneOrMore<Test<"all"> >> => {
522
527
<>.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None }).collect()
523
528
},
524
529
}
@@ -746,26 +751,27 @@ Test<Goal>: ast::Expr = {
746
751
};
747
752
748
753
NamedExpressionTest: ast::Expr = {
749
- <location:@L> <left: (Identifier ":=")?> <right:Test<"all">> <end_location:@R> => {
750
- if let Some(l) = left {
751
- ast::Expr {
752
- location,
753
- end_location: Some(end_location),
754
- custom: (),
755
- node: ast::ExprKind::NamedExpr {
756
- target: Box::new(ast::Expr::new(
757
- location,
758
- end_location,
759
- ast::ExprKind::Name { id: l.0, ctx: ast::ExprContext::Store },
760
- )),
761
- value: Box::new(right),
762
- }
754
+ NamedExpression,
755
+ Test<"all">,
756
+ }
757
+
758
+ NamedExpression: ast::Expr = {
759
+ <location:@L> <id:Identifier> ":=" <value:Test<"all">> <end_location:@R> => {
760
+ ast::Expr {
761
+ location,
762
+ end_location: Some(end_location),
763
+ custom: (),
764
+ node: ast::ExprKind::NamedExpr {
765
+ target: Box::new(ast::Expr::new(
766
+ location,
767
+ end_location,
768
+ ast::ExprKind::Name { id, ctx: ast::ExprContext::Store },
769
+ )),
770
+ value: Box::new(value),
763
771
}
764
- } else {
765
- right
766
772
}
767
- }
768
- }
773
+ },
774
+ };
769
775
770
776
LambdaDef: ast::Expr = {
771
777
<location:@L> "lambda" <p:ParameterList<UntypedParameter>?> ":" <body:Test<"all">> <end_location:@R> => {
@@ -1066,7 +1072,7 @@ Atom<Goal>: ast::Expr = {
1066
1072
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
1067
1073
}
1068
1074
},
1069
- <location:@L> "(" <elts:OneOrMore<NamedExpressionTest >> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => {
1075
+ <location:@L> "(" <elts:OneOrMore<Test<"all"> >> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => {
1070
1076
if elts.len() == 1 && trailing_comma.is_none() {
1071
1077
elts.into_iter().next().unwrap()
1072
1078
} else {
@@ -1077,19 +1083,23 @@ Atom<Goal>: ast::Expr = {
1077
1083
)
1078
1084
}
1079
1085
},
1080
- <location:@L> "(" <left:(<OneOrMore<NamedExpressionTest>> ",")?> <mid:StarExpr > <right:("," <TestOrStarNamedExpr>)*> <trailing_comma:","?> ")" <end_location:@R> =>? {
1086
+ <location:@L> "(" <left:(<OneOrMore<Test<"all">>> ",")?> <mid:NamedOrStarExpr > <right:("," <TestOrStarNamedExpr>)*> <trailing_comma:","?> ")" <end_location:@R> =>? {
1081
1087
if left.is_none() && right.is_empty() && trailing_comma.is_none() {
1082
- Err(LexicalError{
1083
- error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1084
- location: mid.location,
1085
- })?
1088
+ if matches!(mid.node, ast::ExprKind::Starred { .. }) {
1089
+ Err(LexicalError{
1090
+ error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1091
+ location: mid.location,
1092
+ })?
1093
+ }
1094
+ Ok(mid)
1095
+ } else {
1096
+ let elts = left.into_iter().flatten().chain([mid]).chain(right).collect();
1097
+ Ok(ast::Expr::new(
1098
+ location,
1099
+ end_location,
1100
+ ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load },
1101
+ ))
1086
1102
}
1087
- let elts = left.into_iter().flatten().chain([mid]).chain(right).collect();
1088
- Ok(ast::Expr::new(
1089
- location,
1090
- end_location,
1091
- ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load },
1092
- ))
1093
1103
},
1094
1104
<location:@L> "(" ")" <end_location:@R> => ast::Expr::new(
1095
1105
location,
0 commit comments