From 6741e80f660ee05ad390cc462d2676db8affd92c Mon Sep 17 00:00:00 2001 From: MarkosTh09 Date: Sun, 26 Nov 2023 15:42:42 +0400 Subject: [PATCH] replace colored-rs with owo-colorize --- Cargo.toml | 2 +- src/compiler.rs | 53 +++++++++++++++++++++++++++---------------------- src/error.rs | 18 ++++++++++------- src/main.rs | 2 +- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9c6738e..b1f3fbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/compiler.rs b/src/compiler.rs index 0497a6a..aa98c9a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,4 +1,4 @@ -use std::iter::Peekable; +use std::{fmt::Write, iter::Peekable}; use crate::parser::{CodeBlock, ComplexToken, Expression}; @@ -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()); @@ -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"; @@ -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!(), @@ -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 diff --git a/src/error.rs b/src/error.rs index 4ca811d..f395e6e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use std::{ sync::{Arc, Mutex, OnceLock}, }; -use colored::Colorize; +use owo_colors::{colors, AnsiColors, OwoColorize}; use crate::lexer::Position; @@ -170,11 +170,10 @@ 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() { @@ -182,7 +181,12 @@ impl fmt::Display for Diagnostic { writeln!(f)?; } writeln!(f, "{:4} | {}", i + position.line, line)?; - write!(f, " | {}{}", " ".repeat(start), pointer)?; + write!( + f, + " | {}{}", + " ".repeat(start), + "^".repeat(end - start).color(color).bold() + )?; } } } diff --git a/src/main.rs b/src/main.rs index bd486f9..00f304f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,