new_syntax_error working without full compiler

Fix #4100
This commit is contained in:
Jeong YunWon 2022-08-23 02:04:56 +09:00
parent 4724cc63f7
commit a1640e4aa1
7 changed files with 66 additions and 106 deletions

View file

@ -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")
}