Skip to content

Commit

Permalink
Remove error rendering via ariadne from the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed May 14, 2024
1 parent 8d73cd1 commit 4452939
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 176 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/codegen/parser/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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

44 changes: 44 additions & 0 deletions crates/codegen/parser/runtime/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,47 @@ pub trait Diagnostic: Error {
fn severity(&self) -> Severity;
fn message(&self) -> String;
}

#[cfg(feature = "ariadne")]
pub fn render<D: Diagnostic>(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();
}
53 changes: 1 addition & 52 deletions crates/codegen/parser/runtime/src/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -75,50 +71,3 @@ impl Diagnostic for ParseError {
ToString::to_string(&self)
}
}

pub(crate) fn render_error_report<D: Diagnostic>(
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();
}
7 changes: 4 additions & 3 deletions crates/solidity/outputs/cargo/slang_solidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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 }
Expand All @@ -55,4 +57,3 @@ thiserror = { workspace = true }

[lints]
workspace = true

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/solidity/outputs/cargo/slang_solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _};
}
3 changes: 2 additions & 1 deletion crates/solidity/outputs/cargo/slang_solidity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -51,4 +50,3 @@ thiserror = { workspace = true }

[lints]
workspace = true

5 changes: 4 additions & 1 deletion crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion crates/solidity/testing/sanctuary/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}
Expand Down
2 changes: 0 additions & 2 deletions crates/testlang/outputs/cargo/slang_testlang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -23,4 +22,3 @@ thiserror = { workspace = true }

[lints]
workspace = true

Loading

0 comments on commit 4452939

Please sign in to comment.