Move range from Attributed to Nodes (#22)

* Move `range` from `Attributed` to `Node`s

* No Attributed + custom for Range PoC

* Generate all located variants, generate enum implementations

* Implement `Copy` on simple enums

* Move `Suite` to `ranged` and `located`

* Update tests
---------

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
This commit is contained in:
Micha Reiser 2023-05-15 08:08:12 +02:00 committed by GitHub
parent a983f4383f
commit 192379cede
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
126 changed files with 29410 additions and 30670 deletions

View file

@ -1,39 +1,15 @@
use crate::builtin::Attributed;
use rustpython_parser_core::source_code::{SourceLocation, SourceLocator, SourceRange};
use rustpython_parser_core::{
source_code::{SourceLocator, SourceRange},
text_size::TextRange,
};
impl crate::fold::Fold<()> for SourceLocator<'_> {
impl crate::fold::Fold<TextRange> for SourceLocator<'_> {
type TargetU = SourceRange;
type Error = std::convert::Infallible;
#[cold]
fn map_user(&mut self, _user: ()) -> Result<Self::TargetU, Self::Error> {
unreachable!("implemented map_attributed");
}
fn map_attributed<T>(
&mut self,
node: Attributed<T, ()>,
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
let start = self.locate(node.range.start());
let end = self.locate(node.range.end());
Ok(Attributed {
range: node.range,
custom: (start..end).into(),
node: node.node,
})
}
}
impl<T> Attributed<T, SourceRange> {
/// Returns the absolute start position of the node from the beginning of the document.
#[inline]
pub const fn location(&self) -> SourceLocation {
self.custom.start
}
/// Returns the absolute position at which the node ends in the source document.
#[inline]
pub const fn end_location(&self) -> Option<SourceLocation> {
self.custom.end
fn map_user(&mut self, user: TextRange) -> Result<Self::TargetU, Self::Error> {
let start = self.locate(user.start());
let end = self.locate(user.end());
Ok((start..end).into())
}
}