Generic types to generic (#30)

This commit is contained in:
Jeong, YunWon 2023-05-15 20:22:42 +09:00 committed by GitHub
parent 718354673e
commit b78001c953
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 68 deletions

View file

@ -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<R>,", depth + 1)
self.emit("pub range: OptionalRange<R>,", depth + 1)
def simple_sum(self, sum, name, depth):
rust_name = rust_type_name(name)

View file

@ -3,7 +3,7 @@
use crate::text_size::TextRange;
#[derive(Clone, Debug, PartialEq)]
pub struct ModModule<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
pub body: Vec<Stmt<R>>,
pub type_ignores: Vec<TypeIgnore<R>>,
}
@ -16,7 +16,7 @@ impl<R> From<ModModule<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModInteractive<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
pub body: Vec<Stmt<R>>,
}
@ -28,7 +28,7 @@ impl<R> From<ModInteractive<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModExpression<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
pub body: Box<Expr<R>>,
}
@ -40,7 +40,7 @@ impl<R> From<ModExpression<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModFunctionType<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
pub argtypes: Vec<Expr<R>>,
pub returns: Box<Expr<R>>,
}
@ -957,7 +957,7 @@ pub struct Comprehension<R = TextRange> {
pub iter: Expr<R>,
pub ifs: Vec<Expr<R>>,
pub is_async: bool,
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -988,7 +988,7 @@ pub struct Arguments<R = TextRange> {
pub kw_defaults: Vec<Expr<R>>,
pub kwarg: Option<Box<Arg<R>>>,
pub defaults: Vec<Expr<R>>,
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -1017,7 +1017,7 @@ pub struct Alias<R = TextRange> {
pub struct Withitem<R = TextRange> {
pub context_expr: Expr<R>,
pub optional_vars: Option<Box<Expr<R>>>,
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -1025,7 +1025,7 @@ pub struct MatchCase<R = TextRange> {
pub pattern: Pattern<R>,
pub guard: Option<Box<Expr<R>>>,
pub body: Vec<Stmt<R>>,
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -1144,7 +1144,7 @@ pub enum Pattern<R = TextRange> {
#[derive(Clone, Debug, PartialEq)]
pub struct TypeIgnoreTypeIgnore<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub range: OptionalRange<R>,
pub lineno: Int,
pub tag: String,
}

48
ast/src/generic.rs Normal file
View file

@ -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<R = TextRange> = Vec<Stmt<R>>;
#[cfg(feature = "all-nodes-with-ranges")]
pub type OptionalRange<R> = R;
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub type OptionalRange<R> = EmptyRange<R>;
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
phantom: PhantomData<R>,
}
impl<R> EmptyRange<R> {
#[inline(always)]
pub fn new(_start: TextSize, _end: TextSize) -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<R> Display for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("()")
}
}
impl<R> Debug for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl<R> Default for EmptyRange<R> {
fn default() -> Self {
EmptyRange {
phantom: PhantomData,
}
}
}
include!("gen/generic.rs");

View file

@ -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;

View file

@ -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<R = TextRange> = Vec<Stmt<R>>;
#[cfg(feature = "all-nodes-with-ranges")]
pub type OptionalRange<R> = R;
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub type OptionalRange<R> = EmptyRange<R>;
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
phantom: PhantomData<R>,
}
impl<R> EmptyRange<R> {
#[inline(always)]
pub fn new(_start: TextSize, _end: TextSize) -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<R> Display for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("()")
}
}
impl<R> Debug for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl<R> Default for EmptyRange<R> {
fn default() -> Self {
EmptyRange {
phantom: PhantomData,
}
}
}
include!("gen/ranged.rs");