mirror of
https://github.com/Noratrieb/ub.git
synced 2026-01-16 09:35:05 +01:00
better diagnostics
This commit is contained in:
parent
6260ca0307
commit
1734bab8cf
2 changed files with 14 additions and 9 deletions
12
src/lib.rs
12
src/lib.rs
|
|
@ -4,9 +4,7 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use ariadne::{Color, Fmt, Label, Report, ReportKind, Source};
|
use ariadne::{Color, Fmt, Label, Report, ReportKind, Source};
|
||||||
use chumsky::prelude::Simple;
|
use parser::Error;
|
||||||
|
|
||||||
use crate::lexer::Token;
|
|
||||||
|
|
||||||
mod ast;
|
mod ast;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
|
|
@ -29,7 +27,7 @@ pub trait Db: salsa::DbWithJar<Jar> {}
|
||||||
impl<DB> Db for DB where DB: ?Sized + salsa::DbWithJar<Jar> {}
|
impl<DB> Db for DB where DB: ?Sized + salsa::DbWithJar<Jar> {}
|
||||||
|
|
||||||
#[salsa::accumulator]
|
#[salsa::accumulator]
|
||||||
pub struct Diagnostics(Simple<Token>);
|
pub struct Diagnostics(Error);
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
#[salsa::db(crate::Jar)]
|
#[salsa::db(crate::Jar)]
|
||||||
|
|
@ -60,13 +58,15 @@ fn aa() {}
|
||||||
let db = Database::default();
|
let db = Database::default();
|
||||||
let source_program = SourceProgram::new(&db, src.to_string(), "uwu.ub".into());
|
let source_program = SourceProgram::new(&db, src.to_string(), "uwu.ub".into());
|
||||||
|
|
||||||
let (file, errors) = parser::parse(&db, source_program);
|
let file = parser::parse(&db, source_program);
|
||||||
|
|
||||||
if let Some(file) = file {
|
if let Some(file) = file {
|
||||||
println!("{}", pretty::pretty_print_ast(&file));
|
println!("{}", pretty::pretty_print_ast(&file));
|
||||||
}
|
}
|
||||||
|
|
||||||
report_errors(src, errors);
|
let errs = parser::parse::accumulated::<Diagnostics>(&db, source_program);
|
||||||
|
|
||||||
|
report_errors(src, errs);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report_errors(src: &str, errors: Vec<parser::Error>) {
|
fn report_errors(src: &str, errors: Vec<parser::Error>) {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
WhileStmt,
|
WhileStmt,
|
||||||
},
|
},
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
SourceProgram,
|
Db, Diagnostics, SourceProgram,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
|
@ -430,13 +430,18 @@ fn file_parser<'src>(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[salsa::tracked]
|
#[salsa::tracked]
|
||||||
pub fn parse(db: &dyn crate::Db, source: SourceProgram) -> (Option<File>, Vec<Error>) {
|
pub fn parse(db: &dyn Db, source: SourceProgram) -> Option<File> {
|
||||||
let lexer = Token::lexer(source.text(db));
|
let lexer = Token::lexer(source.text(db));
|
||||||
let len = lexer.source().len();
|
let len = lexer.source().len();
|
||||||
let state = ParserState::default();
|
let state = ParserState::default();
|
||||||
|
|
||||||
let result = file_parser(source.file_name(db).clone(), &state)
|
let (result, errs) = file_parser(source.file_name(db).clone(), &state)
|
||||||
.parse_recovery_verbose(Stream::from_iter(len..len + 1, lexer.spanned()));
|
.parse_recovery_verbose(Stream::from_iter(len..len + 1, lexer.spanned()));
|
||||||
|
|
||||||
|
for err in errs {
|
||||||
|
Diagnostics::push(db, err);
|
||||||
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue