mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 21:55:26 +00:00
Generic types to generic
This commit is contained in:
parent
ba54e64c0e
commit
708d8061d3
5 changed files with 65 additions and 69 deletions
|
@ -290,7 +290,7 @@ class StructVisitor(EmitVisitor):
|
||||||
if has_attributes:
|
if has_attributes:
|
||||||
self.emit("pub range: R,", depth + 1)
|
self.emit("pub range: R,", depth + 1)
|
||||||
else:
|
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):
|
def simple_sum(self, sum, name, depth):
|
||||||
rust_name = rust_type_name(name)
|
rust_name = rust_type_name(name)
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use crate::text_size::TextRange;
|
use crate::text_size::TextRange;
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ModModule<R = TextRange> {
|
pub struct ModModule<R = TextRange> {
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
pub body: Vec<Stmt<R>>,
|
pub body: Vec<Stmt<R>>,
|
||||||
pub type_ignores: Vec<TypeIgnore<R>>,
|
pub type_ignores: Vec<TypeIgnore<R>>,
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ impl<R> From<ModModule<R>> for Mod<R> {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ModInteractive<R = TextRange> {
|
pub struct ModInteractive<R = TextRange> {
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
pub body: Vec<Stmt<R>>,
|
pub body: Vec<Stmt<R>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ impl<R> From<ModInteractive<R>> for Mod<R> {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ModExpression<R = TextRange> {
|
pub struct ModExpression<R = TextRange> {
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
pub body: Box<Expr<R>>,
|
pub body: Box<Expr<R>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ impl<R> From<ModExpression<R>> for Mod<R> {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ModFunctionType<R = TextRange> {
|
pub struct ModFunctionType<R = TextRange> {
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
pub argtypes: Vec<Expr<R>>,
|
pub argtypes: Vec<Expr<R>>,
|
||||||
pub returns: Box<Expr<R>>,
|
pub returns: Box<Expr<R>>,
|
||||||
}
|
}
|
||||||
|
@ -1246,7 +1246,7 @@ pub struct Comprehension<R = TextRange> {
|
||||||
pub iter: Expr<R>,
|
pub iter: Expr<R>,
|
||||||
pub ifs: Vec<Expr<R>>,
|
pub ifs: Vec<Expr<R>>,
|
||||||
pub is_async: bool,
|
pub is_async: bool,
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R> Node for Comprehension<R> {
|
impl<R> Node for Comprehension<R> {
|
||||||
|
@ -1291,7 +1291,7 @@ pub struct Arguments<R = TextRange> {
|
||||||
pub kw_defaults: Vec<Expr<R>>,
|
pub kw_defaults: Vec<Expr<R>>,
|
||||||
pub kwarg: Option<Box<Arg<R>>>,
|
pub kwarg: Option<Box<Arg<R>>>,
|
||||||
pub defaults: Vec<Expr<R>>,
|
pub defaults: Vec<Expr<R>>,
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R> Node for Arguments<R> {
|
impl<R> Node for Arguments<R> {
|
||||||
|
@ -1348,7 +1348,7 @@ impl<R> Node for Alias<R> {
|
||||||
pub struct Withitem<R = TextRange> {
|
pub struct Withitem<R = TextRange> {
|
||||||
pub context_expr: Expr<R>,
|
pub context_expr: Expr<R>,
|
||||||
pub optional_vars: Option<Box<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> {
|
impl<R> Node for Withitem<R> {
|
||||||
|
@ -1361,7 +1361,7 @@ pub struct MatchCase<R = TextRange> {
|
||||||
pub pattern: Pattern<R>,
|
pub pattern: Pattern<R>,
|
||||||
pub guard: Option<Box<Expr<R>>>,
|
pub guard: Option<Box<Expr<R>>>,
|
||||||
pub body: Vec<Stmt<R>>,
|
pub body: Vec<Stmt<R>>,
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R> Node for MatchCase<R> {
|
impl<R> Node for MatchCase<R> {
|
||||||
|
@ -1522,7 +1522,7 @@ impl<R> Node for Pattern<R> {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct TypeIgnoreTypeIgnore<R = TextRange> {
|
pub struct TypeIgnoreTypeIgnore<R = TextRange> {
|
||||||
pub range: crate::ranged::OptionalRange<R>,
|
pub range: OptionalRange<R>,
|
||||||
pub lineno: Int,
|
pub lineno: Int,
|
||||||
pub tag: String,
|
pub tag: String,
|
||||||
}
|
}
|
||||||
|
|
48
ast/src/generic.rs
Normal file
48
ast/src/generic.rs
Normal 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");
|
|
@ -1,23 +1,13 @@
|
||||||
mod builtin;
|
mod builtin;
|
||||||
#[cfg(feature = "fold")]
|
mod generic;
|
||||||
mod fold_helpers;
|
|
||||||
mod generic {
|
|
||||||
#![allow(clippy::derive_partial_eq_without_eq)]
|
|
||||||
use super::Node;
|
|
||||||
pub use crate::builtin::*;
|
|
||||||
|
|
||||||
include!("gen/generic.rs");
|
|
||||||
}
|
|
||||||
mod impls;
|
mod impls;
|
||||||
mod ranged;
|
mod ranged;
|
||||||
#[cfg(feature = "location")]
|
|
||||||
mod source_locator;
|
|
||||||
#[cfg(feature = "unparse")]
|
#[cfg(feature = "unparse")]
|
||||||
mod unparse;
|
mod unparse;
|
||||||
|
|
||||||
pub use builtin::*;
|
pub use builtin::*;
|
||||||
pub use generic::*;
|
pub use generic::*;
|
||||||
pub use ranged::{EmptyRange, OptionalRange, Ranged, Suite};
|
pub use ranged::Ranged;
|
||||||
pub use rustpython_parser_core::{text_size, ConversionFlag};
|
pub use rustpython_parser_core::{text_size, ConversionFlag};
|
||||||
|
|
||||||
pub trait Node {
|
pub trait Node {
|
||||||
|
@ -25,6 +15,8 @@ pub trait Node {
|
||||||
const FIELD_NAMES: &'static [&'static str];
|
const FIELD_NAMES: &'static [&'static str];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "fold")]
|
||||||
|
mod fold_helpers;
|
||||||
#[cfg(feature = "fold")]
|
#[cfg(feature = "fold")]
|
||||||
pub mod fold {
|
pub mod fold {
|
||||||
use super::generic::*;
|
use super::generic::*;
|
||||||
|
@ -43,14 +35,15 @@ mod visitor {
|
||||||
|
|
||||||
#[cfg(feature = "location")]
|
#[cfg(feature = "location")]
|
||||||
pub mod located;
|
pub mod located;
|
||||||
|
#[cfg(feature = "location")]
|
||||||
|
mod source_locator;
|
||||||
#[cfg(feature = "location")]
|
#[cfg(feature = "location")]
|
||||||
pub use rustpython_parser_core::source_code;
|
pub use rustpython_parser_core::source_code;
|
||||||
|
|
||||||
#[cfg(feature = "visitor")]
|
#[cfg(feature = "visitor")]
|
||||||
pub use visitor::Visitor;
|
pub use visitor::Visitor;
|
||||||
|
|
||||||
#[cfg(feature = "constant-optimization")]
|
#[cfg(feature = "constant-optimization")]
|
||||||
mod optimizer;
|
mod optimizer;
|
||||||
|
|
||||||
#[cfg(feature = "constant-optimization")]
|
#[cfg(feature = "constant-optimization")]
|
||||||
pub use optimizer::ConstantOptimizer;
|
pub use optimizer::ConstantOptimizer;
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use crate::text_size::{TextRange, TextSize};
|
use crate::text_size::{TextRange, TextSize};
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
pub use crate::builtin::*;
|
pub use crate::builtin::*;
|
||||||
use crate::Stmt;
|
|
||||||
|
|
||||||
pub trait Ranged {
|
pub trait Ranged {
|
||||||
fn range(&self) -> TextRange;
|
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");
|
include!("gen/ranged.rs");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue