Generic types to generic

This commit is contained in:
Jeong YunWon 2023-05-15 17:40:25 +09:00
parent ba54e64c0e
commit 708d8061d3
5 changed files with 65 additions and 69 deletions

View file

@ -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<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>>,
}
@ -20,7 +20,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>>,
}
@ -36,7 +36,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>>,
}
@ -52,7 +52,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>>,
}
@ -1246,7 +1246,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>,
}
impl<R> Node for Comprehension<R> {
@ -1291,7 +1291,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>,
}
impl<R> Node for Arguments<R> {
@ -1348,7 +1348,7 @@ impl<R> Node for Alias<R> {
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>,
}
impl<R> Node for Withitem<R> {
@ -1361,7 +1361,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>,
}
impl<R> Node for MatchCase<R> {
@ -1522,7 +1522,7 @@ impl<R> Node for Pattern<R> {
#[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, Node};
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,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;

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");