Move ParenthesizedExpr to ruff_python_parser (#8987)

This commit is contained in:
Micha Reiser 2023-12-04 14:36:28 +09:00 committed by GitHub
parent 0bf0aa28ac
commit 7e390d3772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1755 additions and 1744 deletions

View file

@ -3729,204 +3729,6 @@ impl Ranged for crate::nodes::ParameterWithDefault {
} }
} }
/// An expression that may be parenthesized.
#[derive(Clone, Debug)]
pub struct ParenthesizedExpr {
/// The range of the expression, including any parentheses.
pub range: TextRange,
/// The underlying expression.
pub expr: Expr,
}
impl ParenthesizedExpr {
/// Returns `true` if the expression is may be parenthesized.
pub fn is_parenthesized(&self) -> bool {
self.range != self.expr.range()
}
}
impl Ranged for ParenthesizedExpr {
fn range(&self) -> TextRange {
self.range
}
}
impl From<Expr> for ParenthesizedExpr {
fn from(expr: Expr) -> Self {
ParenthesizedExpr {
range: expr.range(),
expr,
}
}
}
impl From<ParenthesizedExpr> for Expr {
fn from(parenthesized_expr: ParenthesizedExpr) -> Self {
parenthesized_expr.expr
}
}
impl From<ExprIpyEscapeCommand> for ParenthesizedExpr {
fn from(payload: ExprIpyEscapeCommand) -> Self {
Expr::IpyEscapeCommand(payload).into()
}
}
impl From<ExprBoolOp> for ParenthesizedExpr {
fn from(payload: ExprBoolOp) -> Self {
Expr::BoolOp(payload).into()
}
}
impl From<ExprNamedExpr> for ParenthesizedExpr {
fn from(payload: ExprNamedExpr) -> Self {
Expr::NamedExpr(payload).into()
}
}
impl From<ExprBinOp> for ParenthesizedExpr {
fn from(payload: ExprBinOp) -> Self {
Expr::BinOp(payload).into()
}
}
impl From<ExprUnaryOp> for ParenthesizedExpr {
fn from(payload: ExprUnaryOp) -> Self {
Expr::UnaryOp(payload).into()
}
}
impl From<ExprLambda> for ParenthesizedExpr {
fn from(payload: ExprLambda) -> Self {
Expr::Lambda(payload).into()
}
}
impl From<ExprIfExp> for ParenthesizedExpr {
fn from(payload: ExprIfExp) -> Self {
Expr::IfExp(payload).into()
}
}
impl From<ExprDict> for ParenthesizedExpr {
fn from(payload: ExprDict) -> Self {
Expr::Dict(payload).into()
}
}
impl From<ExprSet> for ParenthesizedExpr {
fn from(payload: ExprSet) -> Self {
Expr::Set(payload).into()
}
}
impl From<ExprListComp> for ParenthesizedExpr {
fn from(payload: ExprListComp) -> Self {
Expr::ListComp(payload).into()
}
}
impl From<ExprSetComp> for ParenthesizedExpr {
fn from(payload: ExprSetComp) -> Self {
Expr::SetComp(payload).into()
}
}
impl From<ExprDictComp> for ParenthesizedExpr {
fn from(payload: ExprDictComp) -> Self {
Expr::DictComp(payload).into()
}
}
impl From<ExprGeneratorExp> for ParenthesizedExpr {
fn from(payload: ExprGeneratorExp) -> Self {
Expr::GeneratorExp(payload).into()
}
}
impl From<ExprAwait> for ParenthesizedExpr {
fn from(payload: ExprAwait) -> Self {
Expr::Await(payload).into()
}
}
impl From<ExprYield> for ParenthesizedExpr {
fn from(payload: ExprYield) -> Self {
Expr::Yield(payload).into()
}
}
impl From<ExprYieldFrom> for ParenthesizedExpr {
fn from(payload: ExprYieldFrom) -> Self {
Expr::YieldFrom(payload).into()
}
}
impl From<ExprCompare> for ParenthesizedExpr {
fn from(payload: ExprCompare) -> Self {
Expr::Compare(payload).into()
}
}
impl From<ExprCall> for ParenthesizedExpr {
fn from(payload: ExprCall) -> Self {
Expr::Call(payload).into()
}
}
impl From<ExprFormattedValue> for ParenthesizedExpr {
fn from(payload: ExprFormattedValue) -> Self {
Expr::FormattedValue(payload).into()
}
}
impl From<ExprFString> for ParenthesizedExpr {
fn from(payload: ExprFString) -> Self {
Expr::FString(payload).into()
}
}
impl From<ExprStringLiteral> for ParenthesizedExpr {
fn from(payload: ExprStringLiteral) -> Self {
Expr::StringLiteral(payload).into()
}
}
impl From<ExprBytesLiteral> for ParenthesizedExpr {
fn from(payload: ExprBytesLiteral) -> Self {
Expr::BytesLiteral(payload).into()
}
}
impl From<ExprNumberLiteral> for ParenthesizedExpr {
fn from(payload: ExprNumberLiteral) -> Self {
Expr::NumberLiteral(payload).into()
}
}
impl From<ExprBooleanLiteral> for ParenthesizedExpr {
fn from(payload: ExprBooleanLiteral) -> Self {
Expr::BooleanLiteral(payload).into()
}
}
impl From<ExprNoneLiteral> for ParenthesizedExpr {
fn from(payload: ExprNoneLiteral) -> Self {
Expr::NoneLiteral(payload).into()
}
}
impl From<ExprEllipsisLiteral> for ParenthesizedExpr {
fn from(payload: ExprEllipsisLiteral) -> Self {
Expr::EllipsisLiteral(payload).into()
}
}
impl From<ExprAttribute> for ParenthesizedExpr {
fn from(payload: ExprAttribute) -> Self {
Expr::Attribute(payload).into()
}
}
impl From<ExprSubscript> for ParenthesizedExpr {
fn from(payload: ExprSubscript) -> Self {
Expr::Subscript(payload).into()
}
}
impl From<ExprStarred> for ParenthesizedExpr {
fn from(payload: ExprStarred) -> Self {
Expr::Starred(payload).into()
}
}
impl From<ExprName> for ParenthesizedExpr {
fn from(payload: ExprName) -> Self {
Expr::Name(payload).into()
}
}
impl From<ExprList> for ParenthesizedExpr {
fn from(payload: ExprList) -> Self {
Expr::List(payload).into()
}
}
impl From<ExprTuple> for ParenthesizedExpr {
fn from(payload: ExprTuple) -> Self {
Expr::Tuple(payload).into()
}
}
impl From<ExprSlice> for ParenthesizedExpr {
fn from(payload: ExprSlice) -> Self {
Expr::Slice(payload).into()
}
}
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
mod size_assertions { mod size_assertions {
use static_assertions::assert_eq_size; use static_assertions::assert_eq_size;

View file

@ -16,7 +16,7 @@ use std::{fmt, iter};
use itertools::Itertools; use itertools::Itertools;
pub(super) use lalrpop_util::ParseError as LalrpopError; pub(super) use lalrpop_util::ParseError as LalrpopError;
use ruff_text_size::{TextRange, TextSize}; use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::lexer::{lex, lex_starts_at, Spanned}; use crate::lexer::{lex, lex_starts_at, Spanned};
use crate::{ use crate::{
@ -25,8 +25,14 @@ use crate::{
token::Tok, token::Tok,
Mode, Mode,
}; };
use ruff_python_ast as ast; use ruff_python_ast::{
use ruff_python_ast::{Mod, ModModule, Suite}; Expr, ExprAttribute, ExprAwait, ExprBinOp, ExprBoolOp, ExprBooleanLiteral, ExprBytesLiteral,
ExprCall, ExprCompare, ExprDict, ExprDictComp, ExprEllipsisLiteral, ExprFString,
ExprGeneratorExp, ExprIfExp, ExprIpyEscapeCommand, ExprLambda, ExprList, ExprListComp,
ExprName, ExprNamedExpr, ExprNoneLiteral, ExprNumberLiteral, ExprSet, ExprSetComp, ExprSlice,
ExprStarred, ExprStringLiteral, ExprSubscript, ExprTuple, ExprUnaryOp, ExprYield,
ExprYieldFrom, Mod, ModModule, Suite,
};
/// Parse a full Python program usually consisting of multiple lines. /// Parse a full Python program usually consisting of multiple lines.
/// ///
@ -76,7 +82,7 @@ pub fn parse_suite(source: &str, source_path: &str) -> Result<Suite, ParseError>
/// assert!(expr.is_ok()); /// assert!(expr.is_ok());
/// ///
/// ``` /// ```
pub fn parse_expression(source: &str, source_path: &str) -> Result<ast::Expr, ParseError> { pub fn parse_expression(source: &str, source_path: &str) -> Result<Expr, ParseError> {
let lexer = lex(source, Mode::Expression); let lexer = lex(source, Mode::Expression);
match parse_tokens(lexer, source, Mode::Expression, source_path)? { match parse_tokens(lexer, source, Mode::Expression, source_path)? {
Mod::Expression(expression) => Ok(*expression.body), Mod::Expression(expression) => Ok(*expression.body),
@ -105,7 +111,7 @@ pub fn parse_expression_starts_at(
source: &str, source: &str,
source_path: &str, source_path: &str,
offset: TextSize, offset: TextSize,
) -> Result<ast::Expr, ParseError> { ) -> Result<Expr, ParseError> {
let lexer = lex_starts_at(source, Mode::Module, offset); let lexer = lex_starts_at(source, Mode::Module, offset);
match parse_tokens(lexer, source, Mode::Expression, source_path)? { match parse_tokens(lexer, source, Mode::Expression, source_path)? {
Mod::Expression(expression) => Ok(*expression.body), Mod::Expression(expression) => Ok(*expression.body),
@ -418,6 +424,209 @@ impl ParseErrorType {
} }
} }
/// An expression that may be parenthesized.
#[derive(Clone, Debug)]
pub(super) struct ParenthesizedExpr {
/// The range of the expression, including any parentheses.
pub(super) range: TextRange,
/// The underlying expression.
pub(super) expr: Expr,
}
impl ParenthesizedExpr {
/// Returns `true` if the expression is parenthesized.
pub(super) fn is_parenthesized(&self) -> bool {
self.range.start() != self.expr.range().start()
}
}
impl Ranged for ParenthesizedExpr {
fn range(&self) -> TextRange {
self.range
}
}
impl From<Expr> for ParenthesizedExpr {
fn from(expr: Expr) -> Self {
ParenthesizedExpr {
range: expr.range(),
expr,
}
}
}
impl From<ParenthesizedExpr> for Expr {
fn from(parenthesized_expr: ParenthesizedExpr) -> Self {
parenthesized_expr.expr
}
}
impl From<ExprIpyEscapeCommand> for ParenthesizedExpr {
fn from(payload: ExprIpyEscapeCommand) -> Self {
Expr::IpyEscapeCommand(payload).into()
}
}
impl From<ExprBoolOp> for ParenthesizedExpr {
fn from(payload: ExprBoolOp) -> Self {
Expr::BoolOp(payload).into()
}
}
impl From<ExprNamedExpr> for ParenthesizedExpr {
fn from(payload: ExprNamedExpr) -> Self {
Expr::NamedExpr(payload).into()
}
}
impl From<ExprBinOp> for ParenthesizedExpr {
fn from(payload: ExprBinOp) -> Self {
Expr::BinOp(payload).into()
}
}
impl From<ExprUnaryOp> for ParenthesizedExpr {
fn from(payload: ExprUnaryOp) -> Self {
Expr::UnaryOp(payload).into()
}
}
impl From<ExprLambda> for ParenthesizedExpr {
fn from(payload: ExprLambda) -> Self {
Expr::Lambda(payload).into()
}
}
impl From<ExprIfExp> for ParenthesizedExpr {
fn from(payload: ExprIfExp) -> Self {
Expr::IfExp(payload).into()
}
}
impl From<ExprDict> for ParenthesizedExpr {
fn from(payload: ExprDict) -> Self {
Expr::Dict(payload).into()
}
}
impl From<ExprSet> for ParenthesizedExpr {
fn from(payload: ExprSet) -> Self {
Expr::Set(payload).into()
}
}
impl From<ExprListComp> for ParenthesizedExpr {
fn from(payload: ExprListComp) -> Self {
Expr::ListComp(payload).into()
}
}
impl From<ExprSetComp> for ParenthesizedExpr {
fn from(payload: ExprSetComp) -> Self {
Expr::SetComp(payload).into()
}
}
impl From<ExprDictComp> for ParenthesizedExpr {
fn from(payload: ExprDictComp) -> Self {
Expr::DictComp(payload).into()
}
}
impl From<ExprGeneratorExp> for ParenthesizedExpr {
fn from(payload: ExprGeneratorExp) -> Self {
Expr::GeneratorExp(payload).into()
}
}
impl From<ExprAwait> for ParenthesizedExpr {
fn from(payload: ExprAwait) -> Self {
Expr::Await(payload).into()
}
}
impl From<ExprYield> for ParenthesizedExpr {
fn from(payload: ExprYield) -> Self {
Expr::Yield(payload).into()
}
}
impl From<ExprYieldFrom> for ParenthesizedExpr {
fn from(payload: ExprYieldFrom) -> Self {
Expr::YieldFrom(payload).into()
}
}
impl From<ExprCompare> for ParenthesizedExpr {
fn from(payload: ExprCompare) -> Self {
Expr::Compare(payload).into()
}
}
impl From<ExprCall> for ParenthesizedExpr {
fn from(payload: ExprCall) -> Self {
Expr::Call(payload).into()
}
}
impl From<ExprFString> for ParenthesizedExpr {
fn from(payload: ExprFString) -> Self {
Expr::FString(payload).into()
}
}
impl From<ExprStringLiteral> for ParenthesizedExpr {
fn from(payload: ExprStringLiteral) -> Self {
Expr::StringLiteral(payload).into()
}
}
impl From<ExprBytesLiteral> for ParenthesizedExpr {
fn from(payload: ExprBytesLiteral) -> Self {
Expr::BytesLiteral(payload).into()
}
}
impl From<ExprNumberLiteral> for ParenthesizedExpr {
fn from(payload: ExprNumberLiteral) -> Self {
Expr::NumberLiteral(payload).into()
}
}
impl From<ExprBooleanLiteral> for ParenthesizedExpr {
fn from(payload: ExprBooleanLiteral) -> Self {
Expr::BooleanLiteral(payload).into()
}
}
impl From<ExprNoneLiteral> for ParenthesizedExpr {
fn from(payload: ExprNoneLiteral) -> Self {
Expr::NoneLiteral(payload).into()
}
}
impl From<ExprEllipsisLiteral> for ParenthesizedExpr {
fn from(payload: ExprEllipsisLiteral) -> Self {
Expr::EllipsisLiteral(payload).into()
}
}
impl From<ExprAttribute> for ParenthesizedExpr {
fn from(payload: ExprAttribute) -> Self {
Expr::Attribute(payload).into()
}
}
impl From<ExprSubscript> for ParenthesizedExpr {
fn from(payload: ExprSubscript) -> Self {
Expr::Subscript(payload).into()
}
}
impl From<ExprStarred> for ParenthesizedExpr {
fn from(payload: ExprStarred) -> Self {
Expr::Starred(payload).into()
}
}
impl From<ExprName> for ParenthesizedExpr {
fn from(payload: ExprName) -> Self {
Expr::Name(payload).into()
}
}
impl From<ExprList> for ParenthesizedExpr {
fn from(payload: ExprList) -> Self {
Expr::List(payload).into()
}
}
impl From<ExprTuple> for ParenthesizedExpr {
fn from(payload: ExprTuple) -> Self {
Expr::Tuple(payload).into()
}
}
impl From<ExprSlice> for ParenthesizedExpr {
fn from(payload: ExprSlice) -> Self {
Expr::Slice(payload).into()
}
}
#[cfg(target_pointer_width = "64")]
mod size_assertions {
use crate::parser::ParenthesizedExpr;
use static_assertions::assert_eq_size;
assert_eq_size!(ParenthesizedExpr, [u8; 88]);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use insta::assert_debug_snapshot; use insta::assert_debug_snapshot;

View file

@ -156,33 +156,33 @@ ExpressionStatement: ast::Stmt = {
}, },
}; };
AssignSuffix: ast::ParenthesizedExpr = { AssignSuffix: crate::parser::ParenthesizedExpr = {
"=" <e:TestListOrYieldExpr> => e, "=" <e:TestListOrYieldExpr> => e,
"=" <e:IpyEscapeCommandExpr> => e "=" <e:IpyEscapeCommandExpr> => e
}; };
TestListOrYieldExpr: ast::ParenthesizedExpr = { TestListOrYieldExpr: crate::parser::ParenthesizedExpr = {
TestList, TestList,
YieldExpr YieldExpr
} }
#[inline] #[inline]
TestOrStarExprList: ast::ParenthesizedExpr = { TestOrStarExprList: crate::parser::ParenthesizedExpr = {
// as far as I can tell, these were the same // as far as I can tell, these were the same
TestList TestList
}; };
TestOrStarExpr: ast::ParenthesizedExpr = { TestOrStarExpr: crate::parser::ParenthesizedExpr = {
Test<"all">, Test<"all">,
StarExpr, StarExpr,
}; };
NamedOrStarExpr: ast::ParenthesizedExpr = { NamedOrStarExpr: crate::parser::ParenthesizedExpr = {
NamedExpression, NamedExpression,
StarExpr, StarExpr,
}; };
TestOrStarNamedExpr: ast::ParenthesizedExpr = { TestOrStarNamedExpr: crate::parser::ParenthesizedExpr = {
NamedExpressionTest, NamedExpressionTest,
StarExpr, StarExpr,
}; };
@ -345,7 +345,7 @@ IpyEscapeCommandStatement: ast::Stmt = {
} }
} }
IpyEscapeCommandExpr: ast::ParenthesizedExpr = { IpyEscapeCommandExpr: crate::parser::ParenthesizedExpr = {
<location:@L> <c:ipy_escape_command> <end_location:@R> =>? { <location:@L> <c:ipy_escape_command> <end_location:@R> =>? {
if mode == Mode::Ipython { if mode == Mode::Ipython {
// This should never occur as the lexer won't allow it. // This should never occur as the lexer won't allow it.
@ -630,13 +630,13 @@ StarPattern: ast::Pattern = {
}.into(), }.into(),
} }
NumberAtom: ast::ParenthesizedExpr = { NumberAtom: crate::parser::ParenthesizedExpr = {
<location:@L> <value:Number> <end_location:@R> => ast::Expr::NumberLiteral( <location:@L> <value:Number> <end_location:@R> => ast::Expr::NumberLiteral(
ast::ExprNumberLiteral { value, range: (location..end_location).into() } ast::ExprNumberLiteral { value, range: (location..end_location).into() }
).into(), ).into(),
} }
NumberExpr: ast::ParenthesizedExpr = { NumberExpr: crate::parser::ParenthesizedExpr = {
NumberAtom, NumberAtom,
<location:@L> "-" <operand:NumberAtom> <end_location:@R> => ast::Expr::UnaryOp( <location:@L> "-" <operand:NumberAtom> <end_location:@R> => ast::Expr::UnaryOp(
ast::ExprUnaryOp { ast::ExprUnaryOp {
@ -647,7 +647,7 @@ NumberExpr: ast::ParenthesizedExpr = {
).into(), ).into(),
} }
AddOpExpr: ast::ParenthesizedExpr = { AddOpExpr: crate::parser::ParenthesizedExpr = {
<location:@L> <left:NumberExpr> <op:AddOp> <right:NumberAtom> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:NumberExpr> <op:AddOp> <right:NumberAtom> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op, op,
@ -1316,7 +1316,7 @@ Decorator: ast::Decorator = {
}, },
}; };
YieldExpr: ast::ParenthesizedExpr = { YieldExpr: crate::parser::ParenthesizedExpr = {
<location:@L> "yield" <value:TestList?> <end_location:@R> => ast::ExprYield { <location:@L> "yield" <value:TestList?> <end_location:@R> => ast::ExprYield {
value: value.map(ast::Expr::from).map(Box::new), value: value.map(ast::Expr::from).map(Box::new),
range: (location..end_location).into(), range: (location..end_location).into(),
@ -1327,7 +1327,7 @@ YieldExpr: ast::ParenthesizedExpr = {
}.into(), }.into(),
}; };
Test<Goal>: ast::ParenthesizedExpr = { Test<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <body:OrTest<"all">> "if" <test:OrTest<"all">> "else" <orelse:Test<"all">> <end_location:@R> => ast::ExprIfExp { <location:@L> <body:OrTest<"all">> "if" <test:OrTest<"all">> "else" <orelse:Test<"all">> <end_location:@R> => ast::ExprIfExp {
test: Box::new(test.into()), test: Box::new(test.into()),
body: Box::new(body.into()), body: Box::new(body.into()),
@ -1338,12 +1338,12 @@ Test<Goal>: ast::ParenthesizedExpr = {
LambdaDef, LambdaDef,
}; };
NamedExpressionTest: ast::ParenthesizedExpr = { NamedExpressionTest: crate::parser::ParenthesizedExpr = {
NamedExpression, NamedExpression,
Test<"all">, Test<"all">,
} }
NamedExpressionName: ast::ParenthesizedExpr = { NamedExpressionName: crate::parser::ParenthesizedExpr = {
<location:@L> <id:Identifier> <end_location:@R> => ast::ExprName { <location:@L> <id:Identifier> <end_location:@R> => ast::ExprName {
id: id.into(), id: id.into(),
ctx: ast::ExprContext::Store, ctx: ast::ExprContext::Store,
@ -1351,7 +1351,7 @@ NamedExpressionName: ast::ParenthesizedExpr = {
}.into(), }.into(),
} }
NamedExpression: ast::ParenthesizedExpr = { NamedExpression: crate::parser::ParenthesizedExpr = {
<location:@L> <target:NamedExpressionName> ":=" <value:Test<"all">> <end_location:@R> => { <location:@L> <target:NamedExpressionName> ":=" <value:Test<"all">> <end_location:@R> => {
ast::ExprNamedExpr { ast::ExprNamedExpr {
target: Box::new(target.into()), target: Box::new(target.into()),
@ -1361,7 +1361,7 @@ NamedExpression: ast::ParenthesizedExpr = {
}, },
}; };
LambdaDef: ast::ParenthesizedExpr = { LambdaDef: crate::parser::ParenthesizedExpr = {
<location:@L> "lambda" <location_args:@L> <parameters:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> <end_location_args:@R> ":" <fstring_middle:fstring_middle?> <body:Test<"all">> <end_location:@R> =>? { <location:@L> "lambda" <location_args:@L> <parameters:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> <end_location_args:@R> ":" <fstring_middle:fstring_middle?> <body:Test<"all">> <end_location:@R> =>? {
if fstring_middle.is_some() { if fstring_middle.is_some() {
return Err(LexicalError { return Err(LexicalError {
@ -1379,7 +1379,7 @@ LambdaDef: ast::ParenthesizedExpr = {
} }
} }
OrTest<Goal>: ast::ParenthesizedExpr = { OrTest<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <values:(<AndTest<"all">> "or")+> <last: AndTest<"all">> <end_location:@R> => { <location:@L> <values:(<AndTest<"all">> "or")+> <last: AndTest<"all">> <end_location:@R> => {
let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect();
ast::ExprBoolOp { op: ast::BoolOp::Or, values, range: (location..end_location).into() }.into() ast::ExprBoolOp { op: ast::BoolOp::Or, values, range: (location..end_location).into() }.into()
@ -1387,7 +1387,7 @@ OrTest<Goal>: ast::ParenthesizedExpr = {
AndTest<Goal>, AndTest<Goal>,
}; };
AndTest<Goal>: ast::ParenthesizedExpr = { AndTest<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <values:(<NotTest<"all">> "and")+> <last:NotTest<"all">> <end_location:@R> => { <location:@L> <values:(<NotTest<"all">> "and")+> <last:NotTest<"all">> <end_location:@R> => {
let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect();
ast::ExprBoolOp { op: ast::BoolOp::And, values, range: (location..end_location).into() }.into() ast::ExprBoolOp { op: ast::BoolOp::And, values, range: (location..end_location).into() }.into()
@ -1395,7 +1395,7 @@ AndTest<Goal>: ast::ParenthesizedExpr = {
NotTest<Goal>, NotTest<Goal>,
}; };
NotTest<Goal>: ast::ParenthesizedExpr = { NotTest<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> "not" <operand:NotTest<"all">> <end_location:@R> => ast::ExprUnaryOp { <location:@L> "not" <operand:NotTest<"all">> <end_location:@R> => ast::ExprUnaryOp {
operand: Box::new(operand.into()), operand: Box::new(operand.into()),
op: ast::UnaryOp::Not, op: ast::UnaryOp::Not,
@ -1404,7 +1404,7 @@ NotTest<Goal>: ast::ParenthesizedExpr = {
Comparison<Goal>, Comparison<Goal>,
}; };
Comparison<Goal>: ast::ParenthesizedExpr = { Comparison<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:Expression<"all">> <comparisons:(CompOp Expression<"all">)+> <end_location:@R> => { <location:@L> <left:Expression<"all">> <comparisons:(CompOp Expression<"all">)+> <end_location:@R> => {
let (ops, comparators) = comparisons.into_iter().map(|(op, comparator)| (op, ast::Expr::from(comparator))).unzip(); let (ops, comparators) = comparisons.into_iter().map(|(op, comparator)| (op, ast::Expr::from(comparator))).unzip();
ast::ExprCompare { left: Box::new(left.into()), ops, comparators, range: (location..end_location).into() }.into() ast::ExprCompare { left: Box::new(left.into()), ops, comparators, range: (location..end_location).into() }.into()
@ -1425,7 +1425,7 @@ CompOp: ast::CmpOp = {
"is" "not" => ast::CmpOp::IsNot, "is" "not" => ast::CmpOp::IsNot,
}; };
Expression<Goal>: ast::ParenthesizedExpr = { Expression<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:Expression<"all">> "|" <right:XorExpression<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:Expression<"all">> "|" <right:XorExpression<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op: ast::Operator::BitOr, op: ast::Operator::BitOr,
@ -1435,7 +1435,7 @@ Expression<Goal>: ast::ParenthesizedExpr = {
XorExpression<Goal>, XorExpression<Goal>,
}; };
XorExpression<Goal>: ast::ParenthesizedExpr = { XorExpression<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:XorExpression<"all">> "^" <right:AndExpression<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:XorExpression<"all">> "^" <right:AndExpression<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op: ast::Operator::BitXor, op: ast::Operator::BitXor,
@ -1445,7 +1445,7 @@ XorExpression<Goal>: ast::ParenthesizedExpr = {
AndExpression<Goal>, AndExpression<Goal>,
}; };
AndExpression<Goal>: ast::ParenthesizedExpr = { AndExpression<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:AndExpression<"all">> "&" <right:ShiftExpression<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:AndExpression<"all">> "&" <right:ShiftExpression<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op: ast::Operator::BitAnd, op: ast::Operator::BitAnd,
@ -1455,7 +1455,7 @@ AndExpression<Goal>: ast::ParenthesizedExpr = {
ShiftExpression<Goal>, ShiftExpression<Goal>,
}; };
ShiftExpression<Goal>: ast::ParenthesizedExpr = { ShiftExpression<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:ShiftExpression<"all">> <op:ShiftOp> <right:ArithmeticExpression<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:ShiftExpression<"all">> <op:ShiftOp> <right:ArithmeticExpression<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op, op,
@ -1470,7 +1470,7 @@ ShiftOp: ast::Operator = {
">>" => ast::Operator::RShift, ">>" => ast::Operator::RShift,
}; };
ArithmeticExpression<Goal>: ast::ParenthesizedExpr = { ArithmeticExpression<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:ArithmeticExpression<"all">> <op:AddOp> <right:Term<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:ArithmeticExpression<"all">> <op:AddOp> <right:Term<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op, op,
@ -1485,7 +1485,7 @@ AddOp: ast::Operator = {
"-" => ast::Operator::Sub, "-" => ast::Operator::Sub,
}; };
Term<Goal>: ast::ParenthesizedExpr = { Term<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:Term<"all">> <op:MulOp> <right:Factor<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:Term<"all">> <op:MulOp> <right:Factor<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op, op,
@ -1503,7 +1503,7 @@ MulOp: ast::Operator = {
"@" => ast::Operator::MatMult, "@" => ast::Operator::MatMult,
}; };
Factor<Goal>: ast::ParenthesizedExpr = { Factor<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <op:UnaryOp> <operand:Factor<"all">> <end_location:@R> => ast::ExprUnaryOp { <location:@L> <op:UnaryOp> <operand:Factor<"all">> <end_location:@R> => ast::ExprUnaryOp {
operand: Box::new(operand.into()), operand: Box::new(operand.into()),
op, op,
@ -1518,7 +1518,7 @@ UnaryOp: ast::UnaryOp = {
"~" => ast::UnaryOp::Invert, "~" => ast::UnaryOp::Invert,
}; };
Power<Goal>: ast::ParenthesizedExpr = { Power<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> <left:AtomExpr<"all">> "**" <right:Factor<"all">> <end_location:@R> => ast::ExprBinOp { <location:@L> <left:AtomExpr<"all">> "**" <right:Factor<"all">> <end_location:@R> => ast::ExprBinOp {
left: Box::new(left.into()), left: Box::new(left.into()),
op: ast::Operator::Pow, op: ast::Operator::Pow,
@ -1528,14 +1528,14 @@ Power<Goal>: ast::ParenthesizedExpr = {
AtomExpr<Goal>, AtomExpr<Goal>,
}; };
AtomExpr<Goal>: ast::ParenthesizedExpr = { AtomExpr<Goal>: crate::parser::ParenthesizedExpr = {
<location:@L> "await" <value:AtomExpr2<"all">> <end_location:@R> => { <location:@L> "await" <value:AtomExpr2<"all">> <end_location:@R> => {
ast::ExprAwait { value: Box::new(value.into()), range: (location..end_location).into() }.into() ast::ExprAwait { value: Box::new(value.into()), range: (location..end_location).into() }.into()
}, },
AtomExpr2<Goal>, AtomExpr2<Goal>,
} }
AtomExpr2<Goal>: ast::ParenthesizedExpr = { AtomExpr2<Goal>: crate::parser::ParenthesizedExpr = {
Atom<Goal>, Atom<Goal>,
<location:@L> <func:AtomExpr2<"all">> <arguments:Arguments> <end_location:@R> => ast::ExprCall { <location:@L> <func:AtomExpr2<"all">> <arguments:Arguments> <end_location:@R> => ast::ExprCall {
func: Box::new(func.into()), func: Box::new(func.into()),
@ -1556,7 +1556,7 @@ AtomExpr2<Goal>: ast::ParenthesizedExpr = {
}.into(), }.into(),
}; };
SubscriptList: ast::ParenthesizedExpr = { SubscriptList: crate::parser::ParenthesizedExpr = {
Subscript, Subscript,
<location:@L> <s1:Subscript> "," <end_location:@R> => { <location:@L> <s1:Subscript> "," <end_location:@R> => {
ast::ExprTuple { ast::ExprTuple {
@ -1575,7 +1575,7 @@ SubscriptList: ast::ParenthesizedExpr = {
} }
}; };
Subscript: ast::ParenthesizedExpr = { Subscript: crate::parser::ParenthesizedExpr = {
TestOrStarNamedExpr, TestOrStarNamedExpr,
<location:@L> <lower:Test<"all">?> ":" <upper:Test<"all">?> <step:SliceOp?> <end_location:@R> => { <location:@L> <lower:Test<"all">?> ":" <upper:Test<"all">?> <step:SliceOp?> <end_location:@R> => {
let lower = lower.map(ast::Expr::from).map(Box::new); let lower = lower.map(ast::Expr::from).map(Box::new);
@ -1587,7 +1587,7 @@ Subscript: ast::ParenthesizedExpr = {
} }
}; };
SliceOp: Option<ast::ParenthesizedExpr> = { SliceOp: Option<crate::parser::ParenthesizedExpr> = {
<location:@L> ":" <e:Test<"all">?> => e, <location:@L> ":" <e:Test<"all">?> => e,
} }
@ -1693,7 +1693,7 @@ FStringConversion: (TextSize, ast::ConversionFlag) = {
} }
}; };
Atom<Goal>: ast::ParenthesizedExpr = { Atom<Goal>: crate::parser::ParenthesizedExpr = {
<expr:String> => expr.into(), <expr:String> => expr.into(),
<location:@L> <value:Number> <end_location:@R> => ast::ExprNumberLiteral { <location:@L> <value:Number> <end_location:@R> => ast::ExprNumberLiteral {
value, value,
@ -1713,7 +1713,7 @@ Atom<Goal>: ast::ParenthesizedExpr = {
}, },
<location:@L> "(" <elts:OneOrMore<Test<"all">>> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => { <location:@L> "(" <elts:OneOrMore<Test<"all">>> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => {
if elts.len() == 1 && trailing_comma.is_none() { if elts.len() == 1 && trailing_comma.is_none() {
ast::ParenthesizedExpr { crate::parser::ParenthesizedExpr {
expr: elts.into_iter().next().unwrap().into(), expr: elts.into_iter().next().unwrap().into(),
range: (location..end_location).into(), range: (location..end_location).into(),
} }
@ -1730,7 +1730,7 @@ Atom<Goal>: ast::ParenthesizedExpr = {
location: mid.start(), location: mid.start(),
})?; })?;
} }
Ok(ast::ParenthesizedExpr { Ok(crate::parser::ParenthesizedExpr {
expr: mid.into(), expr: mid.into(),
range: (location..end_location).into(), range: (location..end_location).into(),
}) })
@ -1744,7 +1744,7 @@ Atom<Goal>: ast::ParenthesizedExpr = {
ctx: ast::ExprContext::Load, ctx: ast::ExprContext::Load,
range: (location..end_location).into(), range: (location..end_location).into(),
}.into(), }.into(),
<location:@L> "(" <e:YieldExpr> ")" <end_location:@R> => ast::ParenthesizedExpr { <location:@L> "(" <e:YieldExpr> ")" <end_location:@R> => crate::parser::ParenthesizedExpr {
expr: e.into(), expr: e.into(),
range: (location..end_location).into(), range: (location..end_location).into(),
}, },
@ -1793,37 +1793,37 @@ Atom<Goal>: ast::ParenthesizedExpr = {
<location:@L> "..." <end_location:@R> => ast::ExprEllipsisLiteral { range: (location..end_location).into() }.into(), <location:@L> "..." <end_location:@R> => ast::ExprEllipsisLiteral { range: (location..end_location).into() }.into(),
}; };
ListLiteralValues: Vec<ast::ParenthesizedExpr> = { ListLiteralValues: Vec<crate::parser::ParenthesizedExpr> = {
<e:OneOrMore<TestOrStarNamedExpr>> ","? => e, <e:OneOrMore<TestOrStarNamedExpr>> ","? => e,
}; };
DictLiteralValues: Vec<(Option<Box<ast::ParenthesizedExpr>>, ast::ParenthesizedExpr)> = { DictLiteralValues: Vec<(Option<Box<crate::parser::ParenthesizedExpr>>, crate::parser::ParenthesizedExpr)> = {
<elements:OneOrMore<DictElement>> ","? => elements, <elements:OneOrMore<DictElement>> ","? => elements,
}; };
DictEntry: (ast::ParenthesizedExpr, ast::ParenthesizedExpr) = { DictEntry: (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr) = {
<e1: Test<"all">> ":" <e2: Test<"all">> => (e1, e2), <e1: Test<"all">> ":" <e2: Test<"all">> => (e1, e2),
}; };
DictElement: (Option<Box<ast::ParenthesizedExpr>>, ast::ParenthesizedExpr) = { DictElement: (Option<Box<crate::parser::ParenthesizedExpr>>, crate::parser::ParenthesizedExpr) = {
<e:DictEntry> => (Some(Box::new(e.0)), e.1), <e:DictEntry> => (Some(Box::new(e.0)), e.1),
"**" <e:Expression<"all">> => (None, e), "**" <e:Expression<"all">> => (None, e),
}; };
SetLiteralValues: Vec<ast::ParenthesizedExpr> = { SetLiteralValues: Vec<crate::parser::ParenthesizedExpr> = {
<e1:OneOrMore<TestOrStarNamedExpr>> ","? => e1 <e1:OneOrMore<TestOrStarNamedExpr>> ","? => e1
}; };
ExpressionOrStarExpression: ast::ParenthesizedExpr = { ExpressionOrStarExpression: crate::parser::ParenthesizedExpr = {
Expression<"all">, Expression<"all">,
StarExpr StarExpr
}; };
ExpressionList: ast::ParenthesizedExpr = { ExpressionList: crate::parser::ParenthesizedExpr = {
GenericList<ExpressionOrStarExpression> GenericList<ExpressionOrStarExpression>
}; };
ExpressionList2: Vec<ast::ParenthesizedExpr> = { ExpressionList2: Vec<crate::parser::ParenthesizedExpr> = {
<elements:OneOrMore<ExpressionOrStarExpression>> ","? => elements, <elements:OneOrMore<ExpressionOrStarExpression>> ","? => elements,
}; };
@ -1832,14 +1832,14 @@ ExpressionList2: Vec<ast::ParenthesizedExpr> = {
// - a single expression // - a single expression
// - a single expression followed by a trailing comma // - a single expression followed by a trailing comma
#[inline] #[inline]
TestList: ast::ParenthesizedExpr = { TestList: crate::parser::ParenthesizedExpr = {
GenericList<TestOrStarExpr> GenericList<TestOrStarExpr>
}; };
GenericList<Element>: ast::ParenthesizedExpr = { GenericList<Element>: crate::parser::ParenthesizedExpr = {
<location:@L> <elts:OneOrMore<Element>> <trailing_comma:","?> <end_location:@R> => { <location:@L> <elts:OneOrMore<Element>> <trailing_comma:","?> <end_location:@R> => {
if elts.len() == 1 && trailing_comma.is_none() { if elts.len() == 1 && trailing_comma.is_none() {
ast::ParenthesizedExpr { crate::parser::ParenthesizedExpr {
expr: elts.into_iter().next().unwrap().into(), expr: elts.into_iter().next().unwrap().into(),
range: (location..end_location).into(), range: (location..end_location).into(),
} }
@ -1851,7 +1851,7 @@ GenericList<Element>: ast::ParenthesizedExpr = {
} }
// Test // Test
StarExpr: ast::ParenthesizedExpr = { StarExpr: crate::parser::ParenthesizedExpr = {
<location:@L> "*" <value:Expression<"all">> <end_location:@R> => ast::ExprStarred { <location:@L> "*" <value:Expression<"all">> <end_location:@R> => ast::ExprStarred {
value: Box::new(value.into()), value: Box::new(value.into()),
ctx: ast::ExprContext::Load, ctx: ast::ExprContext::Load,
@ -1876,8 +1876,8 @@ SingleForComprehension: ast::Comprehension = {
} }
}; };
ExpressionNoCond: ast::ParenthesizedExpr = OrTest<"all">; ExpressionNoCond: crate::parser::ParenthesizedExpr = OrTest<"all">;
ComprehensionIf: ast::ParenthesizedExpr = "if" <c:ExpressionNoCond> => c; ComprehensionIf: crate::parser::ParenthesizedExpr = "if" <c:ExpressionNoCond> => c;
Arguments: ast::Arguments = { Arguments: ast::Arguments = {
<location:@L> "(" <e: Comma<FunctionArgument>> ")" <end_location:@R> =>? { <location:@L> "(" <e: Comma<FunctionArgument>> ")" <end_location:@R> =>? {

File diff suppressed because it is too large Load diff