Skip to content

Commit f4774e3

Browse files
committed
use ast namespace
1 parent eb50246 commit f4774e3

File tree

20 files changed

+864
-878
lines changed

20 files changed

+864
-878
lines changed

crates/codegen/src/compile.rs

Lines changed: 433 additions & 414 deletions
Large diffs are not rendered by default.

crates/codegen/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod symboltable;
1818
mod unparse;
1919

2020
pub use compile::CompileOpts;
21-
use ruff_python_ast::Expr;
21+
use ruff_python_ast as ast;
2222

2323
pub(crate) use compile::InternalResult;
2424

@@ -27,7 +27,7 @@ pub trait ToPythonName {
2727
fn python_name(&self) -> &'static str;
2828
}
2929

30-
impl ToPythonName for Expr {
30+
impl ToPythonName for ast::Expr {
3131
fn python_name(&self) -> &'static str {
3232
match self {
3333
Self::BoolOp { .. } | Self::BinOp { .. } | Self::UnaryOp { .. } => "operator",

crates/codegen/src/string_parser.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use core::convert::Infallible;
99

10-
use ruff_python_ast::{AnyStringFlags, StringFlags};
10+
use ruff_python_ast::{self as ast, StringFlags as _};
1111
use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
1212

1313
// use ruff_python_parser::{LexicalError, LexicalErrorType};
@@ -24,11 +24,11 @@ struct StringParser {
2424
/// Current position of the parser in the source.
2525
cursor: usize,
2626
/// Flags that can be used to query information about the string.
27-
flags: AnyStringFlags,
27+
flags: ast::AnyStringFlags,
2828
}
2929

3030
impl StringParser {
31-
const fn new(source: Box<str>, flags: AnyStringFlags) -> Self {
31+
const fn new(source: Box<str>, flags: ast::AnyStringFlags) -> Self {
3232
Self {
3333
source,
3434
cursor: 0,
@@ -272,15 +272,18 @@ impl StringParser {
272272
}
273273
}
274274

275-
pub(crate) fn parse_string_literal(source: &str, flags: AnyStringFlags) -> Box<Wtf8> {
275+
pub(crate) fn parse_string_literal(source: &str, flags: ast::AnyStringFlags) -> Box<Wtf8> {
276276
let source = &source[flags.opener_len().to_usize()..];
277277
let source = &source[..source.len() - flags.quote_len().to_usize()];
278278
StringParser::new(source.into(), flags)
279279
.parse_string()
280280
.unwrap_or_else(|x| match x {})
281281
}
282282

283-
pub(crate) fn parse_fstring_literal_element(source: Box<str>, flags: AnyStringFlags) -> Box<Wtf8> {
283+
pub(crate) fn parse_fstring_literal_element(
284+
source: Box<str>,
285+
flags: ast::AnyStringFlags,
286+
) -> Box<Wtf8> {
284287
StringParser::new(source, flags)
285288
.parse_fstring_middle()
286289
.unwrap_or_else(|x| match x {})

crates/codegen/src/symboltable.rs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ use crate::{
1313
};
1414
use alloc::{borrow::Cow, fmt};
1515
use bitflags::bitflags;
16-
use ruff_python_ast::{
17-
self as ast, Comprehension, Decorator, Expr, Identifier, ModExpression, ModModule, Parameter,
18-
ParameterWithDefault, Parameters, Pattern, PatternMatchAs, PatternMatchClass,
19-
PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchStar, PatternMatchValue,
20-
Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams,
21-
};
16+
use ruff_python_ast as ast;
2217
use ruff_text_size::{Ranged, TextRange};
2318
use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation};
2419
use std::collections::HashSet;
@@ -97,13 +92,19 @@ impl SymbolTable {
9792
}
9893
}
9994

100-
pub fn scan_program(program: &ModModule, source_file: SourceFile) -> SymbolTableResult<Self> {
95+
pub fn scan_program(
96+
program: &ast::ModModule,
97+
source_file: SourceFile,
98+
) -> SymbolTableResult<Self> {
10199
let mut builder = SymbolTableBuilder::new(source_file);
102100
builder.scan_statements(program.body.as_ref())?;
103101
builder.finish()
104102
}
105103

106-
pub fn scan_expr(expr: &ModExpression, source_file: SourceFile) -> SymbolTableResult<Self> {
104+
pub fn scan_expr(
105+
expr: &ast::ModExpression,
106+
source_file: SourceFile,
107+
) -> SymbolTableResult<Self> {
107108
let mut builder = SymbolTableBuilder::new(source_file);
108109
builder.scan_expression(expr.body.as_ref(), ExpressionContext::Load)?;
109110
builder.finish()
@@ -973,21 +974,21 @@ impl SymbolTableBuilder {
973974
.get() as _
974975
}
975976

976-
fn scan_statements(&mut self, statements: &[Stmt]) -> SymbolTableResult {
977+
fn scan_statements(&mut self, statements: &[ast::Stmt]) -> SymbolTableResult {
977978
for statement in statements {
978979
self.scan_statement(statement)?;
979980
}
980981
Ok(())
981982
}
982983

983-
fn scan_parameters(&mut self, parameters: &[ParameterWithDefault]) -> SymbolTableResult {
984+
fn scan_parameters(&mut self, parameters: &[ast::ParameterWithDefault]) -> SymbolTableResult {
984985
for parameter in parameters {
985986
self.scan_parameter(&parameter.parameter)?;
986987
}
987988
Ok(())
988989
}
989990

990-
fn scan_parameter(&mut self, parameter: &Parameter) -> SymbolTableResult {
991+
fn scan_parameter(&mut self, parameter: &ast::Parameter) -> SymbolTableResult {
991992
self.check_name(
992993
parameter.name.as_str(),
993994
ExpressionContext::Store,
@@ -1019,7 +1020,7 @@ impl SymbolTableBuilder {
10191020
self.register_ident(&parameter.name, usage)
10201021
}
10211022

1022-
fn scan_annotation(&mut self, annotation: &Expr) -> SymbolTableResult {
1023+
fn scan_annotation(&mut self, annotation: &ast::Expr) -> SymbolTableResult {
10231024
let current_scope = self.tables.last().map(|t| t.typ);
10241025

10251026
// PEP 649: Check if this is a conditional annotation
@@ -1074,8 +1075,8 @@ impl SymbolTableBuilder {
10741075
result
10751076
}
10761077

1077-
fn scan_statement(&mut self, statement: &Stmt) -> SymbolTableResult {
1078-
use ruff_python_ast::*;
1078+
fn scan_statement(&mut self, statement: &ast::Stmt) -> SymbolTableResult {
1079+
use ast::*;
10791080
if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = &statement
10801081
&& module.as_ref().map(|id| id.as_str()) == Some("__future__")
10811082
{
@@ -1435,7 +1436,7 @@ impl SymbolTableBuilder {
14351436

14361437
fn scan_decorators(
14371438
&mut self,
1438-
decorators: &[Decorator],
1439+
decorators: &[ast::Decorator],
14391440
context: ExpressionContext,
14401441
) -> SymbolTableResult {
14411442
for decorator in decorators {
@@ -1446,7 +1447,7 @@ impl SymbolTableBuilder {
14461447

14471448
fn scan_expressions(
14481449
&mut self,
1449-
expressions: &[Expr],
1450+
expressions: &[ast::Expr],
14501451
context: ExpressionContext,
14511452
) -> SymbolTableResult {
14521453
for expression in expressions {
@@ -1457,10 +1458,10 @@ impl SymbolTableBuilder {
14571458

14581459
fn scan_expression(
14591460
&mut self,
1460-
expression: &Expr,
1461+
expression: &ast::Expr,
14611462
context: ExpressionContext,
14621463
) -> SymbolTableResult {
1463-
use ruff_python_ast::*;
1464+
use ast::*;
14641465

14651466
// Check for expressions not allowed in certain contexts
14661467
// (type parameters, annotations, type aliases, TypeVar bounds/defaults)
@@ -1837,9 +1838,9 @@ impl SymbolTableBuilder {
18371838
fn scan_comprehension(
18381839
&mut self,
18391840
scope_name: &str,
1840-
elt1: &Expr,
1841-
elt2: Option<&Expr>,
1842-
generators: &[Comprehension],
1841+
elt1: &ast::Expr,
1842+
elt2: Option<&ast::Expr>,
1843+
generators: &[ast::Comprehension],
18431844
range: TextRange,
18441845
is_generator: bool,
18451846
) -> SymbolTableResult {
@@ -1906,7 +1907,7 @@ impl SymbolTableBuilder {
19061907
// = symtable_visit_type_param_bound_or_default
19071908
fn scan_type_param_bound_or_default(
19081909
&mut self,
1909-
expr: &Expr,
1910+
expr: &ast::Expr,
19101911
scope_name: &str,
19111912
scope_info: &'static str,
19121913
) -> SymbolTableResult {
@@ -1932,14 +1933,14 @@ impl SymbolTableBuilder {
19321933
result
19331934
}
19341935

1935-
fn scan_type_params(&mut self, type_params: &TypeParams) -> SymbolTableResult {
1936+
fn scan_type_params(&mut self, type_params: &ast::TypeParams) -> SymbolTableResult {
19361937
// Check for duplicate type parameter names
19371938
let mut seen_names: std::collections::HashSet<&str> = std::collections::HashSet::new();
19381939
for type_param in &type_params.type_params {
19391940
let (name, range) = match type_param {
1940-
TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range),
1941-
TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range),
1942-
TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range),
1941+
ast::TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range),
1942+
ast::TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range),
1943+
ast::TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range),
19431944
};
19441945
if !seen_names.insert(name) {
19451946
return Err(SymbolTableError {
@@ -1959,7 +1960,7 @@ impl SymbolTableBuilder {
19591960
// First register all type parameters
19601961
for type_param in &type_params.type_params {
19611962
match type_param {
1962-
TypeParam::TypeVar(TypeParamTypeVar {
1963+
ast::TypeParam::TypeVar(ast::TypeParamTypeVar {
19631964
name,
19641965
bound,
19651966
range: type_var_range,
@@ -1991,7 +1992,7 @@ impl SymbolTableBuilder {
19911992
)?;
19921993
}
19931994
}
1994-
TypeParam::ParamSpec(TypeParamParamSpec {
1995+
ast::TypeParam::ParamSpec(ast::TypeParamParamSpec {
19951996
name,
19961997
range: param_spec_range,
19971998
default,
@@ -2009,7 +2010,7 @@ impl SymbolTableBuilder {
20092010
)?;
20102011
}
20112012
}
2012-
TypeParam::TypeVarTuple(TypeParamTypeVarTuple {
2013+
ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple {
20132014
name,
20142015
range: type_var_tuple_range,
20152016
default,
@@ -2032,22 +2033,24 @@ impl SymbolTableBuilder {
20322033
Ok(())
20332034
}
20342035

2035-
fn scan_patterns(&mut self, patterns: &[Pattern]) -> SymbolTableResult {
2036+
fn scan_patterns(&mut self, patterns: &[ast::Pattern]) -> SymbolTableResult {
20362037
for pattern in patterns {
20372038
self.scan_pattern(pattern)?;
20382039
}
20392040
Ok(())
20402041
}
20412042

2042-
fn scan_pattern(&mut self, pattern: &Pattern) -> SymbolTableResult {
2043-
use Pattern::*;
2043+
fn scan_pattern(&mut self, pattern: &ast::Pattern) -> SymbolTableResult {
2044+
use ast::Pattern::*;
20442045
match pattern {
2045-
MatchValue(PatternMatchValue { value, .. }) => {
2046+
MatchValue(ast::PatternMatchValue { value, .. }) => {
20462047
self.scan_expression(value, ExpressionContext::Load)?
20472048
}
20482049
MatchSingleton(_) => {}
2049-
MatchSequence(PatternMatchSequence { patterns, .. }) => self.scan_patterns(patterns)?,
2050-
MatchMapping(PatternMatchMapping {
2050+
MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
2051+
self.scan_patterns(patterns)?
2052+
}
2053+
MatchMapping(ast::PatternMatchMapping {
20512054
keys,
20522055
patterns,
20532056
rest,
@@ -2059,35 +2062,35 @@ impl SymbolTableBuilder {
20592062
self.register_ident(rest, SymbolUsage::Assigned)?;
20602063
}
20612064
}
2062-
MatchClass(PatternMatchClass { cls, arguments, .. }) => {
2065+
MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => {
20632066
self.scan_expression(cls, ExpressionContext::Load)?;
20642067
self.scan_patterns(&arguments.patterns)?;
20652068
for kw in &arguments.keywords {
20662069
self.scan_pattern(&kw.pattern)?;
20672070
}
20682071
}
2069-
MatchStar(PatternMatchStar { name, .. }) => {
2072+
MatchStar(ast::PatternMatchStar { name, .. }) => {
20702073
if let Some(name) = name {
20712074
self.register_ident(name, SymbolUsage::Assigned)?;
20722075
}
20732076
}
2074-
MatchAs(PatternMatchAs { pattern, name, .. }) => {
2077+
MatchAs(ast::PatternMatchAs { pattern, name, .. }) => {
20752078
if let Some(pattern) = pattern {
20762079
self.scan_pattern(pattern)?;
20772080
}
20782081
if let Some(name) = name {
20792082
self.register_ident(name, SymbolUsage::Assigned)?;
20802083
}
20812084
}
2082-
MatchOr(PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?,
2085+
MatchOr(ast::PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?,
20832086
}
20842087
Ok(())
20852088
}
20862089

20872090
fn enter_scope_with_parameters(
20882091
&mut self,
20892092
name: &str,
2090-
parameters: &Parameters,
2093+
parameters: &ast::Parameters,
20912094
line_number: u32,
20922095
has_return_annotation: bool,
20932096
) -> SymbolTableResult {
@@ -2174,7 +2177,7 @@ impl SymbolTableBuilder {
21742177
Ok(())
21752178
}
21762179

2177-
fn register_ident(&mut self, ident: &Identifier, role: SymbolUsage) -> SymbolTableResult {
2180+
fn register_ident(&mut self, ident: &ast::Identifier, role: SymbolUsage) -> SymbolTableResult {
21782181
self.register_name(ident.as_str(), role, ident.range)
21792182
}
21802183

0 commit comments

Comments
 (0)