mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 13:54:55 +00:00
Separate byteoffset ast and located ast
This commit is contained in:
parent
f47dfca4e3
commit
a14e43e03a
21 changed files with 893 additions and 562 deletions
|
@ -1,11 +1,10 @@
|
|||
use ruff_text_size::TextSize;
|
||||
use std::error::Error as StdError;
|
||||
use crate::{text_size::TextSize, Location};
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct BaseError<T> {
|
||||
pub error: T,
|
||||
pub location: TextSize,
|
||||
pub offset: TextSize,
|
||||
pub source_path: String,
|
||||
}
|
||||
|
||||
|
@ -17,11 +16,11 @@ impl<T> std::ops::Deref for BaseError<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> StdError for BaseError<T>
|
||||
impl<T> std::error::Error for BaseError<T>
|
||||
where
|
||||
T: StdError + 'static,
|
||||
T: std::error::Error + 'static,
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
Some(&self.error)
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +34,7 @@ where
|
|||
f,
|
||||
"{} at byte offset {}",
|
||||
&self.error,
|
||||
u32::from(self.location)
|
||||
u32::from(self.offset)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +50,7 @@ impl<T> BaseError<T> {
|
|||
{
|
||||
Self {
|
||||
error: obj.error.into(),
|
||||
location: obj.location,
|
||||
offset: obj.offset,
|
||||
source_path: obj.source_path,
|
||||
}
|
||||
}
|
||||
|
@ -62,4 +61,64 @@ impl<T> BaseError<T> {
|
|||
{
|
||||
BaseError::from(self)
|
||||
}
|
||||
|
||||
pub fn into_located<U>(self, locator: &str) -> LocatedError<U>
|
||||
where
|
||||
T: Into<U>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct LocatedError<T> {
|
||||
pub error: T,
|
||||
pub location: Location,
|
||||
pub source_path: String,
|
||||
}
|
||||
|
||||
impl<T> LocatedError<T> {
|
||||
pub fn error(self) -> T {
|
||||
self.error
|
||||
}
|
||||
|
||||
pub fn from<U>(obj: LocatedError<U>) -> Self
|
||||
where
|
||||
U: Into<T>,
|
||||
{
|
||||
Self {
|
||||
error: obj.error.into(),
|
||||
location: obj.location,
|
||||
source_path: obj.source_path,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into<U>(self) -> LocatedError<U>
|
||||
where
|
||||
T: Into<U>,
|
||||
{
|
||||
LocatedError::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Display for LocatedError<T>
|
||||
where
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{} at row {} col {}",
|
||||
&self.error, self.location.row, self.location.column,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::error::Error for LocatedError<T>
|
||||
where
|
||||
T: std::error::Error + 'static,
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
Some(&self.error)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,13 @@ pub mod marshal;
|
|||
mod mode;
|
||||
|
||||
pub use bytecode::*;
|
||||
pub use error::BaseError;
|
||||
pub use location::Location;
|
||||
pub use error::{BaseError, LocatedError};
|
||||
pub use location::{Location, LocationRange};
|
||||
pub use mode::Mode;
|
||||
|
||||
pub use ruff_text_size as text_size; // re-export mandatory and frequently accessed dependency
|
||||
|
||||
// FIXME: temp code
|
||||
pub fn to_location(offset: &text_size::TextSize, source: &str) -> Location {
|
||||
todo!()
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Source code location.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Location {
|
||||
pub(super) row: u32,
|
||||
pub(super) column: u32,
|
||||
|
@ -96,6 +93,8 @@ impl Location {
|
|||
}
|
||||
}
|
||||
|
||||
pub type LocationRange = std::ops::Range<Location>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue