From 708d8061d30655fac781bf0af115a2730dc83adf Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 15 May 2023 17:40:25 +0900 Subject: [PATCH] Generic types to generic --- ast/asdl_rs.py | 2 +- ast/src/gen/generic.rs | 18 ++++++++-------- ast/src/generic.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ ast/src/lib.rs | 21 ++++++------------ ast/src/ranged.rs | 45 --------------------------------------- 5 files changed, 65 insertions(+), 69 deletions(-) create mode 100644 ast/src/generic.rs diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index 0121fad..5ec4cfd 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -290,7 +290,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 b0e8bb7..40643a2 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>, } @@ -20,7 +20,7 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModInteractive { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub body: Vec>, } @@ -36,7 +36,7 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModExpression { - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, pub body: Box>, } @@ -52,7 +52,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>, } @@ -1246,7 +1246,7 @@ pub struct Comprehension { pub iter: Expr, pub ifs: Vec>, pub is_async: bool, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } impl Node for Comprehension { @@ -1291,7 +1291,7 @@ pub struct Arguments { pub kw_defaults: Vec>, pub kwarg: Option>>, pub defaults: Vec>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } impl Node for Arguments { @@ -1348,7 +1348,7 @@ impl Node for Alias { pub struct Withitem { pub context_expr: Expr, pub optional_vars: Option>>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } impl Node for Withitem { @@ -1361,7 +1361,7 @@ pub struct MatchCase { pub pattern: Pattern, pub guard: Option>>, pub body: Vec>, - pub range: crate::ranged::OptionalRange, + pub range: OptionalRange, } impl Node for MatchCase { @@ -1522,7 +1522,7 @@ impl Node for 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..56ad5ce --- /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, Node}; +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 d6534e7..d1f63d1 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -1,23 +1,13 @@ mod builtin; -#[cfg(feature = "fold")] -mod fold_helpers; -mod generic { - #![allow(clippy::derive_partial_eq_without_eq)] - use super::Node; - 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}; pub trait Node { @@ -25,6 +15,8 @@ pub trait Node { const FIELD_NAMES: &'static [&'static str]; } +#[cfg(feature = "fold")] +mod fold_helpers; #[cfg(feature = "fold")] pub mod fold { use super::generic::*; @@ -43,14 +35,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");