mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
Merge pull request #10 from youknowone/remove-compile-error
Remove CompileError
This commit is contained in:
commit
48920a034e
3 changed files with 2 additions and 58 deletions
|
@ -58,59 +58,3 @@ impl<T> BaseError<T> {
|
|||
BaseError::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CompileError<T> {
|
||||
pub body: BaseError<T>,
|
||||
pub statement: Option<String>,
|
||||
}
|
||||
|
||||
impl<T: StdError + 'static> StdError for CompileError<T> {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
self.body.source()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::ops::Deref for CompileError<T> {
|
||||
type Target = BaseError<T>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.body
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::fmt::Display for CompileError<T>
|
||||
where
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let loc = self.location;
|
||||
if let Some(ref stmt) = self.statement {
|
||||
// visualize the error when location and statement are provided
|
||||
loc.fmt_with(f, &self.error)?;
|
||||
write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^")
|
||||
} else {
|
||||
loc.fmt_with(f, &self.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> CompileError<T> {
|
||||
pub fn from<U>(error: BaseError<U>, source: &str) -> Self
|
||||
where
|
||||
T: From<U>,
|
||||
{
|
||||
let statement = get_statement(source, error.location);
|
||||
CompileError {
|
||||
body: error.into(),
|
||||
statement,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_statement(source: &str, loc: Location) -> Option<String> {
|
||||
if loc.column() == 0 || loc.row() == 0 {
|
||||
return None;
|
||||
}
|
||||
let line = source.split('\n').nth(loc.row() - 1)?.to_owned();
|
||||
Some(line + "\n")
|
||||
}
|
||||
|
|
|
@ -8,6 +8,6 @@ pub mod marshal;
|
|||
mod mode;
|
||||
|
||||
pub use bytecode::*;
|
||||
pub use error::{BaseError, CompileError};
|
||||
pub use error::BaseError;
|
||||
pub use location::Location;
|
||||
pub use mode::Mode;
|
||||
|
|
|
@ -18,7 +18,7 @@ impl Default for Location {
|
|||
impl Location {
|
||||
pub fn fmt_with(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter,
|
||||
f: &mut impl std::fmt::Write,
|
||||
e: &impl std::fmt::Display,
|
||||
) -> std::fmt::Result {
|
||||
write!(f, "{} at line {} column {}", e, self.row(), self.column())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue