Split and simplify some LALRPOP rules

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2022-12-11 03:16:48 -08:00
parent 2b91ffb3ae
commit dec0bf571f

View file

@ -689,22 +689,17 @@ YieldExpr: ast::Expr = {
}; };
Test: ast::Expr = { Test: ast::Expr = {
<expr:OrTest> <condition: (@L "if" OrTest "else" Test @R)?> => { <body:OrTest> <location:@L> "if" <test:OrTest> "else" <orelse:Test> <end_location:@R> => ast::Expr {
if let Some(c) = condition { location,
ast::Expr { end_location: Some(end_location),
location: c.0, custom: (),
end_location: Some(c.5), node: ast::ExprKind::IfExp {
custom: (), test: Box::new(test),
node: ast::ExprKind::IfExp { body: Box::new(body),
test: Box::new(c.2), orelse: Box::new(orelse),
body: Box::new(expr),
orelse: Box::new(c.4),
}
}
} else {
expr
} }
}, },
OrTest,
LambdaDef, LambdaDef,
}; };
@ -756,37 +751,31 @@ LambdaDef: ast::Expr = {
} }
OrTest: ast::Expr = { OrTest: ast::Expr = {
<location:@L> <e1:AndTest> <e2:("or" AndTest)*> <end_location:@R> => { <location:@L> <e1:AndTest> <e2:("or" AndTest)+> <end_location:@R> => {
if e2.is_empty() { let mut values = vec![e1];
e1 values.extend(e2.into_iter().map(|e| e.1));
} else { ast::Expr {
let mut values = vec![e1]; location,
values.extend(e2.into_iter().map(|e| e.1)); end_location: Some(end_location),
ast::Expr { custom: (),
location, node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
end_location: Some(end_location),
custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
}
} }
}, },
AndTest,
}; };
AndTest: ast::Expr = { AndTest: ast::Expr = {
<location:@L> <e1:NotTest> <e2:("and" NotTest)*> <end_location:@R> => { <location:@L> <e1:NotTest> <e2:("and" NotTest)+> <end_location:@R> => {
if e2.is_empty() { let mut values = vec![e1];
e1 values.extend(e2.into_iter().map(|e| e.1));
} else { ast::Expr {
let mut values = vec![e1]; location,
values.extend(e2.into_iter().map(|e| e.1)); end_location: Some(end_location),
ast::Expr { custom: (),
location, node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values }
end_location: Some(end_location),
custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values }
}
} }
}, },
NotTest,
}; };
NotTest: ast::Expr = { NotTest: ast::Expr = {
@ -920,32 +909,23 @@ UnaryOp: ast::Unaryop = {
}; };
Power: ast::Expr = { Power: ast::Expr = {
<e:AtomExpr> <e2:(@L "**" Factor @R)?> => { <e:AtomExpr> <location:@L> "**" <b:Factor> <end_location:@R> => ast::Expr {
match e2 { location,
None => e, end_location: Some(end_location),
Some((location, _, b, end_location)) => ast::Expr { custom: (),
location, node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }
end_location: Some(end_location), },
custom: (), AtomExpr,
node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }
},
}
}
}; };
AtomExpr: ast::Expr = { AtomExpr: ast::Expr = {
<location:@L> <is_await:"await"?> <atom:AtomExpr2> <end_location:@R> => { <location:@L> "await" <atom:AtomExpr2> <end_location:@R> => ast::Expr {
if is_await.is_some() { location,
ast::Expr { end_location: Some(end_location),
location, custom: (),
end_location: Some(end_location), node: ast::ExprKind::Await { value: Box::new(atom) }
custom: (), },
node: ast::ExprKind::Await { value: Box::new(atom) } AtomExpr2,
}
} else {
atom
}
}
} }
AtomExpr2: ast::Expr = { AtomExpr2: ast::Expr = {
@ -1042,30 +1022,24 @@ Atom: ast::Expr = {
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators } node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
} }
}, },
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" <end_location:@R> =>? { "(" <elt:TestOrStarNamedExprList> ")" =>? {
match elements { match elt.node {
Some(elt) => { ast::ExprKind::Starred { .. } => {
match elt.node { Err(LexicalError{
ast::ExprKind::Starred { .. } => { error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
Err(LexicalError{ location: elt.location,
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()), }.into())
location: location, }
}.into()) _ => {
}, Ok(elt)
_ => {
Ok(elt)
}
}
},
None => {
Ok(ast::Expr::new(
location,
end_location,
ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
))
} }
} }
}, },
<location:@L> "(" ")" <end_location:@R> => ast::Expr::new(
location,
end_location,
ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
),
"(" <e:YieldExpr> ")" => e, "(" <e:YieldExpr> ")" => e,
<location:@L> "(" <elt:NamedExpressionTest> <generators:CompFor> ")" <end_location:@R> => { <location:@L> "(" <elt:NamedExpressionTest> <generators:CompFor> ")" <end_location:@R> => {
ast::Expr { ast::Expr {