mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
parent
4724cc63f7
commit
a1640e4aa1
7 changed files with 66 additions and 106 deletions
|
@ -1,6 +1,5 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::Location;
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct BaseError<T> {
|
||||
|
@ -87,3 +86,24 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue