Skip to content

Commit

Permalink
replace colored-rs with owo-colorize
Browse files Browse the repository at this point in the history
  • Loading branch information
Markos-Th09 committed Nov 26, 2023
1 parent bdce6e8 commit 6741e80
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = "1.1.0"
[dependencies]
clap = { version = "4.0.8", features = ["derive"] }
collect_into_rc_slice = "1.0.0"
colored = "2.0.4"
owo-colors = "3.5.0"
rayon = "1.7.0"

[dev-dependencies]
Expand Down
53 changes: 29 additions & 24 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::Peekable;
use std::{fmt::Write, iter::Peekable};

use crate::parser::{CodeBlock, ComplexToken, Expression};

Expand Down Expand Up @@ -345,10 +345,12 @@ fn compile_ast_helper(tree: Expression, scope: usize) -> String {
result += "{\n";
let scope = scope + 1;
result += &indent(scope);
result += &format!(
write!(
result,
"local _internal_expr_{0}, _internal_stop_{0}, _internal_acc_{0} = ",
s
);
)
.unwrap();
result += &compile_expression(scope, expr);
result += ",";
result += &compile_expression(scope, stop.as_ref().unwrap());
Expand Down Expand Up @@ -409,25 +411,28 @@ fn compile_ast_helper(tree: Expression, scope: usize) -> String {
result += &indent_if(tree, scope);
}
Ident { .. } => {
result += &compile_identifier(scope, &ctoken);
result.push(';');
result += &indent_if(tree, scope);
write!(
result,
"{};{}",
compile_identifier(scope, &ctoken),
&indent_if(tree, scope)
)
.unwrap();
}
Call(args) => {
result.push('(');
result += &compile_expressions(scope, args);
result.push(')');
write!(result, "({})", compile_expressions(scope, args)).unwrap();
}
Expr(expr) => {
result.push('(');
result += &compile_expression(scope, expr);
result.push(')');
write!(result, "({})", compile_expression(scope, expr)).unwrap();
}
DoBlock(body) => {
result += "{";
result += &compile_code_block(body.clone(), scope);
result += "}";
result += &indent_if(tree, scope);
write!(
result,
"{{{}}}{}",
compile_code_block(body.clone(), scope),
indent_if(tree, scope)
)
.unwrap();
}
Return(exprs) => {
result += "return";
Expand All @@ -438,8 +443,7 @@ fn compile_ast_helper(tree: Expression, scope: usize) -> String {
}
}
Break => {
result += "break";
result += &indent_if(tree, scope);
write!(result, "{}break", indent(scope)).unwrap();
}

_ => unreachable!(),
Expand All @@ -449,12 +453,13 @@ fn compile_ast_helper(tree: Expression, scope: usize) -> String {

for name in end.iter().rev() {
result.push('\n');
result += &indent(scope);
result += "getmetatable(";
result += name;
result += ").__close(";
result += name;
result += ");";
write!(
result,
"{0}getmetatable({1}).__close({1});",
indent(scope),
name
)
.unwrap();
}

result
Expand Down
18 changes: 11 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::{Arc, Mutex, OnceLock},
};

use colored::Colorize;
use owo_colors::{colors, AnsiColors, OwoColorize};

use crate::lexer::Position;

Expand Down Expand Up @@ -170,19 +170,23 @@ 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(),
let color = match self.level {
DiagnosticLevel::Error => AnsiColors::Red,
DiagnosticLevel::Warning => AnsiColors::Yellow,
DiagnosticLevel::Note => AnsiColors::Blue,
};

for (i, line) in source.lines().enumerate() {
if i > 0 {
writeln!(f)?;
}
writeln!(f, "{:4} | {}", i + position.line, line)?;
write!(f, " | {}{}", " ".repeat(start), pointer)?;
write!(
f,
" | {}{}",
" ".repeat(start),
"^".repeat(end - start).color(color).bold()
)?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cluna::{
lexer::scan_code,
parser::parse_tokens,
};
use colored::Colorize;
use owo_colors::OwoColorize;
use rayon::prelude::*;
use std::{
path::PathBuf,
Expand Down

0 comments on commit 6741e80

Please sign in to comment.