Skip to content

Commit f51ad19

Browse files
committed
Update to parse `group' expression
In CPython, group expression parse single yield and named expression, and handle invalid starred expression at the same time. I added it such parsing algorithm. Update python.lalrpop
1 parent 6b56e51 commit f51ad19

File tree

2 files changed

+7734
-7908
lines changed

2 files changed

+7734
-7908
lines changed

parser/src/python.lalrpop

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::ast;
77
use crate::fstring::parse_located_fstring;
88
use crate::function::{ArgumentList, parse_args, parse_params};
9-
use crate::error::LexicalError;
9+
use crate::error::{LexicalError, LexicalErrorType};
1010
use crate::lexer;
1111
use crate::token::StringKind;
1212

@@ -999,12 +999,29 @@ Atom: ast::Expr = {
999999
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
10001000
}
10011001
},
1002-
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" => {
1003-
elements.unwrap_or(ast::Expr {
1004-
location,
1005-
custom: (),
1006-
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1007-
})
1002+
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" =>? {
1003+
match elements {
1004+
Some(elt) => {
1005+
match elt.node {
1006+
ast::ExprKind::Starred { .. } => {
1007+
Err(LexicalError{
1008+
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1009+
location,
1010+
}.into())
1011+
},
1012+
_ => {
1013+
Ok(elt)
1014+
}
1015+
}
1016+
},
1017+
None => {
1018+
Ok(ast::Expr {
1019+
location,
1020+
custom: (),
1021+
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1022+
})
1023+
}
1024+
}
10081025
},
10091026
"(" <e:YieldExpr> ")" => e,
10101027
<location:@L> "(" <elt:Test> <generators:CompFor> ")" => {
@@ -1014,6 +1031,12 @@ Atom: ast::Expr = {
10141031
node: ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators }
10151032
}
10161033
},
1034+
"(" <location:@L> "**" <e:Expression> ")" =>? {
1035+
Err(LexicalError{
1036+
error : LexicalErrorType::OtherError("cannot use double starred expression here".to_string()),
1037+
location,
1038+
}.into())
1039+
},
10171040
<location:@L> "{" <e:DictLiteralValues?> "}" => {
10181041
let (keys, values) = e.unwrap_or_default();
10191042
ast::Expr {

0 commit comments

Comments
 (0)