Use codespan for *much* prettier error reporting.
This commit is contained in:
39
src/bin.rs
39
src/bin.rs
@@ -1,5 +1,11 @@
|
||||
use clap::Parser;
|
||||
use ngr::syntax::{ParserError, Program};
|
||||
use codespan_reporting::diagnostic::Diagnostic;
|
||||
use codespan_reporting::files::SimpleFiles;
|
||||
use codespan_reporting::term;
|
||||
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
|
||||
use ngr::error::Error;
|
||||
use ngr::syntax::Program;
|
||||
use std::fs;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
@@ -9,21 +15,30 @@ struct CommandLineArguments {
|
||||
output: Option<String>,
|
||||
|
||||
/// The file to parse
|
||||
file: String
|
||||
file: String,
|
||||
}
|
||||
|
||||
fn real_main() -> Result<(), ParserError> {
|
||||
let args = CommandLineArguments::parse();
|
||||
|
||||
let program = Program::from_file(&args.file)?;
|
||||
println!("args: {:?}", args);
|
||||
println!("program: {:?}", program);
|
||||
|
||||
Ok(())
|
||||
fn compile_file(
|
||||
file_database: &mut SimpleFiles<String, String>,
|
||||
initial_file_name: &str,
|
||||
) -> Result<Program, Error> {
|
||||
let initial_file_contents = fs::read_to_string(initial_file_name)?;
|
||||
let initial_file = file_database.add(initial_file_name.to_string(), initial_file_contents);
|
||||
let db_version = file_database.get(initial_file)?;
|
||||
let db_version_source = db_version.source();
|
||||
Ok(Program::parse(initial_file, db_version_source)?)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = real_main() {
|
||||
println!("{}", e);
|
||||
let args = CommandLineArguments::parse();
|
||||
let mut file_database = SimpleFiles::new();
|
||||
let initial_file_name = &args.file;
|
||||
|
||||
if let Err(e) = compile_file(&mut file_database, initial_file_name) {
|
||||
let diagnostic = Diagnostic::from(e);
|
||||
let writer = StandardStream::stderr(ColorChoice::Auto);
|
||||
let config = codespan_reporting::term::Config::default();
|
||||
|
||||
term::emit(&mut writer.lock(), &config, &file_database, &diagnostic).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user