mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 13:54:55 +00:00
More flexible map_user and fold for new constructor nodes (#53)
* make fold.rs file * Split user_map steps * Fold for new constructor nodes
This commit is contained in:
parent
205ee80033
commit
b48834fe2d
6 changed files with 2516 additions and 833 deletions
|
@ -1,4 +1,6 @@
|
|||
use crate::{builtin, fold::Fold, ConversionFlag};
|
||||
use super::generic::*;
|
||||
|
||||
use crate::{builtin, ConversionFlag};
|
||||
|
||||
pub trait Foldable<T, U> {
|
||||
type Mapped;
|
||||
|
@ -49,7 +51,7 @@ where
|
|||
|
||||
macro_rules! simple_fold {
|
||||
($($t:ty),+$(,)?) => {
|
||||
$(impl<T, U> $crate::fold_helpers::Foldable<T, U> for $t {
|
||||
$(impl<T, U> $crate::fold::Foldable<T, U> for $t {
|
||||
type Mapped = Self;
|
||||
#[inline]
|
||||
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
|
||||
|
@ -70,3 +72,5 @@ simple_fold!(
|
|||
ConversionFlag,
|
||||
builtin::Constant
|
||||
);
|
||||
|
||||
include!("gen/fold.rs");
|
3168
ast/src/gen/fold.rs
3168
ast/src/gen/fold.rs
File diff suppressed because it is too large
Load diff
|
@ -16,13 +16,7 @@ pub trait Node {
|
|||
}
|
||||
|
||||
#[cfg(feature = "fold")]
|
||||
mod fold_helpers;
|
||||
#[cfg(feature = "fold")]
|
||||
pub mod fold {
|
||||
use super::generic::*;
|
||||
|
||||
include!("gen/fold.rs");
|
||||
}
|
||||
pub mod fold;
|
||||
#[cfg(feature = "fold")]
|
||||
pub use fold::Fold;
|
||||
|
||||
|
|
|
@ -15,8 +15,12 @@ impl ConstantOptimizer {
|
|||
impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
||||
type TargetU = U;
|
||||
type Error = std::convert::Infallible;
|
||||
type UserContext = ();
|
||||
|
||||
#[inline(always)]
|
||||
fn will_map_user(&mut self, _user: &U) -> Self::UserContext {}
|
||||
#[inline]
|
||||
fn map_user(&mut self, user: U) -> Result<Self::TargetU, Self::Error> {
|
||||
fn map_user(&mut self, user: U, _context: ()) -> Result<Self::TargetU, Self::Error> {
|
||||
Ok(user)
|
||||
}
|
||||
fn fold_expr(&mut self, node: crate::Expr<U>) -> Result<crate::Expr<U>, Self::Error> {
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
use rustpython_parser_core::{
|
||||
source_code::{SourceLocator, SourceRange},
|
||||
source_code::{SourceLocation, SourceLocator, SourceRange},
|
||||
text_size::TextRange,
|
||||
};
|
||||
|
||||
impl crate::fold::Fold<TextRange> for SourceLocator<'_> {
|
||||
type TargetU = SourceRange;
|
||||
type Error = std::convert::Infallible;
|
||||
type UserContext = SourceLocation;
|
||||
|
||||
fn map_user(&mut self, user: TextRange) -> Result<Self::TargetU, Self::Error> {
|
||||
let start = self.locate(user.start());
|
||||
fn will_map_user(&mut self, user: &TextRange) -> Self::UserContext {
|
||||
self.locate(user.start())
|
||||
}
|
||||
|
||||
fn map_user(
|
||||
&mut self,
|
||||
user: TextRange,
|
||||
start: Self::UserContext,
|
||||
) -> Result<Self::TargetU, Self::Error> {
|
||||
let end = self.locate(user.end());
|
||||
Ok((start..end).into())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue