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,
end_location: Some(c.5),
custom: (), custom: (),
node: ast::ExprKind::IfExp { node: ast::ExprKind::IfExp {
test: Box::new(c.2), test: Box::new(test),
body: Box::new(expr), body: Box::new(body),
orelse: Box::new(c.4), orelse: Box::new(orelse),
}
}
} else {
expr
} }
}, },
OrTest,
LambdaDef, LambdaDef,
}; };
@ -756,10 +751,7 @@ 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() {
e1
} else {
let mut values = vec![e1]; let mut values = vec![e1];
values.extend(e2.into_iter().map(|e| e.1)); values.extend(e2.into_iter().map(|e| e.1));
ast::Expr { ast::Expr {
@ -768,15 +760,12 @@ OrTest: ast::Expr = {
custom: (), custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values } 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() {
e1
} else {
let mut values = vec![e1]; let mut values = vec![e1];
values.extend(e2.into_iter().map(|e| e.1)); values.extend(e2.into_iter().map(|e| e.1));
ast::Expr { ast::Expr {
@ -785,8 +774,8 @@ AndTest: ast::Expr = {
custom: (), custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values } 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 {
None => e,
Some((location, _, b, end_location)) => ast::Expr {
location, location,
end_location: Some(end_location), end_location: Some(end_location),
custom: (), custom: (),
node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) } node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }
}, },
} AtomExpr,
}
}; };
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() {
ast::Expr {
location, location,
end_location: Some(end_location), end_location: Some(end_location),
custom: (), custom: (),
node: ast::ExprKind::Await { value: Box::new(atom) } node: ast::ExprKind::Await { value: Box::new(atom) }
} },
} else { AtomExpr2,
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 {
Some(elt) => {
match elt.node { match elt.node {
ast::ExprKind::Starred { .. } => { ast::ExprKind::Starred { .. } => {
Err(LexicalError{ Err(LexicalError{
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()), error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
location: location, location: elt.location,
}.into()) }.into())
}, }
_ => { _ => {
Ok(elt) Ok(elt)
} }
} }
}, },
None => { <location:@L> "(" ")" <end_location:@R> => ast::Expr::new(
Ok(ast::Expr::new(
location, location,
end_location, end_location,
ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load } 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 {