diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index f0f5844..0446dee 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -279,7 +279,7 @@ class StructVisitor(EmitVisitor): if has_attributes: self.emit("pub range: R,", depth + 1) else: - self.emit("pub range: crate::ranged::OptionalRange,", depth + 1) + self.emit("pub range: OptionalRange,", depth + 1) def simple_sum(self, sum, name, depth): rust_name = rust_type_name(name) diff --git a/ast/src/gen/generic.rs b/ast/src/gen/generic.rs index 6d819b1..94969b9 100644 --- a/ast/src/gen/generic.rs +++ b/ast/src/gen/generic.rs @@ -3,7 +3,7 @@ use crate::text_size::TextRange; #[derive(Clone, Debug, PartialEq)] pub struct ModModule { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub body: Vec>, pub type_ignores: Vec>, } @@ -16,7 +16,7 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModInteractive { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub body: Vec>, } @@ -28,7 +28,7 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModExpression { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub body: Box>, } @@ -40,7 +40,7 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModFunctionType { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub argtypes: Vec>, pub returns: Box>, } @@ -957,7 +957,7 @@ pub struct Comprehension { pub iter: Expr, pub ifs: Vec>, pub is_async: bool, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -988,7 +988,7 @@ pub struct Arguments { pub kw_defaults: Vec>, pub kwarg: Option>>, pub defaults: Vec>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -1017,7 +1017,7 @@ pub struct Alias { pub struct Withitem { pub context_expr: Expr, pub optional_vars: Option>>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -1025,7 +1025,7 @@ pub struct MatchCase { pub pattern: Pattern, pub guard: Option>>, pub body: Vec>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -1144,7 +1144,7 @@ pub enum Pattern { #[derive(Clone, Debug, PartialEq)] pub struct TypeIgnoreTypeIgnore { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub lineno: Int, pub tag: String, } diff --git a/ast/src/generic.rs b/ast/src/generic.rs new file mode 100644 index 0000000..b51589d --- /dev/null +++ b/ast/src/generic.rs @@ -0,0 +1,48 @@ +#![allow(clippy::derive_partial_eq_without_eq)] +pub use crate::{builtin::*, text_size::TextSize}; +use std::fmt::{Debug, Display, Formatter}; +use std::marker::PhantomData; + +pub type Suite = Vec>; + +#[cfg(feature = "all-nodes-with-ranges")] +pub type OptionalRange = R; + +#[cfg(not(feature = "all-nodes-with-ranges"))] +pub type OptionalRange = EmptyRange; + +#[derive(Eq, PartialEq, Hash, Copy, Clone)] +pub struct EmptyRange { + phantom: PhantomData, +} + +impl EmptyRange { + #[inline(always)] + pub fn new(_start: TextSize, _end: TextSize) -> Self { + Self { + phantom: PhantomData, + } + } +} + +impl Display for EmptyRange { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str("()") + } +} + +impl Debug for EmptyRange { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Display::fmt(self, f) + } +} + +impl Default for EmptyRange { + fn default() -> Self { + EmptyRange { + phantom: PhantomData, + } + } +} + +include!("gen/generic.rs"); diff --git a/ast/src/lib.rs b/ast/src/lib.rs index 3489040..008a181 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -1,24 +1,17 @@ mod builtin; -#[cfg(feature = "fold")] -mod fold_helpers; -mod generic { - #![allow(clippy::derive_partial_eq_without_eq)] - pub use crate::builtin::*; - - include!("gen/generic.rs"); -} +mod generic; mod impls; mod ranged; -#[cfg(feature = "location")] -mod source_locator; #[cfg(feature = "unparse")] mod unparse; pub use builtin::*; pub use generic::*; -pub use ranged::{EmptyRange, OptionalRange, Ranged, Suite}; +pub use ranged::Ranged; pub use rustpython_parser_core::{text_size, ConversionFlag}; +#[cfg(feature = "fold")] +mod fold_helpers; #[cfg(feature = "fold")] pub mod fold { use super::generic::*; @@ -35,14 +28,15 @@ mod visitor { #[cfg(feature = "location")] pub mod located; - +#[cfg(feature = "location")] +mod source_locator; #[cfg(feature = "location")] pub use rustpython_parser_core::source_code; + #[cfg(feature = "visitor")] pub use visitor::Visitor; #[cfg(feature = "constant-optimization")] mod optimizer; - #[cfg(feature = "constant-optimization")] pub use optimizer::ConstantOptimizer; diff --git a/ast/src/ranged.rs b/ast/src/ranged.rs index 14f2a43..f01c15a 100644 --- a/ast/src/ranged.rs +++ b/ast/src/ranged.rs @@ -1,9 +1,6 @@ use crate::text_size::{TextRange, TextSize}; -use std::fmt::{Debug, Display, Formatter}; -use std::marker::PhantomData; pub use crate::builtin::*; -use crate::Stmt; pub trait Ranged { fn range(&self) -> TextRange; @@ -17,46 +14,4 @@ pub trait Ranged { } } -pub type Suite = Vec>; - -#[cfg(feature = "all-nodes-with-ranges")] -pub type OptionalRange = R; - -#[cfg(not(feature = "all-nodes-with-ranges"))] -pub type OptionalRange = EmptyRange; - -#[derive(Eq, PartialEq, Hash, Copy, Clone)] -pub struct EmptyRange { - phantom: PhantomData, -} - -impl EmptyRange { - #[inline(always)] - pub fn new(_start: TextSize, _end: TextSize) -> Self { - Self { - phantom: PhantomData, - } - } -} - -impl Display for EmptyRange { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str("()") - } -} - -impl Debug for EmptyRange { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - Display::fmt(self, f) - } -} - -impl Default for EmptyRange { - fn default() -> Self { - EmptyRange { - phantom: PhantomData, - } - } -} - include!("gen/ranged.rs");