From dec0bf571f9bb41d94dccaaadeebb041a65efade Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 11 Dec 2022 03:16:48 -0800 Subject: [PATCH] Split and simplify some LALRPOP rules Signed-off-by: Anders Kaseorg --- parser/python.lalrpop | 138 +++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 82 deletions(-) diff --git a/parser/python.lalrpop b/parser/python.lalrpop index 442a5ed..565130c 100644 --- a/parser/python.lalrpop +++ b/parser/python.lalrpop @@ -689,22 +689,17 @@ YieldExpr: ast::Expr = { }; Test: ast::Expr = { - => { - if let Some(c) = condition { - ast::Expr { - location: c.0, - end_location: Some(c.5), - custom: (), - node: ast::ExprKind::IfExp { - test: Box::new(c.2), - body: Box::new(expr), - orelse: Box::new(c.4), - } - } - } else { - expr + "if" "else" => ast::Expr { + location, + end_location: Some(end_location), + custom: (), + node: ast::ExprKind::IfExp { + test: Box::new(test), + body: Box::new(body), + orelse: Box::new(orelse), } }, + OrTest, LambdaDef, }; @@ -756,37 +751,31 @@ LambdaDef: ast::Expr = { } OrTest: ast::Expr = { - => { - if e2.is_empty() { - e1 - } else { - let mut values = vec![e1]; - values.extend(e2.into_iter().map(|e| e.1)); - ast::Expr { - location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values } - } + => { + let mut values = vec![e1]; + values.extend(e2.into_iter().map(|e| e.1)); + ast::Expr { + location, + end_location: Some(end_location), + custom: (), + node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values } } }, + AndTest, }; AndTest: ast::Expr = { - => { - if e2.is_empty() { - e1 - } else { - let mut values = vec![e1]; - values.extend(e2.into_iter().map(|e| e.1)); - ast::Expr { - location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values } - } + => { + let mut values = vec![e1]; + values.extend(e2.into_iter().map(|e| e.1)); + ast::Expr { + location, + end_location: Some(end_location), + custom: (), + node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values } } }, + NotTest, }; NotTest: ast::Expr = { @@ -920,32 +909,23 @@ UnaryOp: ast::Unaryop = { }; Power: ast::Expr = { - => { - match e2 { - None => e, - Some((location, _, b, end_location)) => ast::Expr { - location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) } - }, - } - } + "**" => ast::Expr { + location, + end_location: Some(end_location), + custom: (), + node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) } + }, + AtomExpr, }; AtomExpr: ast::Expr = { - => { - if is_await.is_some() { - ast::Expr { - location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Await { value: Box::new(atom) } - } - } else { - atom - } - } + "await" => ast::Expr { + location, + end_location: Some(end_location), + custom: (), + node: ast::ExprKind::Await { value: Box::new(atom) } + }, + AtomExpr2, } AtomExpr2: ast::Expr = { @@ -1042,30 +1022,24 @@ Atom: ast::Expr = { node: ast::ExprKind::ListComp { elt: Box::new(elt), generators } } }, - "(" ")" =>? { - match elements { - Some(elt) => { - match elt.node { - ast::ExprKind::Starred { .. } => { - Err(LexicalError{ - error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()), - location: location, - }.into()) - }, - _ => { - Ok(elt) - } - } - }, - None => { - Ok(ast::Expr::new( - location, - end_location, - ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load } - )) + "(" ")" =>? { + match elt.node { + ast::ExprKind::Starred { .. } => { + Err(LexicalError{ + error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()), + location: elt.location, + }.into()) + } + _ => { + Ok(elt) } } }, + "(" ")" => ast::Expr::new( + location, + end_location, + ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load } + ), "(" ")" => e, "(" ")" => { ast::Expr {