1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: error reporting

This commit is contained in:
Natapat Samutpong 2022-02-10 04:59:34 +07:00
parent 7040bbaa12
commit cb6b35a2dc
3 changed files with 15 additions and 9 deletions

View file

@ -1 +1,3 @@
(print '(Hello, World!))
(print (format "Hello" (format "World")))
)

View file

@ -36,6 +36,8 @@ fn main() {
if args_index < args.len() {
let file_path: &str = &args[args_index];
let file_content: String = read_to_string(file_path).unwrap();
// Used for error reporting
let file_content_joined: String = file_content.split("\n").collect::<Vec<&str>>().join(" ");
let parsed = parser::parse(&file_content);
let mut ast = Vec::new();
@ -43,7 +45,7 @@ fn main() {
match node {
Ok(node) => { ast.push(node); },
Err(error) => {
eprintln!("ERROR: {}", error);
eprintln!("{}", error.at(&file_content_joined));
exit(1);
}
}

View file

@ -167,16 +167,15 @@ pub struct ParseError {
pub pos: (usize, usize),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} at {}", self.kind, self.pos.0)
}
}
impl ParseError {
fn new(kind: ParseErrorKind, pos: (usize, usize)) -> Self {
ParseError { kind, pos }
}
pub fn at(&self, src: &str) -> String {
let snip = &src[(self.pos.0.saturating_sub(5))..(if self.pos.0 + 5 > src.len() { src.len() } else { self.pos.0 + 5 })];
format!("\n{}..{}\n{}\nError: {} at {}", " ".repeat(3), snip, format!("{}^", " ".repeat(10)), self.kind, self.pos.0)
}
}
fn read<'a>(
@ -192,7 +191,10 @@ fn read<'a>(
match token {
"(" => {
parenths += 1;
if parenths == 1 {
block_start = start;
}
stack.push(Tree::List {
vec: Vec::new(),