Remove unused parser modes

<!--
Thank you for contributing to Ruff! To help us out with reviewing, please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR removes the `Interactive` and `FunctionType` parser modes that are unused by ruff

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

`cargo test`

<!-- How was it tested? -->
This commit is contained in:
Micha Reiser 2023-08-01 13:10:07 +02:00 committed by GitHub
parent 7c7231db2e
commit f45e8645d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 15156 additions and 15587 deletions

View file

@ -22,9 +22,7 @@ pub trait AstNode: Ranged {
#[derive(Clone, Debug, is_macro::Is, PartialEq)]
pub enum AnyNode {
ModModule(ast::ModModule),
ModInteractive(ast::ModInteractive),
ModExpression(ast::ModExpression),
ModFunctionType(ast::ModFunctionType),
StmtFunctionDef(ast::StmtFunctionDef),
StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef),
StmtClassDef(ast::StmtClassDef),
@ -140,9 +138,7 @@ impl AnyNode {
AnyNode::StmtLineMagic(node) => Some(Stmt::LineMagic(node)),
AnyNode::ModModule(_)
| AnyNode::ModInteractive(_)
| AnyNode::ModExpression(_)
| AnyNode::ModFunctionType(_)
| AnyNode::ExprBoolOp(_)
| AnyNode::ExprNamedExpr(_)
| AnyNode::ExprBinOp(_)
@ -228,9 +224,7 @@ impl AnyNode {
AnyNode::ExprLineMagic(node) => Some(Expr::LineMagic(node)),
AnyNode::ModModule(_)
| AnyNode::ModInteractive(_)
| AnyNode::ModExpression(_)
| AnyNode::ModFunctionType(_)
| AnyNode::StmtFunctionDef(_)
| AnyNode::StmtAsyncFunctionDef(_)
| AnyNode::StmtClassDef(_)
@ -288,9 +282,7 @@ impl AnyNode {
pub fn module(self) -> Option<Mod> {
match self {
AnyNode::ModModule(node) => Some(Mod::Module(node)),
AnyNode::ModInteractive(node) => Some(Mod::Interactive(node)),
AnyNode::ModExpression(node) => Some(Mod::Expression(node)),
AnyNode::ModFunctionType(node) => Some(Mod::FunctionType(node)),
AnyNode::StmtFunctionDef(_)
| AnyNode::StmtAsyncFunctionDef(_)
@ -386,9 +378,7 @@ impl AnyNode {
AnyNode::PatternMatchOr(node) => Some(Pattern::MatchOr(node)),
AnyNode::ModModule(_)
| AnyNode::ModInteractive(_)
| AnyNode::ModExpression(_)
| AnyNode::ModFunctionType(_)
| AnyNode::StmtFunctionDef(_)
| AnyNode::StmtAsyncFunctionDef(_)
| AnyNode::StmtClassDef(_)
@ -468,9 +458,7 @@ impl AnyNode {
AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)),
AnyNode::ModModule(_)
| AnyNode::ModInteractive(_)
| AnyNode::ModExpression(_)
| AnyNode::ModFunctionType(_)
| AnyNode::StmtFunctionDef(_)
| AnyNode::StmtAsyncFunctionDef(_)
| AnyNode::StmtClassDef(_)
@ -575,9 +563,7 @@ impl AnyNode {
pub const fn as_ref(&self) -> AnyNodeRef {
match self {
Self::ModModule(node) => AnyNodeRef::ModModule(node),
Self::ModInteractive(node) => AnyNodeRef::ModInteractive(node),
Self::ModExpression(node) => AnyNodeRef::ModExpression(node),
Self::ModFunctionType(node) => AnyNodeRef::ModFunctionType(node),
Self::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
Self::StmtAsyncFunctionDef(node) => AnyNodeRef::StmtAsyncFunctionDef(node),
Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node),
@ -694,34 +680,6 @@ impl AstNode for ast::ModModule {
AnyNode::from(self)
}
}
impl AstNode for ast::ModInteractive {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ModInteractive(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ModInteractive(node) = kind {
Some(node)
} else {
None
}
}
fn as_any_node_ref(&self) -> AnyNodeRef {
AnyNodeRef::from(self)
}
fn into_any_node(self) -> AnyNode {
AnyNode::from(self)
}
}
impl AstNode for ast::ModExpression {
fn cast(kind: AnyNode) -> Option<Self>
where
@ -750,34 +708,6 @@ impl AstNode for ast::ModExpression {
AnyNode::from(self)
}
}
impl AstNode for ast::ModFunctionType {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ModFunctionType(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ModFunctionType(node) = kind {
Some(node)
} else {
None
}
}
fn as_any_node_ref(&self) -> AnyNodeRef {
AnyNodeRef::from(self)
}
fn into_any_node(self) -> AnyNode {
AnyNode::from(self)
}
}
impl AstNode for ast::StmtFunctionDef {
fn cast(kind: AnyNode) -> Option<Self>
where
@ -3067,9 +2997,7 @@ impl From<Mod> for AnyNode {
fn from(module: Mod) -> Self {
match module {
Mod::Module(node) => AnyNode::ModModule(node),
Mod::Interactive(node) => AnyNode::ModInteractive(node),
Mod::Expression(node) => AnyNode::ModExpression(node),
Mod::FunctionType(node) => AnyNode::ModFunctionType(node),
}
}
}
@ -3103,24 +3031,12 @@ impl From<ast::ModModule> for AnyNode {
}
}
impl From<ast::ModInteractive> for AnyNode {
fn from(node: ast::ModInteractive) -> Self {
AnyNode::ModInteractive(node)
}
}
impl From<ast::ModExpression> for AnyNode {
fn from(node: ast::ModExpression) -> Self {
AnyNode::ModExpression(node)
}
}
impl From<ast::ModFunctionType> for AnyNode {
fn from(node: ast::ModFunctionType) -> Self {
AnyNode::ModFunctionType(node)
}
}
impl From<ast::StmtFunctionDef> for AnyNode {
fn from(node: ast::StmtFunctionDef) -> Self {
AnyNode::StmtFunctionDef(node)
@ -3590,9 +3506,7 @@ impl Ranged for AnyNode {
fn range(&self) -> TextRange {
match self {
AnyNode::ModModule(node) => node.range(),
AnyNode::ModInteractive(node) => node.range(),
AnyNode::ModExpression(node) => node.range(),
AnyNode::ModFunctionType(node) => node.range(),
AnyNode::StmtFunctionDef(node) => node.range(),
AnyNode::StmtAsyncFunctionDef(node) => node.range(),
AnyNode::StmtClassDef(node) => node.range(),
@ -3679,9 +3593,7 @@ impl Ranged for AnyNode {
#[derive(Copy, Clone, Debug, is_macro::Is, PartialEq)]
pub enum AnyNodeRef<'a> {
ModModule(&'a ast::ModModule),
ModInteractive(&'a ast::ModInteractive),
ModExpression(&'a ast::ModExpression),
ModFunctionType(&'a ast::ModFunctionType),
StmtFunctionDef(&'a ast::StmtFunctionDef),
StmtAsyncFunctionDef(&'a ast::StmtAsyncFunctionDef),
StmtClassDef(&'a ast::StmtClassDef),
@ -3767,9 +3679,7 @@ impl AnyNodeRef<'_> {
pub fn as_ptr(&self) -> NonNull<()> {
match self {
AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(),
AnyNodeRef::ModInteractive(node) => NonNull::from(*node).cast(),
AnyNodeRef::ModExpression(node) => NonNull::from(*node).cast(),
AnyNodeRef::ModFunctionType(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtFunctionDef(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtAsyncFunctionDef(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(),
@ -3861,9 +3771,7 @@ impl AnyNodeRef<'_> {
pub const fn kind(self) -> NodeKind {
match self {
AnyNodeRef::ModModule(_) => NodeKind::ModModule,
AnyNodeRef::ModInteractive(_) => NodeKind::ModInteractive,
AnyNodeRef::ModExpression(_) => NodeKind::ModExpression,
AnyNodeRef::ModFunctionType(_) => NodeKind::ModFunctionType,
AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef,
AnyNodeRef::StmtAsyncFunctionDef(_) => NodeKind::StmtAsyncFunctionDef,
AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef,
@ -3979,9 +3887,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtLineMagic(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModInteractive(_)
| AnyNodeRef::ModExpression(_)
| AnyNodeRef::ModFunctionType(_)
| AnyNodeRef::ExprBoolOp(_)
| AnyNodeRef::ExprNamedExpr(_)
| AnyNodeRef::ExprBinOp(_)
@ -4067,9 +3973,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprLineMagic(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModInteractive(_)
| AnyNodeRef::ModExpression(_)
| AnyNodeRef::ModFunctionType(_)
| AnyNodeRef::StmtFunctionDef(_)
| AnyNodeRef::StmtAsyncFunctionDef(_)
| AnyNodeRef::StmtClassDef(_)
@ -4126,10 +4030,7 @@ impl AnyNodeRef<'_> {
pub const fn is_module(self) -> bool {
match self {
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModInteractive(_)
| AnyNodeRef::ModExpression(_)
| AnyNodeRef::ModFunctionType(_) => true,
AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) => true,
AnyNodeRef::StmtFunctionDef(_)
| AnyNodeRef::StmtAsyncFunctionDef(_)
@ -4225,9 +4126,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::PatternMatchOr(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModInteractive(_)
| AnyNodeRef::ModExpression(_)
| AnyNodeRef::ModFunctionType(_)
| AnyNodeRef::StmtFunctionDef(_)
| AnyNodeRef::StmtAsyncFunctionDef(_)
| AnyNodeRef::StmtClassDef(_)
@ -4307,9 +4206,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::ExceptHandlerExceptHandler(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModInteractive(_)
| AnyNodeRef::ModExpression(_)
| AnyNodeRef::ModFunctionType(_)
| AnyNodeRef::StmtFunctionDef(_)
| AnyNodeRef::StmtAsyncFunctionDef(_)
| AnyNodeRef::StmtClassDef(_)
@ -4428,24 +4325,12 @@ impl<'a> From<&'a ast::ModModule> for AnyNodeRef<'a> {
}
}
impl<'a> From<&'a ast::ModInteractive> for AnyNodeRef<'a> {
fn from(node: &'a ast::ModInteractive) -> Self {
AnyNodeRef::ModInteractive(node)
}
}
impl<'a> From<&'a ast::ModExpression> for AnyNodeRef<'a> {
fn from(node: &'a ast::ModExpression) -> Self {
AnyNodeRef::ModExpression(node)
}
}
impl<'a> From<&'a ast::ModFunctionType> for AnyNodeRef<'a> {
fn from(node: &'a ast::ModFunctionType) -> Self {
AnyNodeRef::ModFunctionType(node)
}
}
impl<'a> From<&'a ast::StmtFunctionDef> for AnyNodeRef<'a> {
fn from(node: &'a ast::StmtFunctionDef) -> Self {
AnyNodeRef::StmtFunctionDef(node)
@ -4947,9 +4832,7 @@ impl<'a> From<&'a Mod> for AnyNodeRef<'a> {
fn from(module: &'a Mod) -> Self {
match module {
Mod::Module(node) => AnyNodeRef::ModModule(node),
Mod::Interactive(node) => AnyNodeRef::ModInteractive(node),
Mod::Expression(node) => AnyNodeRef::ModExpression(node),
Mod::FunctionType(node) => AnyNodeRef::ModFunctionType(node),
}
}
}
@ -5034,9 +4917,7 @@ impl Ranged for AnyNodeRef<'_> {
fn range(&self) -> TextRange {
match self {
AnyNodeRef::ModModule(node) => node.range(),
AnyNodeRef::ModInteractive(node) => node.range(),
AnyNodeRef::ModExpression(node) => node.range(),
AnyNodeRef::ModFunctionType(node) => node.range(),
AnyNodeRef::StmtFunctionDef(node) => node.range(),
AnyNodeRef::StmtAsyncFunctionDef(node) => node.range(),
AnyNodeRef::StmtClassDef(node) => node.range(),

View file

@ -10,9 +10,7 @@ use std::fmt::Debug;
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Mod {
Module(ModModule),
Interactive(ModInteractive),
Expression(ModExpression),
FunctionType(ModFunctionType),
}
/// See also [Module](https://docs.python.org/3/library/ast.html#ast.Module)
@ -28,19 +26,6 @@ impl From<ModModule> for Mod {
}
}
/// See also [Interactive](https://docs.python.org/3/library/ast.html#ast.Interactive)
#[derive(Clone, Debug, PartialEq)]
pub struct ModInteractive {
pub range: TextRange,
pub body: Vec<Stmt>,
}
impl From<ModInteractive> for Mod {
fn from(payload: ModInteractive) -> Self {
Mod::Interactive(payload)
}
}
/// See also [Expression](https://docs.python.org/3/library/ast.html#ast.Expression)
#[derive(Clone, Debug, PartialEq)]
pub struct ModExpression {
@ -54,20 +39,6 @@ impl From<ModExpression> for Mod {
}
}
/// See also [FunctionType](https://docs.python.org/3/library/ast.html#ast.FunctionType)
#[derive(Clone, Debug, PartialEq)]
pub struct ModFunctionType {
pub range: TextRange,
pub argtypes: Vec<Expr>,
pub returns: Box<Expr>,
}
impl From<ModFunctionType> for Mod {
fn from(payload: ModFunctionType) -> Self {
Mod::FunctionType(payload)
}
}
/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Stmt {
@ -2474,28 +2445,16 @@ impl Ranged for crate::nodes::ModModule {
self.range
}
}
impl Ranged for crate::nodes::ModInteractive {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::nodes::ModExpression {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::nodes::ModFunctionType {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::Mod {
fn range(&self) -> TextRange {
match self {
Self::Module(node) => node.range(),
Self::Interactive(node) => node.range(),
Self::Expression(node) => node.range(),
Self::FunctionType(node) => node.range(),
}
}
}
@ -3017,5 +2976,5 @@ mod size_assertions {
assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 32]);
assert_eq_size!(Pattern, [u8; 96]);
assert_eq_size!(Mod, [u8; 48]);
assert_eq_size!(Mod, [u8; 32]);
}

View file

@ -109,19 +109,7 @@ where
Mod::Module(ast::ModModule { body, range: _ }) => {
visitor.visit_body(body);
}
Mod::Interactive(ast::ModInteractive { body, range: _ }) => visitor.visit_body(body),
Mod::Expression(ast::ModExpression { body, range: _ }) => visitor.visit_expr(body),
Mod::FunctionType(ast::ModFunctionType {
range: _,
argtypes,
returns,
}) => {
for arg_type in argtypes {
visitor.visit_expr(arg_type);
}
visitor.visit_expr(returns);
}
}
}

View file

@ -147,21 +147,7 @@ where
ast::Mod::Module(ast::ModModule { body, range: _ }) => {
visitor.visit_body(body);
}
ast::Mod::Interactive(ast::ModInteractive { body, range: _ }) => {
visitor.visit_body(body);
}
ast::Mod::Expression(ast::ModExpression { body, range: _ }) => visitor.visit_expr(body),
ast::Mod::FunctionType(ast::ModFunctionType {
range: _,
argtypes,
returns,
}) => {
for arg_type in argtypes {
visitor.visit_expr(arg_type);
}
visitor.visit_expr(returns);
}
}
}

View file

@ -41,46 +41,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ModModule {
}
}
impl FormatRule<ast::ModInteractive, PyFormatContext<'_>>
for crate::module::mod_interactive::FormatModInteractive
{
#[inline]
fn fmt(
&self,
node: &ast::ModInteractive,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::ModInteractive>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ModInteractive {
type Format<'a> = FormatRefWithRule<
'a,
ast::ModInteractive,
crate::module::mod_interactive::FormatModInteractive,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::module::mod_interactive::FormatModInteractive::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ModInteractive {
type Format = FormatOwnedWithRule<
ast::ModInteractive,
crate::module::mod_interactive::FormatModInteractive,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::module::mod_interactive::FormatModInteractive::default(),
)
}
}
impl FormatRule<ast::ModExpression, PyFormatContext<'_>>
for crate::module::mod_expression::FormatModExpression
{
@ -121,46 +81,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ModExpression {
}
}
impl FormatRule<ast::ModFunctionType, PyFormatContext<'_>>
for crate::module::mod_function_type::FormatModFunctionType
{
#[inline]
fn fmt(
&self,
node: &ast::ModFunctionType,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::ModFunctionType>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ModFunctionType {
type Format<'a> = FormatRefWithRule<
'a,
ast::ModFunctionType,
crate::module::mod_function_type::FormatModFunctionType,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::module::mod_function_type::FormatModFunctionType::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ModFunctionType {
type Format = FormatOwnedWithRule<
ast::ModFunctionType,
crate::module::mod_function_type::FormatModFunctionType,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::module::mod_function_type::FormatModFunctionType::default(),
)
}
}
impl FormatRule<ast::StmtFunctionDef, PyFormatContext<'_>>
for crate::statement::stmt_function_def::FormatStmtFunctionDef
{

View file

@ -4,8 +4,6 @@ use ruff_formatter::{Format, FormatOwnedWithRule, FormatRefWithRule, FormatResul
use ruff_python_ast::Mod;
pub(crate) mod mod_expression;
pub(crate) mod mod_function_type;
pub(crate) mod mod_interactive;
pub(crate) mod mod_module;
#[derive(Default)]
@ -15,9 +13,7 @@ impl FormatRule<Mod, PyFormatContext<'_>> for FormatMod {
fn fmt(&self, item: &Mod, f: &mut PyFormatter) -> FormatResult<()> {
match item {
Mod::Module(x) => x.format().fmt(f),
Mod::Interactive(x) => x.format().fmt(f),
Mod::Expression(x) => x.format().fmt(f),
Mod::FunctionType(x) => x.format().fmt(f),
}
}
}

View file

@ -1,12 +0,0 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::ModFunctionType;
#[derive(Default)]
pub struct FormatModFunctionType;
impl FormatNodeRule<ModFunctionType> for FormatModFunctionType {
fn fmt_fields(&self, item: &ModFunctionType, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,12 +0,0 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::ModInteractive;
#[derive(Default)]
pub struct FormatModInteractive;
impl FormatNodeRule<ModInteractive> for FormatModInteractive {
fn fmt_fields(&self, item: &ModInteractive, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -115,7 +115,7 @@ pub use parse::Parse;
pub use parser::{parse, parse_starts_at, parse_tokens, ParseError, ParseErrorType};
#[allow(deprecated)]
pub use parser::{parse_expression, parse_expression_starts_at, parse_program};
use ruff_python_ast::{CmpOp, Expr, Mod, ModModule, Ranged, Suite};
use ruff_python_ast::{CmpOp, Expr, ModModule, Ranged, Suite};
use ruff_text_size::{TextRange, TextSize};
pub use string::FStringErrorType;
pub use token::{StringKind, Tok, TokenKind};
@ -149,10 +149,7 @@ pub fn parse_program_tokens(
lxr: Vec<LexResult>,
source_path: &str,
) -> anyhow::Result<Suite, ParseError> {
parser::parse_tokens(lxr, Mode::Module, source_path).map(|top| match top {
Mod::Module(ModModule { body, .. }) => body,
_ => unreachable!(),
})
ModModule::parse_tokens(lxr, source_path).map(|module| module.body)
}
/// Return the `Range` of the first `Tok::Colon` token in a `Range`.
@ -270,8 +267,6 @@ impl LocatedCmpOp {
pub enum Mode {
/// The code consists of a sequence of statements.
Module,
/// The code consists of a sequence of interactive statement.
Interactive,
/// The code consists of a single expression.
Expression,
/// The code consists of a sequence of statements which are part of a

View file

@ -74,7 +74,7 @@ impl Parse for ast::ModModule {
) -> Result<Self, ParseError> {
match parse_tokens(lxr, Mode::Module, source_path)? {
ast::Mod::Module(m) => Ok(m),
_ => unreachable!("Mode::Module doesn't return other variant"),
ast::Mod::Expression(_) => unreachable!("Mode::Module doesn't return other variant"),
}
}
}
@ -88,20 +88,7 @@ impl Parse for ast::ModExpression {
) -> Result<Self, ParseError> {
match parse_tokens(lxr, Mode::Expression, source_path)? {
ast::Mod::Expression(m) => Ok(m),
_ => unreachable!("Mode::Module doesn't return other variant"),
}
}
}
impl Parse for ast::ModInteractive {
const MODE: Mode = Mode::Interactive;
fn parse_tokens(
lxr: impl IntoIterator<Item = LexResult>,
source_path: &str,
) -> Result<Self, ParseError> {
match parse_tokens(lxr, Mode::Interactive, source_path)? {
ast::Mod::Interactive(m) => Ok(m),
_ => unreachable!("Mode::Module doesn't return other variant"),
ast::Mod::Module(_) => unreachable!("Mode::Module doesn't return other variant"),
}
}
}

View file

@ -25,6 +25,7 @@ use crate::{
Mode, Parse,
};
use ruff_python_ast as ast;
use ruff_python_ast::ModModule;
/// Parse a full Python program usually consisting of multiple lines.
///
@ -48,10 +49,7 @@ use ruff_python_ast as ast;
/// ```
#[deprecated = "Use ruff_python_ast::Suite::parse from ruff_python_parser::Parse trait."]
pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, ParseError> {
parse(source, Mode::Module, source_path).map(|top| match top {
ast::Mod::Module(ast::ModModule { body, .. }) => body,
_ => unreachable!(),
})
ModModule::parse(source, source_path).map(|module| module.body)
}
/// Parses a single Python expression.
@ -718,7 +716,6 @@ except* OSError as e:
assert!(parse(source, Mode::Expression, "<embedded>").is_ok());
assert!(parse(source, Mode::Module, "<embedded>").is_ok());
assert!(parse(source, Mode::Interactive, "<embedded>").is_ok());
}
#[test]

View file

@ -22,7 +22,6 @@ grammar(mode: Mode);
// By having only a single pub function, we reduce this to one.
pub(crate) Top: ast::Mod = {
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, range: (start..end).into() }.into(),
<start:@L> StartInteractive <body:Program> <end:@R> => ast::ModInteractive { body, range: (start..end).into() }.into(),
<start:@L> StartExpression <body:TestList> ("\n")* <end:@R> => ast::ModExpression { body: Box::new(body), range: (start..end).into() }.into()
};
@ -1743,7 +1742,6 @@ extern {
Indent => token::Tok::Indent,
Dedent => token::Tok::Dedent,
StartModule => token::Tok::StartModule,
StartInteractive => token::Tok::StartInteractive,
StartExpression => token::Tok::StartExpression,
"+" => token::Tok::Plus,
"-" => token::Tok::Minus,

File diff suppressed because it is too large Load diff

View file

@ -31,7 +31,7 @@ where
pub fn new(lexer: I, mode: Mode) -> Self {
Self {
underlying: lexer.multipeek(), // spell-checker:ignore multipeek
start_of_line: matches!(mode, Mode::Interactive | Mode::Module),
start_of_line: matches!(mode, Mode::Module),
}
}
}
@ -140,11 +140,7 @@ where
matches!(
tok,
Tok::StartModule
| Tok::StartInteractive
| Tok::Newline
| Tok::Indent
| Tok::Dedent
Tok::StartModule | Tok::Newline | Tok::Indent | Tok::Dedent
)
})
});

View file

@ -203,7 +203,6 @@ pub enum Tok {
// RustPython specific.
StartModule,
StartInteractive,
StartExpression,
}
@ -211,7 +210,6 @@ impl Tok {
pub fn start_marker(mode: Mode) -> Self {
match mode {
Mode::Module | Mode::Jupyter => Tok::StartModule,
Mode::Interactive => Tok::StartInteractive,
Mode::Expression => Tok::StartExpression,
}
}
@ -240,7 +238,6 @@ impl fmt::Display for Tok {
Indent => f.write_str("Indent"),
Dedent => f.write_str("Dedent"),
StartModule => f.write_str("StartProgram"),
StartInteractive => f.write_str("StartInteractive"),
StartExpression => f.write_str("StartExpression"),
EndOfFile => f.write_str("EOF"),
Lpar => f.write_str("'('"),
@ -872,7 +869,6 @@ impl TokenKind {
Tok::With => TokenKind::With,
Tok::Yield => TokenKind::Yield,
Tok::StartModule => TokenKind::StartModule,
Tok::StartInteractive => TokenKind::StartInteractive,
Tok::StartExpression => TokenKind::StartExpression,
}
}