diff --git a/Cargo.lock b/Cargo.lock index 56281d426a..14f4e7a8e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1774,7 +1774,6 @@ dependencies = [ name = "slang_solidity_node_addon" version = "0.14.2" dependencies = [ - "ariadne", "napi", "napi-build", "napi-derive", @@ -1793,7 +1792,6 @@ name = "slang_testlang" version = "0.14.2" dependencies = [ "anyhow", - "ariadne", "codegen_parser_generator", "infra_utils", "nom", @@ -1809,7 +1807,6 @@ dependencies = [ name = "slang_testlang_node_addon" version = "0.14.2" dependencies = [ - "ariadne", "napi", "napi-build", "napi-derive", diff --git a/crates/codegen/parser/runtime/Cargo.toml b/crates/codegen/parser/runtime/Cargo.toml index 9fa7816a75..7664a8ab5c 100644 --- a/crates/codegen/parser/runtime/Cargo.toml +++ b/crates/codegen/parser/runtime/Cargo.toml @@ -8,7 +8,7 @@ publish = false description = "Language-agnostic parser runtime copied over by codegen" [dependencies] -ariadne = { workspace = true } +ariadne = { workspace = true, optional = true } napi = { workspace = true, optional = true } napi-derive = { workspace = true, optional = true } nom = { workspace = true } @@ -22,7 +22,7 @@ strum_macros = { workspace = true } [features] default = ["slang_napi_interfaces"] slang_napi_interfaces = ["dep:napi", "dep:napi-derive", "dep:serde_json"] +ariadne = ["dep:ariadne"] [lints] workspace = true - diff --git a/crates/codegen/parser/runtime/src/diagnostic.rs b/crates/codegen/parser/runtime/src/diagnostic.rs index 15042bcf5e..f528c14f0c 100644 --- a/crates/codegen/parser/runtime/src/diagnostic.rs +++ b/crates/codegen/parser/runtime/src/diagnostic.rs @@ -17,3 +17,47 @@ pub trait Diagnostic: Error { fn severity(&self) -> Severity; fn message(&self) -> String; } + +#[cfg(feature = "ariadne")] +pub fn render(error: &D, source_id: &str, source: &str, with_color: bool) -> String { + use ariadne::{Color, Config, Label, Report, ReportKind, Source}; + + use crate::text_index::TextRangeExtensions as _; + + let kind = match error.severity() { + Severity::Error => ReportKind::Error, + Severity::Warning => ReportKind::Warning, + Severity::Information => ReportKind::Advice, + Severity::Hint => ReportKind::Advice, + }; + + let color = if with_color { Color::Red } else { Color::Unset }; + + let message = error.message(); + + if source.is_empty() { + return format!("{kind}: {message}\n ─[{source_id}:0:0]"); + } + + let range = error.range().char(); + + let report = Report::build(kind, source_id, range.start) + .with_config(Config::default().with_color(with_color)) + .with_message(message) + .with_label( + Label::new((source_id, range)) + .with_color(color) + .with_message("Error occurred here."), + ) + .finish(); + + let mut result = vec![]; + report + .write((source_id, Source::from(&source)), &mut result) + .expect("Failed to write report"); + + return String::from_utf8(result) + .expect("Failed to convert report to utf8") + .trim() + .to_string(); +} diff --git a/crates/codegen/parser/runtime/src/parse_error.rs b/crates/codegen/parser/runtime/src/parse_error.rs index c24a759285..54baa77530 100644 --- a/crates/codegen/parser/runtime/src/parse_error.rs +++ b/crates/codegen/parser/runtime/src/parse_error.rs @@ -4,7 +4,7 @@ use std::fmt; use crate::diagnostic::{self, Diagnostic}; use crate::kinds::TokenKind; -use crate::text_index::{TextRange, TextRangeExtensions}; +use crate::text_index::TextRange; #[derive(Debug, PartialEq, Eq, Clone)] pub struct ParseError { @@ -16,10 +16,6 @@ impl ParseError { pub fn text_range(&self) -> &TextRange { &self.text_range } - - pub fn to_error_report(&self, source_id: &str, source: &str, with_color: bool) -> String { - render_error_report(self, source_id, source, with_color) - } } impl ParseError { @@ -75,50 +71,3 @@ impl Diagnostic for ParseError { ToString::to_string(&self) } } - -pub(crate) fn render_error_report( - error: &D, - source_id: &str, - source: &str, - with_color: bool, -) -> String { - use ariadne::{Color, Config, Label, Report, ReportKind, Source}; - - let kind = match error.severity() { - diagnostic::Severity::Error => ReportKind::Error, - diagnostic::Severity::Warning => ReportKind::Warning, - diagnostic::Severity::Information => ReportKind::Advice, - diagnostic::Severity::Hint => ReportKind::Advice, - }; - - let color = if with_color { Color::Red } else { Color::Unset }; - - let message = error.message(); - - if source.is_empty() { - return format!("{kind}: {message}\n ─[{source_id}:0:0]"); - } - - let range = error.range().char(); - - let mut builder = Report::build(kind, source_id, range.start) - .with_config(Config::default().with_color(with_color)) - .with_message(message); - - builder.add_label( - Label::new((source_id, range)) - .with_color(color) - .with_message("Error occurred here.".to_string()), - ); - - let mut result = vec![]; - builder - .finish() - .write((source_id, Source::from(&source)), &mut result) - .expect("Failed to write report"); - - return String::from_utf8(result) - .expect("Failed to convert report to utf8") - .trim() - .to_string(); -} diff --git a/crates/solidity/outputs/cargo/slang_solidity/Cargo.toml b/crates/solidity/outputs/cargo/slang_solidity/Cargo.toml index 1d9e8e7cc5..8a86ed4f37 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/Cargo.toml +++ b/crates/solidity/outputs/cargo/slang_solidity/Cargo.toml @@ -33,7 +33,9 @@ required-features = ["cli"] [features] default = ["cli"] -cli = ["dep:anyhow", "dep:clap", "dep:serde_json"] +cli = ["dep:anyhow", "dep:clap", "dep:serde_json", "ariadne"] +# This is meant to be used by the CLI or internally only. +ariadne = ["dep:ariadne"] [build-dependencies] # __REMOVE_THIS_LINE_DURING_CARGO_PUBLISH__ anyhow = { workspace = true } # __REMOVE_THIS_LINE_DURING_CARGO_PUBLISH__ @@ -43,7 +45,7 @@ solidity_language = { workspace = true } # __REMOVE_THIS_LINE_DURING_CARG [dependencies] anyhow = { workspace = true, optional = true } -ariadne = { workspace = true } +ariadne = { workspace = true, optional = true } clap = { workspace = true, optional = true } nom = { workspace = true } semver = { workspace = true } @@ -55,4 +57,3 @@ thiserror = { workspace = true } [lints] workspace = true - diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/diagnostic.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/diagnostic.rs index b32446bf2a..c6e4a42a97 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/diagnostic.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/diagnostic.rs @@ -19,3 +19,47 @@ pub trait Diagnostic: Error { fn severity(&self) -> Severity; fn message(&self) -> String; } + +#[cfg(feature = "ariadne")] +pub fn render(error: &D, source_id: &str, source: &str, with_color: bool) -> String { + use ariadne::{Color, Config, Label, Report, ReportKind, Source}; + + use crate::text_index::TextRangeExtensions as _; + + let kind = match error.severity() { + Severity::Error => ReportKind::Error, + Severity::Warning => ReportKind::Warning, + Severity::Information => ReportKind::Advice, + Severity::Hint => ReportKind::Advice, + }; + + let color = if with_color { Color::Red } else { Color::Unset }; + + let message = error.message(); + + if source.is_empty() { + return format!("{kind}: {message}\n ─[{source_id}:0:0]"); + } + + let range = error.range().char(); + + let report = Report::build(kind, source_id, range.start) + .with_config(Config::default().with_color(with_color)) + .with_message(message) + .with_label( + Label::new((source_id, range)) + .with_color(color) + .with_message("Error occurred here."), + ) + .finish(); + + let mut result = vec![]; + report + .write((source_id, Source::from(&source)), &mut result) + .expect("Failed to write report"); + + return String::from_utf8(result) + .expect("Failed to convert report to utf8") + .trim() + .to_string(); +} diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/parse_error.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/parse_error.rs index c033d04588..cc5c56f48c 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/parse_error.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/parse_error.rs @@ -6,7 +6,7 @@ use std::fmt; use crate::diagnostic::{self, Diagnostic}; use crate::kinds::TokenKind; -use crate::text_index::{TextRange, TextRangeExtensions}; +use crate::text_index::TextRange; #[derive(Debug, PartialEq, Eq, Clone)] pub struct ParseError { @@ -18,10 +18,6 @@ impl ParseError { pub fn text_range(&self) -> &TextRange { &self.text_range } - - pub fn to_error_report(&self, source_id: &str, source: &str, with_color: bool) -> String { - render_error_report(self, source_id, source, with_color) - } } impl ParseError { @@ -77,50 +73,3 @@ impl Diagnostic for ParseError { ToString::to_string(&self) } } - -pub(crate) fn render_error_report( - error: &D, - source_id: &str, - source: &str, - with_color: bool, -) -> String { - use ariadne::{Color, Config, Label, Report, ReportKind, Source}; - - let kind = match error.severity() { - diagnostic::Severity::Error => ReportKind::Error, - diagnostic::Severity::Warning => ReportKind::Warning, - diagnostic::Severity::Information => ReportKind::Advice, - diagnostic::Severity::Hint => ReportKind::Advice, - }; - - let color = if with_color { Color::Red } else { Color::Unset }; - - let message = error.message(); - - if source.is_empty() { - return format!("{kind}: {message}\n ─[{source_id}:0:0]"); - } - - let range = error.range().char(); - - let mut builder = Report::build(kind, source_id, range.start) - .with_config(Config::default().with_color(with_color)) - .with_message(message); - - builder.add_label( - Label::new((source_id, range)) - .with_color(color) - .with_message("Error occurred here.".to_string()), - ); - - let mut result = vec![]; - builder - .finish() - .write((source_id, Source::from(&source)), &mut result) - .expect("Failed to write report"); - - return String::from_utf8(result) - .expect("Failed to convert report to utf8") - .trim() - .to_string(); -} diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/lib.rs b/crates/solidity/outputs/cargo/slang_solidity/src/lib.rs index cbf7722d01..8a9148b08a 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/lib.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/lib.rs @@ -9,5 +9,5 @@ pub use generated::*; // https://github.com/rust-lang/cargo/issues/1982 #[cfg(feature = "cli")] mod supress_cli_dependencies { - use {anyhow as _, clap as _, serde_json as _}; + use {anyhow as _, ariadne as _, clap as _, serde_json as _}; } diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/main.rs b/crates/solidity/outputs/cargo/slang_solidity/src/main.rs index 54dfb99241..6d90640471 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/main.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/main.rs @@ -62,7 +62,8 @@ fn execute_parse_command(file_path_string: &str, version: Version, json: bool) - let errors = output.errors(); for error in errors { - let report = error.to_error_report(file_path_string, &input, /* with_color */ true); + const COLOR: bool = true; + let report = slang_solidity::diagnostic::render(error, file_path_string, &input, COLOR); eprintln!("{report}"); } diff --git a/crates/solidity/outputs/cargo/slang_solidity_node_addon/Cargo.toml b/crates/solidity/outputs/cargo/slang_solidity_node_addon/Cargo.toml index bd39d17ecc..dd06b9faa9 100644 --- a/crates/solidity/outputs/cargo/slang_solidity_node_addon/Cargo.toml +++ b/crates/solidity/outputs/cargo/slang_solidity_node_addon/Cargo.toml @@ -38,7 +38,6 @@ napi-build = { workspace = true } slang_solidity = { workspace = true } [dependencies] -ariadne = { workspace = true } napi = { workspace = true } napi-derive = { workspace = true } nom = { workspace = true } @@ -51,4 +50,3 @@ thiserror = { workspace = true } [lints] workspace = true - diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs index ca19fd5fac..435bbe459f 100644 --- a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs @@ -49,7 +49,10 @@ pub fn run(parser_name: &str, test_name: &str) -> Result<()> { let errors = output .errors() .iter() - .map(|error| error.to_error_report(source_id, &source, /* with_color */ false)) + .map(|error| { + const COLOR: bool = false; + slang_solidity::diagnostic::render(error, source_id, &source, COLOR) + }) .collect(); let cursor = output.create_tree_cursor().with_labels(); diff --git a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs index 3524b0c99c..f46d0cb41a 100644 --- a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs +++ b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs @@ -26,7 +26,13 @@ fn using_the_parser() -> Result<()> { // --8<-- [start:print-errors] for error in parse_output.errors() { - eprintln!("{}", error.to_error_report(input_path, source, true)); + use slang_solidity::diagnostic::Diagnostic as _; + + eprintln!( + "Error at byte offset {offset}: {message}", + offset = error.range().start.utf8, + message = error.message() + ); } // --8<-- [end:print-errors] diff --git a/crates/solidity/outputs/npm/tests/src/doc-examples/using-the-parser.ts b/crates/solidity/outputs/npm/tests/src/doc-examples/using-the-parser.ts index d60c12632a..7606fa9395 100644 --- a/crates/solidity/outputs/npm/tests/src/doc-examples/using-the-parser.ts +++ b/crates/solidity/outputs/npm/tests/src/doc-examples/using-the-parser.ts @@ -21,7 +21,7 @@ test("using the parser", async () => { // --8<-- [start:print-errors] for (const error of parseOutput.errors()) { let diagnostic = error.toDiagnostic(); - console.error(`Encountered an error: ${diagnostic.message}`); + console.error(`Encountered an error: ${diagnostic.message()}`); } // --8<-- [end:print-errors] diff --git a/crates/solidity/testing/sanctuary/src/tests.rs b/crates/solidity/testing/sanctuary/src/tests.rs index 773c847a82..eeac9ed63f 100644 --- a/crates/solidity/testing/sanctuary/src/tests.rs +++ b/crates/solidity/testing/sanctuary/src/tests.rs @@ -112,7 +112,7 @@ pub fn run_test(file: &SourceFile, events: &Events) -> Result<()> { let source_id = file.path.strip_repo_root()?.unwrap_str(); for error in output.errors() { - let report = error.to_error_report(source_id, &source, with_color); + let report = slang_solidity::diagnostic::render(error, source_id, &source, with_color); events.parse_error(format!("[{version}] {report}")); } diff --git a/crates/testlang/outputs/cargo/slang_testlang/Cargo.toml b/crates/testlang/outputs/cargo/slang_testlang/Cargo.toml index 3696e97670..f0acb37088 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/Cargo.toml +++ b/crates/testlang/outputs/cargo/slang_testlang/Cargo.toml @@ -13,7 +13,6 @@ infra_utils = { workspace = true } testlang_language = { workspace = true } [dependencies] -ariadne = { workspace = true } nom = { workspace = true } semver = { workspace = true } serde = { workspace = true } @@ -23,4 +22,3 @@ thiserror = { workspace = true } [lints] workspace = true - diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/diagnostic.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/diagnostic.rs index b32446bf2a..c6e4a42a97 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/diagnostic.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/diagnostic.rs @@ -19,3 +19,47 @@ pub trait Diagnostic: Error { fn severity(&self) -> Severity; fn message(&self) -> String; } + +#[cfg(feature = "ariadne")] +pub fn render(error: &D, source_id: &str, source: &str, with_color: bool) -> String { + use ariadne::{Color, Config, Label, Report, ReportKind, Source}; + + use crate::text_index::TextRangeExtensions as _; + + let kind = match error.severity() { + Severity::Error => ReportKind::Error, + Severity::Warning => ReportKind::Warning, + Severity::Information => ReportKind::Advice, + Severity::Hint => ReportKind::Advice, + }; + + let color = if with_color { Color::Red } else { Color::Unset }; + + let message = error.message(); + + if source.is_empty() { + return format!("{kind}: {message}\n ─[{source_id}:0:0]"); + } + + let range = error.range().char(); + + let report = Report::build(kind, source_id, range.start) + .with_config(Config::default().with_color(with_color)) + .with_message(message) + .with_label( + Label::new((source_id, range)) + .with_color(color) + .with_message("Error occurred here."), + ) + .finish(); + + let mut result = vec![]; + report + .write((source_id, Source::from(&source)), &mut result) + .expect("Failed to write report"); + + return String::from_utf8(result) + .expect("Failed to convert report to utf8") + .trim() + .to_string(); +} diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/parse_error.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/parse_error.rs index c033d04588..cc5c56f48c 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/parse_error.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/parse_error.rs @@ -6,7 +6,7 @@ use std::fmt; use crate::diagnostic::{self, Diagnostic}; use crate::kinds::TokenKind; -use crate::text_index::{TextRange, TextRangeExtensions}; +use crate::text_index::TextRange; #[derive(Debug, PartialEq, Eq, Clone)] pub struct ParseError { @@ -18,10 +18,6 @@ impl ParseError { pub fn text_range(&self) -> &TextRange { &self.text_range } - - pub fn to_error_report(&self, source_id: &str, source: &str, with_color: bool) -> String { - render_error_report(self, source_id, source, with_color) - } } impl ParseError { @@ -77,50 +73,3 @@ impl Diagnostic for ParseError { ToString::to_string(&self) } } - -pub(crate) fn render_error_report( - error: &D, - source_id: &str, - source: &str, - with_color: bool, -) -> String { - use ariadne::{Color, Config, Label, Report, ReportKind, Source}; - - let kind = match error.severity() { - diagnostic::Severity::Error => ReportKind::Error, - diagnostic::Severity::Warning => ReportKind::Warning, - diagnostic::Severity::Information => ReportKind::Advice, - diagnostic::Severity::Hint => ReportKind::Advice, - }; - - let color = if with_color { Color::Red } else { Color::Unset }; - - let message = error.message(); - - if source.is_empty() { - return format!("{kind}: {message}\n ─[{source_id}:0:0]"); - } - - let range = error.range().char(); - - let mut builder = Report::build(kind, source_id, range.start) - .with_config(Config::default().with_color(with_color)) - .with_message(message); - - builder.add_label( - Label::new((source_id, range)) - .with_color(color) - .with_message("Error occurred here.".to_string()), - ); - - let mut result = vec![]; - builder - .finish() - .write((source_id, Source::from(&source)), &mut result) - .expect("Failed to write report"); - - return String::from_utf8(result) - .expect("Failed to convert report to utf8") - .trim() - .to_string(); -} diff --git a/crates/testlang/outputs/cargo/slang_testlang_node_addon/Cargo.toml b/crates/testlang/outputs/cargo/slang_testlang_node_addon/Cargo.toml index 9a7652aa0b..fd3dc553b2 100644 --- a/crates/testlang/outputs/cargo/slang_testlang_node_addon/Cargo.toml +++ b/crates/testlang/outputs/cargo/slang_testlang_node_addon/Cargo.toml @@ -38,7 +38,6 @@ napi-build = { workspace = true } slang_testlang = { workspace = true } [dependencies] -ariadne = { workspace = true } napi = { workspace = true } napi-derive = { workspace = true } nom = { workspace = true } @@ -51,4 +50,3 @@ thiserror = { workspace = true } [lints] workspace = true -