mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-16 17:45:24 +00:00
parent
42b95a9a95
commit
1fd898c14c
4 changed files with 58 additions and 74 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