diff --git a/dm/src/main.rs b/dm/src/main.rs index bfff713..fec9c2a 100644 --- a/dm/src/main.rs +++ b/dm/src/main.rs @@ -1,4 +1,8 @@ use clap::Parser; +use codespan_reporting::diagnostic::Label; +use codespan_reporting::files::SimpleFiles; +use codespan_reporting::term; +use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use dmc_lib::constants_table::ConstantsTable; use dmc_lib::diagnostic::Diagnostic; use dmc_lib::parser::parse_compilation_unit; @@ -22,16 +26,20 @@ fn main() { let input = std::fs::read_to_string(&args.script).unwrap(); let mut compilation_unit = parse_compilation_unit(&input); + + let mut files: SimpleFiles<&str, &str> = SimpleFiles::new(); + let script_file_id = files.add(args.script.to_str().unwrap(), &input); + let mut symbol_table = SymbolTable::new(); let gather_names_diagnostics = compilation_unit.gather_declared_names(&mut symbol_table); - check_and_report_diagnostics(&gather_names_diagnostics); + check_and_report_diagnostics(&files, script_file_id, &gather_names_diagnostics); let name_usages_diagnostics = compilation_unit.check_name_usages(&symbol_table); - check_and_report_diagnostics(&name_usages_diagnostics); + check_and_report_diagnostics(&files, script_file_id, &name_usages_diagnostics); let type_check_diagnostics = compilation_unit.type_check(&symbol_table); - check_and_report_diagnostics(&type_check_diagnostics); + check_and_report_diagnostics(&files, script_file_id, &type_check_diagnostics); let mut constants_table = ConstantsTable::new(); let asm_functions = compilation_unit.assemble(&symbol_table, &mut constants_table); @@ -62,10 +70,31 @@ fn main() { println!("{:?}", result); } -fn check_and_report_diagnostics(diagnostics: &[Diagnostic]) { +fn check_and_report_diagnostics( + files: &SimpleFiles<&str, &str>, + script_file_id: usize, + diagnostics: &[Diagnostic], +) { if !diagnostics.is_empty() { + let writer = StandardStream::stderr(ColorChoice::Always); + let config = term::Config::default(); for diagnostic in diagnostics { - println!("{:?}", diagnostic); + let csr_diagnostic = codespan_reporting::diagnostic::Diagnostic::error() + .with_message(diagnostic.message()) + .with_label(Label::primary( + script_file_id, + diagnostic.start()..diagnostic.end(), + )); + + term::emit_to_write_style(&mut writer.lock(), &config, files, &csr_diagnostic).unwrap(); + } + if diagnostics.len() == 1 { + eprintln!("There was 1 diagnostic. See above for more information."); + } else { + eprintln!( + "There were {} diagnostics. See above for more information.", + diagnostics.len() + ); } std::process::exit(1); }