From bdce6e88ebf214c77480f2d6af56650ee0c85496 Mon Sep 17 00:00:00 2001 From: MarkosTh09 Date: Fri, 24 Nov 2023 09:26:10 +0400 Subject: [PATCH] add quiet flag --- .gitignore | 1 + src/compiler.rs | 4 +++- src/error.rs | 13 +++++++------ src/main.rs | 31 ++++++++++++++++++++----------- src/parser.rs | 17 ++++++++++------- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 311b1e0..87cb585 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store target/ Cargo.lock **/*.rs.bk diff --git a/src/compiler.rs b/src/compiler.rs index a13eb65..0497a6a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -82,7 +82,9 @@ fn compile_identifier(scope: usize, ident: &ComplexToken) -> String { use crate::parser::ComplexTokenKind::*; let mut result = String::new(); - let Ident(expr) = ident.token() else {unreachable!()}; + let Ident(expr) = ident.token() else { + unreachable!() + }; for ctoken in expr { result += ctoken.leading(); diff --git a/src/error.rs b/src/error.rs index b11841a..4ca811d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -170,18 +170,19 @@ impl fmt::Display for Diagnostic { let source = source[start..end].to_owned(); let end = position.span.end - start; let start = position.span.start - start; + let pointer = "^".repeat(end - start); + let pointer = match self.level { + DiagnosticLevel::Error => pointer.red().bold(), + DiagnosticLevel::Warning => pointer.yellow().bold(), + DiagnosticLevel::Note => pointer.blue().bold(), + }; for (i, line) in source.lines().enumerate() { if i > 0 { writeln!(f)?; } writeln!(f, "{:4} | {}", i + position.line, line)?; - write!( - f, - " | {}{}", - " ".repeat(start), - "^".repeat(end - start).bright_red() - )?; + write!(f, " | {}{}", " ".repeat(start), pointer)?; } } } diff --git a/src/main.rs b/src/main.rs index e24c9a3..bd486f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,10 +22,15 @@ struct Cli { /// Output directory path. #[clap(long)] out_dir: Option, + /// Supress debug output. + #[clap(short, long)] + quiet: bool, } -fn compile_file(path: &PathBuf, output: Option) -> Result<(), Diagnostic> { - eprintln!(" {} {}", "Compiling".green().bold(), path.display()); +fn compile_file(path: &PathBuf, output: Option, quiet: bool) -> Result<(), Diagnostic> { + if !quiet { + eprintln!(" {} {}", "Compiling".green().bold(), path.display()); + } let code = std::fs::read_to_string(path).map_err(|e| Diagnostic::other(e.to_string()))?; { let files = get_files(); @@ -81,7 +86,7 @@ fn compile() -> Result<(), Diagnostic> { out_dir }) }); - compile_file(&path, output)?; + compile_file(&path, output, args.quiet)?; } else if path.is_dir() { if args.output.is_some() { eprintln!( @@ -92,6 +97,7 @@ fn compile() -> Result<(), Diagnostic> { } let mut stack = vec![path.clone()]; + let files = std::iter::from_fn(move || { while let Some(path) = stack.pop() { if path.is_dir() { @@ -106,14 +112,17 @@ fn compile() -> Result<(), Diagnostic> { files .par_bridge() - .try_for_each(|path| compile_file(&path, None))?; - } - eprintln!( - " {} {} in {}", - "Finished".green().bold(), - path.display(), - format_duration(start.elapsed()) - ); + .try_for_each(|path| compile_file(&path, None, args.quiet))?; + } + + if !args.quiet { + eprintln!( + " {} {} in {}", + "Finished".green().bold(), + path.display(), + format_duration(start.elapsed()) + ); + } Ok(()) } diff --git a/src/parser.rs b/src/parser.rs index bb78280..5485938 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -412,7 +412,9 @@ impl<'a> Parser<'a> { fn parse_identifier_statement(&mut self) -> Result { let ident = self.parse_identifier()?; - let ComplexTokenKind::Ident(expr) = &ident.token() else {unreachable!()}; + let ComplexTokenKind::Ident(expr) = &ident.token() else { + unreachable!() + }; if let Some(ComplexTokenKind::Call(_)) = expr.back().map(|t| t.token()) { return Ok(ident); @@ -717,11 +719,10 @@ impl<'a> Parser<'a> { } } - Err(Diagnostic::expected( - "'end'".to_owned(), - self.path.clone(), - self.position.clone(), - )) + Err( + Diagnostic::expected("'end'".to_owned(), self.path.clone(), self.position.clone()) + .with_hint("if blocks must end with 'end'".to_owned()), + ) } fn parse_expression(&mut self, end: OptionalEnd) -> Result { @@ -1291,7 +1292,9 @@ pub fn parse_tokens(tokens: &[Token], path: Option) -> Result