Add codespan_reporting back into the mix.
This commit is contained in:
parent
0960516c4a
commit
1188e2b671
@ -1,4 +1,8 @@
|
|||||||
use clap::Parser;
|
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::constants_table::ConstantsTable;
|
||||||
use dmc_lib::diagnostic::Diagnostic;
|
use dmc_lib::diagnostic::Diagnostic;
|
||||||
use dmc_lib::parser::parse_compilation_unit;
|
use dmc_lib::parser::parse_compilation_unit;
|
||||||
@ -22,16 +26,20 @@ fn main() {
|
|||||||
let input = std::fs::read_to_string(&args.script).unwrap();
|
let input = std::fs::read_to_string(&args.script).unwrap();
|
||||||
|
|
||||||
let mut compilation_unit = parse_compilation_unit(&input);
|
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 mut symbol_table = SymbolTable::new();
|
||||||
|
|
||||||
let gather_names_diagnostics = compilation_unit.gather_declared_names(&mut symbol_table);
|
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);
|
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);
|
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 mut constants_table = ConstantsTable::new();
|
||||||
let asm_functions = compilation_unit.assemble(&symbol_table, &mut constants_table);
|
let asm_functions = compilation_unit.assemble(&symbol_table, &mut constants_table);
|
||||||
@ -62,10 +70,31 @@ fn main() {
|
|||||||
println!("{:?}", result);
|
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() {
|
if !diagnostics.is_empty() {
|
||||||
|
let writer = StandardStream::stderr(ColorChoice::Always);
|
||||||
|
let config = term::Config::default();
|
||||||
for diagnostic in diagnostics {
|
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);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user