Skip to content

Commit

Permalink
add quiet flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Markos-Th09 committed Nov 24, 2023
1 parent 38638ef commit bdce6e8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
target/
Cargo.lock
**/*.rs.bk
Expand Down
4 changes: 3 additions & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ struct Cli {
/// Output directory path.
#[clap(long)]
out_dir: Option<PathBuf>,
/// Supress debug output.
#[clap(short, long)]
quiet: bool,
}

fn compile_file(path: &PathBuf, output: Option<PathBuf>) -> Result<(), Diagnostic> {
eprintln!(" {} {}", "Compiling".green().bold(), path.display());
fn compile_file(path: &PathBuf, output: Option<PathBuf>, 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();
Expand Down Expand Up @@ -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!(
Expand All @@ -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() {
Expand All @@ -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(())
}
Expand Down
17 changes: 10 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ impl<'a> Parser<'a> {

fn parse_identifier_statement(&mut self) -> Result<ComplexToken, Diagnostic> {
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);
Expand Down Expand Up @@ -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<Expression, Diagnostic> {
Expand Down Expand Up @@ -1291,7 +1292,9 @@ pub fn parse_tokens(tokens: &[Token], path: Option<String>) -> Result<Expression
.push_back(complex_token!(parser.current(), Expr(expr)));
parser.advance();
let call = parser.parse_identifier()?;
let ComplexTokenKind::Ident (expr) = &call.kind else {unreachable!()};
let ComplexTokenKind::Ident(expr) = &call.kind else {
unreachable!()
};

if let Some(ComplexTokenKind::Call(_)) = expr.back().map(|t| t.token()) {
parser.expr.push_back(call);
Expand Down

0 comments on commit bdce6e8

Please sign in to comment.