Skip to content

Commit 78ca48c

Browse files
committed
Split and simplify some LALRPOP rules
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 2edd0d2 commit 78ca48c

File tree

1 file changed

+56
-82
lines changed

1 file changed

+56
-82
lines changed

compiler/parser/python.lalrpop

Lines changed: 56 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -689,22 +689,17 @@ YieldExpr: ast::Expr = {
689689
};
690690

691691
Test: ast::Expr = {
692-
<expr:OrTest> <condition: (@L "if" OrTest "else" Test @R)?> => {
693-
if let Some(c) = condition {
694-
ast::Expr {
695-
location: c.0,
696-
end_location: Some(c.5),
697-
custom: (),
698-
node: ast::ExprKind::IfExp {
699-
test: Box::new(c.2),
700-
body: Box::new(expr),
701-
orelse: Box::new(c.4),
702-
}
703-
}
704-
} else {
705-
expr
692+
<body:OrTest> <location:@L> "if" <test:OrTest> "else" <orelse:Test> <end_location:@R> => ast::Expr {
693+
location,
694+
end_location: Some(end_location),
695+
custom: (),
696+
node: ast::ExprKind::IfExp {
697+
test: Box::new(test),
698+
body: Box::new(body),
699+
orelse: Box::new(orelse),
706700
}
707701
},
702+
OrTest,
708703
LambdaDef,
709704
};
710705

@@ -756,37 +751,31 @@ LambdaDef: ast::Expr = {
756751
}
757752

758753
OrTest: ast::Expr = {
759-
<location:@L> <e1:AndTest> <e2:("or" AndTest)*> <end_location:@R> => {
760-
if e2.is_empty() {
761-
e1
762-
} else {
763-
let mut values = vec![e1];
764-
values.extend(e2.into_iter().map(|e| e.1));
765-
ast::Expr {
766-
location,
767-
end_location: Some(end_location),
768-
custom: (),
769-
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
770-
}
754+
<location:@L> <e1:AndTest> <e2:("or" AndTest)+> <end_location:@R> => {
755+
let mut values = vec![e1];
756+
values.extend(e2.into_iter().map(|e| e.1));
757+
ast::Expr {
758+
location,
759+
end_location: Some(end_location),
760+
custom: (),
761+
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
771762
}
772763
},
764+
AndTest,
773765
};
774766

775767
AndTest: ast::Expr = {
776-
<location:@L> <e1:NotTest> <e2:("and" NotTest)*> <end_location:@R> => {
777-
if e2.is_empty() {
778-
e1
779-
} else {
780-
let mut values = vec![e1];
781-
values.extend(e2.into_iter().map(|e| e.1));
782-
ast::Expr {
783-
location,
784-
end_location: Some(end_location),
785-
custom: (),
786-
node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values }
787-
}
768+
<location:@L> <e1:NotTest> <e2:("and" NotTest)+> <end_location:@R> => {
769+
let mut values = vec![e1];
770+
values.extend(e2.into_iter().map(|e| e.1));
771+
ast::Expr {
772+
location,
773+
end_location: Some(end_location),
774+
custom: (),
775+
node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values }
788776
}
789777
},
778+
NotTest,
790779
};
791780

792781
NotTest: ast::Expr = {
@@ -920,32 +909,23 @@ UnaryOp: ast::Unaryop = {
920909
};
921910

922911
Power: ast::Expr = {
923-
<e:AtomExpr> <e2:(@L "**" Factor @R)?> => {
924-
match e2 {
925-
None => e,
926-
Some((location, _, b, end_location)) => ast::Expr {
927-
location,
928-
end_location: Some(end_location),
929-
custom: (),
930-
node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }
931-
},
932-
}
933-
}
912+
<e:AtomExpr> <location:@L> "**" <b:Factor> <end_location:@R> => ast::Expr {
913+
location,
914+
end_location: Some(end_location),
915+
custom: (),
916+
node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }
917+
},
918+
AtomExpr,
934919
};
935920

936921
AtomExpr: ast::Expr = {
937-
<location:@L> <is_await:"await"?> <atom:AtomExpr2> <end_location:@R> => {
938-
if is_await.is_some() {
939-
ast::Expr {
940-
location,
941-
end_location: Some(end_location),
942-
custom: (),
943-
node: ast::ExprKind::Await { value: Box::new(atom) }
944-
}
945-
} else {
946-
atom
947-
}
948-
}
922+
<location:@L> "await" <atom:AtomExpr2> <end_location:@R> => ast::Expr {
923+
location,
924+
end_location: Some(end_location),
925+
custom: (),
926+
node: ast::ExprKind::Await { value: Box::new(atom) }
927+
},
928+
AtomExpr2,
949929
}
950930

951931
AtomExpr2: ast::Expr = {
@@ -1042,30 +1022,24 @@ Atom: ast::Expr = {
10421022
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
10431023
}
10441024
},
1045-
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" <end_location:@R> =>? {
1046-
match elements {
1047-
Some(elt) => {
1048-
match elt.node {
1049-
ast::ExprKind::Starred { .. } => {
1050-
Err(LexicalError{
1051-
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1052-
location: location,
1053-
}.into())
1054-
},
1055-
_ => {
1056-
Ok(elt)
1057-
}
1058-
}
1059-
},
1060-
None => {
1061-
Ok(ast::Expr::new(
1062-
location,
1063-
end_location,
1064-
ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1065-
))
1025+
"(" <elt:TestOrStarNamedExprList> ")" =>? {
1026+
match elt.node {
1027+
ast::ExprKind::Starred { .. } => {
1028+
Err(LexicalError{
1029+
error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1030+
location: elt.location,
1031+
}.into())
1032+
}
1033+
_ => {
1034+
Ok(elt)
10661035
}
10671036
}
10681037
},
1038+
<location:@L> "(" ")" <end_location:@R> => ast::Expr::new(
1039+
location,
1040+
end_location,
1041+
ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1042+
),
10691043
"(" <e:YieldExpr> ")" => e,
10701044
<location:@L> "(" <elt:NamedExpressionTest> <generators:CompFor> ")" <end_location:@R> => {
10711045
ast::Expr {

0 commit comments

Comments
 (0)