use ast namespace (#6769)

This commit is contained in:
Jeong, YunWon authored and GitHub committed 2026-01-18 17:30:36 +09:00
1 parent eb50246b41
commit 1ccd99ea20
20 files changed
+863 -877

No files matched your search

File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -18,7 +18,7 @@ pub mod symboltable;
mod unparse;
pub use compile::CompileOpts;
use ruff_python_ast::Expr;
use ruff_python_ast as ast;
pub(crate) use compile::InternalResult;
@@ -27,7 +27,7 @@ pub trait ToPythonName {
fn python_name(&self) -> &'static str;
}
impl ToPythonName for Expr {
impl ToPythonName for ast::Expr {
fn python_name(&self) -> &'static str {
match self {
Self::BoolOp { .. } | Self::BinOp { .. } | Self::UnaryOp { .. } => "operator",
+8 -5
View File
@@ -7,7 +7,7 @@
use core::convert::Infallible;
use ruff_python_ast::{AnyStringFlags, StringFlags};
use ruff_python_ast::{self as ast, StringFlags as _};
use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
// use ruff_python_parser::{LexicalError, LexicalErrorType};
@@ -24,11 +24,11 @@ struct StringParser {
/// Current position of the parser in the source.
cursor: usize,
/// Flags that can be used to query information about the string.
flags: AnyStringFlags,
flags: ast::AnyStringFlags,
}
impl StringParser {
const fn new(source: Box<str>, flags: AnyStringFlags) -> Self {
const fn new(source: Box<str>, flags: ast::AnyStringFlags) -> Self {
Self {
source,
cursor: 0,
@@ -272,7 +272,7 @@ impl StringParser {
}
}
pub(crate) fn parse_string_literal(source: &str, flags: AnyStringFlags) -> Box<Wtf8> {
pub(crate) fn parse_string_literal(source: &str, flags: ast::AnyStringFlags) -> Box<Wtf8> {
let source = &source[flags.opener_len().to_usize()..];
let source = &source[..source.len() - flags.quote_len().to_usize()];
StringParser::new(source.into(), flags)
@@ -280,7 +280,10 @@ pub(crate) fn parse_string_literal(source: &str, flags: AnyStringFlags) -> Box<W
.unwrap_or_else(|x| match x {})
}
pub(crate) fn parse_fstring_literal_element(source: Box<str>, flags: AnyStringFlags) -> Box<Wtf8> {
pub(crate) fn parse_fstring_literal_element(
source: Box<str>,
flags: ast::AnyStringFlags,
) -> Box<Wtf8> {
StringParser::new(source, flags)
.parse_fstring_middle()
.unwrap_or_else(|x| match x {})
+44 -41
View File
@@ -13,12 +13,7 @@ use crate::{
};
use alloc::{borrow::Cow, fmt};
use bitflags::bitflags;
use ruff_python_ast::{
self as ast, Comprehension, Decorator, Expr, Identifier, ModExpression, ModModule, Parameter,
ParameterWithDefault, Parameters, Pattern, PatternMatchAs, PatternMatchClass,
PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchStar, PatternMatchValue,
Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams,
};
use ruff_python_ast as ast;
use ruff_text_size::{Ranged, TextRange};
use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation};
use std::collections::HashSet;
@@ -97,13 +92,19 @@ impl SymbolTable {
}
}
pub fn scan_program(program: &ModModule, source_file: SourceFile) -> SymbolTableResult<Self> {
pub fn scan_program(
program: &ast::ModModule,
source_file: SourceFile,
) -> SymbolTableResult<Self> {
let mut builder = SymbolTableBuilder::new(source_file);
builder.scan_statements(program.body.as_ref())?;
builder.finish()
}
pub fn scan_expr(expr: &ModExpression, source_file: SourceFile) -> SymbolTableResult<Self> {
pub fn scan_expr(
expr: &ast::ModExpression,
source_file: SourceFile,
) -> SymbolTableResult<Self> {
let mut builder = SymbolTableBuilder::new(source_file);
builder.scan_expression(expr.body.as_ref(), ExpressionContext::Load)?;
builder.finish()
@@ -973,21 +974,21 @@ impl SymbolTableBuilder {
.get() as _
}
fn scan_statements(&mut self, statements: &[Stmt]) -> SymbolTableResult {
fn scan_statements(&mut self, statements: &[ast::Stmt]) -> SymbolTableResult {
for statement in statements {
self.scan_statement(statement)?;
}
Ok(())
}
fn scan_parameters(&mut self, parameters: &[ParameterWithDefault]) -> SymbolTableResult {
fn scan_parameters(&mut self, parameters: &[ast::ParameterWithDefault]) -> SymbolTableResult {
for parameter in parameters {
self.scan_parameter(&parameter.parameter)?;
}
Ok(())
}
fn scan_parameter(&mut self, parameter: &Parameter) -> SymbolTableResult {
fn scan_parameter(&mut self, parameter: &ast::Parameter) -> SymbolTableResult {
self.check_name(
parameter.name.as_str(),
ExpressionContext::Store,
@@ -1019,7 +1020,7 @@ impl SymbolTableBuilder {
self.register_ident(&parameter.name, usage)
}
fn scan_annotation(&mut self, annotation: &Expr) -> SymbolTableResult {
fn scan_annotation(&mut self, annotation: &ast::Expr) -> SymbolTableResult {
let current_scope = self.tables.last().map(|t| t.typ);
// PEP 649: Check if this is a conditional annotation
@@ -1074,8 +1075,8 @@ impl SymbolTableBuilder {
result
}
fn scan_statement(&mut self, statement: &Stmt) -> SymbolTableResult {
use ruff_python_ast::*;
fn scan_statement(&mut self, statement: &ast::Stmt) -> SymbolTableResult {
use ast::*;
if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = &statement
&& module.as_ref().map(|id| id.as_str()) == Some("__future__")
{
@@ -1435,7 +1436,7 @@ impl SymbolTableBuilder {
fn scan_decorators(
&mut self,
decorators: &[Decorator],
decorators: &[ast::Decorator],
context: ExpressionContext,
) -> SymbolTableResult {
for decorator in decorators {
@@ -1446,7 +1447,7 @@ impl SymbolTableBuilder {
fn scan_expressions(
&mut self,
expressions: &[Expr],
expressions: &[ast::Expr],
context: ExpressionContext,
) -> SymbolTableResult {
for expression in expressions {
@@ -1457,10 +1458,10 @@ impl SymbolTableBuilder {
fn scan_expression(
&mut self,
expression: &Expr,
expression: &ast::Expr,
context: ExpressionContext,
) -> SymbolTableResult {
use ruff_python_ast::*;
use ast::*;
// Check for expressions not allowed in certain contexts
// (type parameters, annotations, type aliases, TypeVar bounds/defaults)
@@ -1837,9 +1838,9 @@ impl SymbolTableBuilder {
fn scan_comprehension(
&mut self,
scope_name: &str,
elt1: &Expr,
elt2: Option<&Expr>,
generators: &[Comprehension],
elt1: &ast::Expr,
elt2: Option<&ast::Expr>,
generators: &[ast::Comprehension],
range: TextRange,
is_generator: bool,
) -> SymbolTableResult {
@@ -1906,7 +1907,7 @@ impl SymbolTableBuilder {
// = symtable_visit_type_param_bound_or_default
fn scan_type_param_bound_or_default(
&mut self,
expr: &Expr,
expr: &ast::Expr,
scope_name: &str,
scope_info: &'static str,
) -> SymbolTableResult {
@@ -1932,14 +1933,14 @@ impl SymbolTableBuilder {
result
}
fn scan_type_params(&mut self, type_params: &TypeParams) -> SymbolTableResult {
fn scan_type_params(&mut self, type_params: &ast::TypeParams) -> SymbolTableResult {
// Check for duplicate type parameter names
let mut seen_names: std::collections::HashSet<&str> = std::collections::HashSet::new();
for type_param in &type_params.type_params {
let (name, range) = match type_param {
TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range),
TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range),
TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range),
ast::TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range),
ast::TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range),
ast::TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range),
};
if !seen_names.insert(name) {
return Err(SymbolTableError {
@@ -1959,7 +1960,7 @@ impl SymbolTableBuilder {
// First register all type parameters
for type_param in &type_params.type_params {
match type_param {
TypeParam::TypeVar(TypeParamTypeVar {
ast::TypeParam::TypeVar(ast::TypeParamTypeVar {
name,
bound,
range: type_var_range,
@@ -1991,7 +1992,7 @@ impl SymbolTableBuilder {
)?;
}
}
TypeParam::ParamSpec(TypeParamParamSpec {
ast::TypeParam::ParamSpec(ast::TypeParamParamSpec {
name,
range: param_spec_range,
default,
@@ -2009,7 +2010,7 @@ impl SymbolTableBuilder {
)?;
}
}
TypeParam::TypeVarTuple(TypeParamTypeVarTuple {
ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple {
name,
range: type_var_tuple_range,
default,
@@ -2032,22 +2033,24 @@ impl SymbolTableBuilder {
Ok(())
}
fn scan_patterns(&mut self, patterns: &[Pattern]) -> SymbolTableResult {
fn scan_patterns(&mut self, patterns: &[ast::Pattern]) -> SymbolTableResult {
for pattern in patterns {
self.scan_pattern(pattern)?;
}
Ok(())
}
fn scan_pattern(&mut self, pattern: &Pattern) -> SymbolTableResult {
use Pattern::*;
fn scan_pattern(&mut self, pattern: &ast::Pattern) -> SymbolTableResult {
use ast::Pattern::*;
match pattern {
MatchValue(PatternMatchValue { value, .. }) => {
MatchValue(ast::PatternMatchValue { value, .. }) => {
self.scan_expression(value, ExpressionContext::Load)?
}
MatchSingleton(_) => {}
MatchSequence(PatternMatchSequence { patterns, .. }) => self.scan_patterns(patterns)?,
MatchMapping(PatternMatchMapping {
MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
self.scan_patterns(patterns)?
}
MatchMapping(ast::PatternMatchMapping {
keys,
patterns,
rest,
@@ -2059,19 +2062,19 @@ impl SymbolTableBuilder {
self.register_ident(rest, SymbolUsage::Assigned)?;
}
}
MatchClass(PatternMatchClass { cls, arguments, .. }) => {
MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => {
self.scan_expression(cls, ExpressionContext::Load)?;
self.scan_patterns(&arguments.patterns)?;
for kw in &arguments.keywords {
self.scan_pattern(&kw.pattern)?;
}
}
MatchStar(PatternMatchStar { name, .. }) => {
MatchStar(ast::PatternMatchStar { name, .. }) => {
if let Some(name) = name {
self.register_ident(name, SymbolUsage::Assigned)?;
}
}
MatchAs(PatternMatchAs { pattern, name, .. }) => {
MatchAs(ast::PatternMatchAs { pattern, name, .. }) => {
if let Some(pattern) = pattern {
self.scan_pattern(pattern)?;
}
@@ -2079,7 +2082,7 @@ impl SymbolTableBuilder {
self.register_ident(name, SymbolUsage::Assigned)?;
}
}
MatchOr(PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?,
MatchOr(ast::PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?,
}
Ok(())
}
@@ -2087,7 +2090,7 @@ impl SymbolTableBuilder {
fn enter_scope_with_parameters(
&mut self,
name: &str,
parameters: &Parameters,
parameters: &ast::Parameters,
line_number: u32,
has_return_annotation: bool,
) -> SymbolTableResult {
@@ -2174,7 +2177,7 @@ impl SymbolTableBuilder {
Ok(())
}
fn register_ident(&mut self, ident: &Identifier, role: SymbolUsage) -> SymbolTableResult {
fn register_ident(&mut self, ident: &ast::Identifier, role: SymbolUsage) -> SymbolTableResult {
self.register_name(ident.as_str(), role, ident.range)
}
+66 -72
View File
@@ -1,9 +1,6 @@
use alloc::fmt;
use core::fmt::Display as _;
use ruff_python_ast::{
self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator,
Parameter, ParameterWithDefault, Parameters,
};
use ruff_python_ast as ast;
use ruff_text_size::Ranged;
use rustpython_compiler_core::SourceFile;
use rustpython_literal::escape::{AsciiEscape, UnicodeEscape};
@@ -40,7 +37,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.f.write_str(s)
}
fn p_id(&mut self, s: &Identifier) -> fmt::Result {
fn p_id(&mut self, s: &ast::Identifier) -> fmt::Result {
self.f.write_str(s.as_str())
}
@@ -59,7 +56,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.f.write_fmt(f)
}
fn unparse_expr(&mut self, ast: &Expr, level: u8) -> fmt::Result {
fn unparse_expr(&mut self, ast: &ast::Expr, level: u8) -> fmt::Result {
macro_rules! op_prec {
($op_ty:ident, $x:expr, $enu:path, $($var:ident($op:literal, $prec:ident)),*$(,)?) => {
match $x {
@@ -83,13 +80,13 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}};
}
match &ast {
Expr::BoolOp(ruff::ExprBoolOp {
ast::Expr::BoolOp(ast::ExprBoolOp {
op,
values,
node_index: _,
range: _range,
}) => {
let (op, prec) = op_prec!(bin, op, BoolOp, And("and", AND), Or("or", OR));
let (op, prec) = op_prec!(bin, op, ast::BoolOp, And("and", AND), Or("or", OR));
group_if!(prec, {
let mut first = true;
for val in values {
@@ -98,7 +95,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
})
}
Expr::Named(ruff::ExprNamed {
ast::Expr::Named(ast::ExprNamed {
target,
value,
node_index: _,
@@ -110,18 +107,18 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(value, precedence::ATOM)?;
})
}
Expr::BinOp(ruff::ExprBinOp {
ast::Expr::BinOp(ast::ExprBinOp {
left,
op,
right,
node_index: _,
range: _range,
}) => {
let right_associative = matches!(op, Operator::Pow);
let right_associative = matches!(op, ast::Operator::Pow);
let (op, prec) = op_prec!(
bin,
op,
Operator,
ast::Operator,
Add("+", ARITH),
Sub("-", ARITH),
Mult("*", TERM),
@@ -142,7 +139,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(right, prec + !right_associative as u8)?;
})
}
Expr::UnaryOp(ruff::ExprUnaryOp {
ast::Expr::UnaryOp(ast::ExprUnaryOp {
op,
operand,
node_index: _,
@@ -151,7 +148,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
let (op, prec) = op_prec!(
un,
op,
ruff::UnaryOp,
ast::UnaryOp,
Invert("~", FACTOR),
Not("not ", NOT),
UAdd("+", FACTOR),
@@ -162,7 +159,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(operand, prec)?;
})
}
Expr::Lambda(ruff::ExprLambda {
ast::Expr::Lambda(ast::ExprLambda {
parameters,
body,
node_index: _,
@@ -178,7 +175,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
write!(self, ": {}", UnparseExpr::new(body, self.source))?;
})
}
Expr::If(ruff::ExprIf {
ast::Expr::If(ast::ExprIf {
test,
body,
orelse,
@@ -193,7 +190,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(orelse, precedence::TEST)?;
})
}
Expr::Dict(ruff::ExprDict {
ast::Expr::Dict(ast::ExprDict {
items,
node_index: _,
range: _range,
@@ -211,7 +208,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
self.p("}")?;
}
Expr::Set(ruff::ExprSet {
ast::Expr::Set(ast::ExprSet {
elts,
node_index: _,
range: _range,
@@ -224,7 +221,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
self.p("}")?;
}
Expr::ListComp(ruff::ExprListComp {
ast::Expr::ListComp(ast::ExprListComp {
elt,
generators,
node_index: _,
@@ -235,7 +232,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_comp(generators)?;
self.p("]")?;
}
Expr::SetComp(ruff::ExprSetComp {
ast::Expr::SetComp(ast::ExprSetComp {
elt,
generators,
node_index: _,
@@ -246,7 +243,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_comp(generators)?;
self.p("}")?;
}
Expr::DictComp(ruff::ExprDictComp {
ast::Expr::DictComp(ast::ExprDictComp {
key,
value,
generators,
@@ -260,7 +257,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_comp(generators)?;
self.p("}")?;
}
Expr::Generator(ruff::ExprGenerator {
ast::Expr::Generator(ast::ExprGenerator {
parenthesized: _,
elt,
generators,
@@ -272,7 +269,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_comp(generators)?;
self.p(")")?;
}
Expr::Await(ruff::ExprAwait {
ast::Expr::Await(ast::ExprAwait {
value,
node_index: _,
range: _range,
@@ -282,7 +279,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(value, precedence::ATOM)?;
})
}
Expr::Yield(ruff::ExprYield {
ast::Expr::Yield(ast::ExprYield {
value,
node_index: _,
range: _range,
@@ -293,7 +290,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.p("(yield)")?;
}
}
Expr::YieldFrom(ruff::ExprYieldFrom {
ast::Expr::YieldFrom(ast::ExprYieldFrom {
value,
node_index: _,
range: _range,
@@ -304,7 +301,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
UnparseExpr::new(value, self.source)
)?;
}
Expr::Compare(ruff::ExprCompare {
ast::Expr::Compare(ast::ExprCompare {
left,
ops,
comparators,
@@ -322,9 +319,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
})
}
Expr::Call(ruff::ExprCall {
ast::Expr::Call(ast::ExprCall {
func,
arguments: Arguments { args, keywords, .. },
arguments: ast::Arguments { args, keywords, .. },
node_index: _,
range: _range,
}) => {
@@ -332,7 +329,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.p("(")?;
if let (
[
Expr::Generator(ruff::ExprGenerator {
ast::Expr::Generator(ast::ExprGenerator {
elt,
generators,
node_index: _,
@@ -365,9 +362,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
self.p(")")?;
}
Expr::FString(ruff::ExprFString { value, .. }) => self.unparse_fstring(value)?,
Expr::TString(_) => self.p("t\"\"")?,
Expr::StringLiteral(ruff::ExprStringLiteral { value, .. }) => {
ast::Expr::FString(ast::ExprFString { value, .. }) => self.unparse_fstring(value)?,
ast::Expr::TString(_) => self.p("t\"\"")?,
ast::Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => {
if value.is_unicode() {
self.p("u")?
}
@@ -375,12 +372,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
.str_repr()
.fmt(self.f)?
}
Expr::BytesLiteral(ruff::ExprBytesLiteral { value, .. }) => {
ast::Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. }) => {
AsciiEscape::new_repr(&value.bytes().collect::<Vec<_>>())
.bytes_repr()
.fmt(self.f)?
}
Expr::NumberLiteral(ruff::ExprNumberLiteral { value, .. }) => {
ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => {
#[allow(clippy::correctness, clippy::assertions_on_constants)]
const {
assert!(f64::MAX_10_EXP == 308)
@@ -388,28 +385,28 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
let inf_str = "1e309";
match value {
ruff::Number::Int(int) => int.fmt(self.f)?,
&ruff::Number::Float(fp) => {
ast::Number::Int(int) => int.fmt(self.f)?,
&ast::Number::Float(fp) => {
if fp.is_infinite() {
self.p(inf_str)?
} else {
self.p(&rustpython_literal::float::to_string(fp))?
}
}
&ruff::Number::Complex { real, imag } => self
&ast::Number::Complex { real, imag } => self
.p(&rustpython_literal::complex::to_string(real, imag)
.replace("inf", inf_str))?,
}
}
Expr::BooleanLiteral(ruff::ExprBooleanLiteral { value, .. }) => {
ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) => {
self.p(if *value { "True" } else { "False" })?
}
Expr::NoneLiteral(ruff::ExprNoneLiteral { .. }) => self.p("None")?,
Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { .. }) => self.p("...")?,
Expr::Attribute(ruff::ExprAttribute { value, attr, .. }) => {
ast::Expr::NoneLiteral(ast::ExprNoneLiteral { .. }) => self.p("None")?,
ast::Expr::EllipsisLiteral(ast::ExprEllipsisLiteral { .. }) => self.p("...")?,
ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
self.unparse_expr(value, precedence::ATOM)?;
let period = if let Expr::NumberLiteral(ruff::ExprNumberLiteral {
value: ruff::Number::Int(_),
let period = if let ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
value: ast::Number::Int(_),
..
}) = value.as_ref()
{
@@ -420,19 +417,19 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.p(period)?;
self.p_id(attr)?;
}
Expr::Subscript(ruff::ExprSubscript { value, slice, .. }) => {
ast::Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
self.unparse_expr(value, precedence::ATOM)?;
let lvl = precedence::TUPLE;
self.p("[")?;
self.unparse_expr(slice, lvl)?;
self.p("]")?;
}
Expr::Starred(ruff::ExprStarred { value, .. }) => {
ast::Expr::Starred(ast::ExprStarred { value, .. }) => {
self.p("*")?;
self.unparse_expr(value, precedence::EXPR)?;
}
Expr::Name(ruff::ExprName { id, .. }) => self.p(id.as_str())?,
Expr::List(ruff::ExprList { elts, .. }) => {
ast::Expr::Name(ast::ExprName { id, .. }) => self.p(id.as_str())?,
ast::Expr::List(ast::ExprList { elts, .. }) => {
self.p("[")?;
let mut first = true;
for elt in elts {
@@ -441,7 +438,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
self.p("]")?;
}
Expr::Tuple(ruff::ExprTuple { elts, .. }) => {
ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => {
if elts.is_empty() {
self.p("()")?;
} else {
@@ -455,7 +452,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
})
}
}
Expr::Slice(ruff::ExprSlice {
ast::Expr::Slice(ast::ExprSlice {
lower,
upper,
step,
@@ -474,12 +471,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.unparse_expr(step, precedence::TEST)?;
}
}
Expr::IpyEscapeCommand(_) => {}
ast::Expr::IpyEscapeCommand(_) => {}
}
Ok(())
}
fn unparse_arguments(&mut self, args: &Parameters) -> fmt::Result {
fn unparse_arguments(&mut self, args: &ast::Parameters) -> fmt::Result {
let mut first = true;
for (i, arg) in args.posonlyargs.iter().chain(&args.args).enumerate() {
self.p_delim(&mut first, ", ")?;
@@ -504,7 +501,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
Ok(())
}
fn unparse_function_arg(&mut self, arg: &ParameterWithDefault) -> fmt::Result {
fn unparse_function_arg(&mut self, arg: &ast::ParameterWithDefault) -> fmt::Result {
self.unparse_arg(&arg.parameter)?;
if let Some(default) = &arg.default {
write!(self, "={}", UnparseExpr::new(default, self.source))?;
@@ -512,7 +509,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
Ok(())
}
fn unparse_arg(&mut self, arg: &Parameter) -> fmt::Result {
fn unparse_arg(&mut self, arg: &ast::Parameter) -> fmt::Result {
self.p_id(&arg.name)?;
if let Some(ann) = &arg.annotation {
write!(self, ": {}", UnparseExpr::new(ann, self.source))?;
@@ -520,7 +517,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
Ok(())
}
fn unparse_comp(&mut self, generators: &[Comprehension]) -> fmt::Result {
fn unparse_comp(&mut self, generators: &[ast::Comprehension]) -> fmt::Result {
for comp in generators {
self.p(if comp.is_async {
" async for "
@@ -538,10 +535,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
Ok(())
}
fn unparse_fstring_body(
&mut self,
elements: &[ruff::InterpolatedStringElement],
) -> fmt::Result {
fn unparse_fstring_body(&mut self, elements: &[ast::InterpolatedStringElement]) -> fmt::Result {
for elem in elements {
self.unparse_fstring_elem(elem)?;
}
@@ -550,15 +544,15 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
fn unparse_formatted(
&mut self,
val: &Expr,
debug_text: Option<&ruff::DebugText>,
conversion: ConversionFlag,
spec: Option<&ruff::InterpolatedStringFormatSpec>,
val: &ast::Expr,
debug_text: Option<&ast::DebugText>,
conversion: ast::ConversionFlag,
spec: Option<&ast::InterpolatedStringFormatSpec>,
) -> fmt::Result {
let buffered = to_string_fmt(|f| {
Unparser::new(f, self.source).unparse_expr(val, precedence::TEST + 1)
});
if let Some(ruff::DebugText { leading, trailing }) = debug_text {
if let Some(ast::DebugText { leading, trailing }) = debug_text {
self.p(leading)?;
self.p(self.source.slice(val.range()))?;
self.p(trailing)?;
@@ -573,7 +567,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.p(&buffered)?;
drop(buffered);
if conversion != ConversionFlag::None {
if conversion != ast::ConversionFlag::None {
self.p("!")?;
let buf = &[conversion as u8];
let c = core::str::from_utf8(buf).unwrap();
@@ -590,9 +584,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
Ok(())
}
fn unparse_fstring_elem(&mut self, elem: &ruff::InterpolatedStringElement) -> fmt::Result {
fn unparse_fstring_elem(&mut self, elem: &ast::InterpolatedStringElement) -> fmt::Result {
match elem {
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
expression,
debug_text,
conversion,
@@ -604,7 +598,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
*conversion,
format_spec.as_deref(),
),
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
value,
..
}) => self.unparse_fstring_str(value),
@@ -616,12 +610,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
self.p(&s)
}
fn unparse_fstring(&mut self, value: &ruff::FStringValue) -> fmt::Result {
fn unparse_fstring(&mut self, value: &ast::FStringValue) -> fmt::Result {
self.p("f")?;
let body = to_string_fmt(|f| {
value.iter().try_for_each(|part| match part {
ruff::FStringPart::Literal(lit) => f.write_str(lit),
ruff::FStringPart::FString(ruff::FString { elements, .. }) => {
ast::FStringPart::Literal(lit) => f.write_str(lit),
ast::FStringPart::FString(ast::FString { elements, .. }) => {
Unparser::new(f, self.source).unparse_fstring_body(elements)
}
})
@@ -634,12 +628,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}
pub struct UnparseExpr<'a> {
expr: &'a Expr,
expr: &'a ast::Expr,
source: &'a SourceFile,
}
impl<'a> UnparseExpr<'a> {
pub const fn new(expr: &'a Expr, source: &'a SourceFile) -> Self {
pub const fn new(expr: &'a ast::Expr, source: &'a SourceFile) -> Self {
Self { expr, source }
}
}
+6 -6
View File
@@ -19,7 +19,7 @@ use crate::{
convert::ToPyObject,
};
use node::Node;
use ruff_python_ast as ruff;
use ruff_python_ast as ast;
use ruff_text_size::{Ranged, TextRange, TextSize};
use rustpython_compiler_core::{
LineIndex, OneIndexed, PositionEncoding, SourceFile, SourceFileBuilder, SourceLocation,
@@ -283,8 +283,8 @@ pub(crate) fn parse(
})?
.into_syntax();
let top = match top {
ruff::Mod::Module(m) => Mod::Module(m),
ruff::Mod::Expression(e) => Mod::Expression(e),
ast::Mod::Module(m) => Mod::Module(m),
ast::Mod::Expression(e) => Mod::Expression(e),
};
Ok(top.ast_to_object(vm, &source_file))
}
@@ -305,13 +305,13 @@ pub(crate) fn compile(
let source_file = SourceFileBuilder::new(filename.to_owned(), "".to_owned()).finish();
let ast: Mod = Node::ast_from_object(vm, &source_file, object)?;
let ast = match ast {
Mod::Module(m) => ruff::Mod::Module(m),
Mod::Interactive(ModInteractive { range, body }) => ruff::Mod::Module(ruff::ModModule {
Mod::Module(m) => ast::Mod::Module(m),
Mod::Interactive(ModInteractive { range, body }) => ast::Mod::Module(ast::ModModule {
node_index: Default::default(),
range,
body,
}),
Mod::Expression(e) => ruff::Mod::Expression(e),
Mod::Expression(e) => ast::Mod::Expression(e),
Mod::FunctionType(_) => todo!(),
};
// TODO: create a textual representation of the ast
+10 -10
View File
@@ -3,7 +3,7 @@ use rustpython_compiler_core::SourceFile;
pub(super) struct PositionalArguments {
pub range: TextRange,
pub args: Box<[ruff::Expr]>,
pub args: Box<[ast::Expr]>,
}
impl Node for PositionalArguments {
@@ -27,7 +27,7 @@ impl Node for PositionalArguments {
pub(super) struct KeywordArguments {
pub range: TextRange,
pub keywords: Box<[ruff::Keyword]>,
pub keywords: Box<[ast::Keyword]>,
}
impl Node for KeywordArguments {
@@ -53,10 +53,10 @@ impl Node for KeywordArguments {
pub(super) fn merge_function_call_arguments(
pos_args: PositionalArguments,
key_args: KeywordArguments,
) -> ruff::Arguments {
) -> ast::Arguments {
let range = pos_args.range.cover(key_args.range);
ruff::Arguments {
ast::Arguments {
node_index: Default::default(),
range,
args: pos_args.args,
@@ -65,9 +65,9 @@ pub(super) fn merge_function_call_arguments(
}
pub(super) fn split_function_call_arguments(
args: ruff::Arguments,
args: ast::Arguments,
) -> (PositionalArguments, KeywordArguments) {
let ruff::Arguments {
let ast::Arguments {
node_index: _,
range: _,
args,
@@ -100,13 +100,13 @@ pub(super) fn split_function_call_arguments(
}
pub(super) fn split_class_def_args(
args: Option<Box<ruff::Arguments>>,
args: Option<Box<ast::Arguments>>,
) -> (Option<PositionalArguments>, Option<KeywordArguments>) {
let args = match args {
None => return (None, None),
Some(args) => *args,
};
let ruff::Arguments {
let ast::Arguments {
node_index: _,
range: _,
args,
@@ -141,7 +141,7 @@ pub(super) fn split_class_def_args(
pub(super) fn merge_class_def_args(
positional_arguments: Option<PositionalArguments>,
keyword_arguments: Option<KeywordArguments>,
) -> Option<Box<ruff::Arguments>> {
) -> Option<Box<ast::Arguments>> {
if positional_arguments.is_none() && keyword_arguments.is_none() {
return None;
}
@@ -157,7 +157,7 @@ pub(super) fn merge_class_def_args(
vec![].into_boxed_slice()
};
Some(Box::new(ruff::Arguments {
Some(Box::new(ast::Arguments {
node_index: Default::default(),
range: Default::default(), // TODO
args,
+2 -2
View File
@@ -2,7 +2,7 @@ use super::*;
use rustpython_codegen::compile::ruff_int_to_bigint;
use rustpython_compiler_core::SourceFile;
impl Node for ruff::Identifier {
impl Node for ast::Identifier {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let id = self.as_str();
vm.ctx.new_str(id).into()
@@ -18,7 +18,7 @@ impl Node for ruff::Identifier {
}
}
impl Node for ruff::Int {
impl Node for ast::Int {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
vm.ctx.new_int(ruff_int_to_bigint(&self).unwrap()).into()
}
+42 -42
View File
@@ -1,6 +1,6 @@
use super::*;
use crate::builtins::{PyComplex, PyFrozenSet, PyTuple};
use ruff::str_prefix::StringLiteralPrefix;
use ast::str_prefix::StringLiteralPrefix;
use rustpython_compiler_core::SourceFile;
#[derive(Debug)]
@@ -22,7 +22,7 @@ impl Constant {
}
}
pub(super) const fn new_int(value: ruff::Int, range: TextRange) -> Self {
pub(super) const fn new_int(value: ast::Int, range: TextRange) -> Self {
Self {
range,
value: ConstantLiteral::Int(value),
@@ -71,7 +71,7 @@ impl Constant {
}
}
pub(crate) fn into_expr(self) -> ruff::Expr {
pub(crate) fn into_expr(self) -> ast::Expr {
constant_to_ruff_expr(self)
}
}
@@ -85,7 +85,7 @@ pub(crate) enum ConstantLiteral {
prefix: StringLiteralPrefix,
},
Bytes(Box<[u8]>),
Int(ruff::Int),
Int(ast::Int),
Tuple(Vec<ConstantLiteral>),
FrozenSet(Vec<ConstantLiteral>),
Float(f64),
@@ -244,48 +244,48 @@ impl Node for ConstantLiteral {
}
}
fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
fn constant_to_ruff_expr(value: Constant) -> ast::Expr {
let Constant { value, range } = value;
match value {
ConstantLiteral::None => ruff::Expr::NoneLiteral(ruff::ExprNoneLiteral {
ConstantLiteral::None => ast::Expr::NoneLiteral(ast::ExprNoneLiteral {
node_index: Default::default(),
range,
}),
ConstantLiteral::Bool(value) => ruff::Expr::BooleanLiteral(ruff::ExprBooleanLiteral {
ConstantLiteral::Bool(value) => ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral {
node_index: Default::default(),
range,
value,
}),
ConstantLiteral::Str { value, prefix } => {
ruff::Expr::StringLiteral(ruff::ExprStringLiteral {
ast::Expr::StringLiteral(ast::ExprStringLiteral {
node_index: Default::default(),
range,
value: ruff::StringLiteralValue::single(ruff::StringLiteral {
value: ast::StringLiteralValue::single(ast::StringLiteral {
node_index: Default::default(),
range,
value,
flags: ruff::StringLiteralFlags::empty().with_prefix(prefix),
flags: ast::StringLiteralFlags::empty().with_prefix(prefix),
}),
})
}
ConstantLiteral::Bytes(value) => {
ruff::Expr::BytesLiteral(ruff::ExprBytesLiteral {
ast::Expr::BytesLiteral(ast::ExprBytesLiteral {
node_index: Default::default(),
range,
value: ruff::BytesLiteralValue::single(ruff::BytesLiteral {
value: ast::BytesLiteralValue::single(ast::BytesLiteral {
node_index: Default::default(),
range,
value,
flags: ruff::BytesLiteralFlags::empty(), // TODO
flags: ast::BytesLiteralFlags::empty(), // TODO
}),
})
}
ConstantLiteral::Int(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral {
ConstantLiteral::Int(value) => ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
node_index: Default::default(),
range,
value: ruff::Number::Int(value),
value: ast::Number::Int(value),
}),
ConstantLiteral::Tuple(value) => ruff::Expr::Tuple(ruff::ExprTuple {
ConstantLiteral::Tuple(value) => ast::Expr::Tuple(ast::ExprTuple {
node_index: Default::default(),
range,
elts: value
@@ -297,21 +297,21 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
})
})
.collect(),
ctx: ruff::ExprContext::Load,
ctx: ast::ExprContext::Load,
// TODO: Does this matter?
parenthesized: true,
}),
ConstantLiteral::FrozenSet(value) => ruff::Expr::Call(ruff::ExprCall {
ConstantLiteral::FrozenSet(value) => ast::Expr::Call(ast::ExprCall {
node_index: Default::default(),
range,
// idk lol
func: Box::new(ruff::Expr::Name(ruff::ExprName {
func: Box::new(ast::Expr::Name(ast::ExprName {
node_index: Default::default(),
range: TextRange::default(),
id: ruff::name::Name::new_static("frozenset"),
ctx: ruff::ExprContext::Load,
id: ast::name::Name::new_static("frozenset"),
ctx: ast::ExprContext::Load,
})),
arguments: ruff::Arguments {
arguments: ast::Arguments {
node_index: Default::default(),
range,
args: value
@@ -326,19 +326,19 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
keywords: Box::default(),
},
}),
ConstantLiteral::Float(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral {
ConstantLiteral::Float(value) => ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
node_index: Default::default(),
range,
value: ruff::Number::Float(value),
value: ast::Number::Float(value),
}),
ConstantLiteral::Complex { real, imag } => {
ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral {
ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
node_index: Default::default(),
range,
value: ruff::Number::Complex { real, imag },
value: ast::Number::Complex { real, imag },
})
}
ConstantLiteral::Ellipsis => ruff::Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral {
ConstantLiteral::Ellipsis => ast::Expr::EllipsisLiteral(ast::ExprEllipsisLiteral {
node_index: Default::default(),
range,
}),
@@ -348,17 +348,17 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
pub(super) fn number_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprNumberLiteral,
constant: ast::ExprNumberLiteral,
) -> PyObjectRef {
let ruff::ExprNumberLiteral {
let ast::ExprNumberLiteral {
node_index: _,
range,
value,
} = constant;
let c = match value {
ruff::Number::Int(n) => Constant::new_int(n, range),
ruff::Number::Float(n) => Constant::new_float(n, range),
ruff::Number::Complex { real, imag } => Constant::new_complex(real, imag, range),
ast::Number::Int(n) => Constant::new_int(n, range),
ast::Number::Float(n) => Constant::new_float(n, range),
ast::Number::Complex { real, imag } => Constant::new_complex(real, imag, range),
};
c.ast_to_object(vm, source_file)
}
@@ -366,9 +366,9 @@ pub(super) fn number_literal_to_object(
pub(super) fn string_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprStringLiteral,
constant: ast::ExprStringLiteral,
) -> PyObjectRef {
let ruff::ExprStringLiteral {
let ast::ExprStringLiteral {
node_index: _,
range,
value,
@@ -384,9 +384,9 @@ pub(super) fn string_literal_to_object(
pub(super) fn bytes_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprBytesLiteral,
constant: ast::ExprBytesLiteral,
) -> PyObjectRef {
let ruff::ExprBytesLiteral {
let ast::ExprBytesLiteral {
node_index: _,
range,
value,
@@ -399,9 +399,9 @@ pub(super) fn bytes_literal_to_object(
pub(super) fn boolean_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprBooleanLiteral,
constant: ast::ExprBooleanLiteral,
) -> PyObjectRef {
let ruff::ExprBooleanLiteral {
let ast::ExprBooleanLiteral {
node_index: _,
range,
value,
@@ -413,9 +413,9 @@ pub(super) fn boolean_literal_to_object(
pub(super) fn none_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprNoneLiteral,
constant: ast::ExprNoneLiteral,
) -> PyObjectRef {
let ruff::ExprNoneLiteral {
let ast::ExprNoneLiteral {
node_index: _,
range,
} = constant;
@@ -426,9 +426,9 @@ pub(super) fn none_literal_to_object(
pub(super) fn ellipsis_literal_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
constant: ruff::ExprEllipsisLiteral,
constant: ast::ExprEllipsisLiteral,
) -> PyObjectRef {
let ruff::ExprEllipsisLiteral {
let ast::ExprEllipsisLiteral {
node_index: _,
range,
} = constant;
+10 -10
View File
@@ -2,12 +2,12 @@ use super::*;
use rustpython_compiler_core::SourceFile;
pub(super) fn ast_to_object(
clause: ruff::ElifElseClause,
mut rest: alloc::vec::IntoIter<ruff::ElifElseClause>,
clause: ast::ElifElseClause,
mut rest: alloc::vec::IntoIter<ast::ElifElseClause>,
vm: &VirtualMachine,
source_file: &SourceFile,
) -> PyObjectRef {
let ruff::ElifElseClause {
let ast::ElifElseClause {
node_index: _,
range,
test,
@@ -48,18 +48,18 @@ pub(super) fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<ruff::StmtIf> {
) -> PyResult<ast::StmtIf> {
let test = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "test", "If")?)?;
let body = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "body", "If")?)?;
let orelse: Vec<ruff::Stmt> = Node::ast_from_object(
let orelse: Vec<ast::Stmt> = Node::ast_from_object(
vm,
source_file,
get_node_field(vm, &object, "orelse", "If")?,
)?;
let range = range_from_object(vm, source_file, object, "If")?;
let elif_else_clauses = if let [ruff::Stmt::If(_)] = &*orelse {
let Some(ruff::Stmt::If(ruff::StmtIf {
let elif_else_clauses = if let [ast::Stmt::If(_)] = &*orelse {
let Some(ast::Stmt::If(ast::StmtIf {
node_index: _,
range,
test,
@@ -71,7 +71,7 @@ pub(super) fn ast_from_object(
};
elif_else_clauses.insert(
0,
ruff::ElifElseClause {
ast::ElifElseClause {
node_index: Default::default(),
range,
test: Some(*test),
@@ -80,7 +80,7 @@ pub(super) fn ast_from_object(
);
elif_else_clauses
} else {
vec![ruff::ElifElseClause {
vec![ast::ElifElseClause {
node_index: Default::default(),
range,
test: None,
@@ -88,7 +88,7 @@ pub(super) fn ast_from_object(
}]
};
Ok(ruff::StmtIf {
Ok(ast::StmtIf {
node_index: Default::default(),
test,
body,
+3 -3
View File
@@ -2,7 +2,7 @@ use super::*;
use rustpython_compiler_core::SourceFile;
// sum
impl Node for ruff::ExceptHandler {
impl Node for ast::ExceptHandler {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Self::ExceptHandler(cons) => cons.ast_to_object(vm, source_file),
@@ -16,7 +16,7 @@ impl Node for ruff::ExceptHandler {
let _cls = _object.class();
Ok(
if _cls.is(pyast::NodeExceptHandlerExceptHandler::static_type()) {
Self::ExceptHandler(ruff::ExceptHandlerExceptHandler::ast_from_object(
Self::ExceptHandler(ast::ExceptHandlerExceptHandler::ast_from_object(
_vm,
source_file,
_object,
@@ -32,7 +32,7 @@ impl Node for ruff::ExceptHandler {
}
// constructor
impl Node for ruff::ExceptHandlerExceptHandler {
impl Node for ast::ExceptHandlerExceptHandler {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
+53 -61
View File
@@ -7,7 +7,7 @@ use crate::stdlib::ast::{
use rustpython_compiler_core::SourceFile;
// sum
impl Node for ruff::Expr {
impl Node for ast::Expr {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Self::BoolOp(cons) => cons.ast_to_object(vm, source_file),
@@ -59,77 +59,69 @@ impl Node for ruff::Expr {
) -> PyResult<Self> {
let cls = object.class();
Ok(if cls.is(pyast::NodeExprBoolOp::static_type()) {
Self::BoolOp(ruff::ExprBoolOp::ast_from_object(vm, source_file, object)?)
Self::BoolOp(ast::ExprBoolOp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprNamedExpr::static_type()) {
Self::Named(ruff::ExprNamed::ast_from_object(vm, source_file, object)?)
Self::Named(ast::ExprNamed::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprBinOp::static_type()) {
Self::BinOp(ruff::ExprBinOp::ast_from_object(vm, source_file, object)?)
Self::BinOp(ast::ExprBinOp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprUnaryOp::static_type()) {
Self::UnaryOp(ruff::ExprUnaryOp::ast_from_object(vm, source_file, object)?)
Self::UnaryOp(ast::ExprUnaryOp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprLambda::static_type()) {
Self::Lambda(ruff::ExprLambda::ast_from_object(vm, source_file, object)?)
Self::Lambda(ast::ExprLambda::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprIfExp::static_type()) {
Self::If(ruff::ExprIf::ast_from_object(vm, source_file, object)?)
Self::If(ast::ExprIf::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprDict::static_type()) {
Self::Dict(ruff::ExprDict::ast_from_object(vm, source_file, object)?)
Self::Dict(ast::ExprDict::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprSet::static_type()) {
Self::Set(ruff::ExprSet::ast_from_object(vm, source_file, object)?)
Self::Set(ast::ExprSet::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprListComp::static_type()) {
Self::ListComp(ruff::ExprListComp::ast_from_object(
vm,
source_file,
object,
)?)
Self::ListComp(ast::ExprListComp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprSetComp::static_type()) {
Self::SetComp(ruff::ExprSetComp::ast_from_object(vm, source_file, object)?)
Self::SetComp(ast::ExprSetComp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprDictComp::static_type()) {
Self::DictComp(ruff::ExprDictComp::ast_from_object(
vm,
source_file,
object,
)?)
Self::DictComp(ast::ExprDictComp::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprGeneratorExp::static_type()) {
Self::Generator(ruff::ExprGenerator::ast_from_object(
Self::Generator(ast::ExprGenerator::ast_from_object(
vm,
source_file,
object,
)?)
} else if cls.is(pyast::NodeExprAwait::static_type()) {
Self::Await(ruff::ExprAwait::ast_from_object(vm, source_file, object)?)
Self::Await(ast::ExprAwait::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprYield::static_type()) {
Self::Yield(ruff::ExprYield::ast_from_object(vm, source_file, object)?)
Self::Yield(ast::ExprYield::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprYieldFrom::static_type()) {
Self::YieldFrom(ruff::ExprYieldFrom::ast_from_object(
Self::YieldFrom(ast::ExprYieldFrom::ast_from_object(
vm,
source_file,
object,
)?)
} else if cls.is(pyast::NodeExprCompare::static_type()) {
Self::Compare(ruff::ExprCompare::ast_from_object(vm, source_file, object)?)
Self::Compare(ast::ExprCompare::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprCall::static_type()) {
Self::Call(ruff::ExprCall::ast_from_object(vm, source_file, object)?)
Self::Call(ast::ExprCall::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprAttribute::static_type()) {
Self::Attribute(ruff::ExprAttribute::ast_from_object(
Self::Attribute(ast::ExprAttribute::ast_from_object(
vm,
source_file,
object,
)?)
} else if cls.is(pyast::NodeExprSubscript::static_type()) {
Self::Subscript(ruff::ExprSubscript::ast_from_object(
Self::Subscript(ast::ExprSubscript::ast_from_object(
vm,
source_file,
object,
)?)
} else if cls.is(pyast::NodeExprStarred::static_type()) {
Self::Starred(ruff::ExprStarred::ast_from_object(vm, source_file, object)?)
Self::Starred(ast::ExprStarred::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprName::static_type()) {
Self::Name(ruff::ExprName::ast_from_object(vm, source_file, object)?)
Self::Name(ast::ExprName::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprList::static_type()) {
Self::List(ruff::ExprList::ast_from_object(vm, source_file, object)?)
Self::List(ast::ExprList::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprTuple::static_type()) {
Self::Tuple(ruff::ExprTuple::ast_from_object(vm, source_file, object)?)
Self::Tuple(ast::ExprTuple::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprSlice::static_type()) {
Self::Slice(ruff::ExprSlice::ast_from_object(vm, source_file, object)?)
Self::Slice(ast::ExprSlice::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeExprConstant::static_type()) {
Constant::ast_from_object(vm, source_file, object)?.into_expr()
} else if cls.is(pyast::NodeExprJoinedStr::static_type()) {
@@ -144,7 +136,7 @@ impl Node for ruff::Expr {
}
// constructor
impl Node for ruff::ExprBoolOp {
impl Node for ast::ExprBoolOp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -187,7 +179,7 @@ impl Node for ruff::ExprBoolOp {
}
// constructor
impl Node for ruff::ExprNamed {
impl Node for ast::ExprNamed {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -230,7 +222,7 @@ impl Node for ruff::ExprNamed {
}
// constructor
impl Node for ruff::ExprBinOp {
impl Node for ast::ExprBinOp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -281,7 +273,7 @@ impl Node for ruff::ExprBinOp {
}
// constructor
impl Node for ruff::ExprUnaryOp {
impl Node for ast::ExprUnaryOp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -323,7 +315,7 @@ impl Node for ruff::ExprUnaryOp {
}
// constructor
impl Node for ruff::ExprLambda {
impl Node for ast::ExprLambda {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -366,7 +358,7 @@ impl Node for ruff::ExprLambda {
}
// constructor
impl Node for ruff::ExprIf {
impl Node for ast::ExprIf {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -417,7 +409,7 @@ impl Node for ruff::ExprIf {
}
// constructor
impl Node for ruff::ExprDict {
impl Node for ast::ExprDict {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -449,7 +441,7 @@ impl Node for ruff::ExprDict {
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
let keys: Vec<Option<ruff::Expr>> = Node::ast_from_object(
let keys: Vec<Option<ast::Expr>> = Node::ast_from_object(
vm,
source_file,
get_node_field(vm, &object, "keys", "Dict")?,
@@ -462,7 +454,7 @@ impl Node for ruff::ExprDict {
let items = keys
.into_iter()
.zip(values)
.map(|(key, value)| ruff::DictItem { key, value })
.map(|(key, value)| ast::DictItem { key, value })
.collect();
Ok(Self {
node_index: Default::default(),
@@ -473,7 +465,7 @@ impl Node for ruff::ExprDict {
}
// constructor
impl Node for ruff::ExprSet {
impl Node for ast::ExprSet {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -507,7 +499,7 @@ impl Node for ruff::ExprSet {
}
// constructor
impl Node for ruff::ExprListComp {
impl Node for ast::ExprListComp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -550,7 +542,7 @@ impl Node for ruff::ExprListComp {
}
// constructor
impl Node for ruff::ExprSetComp {
impl Node for ast::ExprSetComp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -593,7 +585,7 @@ impl Node for ruff::ExprSetComp {
}
// constructor
impl Node for ruff::ExprDictComp {
impl Node for ast::ExprDictComp {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -644,7 +636,7 @@ impl Node for ruff::ExprDictComp {
}
// constructor
impl Node for ruff::ExprGenerator {
impl Node for ast::ExprGenerator {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -690,7 +682,7 @@ impl Node for ruff::ExprGenerator {
}
// constructor
impl Node for ruff::ExprAwait {
impl Node for ast::ExprAwait {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -724,7 +716,7 @@ impl Node for ruff::ExprAwait {
}
// constructor
impl Node for ruff::ExprYield {
impl Node for ast::ExprYield {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -757,7 +749,7 @@ impl Node for ruff::ExprYield {
}
// constructor
impl Node for ruff::ExprYieldFrom {
impl Node for ast::ExprYieldFrom {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -792,7 +784,7 @@ impl Node for ruff::ExprYieldFrom {
}
// constructor
impl Node for ruff::ExprCompare {
impl Node for ast::ExprCompare {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -853,7 +845,7 @@ impl Node for ruff::ExprCompare {
}
// constructor
impl Node for ruff::ExprCall {
impl Node for ast::ExprCall {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -914,7 +906,7 @@ impl Node for ruff::ExprCall {
}
// constructor
impl Node for ruff::ExprAttribute {
impl Node for ast::ExprAttribute {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -965,7 +957,7 @@ impl Node for ruff::ExprAttribute {
}
// constructor
impl Node for ruff::ExprSubscript {
impl Node for ast::ExprSubscript {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1015,7 +1007,7 @@ impl Node for ruff::ExprSubscript {
}
// constructor
impl Node for ruff::ExprStarred {
impl Node for ast::ExprStarred {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1057,7 +1049,7 @@ impl Node for ruff::ExprStarred {
}
// constructor
impl Node for ruff::ExprName {
impl Node for ast::ExprName {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1095,7 +1087,7 @@ impl Node for ruff::ExprName {
}
// constructor
impl Node for ruff::ExprList {
impl Node for ast::ExprList {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1138,7 +1130,7 @@ impl Node for ruff::ExprList {
}
// constructor
impl Node for ruff::ExprTuple {
impl Node for ast::ExprTuple {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1183,7 +1175,7 @@ impl Node for ruff::ExprTuple {
}
// constructor
impl Node for ruff::ExprSlice {
impl Node for ast::ExprSlice {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1228,7 +1220,7 @@ impl Node for ruff::ExprSlice {
}
// sum
impl Node for ruff::ExprContext {
impl Node for ast::ExprContext {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let node_type = match self {
Self::Load => pyast::NodeExprContextLoad::static_type(),
@@ -1266,7 +1258,7 @@ impl Node for ruff::ExprContext {
}
// product
impl Node for ruff::Comprehension {
impl Node for ast::Comprehension {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
+9 -9
View File
@@ -18,9 +18,9 @@ use rustpython_compiler_core::SourceFile;
/// - `FunctionType`: A function signature with argument and return type
/// annotations, representing the type hints of a function (e.g., `def add(x: int, y: int) -> int`).
pub(super) enum Mod {
Module(ruff::ModModule),
Module(ast::ModModule),
Interactive(ModInteractive),
Expression(ruff::ModExpression),
Expression(ast::ModExpression),
FunctionType(ModFunctionType),
}
@@ -42,11 +42,11 @@ impl Node for Mod {
) -> PyResult<Self> {
let cls = object.class();
Ok(if cls.is(pyast::NodeModModule::static_type()) {
Self::Module(ruff::ModModule::ast_from_object(vm, source_file, object)?)
Self::Module(ast::ModModule::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeModInteractive::static_type()) {
Self::Interactive(ModInteractive::ast_from_object(vm, source_file, object)?)
} else if cls.is(pyast::NodeModExpression::static_type()) {
Self::Expression(ruff::ModExpression::ast_from_object(
Self::Expression(ast::ModExpression::ast_from_object(
vm,
source_file,
object,
@@ -63,7 +63,7 @@ impl Node for Mod {
}
// constructor
impl Node for ruff::ModModule {
impl Node for ast::ModModule {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -113,7 +113,7 @@ impl Node for ruff::ModModule {
pub(super) struct ModInteractive {
pub(crate) range: TextRange,
pub(crate) body: Vec<ruff::Stmt>,
pub(crate) body: Vec<ast::Stmt>,
}
// constructor
@@ -147,7 +147,7 @@ impl Node for ModInteractive {
}
// constructor
impl Node for ruff::ModExpression {
impl Node for ast::ModExpression {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -182,8 +182,8 @@ impl Node for ruff::ModExpression {
}
pub(super) struct ModFunctionType {
pub(crate) argtypes: Box<[ruff::Expr]>,
pub(crate) returns: ruff::Expr,
pub(crate) argtypes: Box<[ast::Expr]>,
pub(crate) returns: ast::Expr,
pub(crate) range: TextRange,
}
+4 -4
View File
@@ -2,7 +2,7 @@ use super::*;
use rustpython_compiler_core::SourceFile;
// sum
impl Node for ruff::BoolOp {
impl Node for ast::BoolOp {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let node_type = match self {
Self::And => pyast::NodeBoolOpAnd::static_type(),
@@ -34,7 +34,7 @@ impl Node for ruff::BoolOp {
}
// sum
impl Node for ruff::Operator {
impl Node for ast::Operator {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let node_type = match self {
Self::Add => pyast::NodeOperatorAdd::static_type(),
@@ -99,7 +99,7 @@ impl Node for ruff::Operator {
}
// sum
impl Node for ruff::UnaryOp {
impl Node for ast::UnaryOp {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let node_type = match self {
Self::Invert => pyast::NodeUnaryOpInvert::static_type(),
@@ -137,7 +137,7 @@ impl Node for ruff::UnaryOp {
}
// sum
impl Node for ruff::CmpOp {
impl Node for ast::CmpOp {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
let node_type = match self {
Self::Eq => pyast::NodeCmpOpEq::static_type(),
+7 -7
View File
@@ -1,7 +1,7 @@
use super::*;
use rustpython_compiler_core::SourceFile;
impl Node for ruff::ConversionFlag {
impl Node for ast::ConversionFlag {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
vm.ctx.new_int(self as u8).into()
}
@@ -24,7 +24,7 @@ impl Node for ruff::ConversionFlag {
}
// /// This is just a string, not strictly an AST node. But it makes AST conversions easier.
impl Node for ruff::name::Name {
impl Node for ast::name::Name {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
vm.ctx.new_str(self.as_str()).to_pyobject(vm)
}
@@ -41,9 +41,9 @@ impl Node for ruff::name::Name {
}
}
impl Node for ruff::Decorator {
impl Node for ast::Decorator {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
ruff::Expr::ast_to_object(self.expression, vm, source_file)
ast::Expr::ast_to_object(self.expression, vm, source_file)
}
fn ast_from_object(
@@ -51,7 +51,7 @@ impl Node for ruff::Decorator {
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
let expression = ruff::Expr::ast_from_object(vm, source_file, object)?;
let expression = ast::Expr::ast_from_object(vm, source_file, object)?;
let range = expression.range();
Ok(Self {
node_index: Default::default(),
@@ -62,7 +62,7 @@ impl Node for ruff::Decorator {
}
// product
impl Node for ruff::Alias {
impl Node for ast::Alias {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -103,7 +103,7 @@ impl Node for ruff::Alias {
}
// product
impl Node for ruff::WithItem {
impl Node for ast::WithItem {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
+15 -15
View File
@@ -2,7 +2,7 @@ use super::*;
use rustpython_compiler_core::SourceFile;
// product
impl Node for ruff::Parameters {
impl Node for ast::Parameters {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -101,7 +101,7 @@ impl Node for ruff::Parameters {
}
// product
impl Node for ruff::Parameter {
impl Node for ast::Parameter {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -156,7 +156,7 @@ impl Node for ruff::Parameter {
}
// product
impl Node for ruff::Keyword {
impl Node for ast::Keyword {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -197,7 +197,7 @@ impl Node for ruff::Keyword {
struct PositionalParameters {
pub _range: TextRange, // TODO: Use this
pub args: Box<[ruff::Parameter]>,
pub args: Box<[ast::Parameter]>,
}
impl Node for PositionalParameters {
@@ -220,7 +220,7 @@ impl Node for PositionalParameters {
struct KeywordParameters {
pub _range: TextRange, // TODO: Use this
pub keywords: Box<[ruff::Parameter]>,
pub keywords: Box<[ast::Parameter]>,
}
impl Node for KeywordParameters {
@@ -243,7 +243,7 @@ impl Node for KeywordParameters {
struct ParameterDefaults {
pub _range: TextRange, // TODO: Use this
defaults: Box<[Option<Box<ruff::Expr>>]>,
defaults: Box<[Option<Box<ast::Expr>>]>,
}
impl Node for ParameterDefaults {
@@ -265,8 +265,8 @@ impl Node for ParameterDefaults {
}
fn extract_positional_parameter_defaults(
pos_only_args: Vec<ruff::ParameterWithDefault>,
args: Vec<ruff::ParameterWithDefault>,
pos_only_args: Vec<ast::ParameterWithDefault>,
args: Vec<ast::ParameterWithDefault>,
) -> (
PositionalParameters,
PositionalParameters,
@@ -325,15 +325,15 @@ fn merge_positional_parameter_defaults(
args: PositionalParameters,
defaults: ParameterDefaults,
) -> (
Vec<ruff::ParameterWithDefault>,
Vec<ruff::ParameterWithDefault>,
Vec<ast::ParameterWithDefault>,
Vec<ast::ParameterWithDefault>,
) {
let posonlyargs = posonlyargs.args;
let args = args.args;
let defaults = defaults.defaults;
let mut posonlyargs: Vec<_> = <Box<[_]> as IntoIterator>::into_iter(posonlyargs)
.map(|parameter| ruff::ParameterWithDefault {
.map(|parameter| ast::ParameterWithDefault {
node_index: Default::default(),
range: Default::default(),
parameter,
@@ -341,7 +341,7 @@ fn merge_positional_parameter_defaults(
})
.collect();
let mut args: Vec<_> = <Box<[_]> as IntoIterator>::into_iter(args)
.map(|parameter| ruff::ParameterWithDefault {
.map(|parameter| ast::ParameterWithDefault {
node_index: Default::default(),
range: Default::default(),
parameter,
@@ -366,7 +366,7 @@ fn merge_positional_parameter_defaults(
}
fn extract_keyword_parameter_defaults(
kw_only_args: Vec<ruff::ParameterWithDefault>,
kw_only_args: Vec<ast::ParameterWithDefault>,
) -> (KeywordParameters, ParameterDefaults) {
let mut defaults = vec![];
defaults.extend(kw_only_args.iter().map(|item| item.default.clone()));
@@ -402,9 +402,9 @@ fn extract_keyword_parameter_defaults(
fn merge_keyword_parameter_defaults(
kw_only_args: KeywordParameters,
defaults: ParameterDefaults,
) -> Vec<ruff::ParameterWithDefault> {
) -> Vec<ast::ParameterWithDefault> {
core::iter::zip(kw_only_args.keywords, defaults.defaults)
.map(|(parameter, default)| ruff::ParameterWithDefault {
.map(|(parameter, default)| ast::ParameterWithDefault {
node_index: Default::default(),
parameter,
default,
+32 -32
View File
@@ -2,7 +2,7 @@ use super::*;
use rustpython_compiler_core::SourceFile;
// product
impl Node for ruff::MatchCase {
impl Node for ast::MatchCase {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -50,7 +50,7 @@ impl Node for ruff::MatchCase {
}
// sum
impl Node for ruff::Pattern {
impl Node for ast::Pattern {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Self::MatchValue(cons) => cons.ast_to_object(vm, source_file),
@@ -70,49 +70,49 @@ impl Node for ruff::Pattern {
) -> PyResult<Self> {
let _cls = _object.class();
Ok(if _cls.is(pyast::NodePatternMatchValue::static_type()) {
Self::MatchValue(ruff::PatternMatchValue::ast_from_object(
Self::MatchValue(ast::PatternMatchValue::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchSingleton::static_type()) {
Self::MatchSingleton(ruff::PatternMatchSingleton::ast_from_object(
Self::MatchSingleton(ast::PatternMatchSingleton::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchSequence::static_type()) {
Self::MatchSequence(ruff::PatternMatchSequence::ast_from_object(
Self::MatchSequence(ast::PatternMatchSequence::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchMapping::static_type()) {
Self::MatchMapping(ruff::PatternMatchMapping::ast_from_object(
Self::MatchMapping(ast::PatternMatchMapping::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchClass::static_type()) {
Self::MatchClass(ruff::PatternMatchClass::ast_from_object(
Self::MatchClass(ast::PatternMatchClass::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchStar::static_type()) {
Self::MatchStar(ruff::PatternMatchStar::ast_from_object(
Self::MatchStar(ast::PatternMatchStar::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchAs::static_type()) {
Self::MatchAs(ruff::PatternMatchAs::ast_from_object(
Self::MatchAs(ast::PatternMatchAs::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodePatternMatchOr::static_type()) {
Self::MatchOr(ruff::PatternMatchOr::ast_from_object(
Self::MatchOr(ast::PatternMatchOr::ast_from_object(
_vm,
source_file,
_object,
@@ -126,7 +126,7 @@ impl Node for ruff::Pattern {
}
}
// constructor
impl Node for ruff::PatternMatchValue {
impl Node for ast::PatternMatchValue {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -161,7 +161,7 @@ impl Node for ruff::PatternMatchValue {
}
// constructor
impl Node for ruff::PatternMatchSingleton {
impl Node for ast::PatternMatchSingleton {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -198,12 +198,12 @@ impl Node for ruff::PatternMatchSingleton {
}
}
impl Node for ruff::Singleton {
impl Node for ast::Singleton {
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
match self {
ruff::Singleton::None => vm.ctx.none(),
ruff::Singleton::True => vm.ctx.new_bool(true).into(),
ruff::Singleton::False => vm.ctx.new_bool(false).into(),
ast::Singleton::None => vm.ctx.none(),
ast::Singleton::True => vm.ctx.new_bool(true).into(),
ast::Singleton::False => vm.ctx.new_bool(false).into(),
}
}
@@ -213,11 +213,11 @@ impl Node for ruff::Singleton {
object: PyObjectRef,
) -> PyResult<Self> {
if vm.is_none(&object) {
Ok(ruff::Singleton::None)
Ok(ast::Singleton::None)
} else if object.is(&vm.ctx.true_value) {
Ok(ruff::Singleton::True)
Ok(ast::Singleton::True)
} else if object.is(&vm.ctx.false_value) {
Ok(ruff::Singleton::False)
Ok(ast::Singleton::False)
} else {
Err(vm.new_value_error(format!(
"Expected None, True, or False, got {:?}",
@@ -228,7 +228,7 @@ impl Node for ruff::Singleton {
}
// constructor
impl Node for ruff::PatternMatchSequence {
impl Node for ast::PatternMatchSequence {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -266,7 +266,7 @@ impl Node for ruff::PatternMatchSequence {
}
// constructor
impl Node for ruff::PatternMatchMapping {
impl Node for ast::PatternMatchMapping {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -318,7 +318,7 @@ impl Node for ruff::PatternMatchMapping {
}
// constructor
impl Node for ruff::PatternMatchClass {
impl Node for ast::PatternMatchClass {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -377,7 +377,7 @@ impl Node for ruff::PatternMatchClass {
get_node_field(vm, &object, "cls", "MatchClass")?,
)?,
range: range_from_object(vm, source_file, object, "MatchClass")?,
arguments: ruff::PatternArguments {
arguments: ast::PatternArguments {
node_index: Default::default(),
range: Default::default(),
patterns,
@@ -387,7 +387,7 @@ impl Node for ruff::PatternMatchClass {
}
}
struct PatternMatchClassPatterns(Vec<ruff::Pattern>);
struct PatternMatchClassPatterns(Vec<ast::Pattern>);
impl Node for PatternMatchClassPatterns {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
@@ -403,7 +403,7 @@ impl Node for PatternMatchClassPatterns {
}
}
struct PatternMatchClassKeywordAttributes(Vec<ruff::Identifier>);
struct PatternMatchClassKeywordAttributes(Vec<ast::Identifier>);
impl Node for PatternMatchClassKeywordAttributes {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
@@ -419,7 +419,7 @@ impl Node for PatternMatchClassKeywordAttributes {
}
}
struct PatternMatchClassKeywordPatterns(Vec<ruff::Pattern>);
struct PatternMatchClassKeywordPatterns(Vec<ast::Pattern>);
impl Node for PatternMatchClassKeywordPatterns {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
@@ -435,7 +435,7 @@ impl Node for PatternMatchClassKeywordPatterns {
}
}
// constructor
impl Node for ruff::PatternMatchStar {
impl Node for ast::PatternMatchStar {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -468,7 +468,7 @@ impl Node for ruff::PatternMatchStar {
}
// constructor
impl Node for ruff::PatternMatchAs {
impl Node for ast::PatternMatchAs {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -507,7 +507,7 @@ impl Node for ruff::PatternMatchAs {
}
// constructor
impl Node for ruff::PatternMatchOr {
impl Node for ast::PatternMatchOr {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -541,7 +541,7 @@ impl Node for ruff::PatternMatchOr {
}
fn split_pattern_match_class(
arguments: ruff::PatternArguments,
arguments: ast::PatternArguments,
) -> (
PatternMatchClassPatterns,
PatternMatchClassKeywordAttributes,
@@ -562,12 +562,12 @@ fn merge_pattern_match_class(
patterns: PatternMatchClassPatterns,
kwd_attrs: PatternMatchClassKeywordAttributes,
kwd_patterns: PatternMatchClassKeywordPatterns,
) -> (Vec<ruff::Pattern>, Vec<ruff::PatternKeyword>) {
) -> (Vec<ast::Pattern>, Vec<ast::PatternKeyword>) {
let keywords = kwd_attrs
.0
.into_iter()
.zip(kwd_patterns.0)
.map(|(attr, pattern)| ruff::PatternKeyword {
.map(|(attr, pattern)| ast::PatternKeyword {
range: Default::default(),
node_index: Default::default(),
attr,
+54 -78
View File
@@ -3,7 +3,7 @@ use crate::stdlib::ast::argument::{merge_class_def_args, split_class_def_args};
use rustpython_compiler_core::SourceFile;
// sum
impl Node for ruff::Stmt {
impl Node for ast::Stmt {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Self::FunctionDef(cons) => cons.ast_to_object(vm, source_file),
@@ -44,117 +44,93 @@ impl Node for ruff::Stmt {
) -> PyResult<Self> {
let _cls = _object.class();
Ok(if _cls.is(pyast::NodeStmtFunctionDef::static_type()) {
Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object(
Self::FunctionDef(ast::StmtFunctionDef::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtAsyncFunctionDef::static_type()) {
Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object(
Self::FunctionDef(ast::StmtFunctionDef::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtClassDef::static_type()) {
Self::ClassDef(ruff::StmtClassDef::ast_from_object(
Self::ClassDef(ast::StmtClassDef::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtReturn::static_type()) {
Self::Return(ruff::StmtReturn::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Return(ast::StmtReturn::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtDelete::static_type()) {
Self::Delete(ruff::StmtDelete::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Delete(ast::StmtDelete::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtAssign::static_type()) {
Self::Assign(ruff::StmtAssign::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Assign(ast::StmtAssign::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtTypeAlias::static_type()) {
Self::TypeAlias(ruff::StmtTypeAlias::ast_from_object(
Self::TypeAlias(ast::StmtTypeAlias::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtAugAssign::static_type()) {
Self::AugAssign(ruff::StmtAugAssign::ast_from_object(
Self::AugAssign(ast::StmtAugAssign::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtAnnAssign::static_type()) {
Self::AnnAssign(ruff::StmtAnnAssign::ast_from_object(
Self::AnnAssign(ast::StmtAnnAssign::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtFor::static_type()) {
Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?)
Self::For(ast::StmtFor::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtAsyncFor::static_type()) {
Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?)
Self::For(ast::StmtFor::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtWhile::static_type()) {
Self::While(ruff::StmtWhile::ast_from_object(_vm, source_file, _object)?)
Self::While(ast::StmtWhile::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtIf::static_type()) {
Self::If(ruff::StmtIf::ast_from_object(_vm, source_file, _object)?)
Self::If(ast::StmtIf::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtWith::static_type()) {
Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?)
Self::With(ast::StmtWith::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtAsyncWith::static_type()) {
Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?)
Self::With(ast::StmtWith::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtMatch::static_type()) {
Self::Match(ruff::StmtMatch::ast_from_object(_vm, source_file, _object)?)
Self::Match(ast::StmtMatch::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtRaise::static_type()) {
Self::Raise(ruff::StmtRaise::ast_from_object(_vm, source_file, _object)?)
Self::Raise(ast::StmtRaise::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtTry::static_type()) {
Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?)
Self::Try(ast::StmtTry::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtTryStar::static_type()) {
Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?)
Self::Try(ast::StmtTry::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtAssert::static_type()) {
Self::Assert(ruff::StmtAssert::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Assert(ast::StmtAssert::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtImport::static_type()) {
Self::Import(ruff::StmtImport::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Import(ast::StmtImport::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtImportFrom::static_type()) {
Self::ImportFrom(ruff::StmtImportFrom::ast_from_object(
Self::ImportFrom(ast::StmtImportFrom::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtGlobal::static_type()) {
Self::Global(ruff::StmtGlobal::ast_from_object(
_vm,
source_file,
_object,
)?)
Self::Global(ast::StmtGlobal::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtNonlocal::static_type()) {
Self::Nonlocal(ruff::StmtNonlocal::ast_from_object(
Self::Nonlocal(ast::StmtNonlocal::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeStmtExpr::static_type()) {
Self::Expr(ruff::StmtExpr::ast_from_object(_vm, source_file, _object)?)
Self::Expr(ast::StmtExpr::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtPass::static_type()) {
Self::Pass(ruff::StmtPass::ast_from_object(_vm, source_file, _object)?)
Self::Pass(ast::StmtPass::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtBreak::static_type()) {
Self::Break(ruff::StmtBreak::ast_from_object(_vm, source_file, _object)?)
Self::Break(ast::StmtBreak::ast_from_object(_vm, source_file, _object)?)
} else if _cls.is(pyast::NodeStmtContinue::static_type()) {
Self::Continue(ruff::StmtContinue::ast_from_object(
Self::Continue(ast::StmtContinue::ast_from_object(
_vm,
source_file,
_object,
@@ -169,7 +145,7 @@ impl Node for ruff::Stmt {
}
// constructor
impl Node for ruff::StmtFunctionDef {
impl Node for ast::StmtFunctionDef {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -266,7 +242,7 @@ impl Node for ruff::StmtFunctionDef {
}
// constructor
impl Node for ruff::StmtClassDef {
impl Node for ast::StmtClassDef {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -348,7 +324,7 @@ impl Node for ruff::StmtClassDef {
}
}
// constructor
impl Node for ruff::StmtReturn {
impl Node for ast::StmtReturn {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -379,7 +355,7 @@ impl Node for ruff::StmtReturn {
}
}
// constructor
impl Node for ruff::StmtDelete {
impl Node for ast::StmtDelete {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -413,7 +389,7 @@ impl Node for ruff::StmtDelete {
}
// constructor
impl Node for ruff::StmtAssign {
impl Node for ast::StmtAssign {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -461,7 +437,7 @@ impl Node for ruff::StmtAssign {
}
// constructor
impl Node for ruff::StmtTypeAlias {
impl Node for ast::StmtTypeAlias {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -516,7 +492,7 @@ impl Node for ruff::StmtTypeAlias {
}
// constructor
impl Node for ruff::StmtAugAssign {
impl Node for ast::StmtAugAssign {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -566,7 +542,7 @@ impl Node for ruff::StmtAugAssign {
}
// constructor
impl Node for ruff::StmtAnnAssign {
impl Node for ast::StmtAnnAssign {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -626,7 +602,7 @@ impl Node for ruff::StmtAnnAssign {
}
// constructor
impl Node for ruff::StmtFor {
impl Node for ast::StmtFor {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -704,7 +680,7 @@ impl Node for ruff::StmtFor {
}
// constructor
impl Node for ruff::StmtWhile {
impl Node for ast::StmtWhile {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -754,7 +730,7 @@ impl Node for ruff::StmtWhile {
}
}
// constructor
impl Node for ruff::StmtIf {
impl Node for ast::StmtIf {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -764,7 +740,7 @@ impl Node for ruff::StmtIf {
elif_else_clauses,
} = self;
elif_else_clause::ast_to_object(
ruff::ElifElseClause {
ast::ElifElseClause {
node_index: Default::default(),
range,
test: Some(*test),
@@ -784,7 +760,7 @@ impl Node for ruff::StmtIf {
}
}
// constructor
impl Node for ruff::StmtWith {
impl Node for ast::StmtWith {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -844,7 +820,7 @@ impl Node for ruff::StmtWith {
}
}
// constructor
impl Node for ruff::StmtMatch {
impl Node for ast::StmtMatch {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -885,7 +861,7 @@ impl Node for ruff::StmtMatch {
}
}
// constructor
impl Node for ruff::StmtRaise {
impl Node for ast::StmtRaise {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -922,7 +898,7 @@ impl Node for ruff::StmtRaise {
}
}
// constructor
impl Node for ruff::StmtTry {
impl Node for ast::StmtTry {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -996,7 +972,7 @@ impl Node for ruff::StmtTry {
}
}
// constructor
impl Node for ruff::StmtAssert {
impl Node for ast::StmtAssert {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1035,7 +1011,7 @@ impl Node for ruff::StmtAssert {
}
}
// constructor
impl Node for ruff::StmtImport {
impl Node for ast::StmtImport {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1068,7 +1044,7 @@ impl Node for ruff::StmtImport {
}
}
// constructor
impl Node for ruff::StmtImportFrom {
impl Node for ast::StmtImportFrom {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1117,7 +1093,7 @@ impl Node for ruff::StmtImportFrom {
}
}
// constructor
impl Node for ruff::StmtGlobal {
impl Node for ast::StmtGlobal {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1150,7 +1126,7 @@ impl Node for ruff::StmtGlobal {
}
}
// constructor
impl Node for ruff::StmtNonlocal {
impl Node for ast::StmtNonlocal {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1183,7 +1159,7 @@ impl Node for ruff::StmtNonlocal {
}
}
// constructor
impl Node for ruff::StmtExpr {
impl Node for ast::StmtExpr {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1216,7 +1192,7 @@ impl Node for ruff::StmtExpr {
}
}
// constructor
impl Node for ruff::StmtPass {
impl Node for ast::StmtPass {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1241,7 +1217,7 @@ impl Node for ruff::StmtPass {
}
}
// constructor
impl Node for ruff::StmtBreak {
impl Node for ast::StmtBreak {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -1268,7 +1244,7 @@ impl Node for ruff::StmtBreak {
}
// constructor
impl Node for ruff::StmtContinue {
impl Node for ast::StmtContinue {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
+55 -56
View File
@@ -2,14 +2,13 @@ use super::constant::{Constant, ConstantLiteral};
use super::*;
fn ruff_fstring_element_into_iter(
mut fstring_element: ruff::InterpolatedStringElements,
) -> impl Iterator<Item = ruff::InterpolatedStringElement> {
let default =
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
node_index: Default::default(),
range: Default::default(),
value: Default::default(),
});
mut fstring_element: ast::InterpolatedStringElements,
) -> impl Iterator<Item = ast::InterpolatedStringElement> {
let default = ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
node_index: Default::default(),
range: Default::default(),
value: Default::default(),
});
fstring_element
.iter_mut()
.map(move |elem| core::mem::replace(elem, default.clone()))
@@ -18,19 +17,19 @@ fn ruff_fstring_element_into_iter(
}
fn ruff_fstring_element_to_joined_str_part(
element: ruff::InterpolatedStringElement,
element: ast::InterpolatedStringElement,
) -> JoinedStrPart {
match element {
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
range,
value,
node_index: _,
}) => JoinedStrPart::Constant(Constant::new_str(
value,
ruff::str_prefix::StringLiteralPrefix::Empty,
ast::str_prefix::StringLiteralPrefix::Empty,
range,
)),
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
range,
expression,
debug_text: _, // TODO: What is this?
@@ -47,12 +46,12 @@ fn ruff_fstring_element_to_joined_str_part(
}
fn ruff_format_spec_to_joined_str(
format_spec: Option<Box<ruff::InterpolatedStringFormatSpec>>,
format_spec: Option<Box<ast::InterpolatedStringFormatSpec>>,
) -> Option<Box<JoinedStr>> {
match format_spec {
None => None,
Some(format_spec) => {
let ruff::InterpolatedStringFormatSpec {
let ast::InterpolatedStringFormatSpec {
range,
elements,
node_index: _,
@@ -67,36 +66,36 @@ fn ruff_format_spec_to_joined_str(
}
fn ruff_fstring_element_to_ruff_fstring_part(
element: ruff::InterpolatedStringElement,
) -> ruff::FStringPart {
element: ast::InterpolatedStringElement,
) -> ast::FStringPart {
match element {
ruff::InterpolatedStringElement::Literal(value) => {
let ruff::InterpolatedStringLiteralElement {
ast::InterpolatedStringElement::Literal(value) => {
let ast::InterpolatedStringLiteralElement {
node_index,
range,
value,
} = value;
ruff::FStringPart::Literal(ruff::StringLiteral {
ast::FStringPart::Literal(ast::StringLiteral {
node_index,
range,
value,
flags: ruff::StringLiteralFlags::empty(),
flags: ast::StringLiteralFlags::empty(),
})
}
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
range, ..
}) => ruff::FStringPart::FString(ruff::FString {
}) => ast::FStringPart::FString(ast::FString {
node_index: Default::default(),
range,
elements: vec![element].into(),
flags: ruff::FStringFlags::empty(),
flags: ast::FStringFlags::empty(),
}),
}
}
fn joined_str_to_ruff_format_spec(
joined_str: Option<Box<JoinedStr>>,
) -> Option<Box<ruff::InterpolatedStringFormatSpec>> {
) -> Option<Box<ast::InterpolatedStringFormatSpec>> {
match joined_str {
None => None,
Some(joined_str) => {
@@ -104,7 +103,7 @@ fn joined_str_to_ruff_format_spec(
let elements: Vec<_> = Box::into_iter(values)
.map(joined_str_part_to_ruff_fstring_element)
.collect();
let format_spec = ruff::InterpolatedStringFormatSpec {
let format_spec = ast::InterpolatedStringFormatSpec {
node_index: Default::default(),
range,
elements: elements.into(),
@@ -121,32 +120,32 @@ pub(super) struct JoinedStr {
}
impl JoinedStr {
pub(super) fn into_expr(self) -> ruff::Expr {
pub(super) fn into_expr(self) -> ast::Expr {
let Self { range, values } = self;
ruff::Expr::FString(ruff::ExprFString {
ast::Expr::FString(ast::ExprFString {
node_index: Default::default(),
range: Default::default(),
value: match values.len() {
// ruff represents an empty fstring like this:
0 => ruff::FStringValue::single(ruff::FString {
0 => ast::FStringValue::single(ast::FString {
node_index: Default::default(),
range,
elements: vec![].into(),
flags: ruff::FStringFlags::empty(),
flags: ast::FStringFlags::empty(),
}),
1 => ruff::FStringValue::single(
1 => ast::FStringValue::single(
Box::<[_]>::into_iter(values)
.map(joined_str_part_to_ruff_fstring_element)
.map(|element| ruff::FString {
.map(|element| ast::FString {
node_index: Default::default(),
range,
elements: vec![element].into(),
flags: ruff::FStringFlags::empty(),
flags: ast::FStringFlags::empty(),
})
.next()
.expect("FString has exactly one part"),
),
_ => ruff::FStringValue::concatenated(
_ => ast::FStringValue::concatenated(
Box::<[_]>::into_iter(values)
.map(joined_str_part_to_ruff_fstring_element)
.map(ruff_fstring_element_to_ruff_fstring_part)
@@ -157,10 +156,10 @@ impl JoinedStr {
}
}
fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::InterpolatedStringElement {
fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ast::InterpolatedStringElement {
match part {
JoinedStrPart::FormattedValue(value) => {
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
node_index: Default::default(),
range: value.range,
expression: value.value.clone(),
@@ -170,7 +169,7 @@ fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::Interpo
})
}
JoinedStrPart::Constant(value) => {
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
node_index: Default::default(),
range: value.range,
value: match value.value {
@@ -254,8 +253,8 @@ impl Node for JoinedStrPart {
#[derive(Debug)]
pub(super) struct FormattedValue {
value: Box<ruff::Expr>,
conversion: ruff::ConversionFlag,
value: Box<ast::Expr>,
conversion: ast::ConversionFlag,
format_spec: Option<Box<JoinedStr>>,
range: TextRange,
}
@@ -313,24 +312,24 @@ impl Node for FormattedValue {
pub(super) fn fstring_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
expression: ruff::ExprFString,
expression: ast::ExprFString,
) -> PyObjectRef {
let ruff::ExprFString {
let ast::ExprFString {
range,
mut value,
node_index: _,
} = expression;
let default_part = ruff::FStringPart::FString(ruff::FString {
let default_part = ast::FStringPart::FString(ast::FString {
node_index: Default::default(),
range: Default::default(),
elements: Default::default(),
flags: ruff::FStringFlags::empty(),
flags: ast::FStringFlags::empty(),
});
let mut values = Vec::new();
for i in 0..value.as_slice().len() {
let part = core::mem::replace(value.iter_mut().nth(i).unwrap(), default_part.clone());
match part {
ruff::FStringPart::Literal(ruff::StringLiteral {
ast::FStringPart::Literal(ast::StringLiteral {
range,
value,
flags,
@@ -342,7 +341,7 @@ pub(super) fn fstring_to_object(
range,
)));
}
ruff::FStringPart::FString(ruff::FString {
ast::FStringPart::FString(ast::FString {
range: _,
elements,
flags: _,
@@ -364,20 +363,20 @@ pub(super) fn fstring_to_object(
// ===== TString (Template String) Support =====
fn ruff_tstring_element_to_template_str_part(
element: ruff::InterpolatedStringElement,
element: ast::InterpolatedStringElement,
source_file: &SourceFile,
) -> TemplateStrPart {
match element {
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
range,
value,
node_index: _,
}) => TemplateStrPart::Constant(Constant::new_str(
value,
ruff::str_prefix::StringLiteralPrefix::Empty,
ast::str_prefix::StringLiteralPrefix::Empty,
range,
)),
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
range,
expression,
debug_text,
@@ -401,13 +400,13 @@ fn ruff_tstring_element_to_template_str_part(
}
fn ruff_format_spec_to_template_str(
format_spec: Option<Box<ruff::InterpolatedStringFormatSpec>>,
format_spec: Option<Box<ast::InterpolatedStringFormatSpec>>,
source_file: &SourceFile,
) -> Option<Box<TemplateStr>> {
match format_spec {
None => None,
Some(format_spec) => {
let ruff::InterpolatedStringFormatSpec {
let ast::InterpolatedStringFormatSpec {
range,
elements,
node_index: _,
@@ -499,9 +498,9 @@ impl Node for TemplateStrPart {
#[derive(Debug)]
pub(super) struct TStringInterpolation {
value: Box<ruff::Expr>,
value: Box<ast::Expr>,
str: String,
conversion: ruff::ConversionFlag,
conversion: ast::ConversionFlag,
format_spec: Option<Box<TemplateStr>>,
range: TextRange,
}
@@ -565,18 +564,18 @@ impl Node for TStringInterpolation {
pub(super) fn tstring_to_object(
vm: &VirtualMachine,
source_file: &SourceFile,
expression: ruff::ExprTString,
expression: ast::ExprTString,
) -> PyObjectRef {
let ruff::ExprTString {
let ast::ExprTString {
range,
mut value,
node_index: _,
} = expression;
let default_tstring = ruff::TString {
let default_tstring = ast::TString {
node_index: Default::default(),
range: Default::default(),
elements: Default::default(),
flags: ruff::TStringFlags::empty(),
flags: ast::TStringFlags::empty(),
};
let mut values = Vec::new();
for i in 0..value.as_slice().len() {
+9 -9
View File
@@ -1,7 +1,7 @@
use super::*;
use rustpython_compiler_core::SourceFile;
impl Node for ruff::TypeParams {
impl Node for ast::TypeParams {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
self.type_params.ast_to_object(vm, source_file)
}
@@ -11,7 +11,7 @@ impl Node for ruff::TypeParams {
_source_file: &SourceFile,
_object: PyObjectRef,
) -> PyResult<Self> {
let type_params: Vec<ruff::TypeParam> = Node::ast_from_object(_vm, _source_file, _object)?;
let type_params: Vec<ast::TypeParam> = Node::ast_from_object(_vm, _source_file, _object)?;
let range = Option::zip(type_params.first(), type_params.last())
.map(|(first, last)| first.range().cover(last.range()))
.unwrap_or_default();
@@ -28,7 +28,7 @@ impl Node for ruff::TypeParams {
}
// sum
impl Node for ruff::TypeParam {
impl Node for ast::TypeParam {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Self::TypeVar(cons) => cons.ast_to_object(vm, source_file),
@@ -44,19 +44,19 @@ impl Node for ruff::TypeParam {
) -> PyResult<Self> {
let _cls = _object.class();
Ok(if _cls.is(pyast::NodeTypeParamTypeVar::static_type()) {
Self::TypeVar(ruff::TypeParamTypeVar::ast_from_object(
Self::TypeVar(ast::TypeParamTypeVar::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeTypeParamParamSpec::static_type()) {
Self::ParamSpec(ruff::TypeParamParamSpec::ast_from_object(
Self::ParamSpec(ast::TypeParamParamSpec::ast_from_object(
_vm,
source_file,
_object,
)?)
} else if _cls.is(pyast::NodeTypeParamTypeVarTuple::static_type()) {
Self::TypeVarTuple(ruff::TypeParamTypeVarTuple::ast_from_object(
Self::TypeVarTuple(ast::TypeParamTypeVarTuple::ast_from_object(
_vm,
source_file,
_object,
@@ -71,7 +71,7 @@ impl Node for ruff::TypeParam {
}
// constructor
impl Node for ruff::TypeParamTypeVar {
impl Node for ast::TypeParamTypeVar {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -118,7 +118,7 @@ impl Node for ruff::TypeParamTypeVar {
}
// constructor
impl Node for ruff::TypeParamParamSpec {
impl Node for ast::TypeParamParamSpec {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,
@@ -165,7 +165,7 @@ impl Node for ruff::TypeParamParamSpec {
}
// constructor
impl Node for ruff::TypeParamTypeVarTuple {
impl Node for ast::TypeParamTypeVarTuple {
fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
let Self {
node_index: _,