feat: add better diagnostic rendering
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::fmt::Display;
|
||||
use std::{fmt::Display, ops::Range};
|
||||
|
||||
/// A half-open interval `[start, end)` representing a location in the source text.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -23,6 +23,12 @@ impl Span {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Span> for Range<usize> {
|
||||
fn from(value: Span) -> Self {
|
||||
value.start..value.end
|
||||
}
|
||||
}
|
||||
|
||||
/// A fundamental, categorized unit of source code produced during lexical analysis.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Token<'src> {
|
||||
|
||||
+24
-4
@@ -1,12 +1,16 @@
|
||||
use std::{fs::read_to_string, path::PathBuf, process::exit};
|
||||
|
||||
use clap::Parser as ClapParser;
|
||||
use codespan_reporting::diagnostic::{Diagnostic, Label};
|
||||
use codespan_reporting::files::SimpleFile;
|
||||
use codespan_reporting::term;
|
||||
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
|
||||
|
||||
use crate::frontend::parser::Parser;
|
||||
use crate::frontend::sema::Sema;
|
||||
use crate::middle::builder::MirBuilder;
|
||||
use crate::middle::dce::eliminate_dead_code;
|
||||
use crate::middle::fold::fold_constants;
|
||||
use clap::Parser as ClapParser;
|
||||
|
||||
pub mod backend;
|
||||
pub mod frontend;
|
||||
@@ -37,12 +41,20 @@ fn main() {
|
||||
exit(1);
|
||||
});
|
||||
|
||||
let writer = StandardStream::stderr(ColorChoice::Always);
|
||||
let config = term::Config::default();
|
||||
let file = SimpleFile::new(&cli.input, &content);
|
||||
|
||||
let mut parser = Parser::new(&content);
|
||||
let module = parser.parse_module();
|
||||
|
||||
if let Some(errors) = parser.errors() {
|
||||
for error in errors {
|
||||
eprintln!("{:?}", error);
|
||||
let diagnostic = Diagnostic::error()
|
||||
.with_message(error.message)
|
||||
.with_label(Label::primary((), error.span));
|
||||
|
||||
term::emit_to_write_style(&mut writer.lock(), &config, &file, &diagnostic).unwrap();
|
||||
}
|
||||
|
||||
exit(1);
|
||||
@@ -53,7 +65,11 @@ fn main() {
|
||||
|
||||
if let Some(errors) = sema.errors() {
|
||||
for error in errors {
|
||||
eprintln!("{:?}", error);
|
||||
let diagnostic = Diagnostic::error()
|
||||
.with_message(error.message)
|
||||
.with_label(Label::primary((), error.span));
|
||||
|
||||
term::emit_to_write_style(&mut writer.lock(), &config, &file, &diagnostic).unwrap();
|
||||
}
|
||||
|
||||
exit(1);
|
||||
@@ -64,7 +80,11 @@ fn main() {
|
||||
let warnings = eliminate_dead_code(&mut mir_module);
|
||||
|
||||
for warning in warnings {
|
||||
eprintln!("Warning: {} at {:?}", warning.message, warning.span);
|
||||
let diagnostic = Diagnostic::warning()
|
||||
.with_message(warning.message)
|
||||
.with_label(Label::primary((), warning.span));
|
||||
|
||||
term::emit_to_write_style(&mut writer.lock(), &config, &file, &diagnostic).unwrap();
|
||||
}
|
||||
|
||||
let backend = CraneliftBackend::new();
|
||||
|
||||
Reference in New Issue
Block a user