Skip to content

Commit 4013357

Browse files
committed
Use ariadne
#1323
1 parent 67fc2ab commit 4013357

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

justfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export JUST_LOG := log
1212
watch +args='test':
1313
cargo watch --clear --exec '{{ args }}'
1414

15-
[group: 'test']
15+
[group('test')]
1616
test:
1717
cargo test --all
1818

@@ -34,6 +34,7 @@ run:
3434
filter PATTERN:
3535
cargo test {{PATTERN}}
3636

37+
[group: 'misc']
3738
[group: 'misc']
3839
build:
3940
cargo build

src/compile_error.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,114 @@ impl<'src> CompileError<'src> {
1919
}
2020
}
2121

22+
pub(crate) fn render_compile_error(error: &CompileError) {
23+
use ariadne::{Label, Report, ReportKind, Source};
24+
25+
let token = error.token;
26+
let source = Source::from(token.src);
27+
28+
let start = token.offset;
29+
let end = token.offset + token.length;
30+
31+
let path = format!("{}", token.path.display());
32+
let label = Label::new((&path, start..end));
33+
34+
let report = Report::build(ReportKind::Error, &path, start);
35+
36+
let report = match &*error.kind {
37+
CompileErrorKind::AttributeArgumentCountMismatch {
38+
attribute,
39+
found,
40+
min,
41+
max,
42+
} => {
43+
let label_msg = format!("Found {found} {}", Count("argument", *found));
44+
45+
let note = if min == max {
46+
format!("`{attribute}` takes {min} {}", Count("argument", *min))
47+
} else {
48+
format!("`{attribute}` takes between {min} and {max} arguments")
49+
};
50+
51+
report
52+
.with_code("E01")
53+
.with_message("Attribute argument count mismatch")
54+
.with_label(label.with_message(label_msg))
55+
.with_note(note)
56+
.finish()
57+
}
58+
/*
59+
CompileErrorKind::BacktickShebang => todo!(),
60+
CompileErrorKind::CircularRecipeDependency { recipe, circle } => todo!(),
61+
CompileErrorKind::CircularVariableDependency { variable, circle } => todo!(),
62+
CompileErrorKind::DependencyArgumentCountMismatch { dependency, found, min, max } => todo!(),
63+
CompileErrorKind::Redefinition { first, first_type, name, second_type } => todo!(),
64+
*/
65+
CompileErrorKind::DuplicateAttribute { attribute, first } => {
66+
let original_label = source
67+
.line(*first)
68+
.map(|line| Label::new((&path, line.span())).with_message("original"));
69+
70+
let mut report = report
71+
.with_code("E02")
72+
.with_message(format!("Duplicate attribute `{attribute}`"));
73+
if let Some(original) = original_label {
74+
report = report.with_label(original);
75+
}
76+
report.with_label(label.with_message("duplicate")).finish()
77+
}
78+
_ => {
79+
let message = format!("{error}");
80+
report.with_message(message).with_label(label).finish()
81+
} /*
82+
CompileErrorKind::DuplicateParameter { recipe, parameter } => todo!(),
83+
CompileErrorKind::DuplicateSet { setting, first } => todo!(),
84+
CompileErrorKind::DuplicateVariable { variable } => todo!(),
85+
CompileErrorKind::DuplicateUnexport { variable } => todo!(),
86+
CompileErrorKind::ExpectedKeyword { expected, found } => todo!(),
87+
CompileErrorKind::ExportUnexported { variable } => todo!(),
88+
CompileErrorKind::ExtraLeadingWhitespace => todo!(),
89+
CompileErrorKind::ExtraneousAttributes { count } => todo!(),
90+
CompileErrorKind::FunctionArgumentCountMismatch { function, found, expected } => todo!(),
91+
CompileErrorKind::Include => todo!(),
92+
CompileErrorKind::InconsistentLeadingWhitespace { expected, found } => todo!(),
93+
CompileErrorKind::Internal { message } => todo!(),
94+
CompileErrorKind::InvalidAttribute { item_kind, item_name, attribute } => todo!(),
95+
CompileErrorKind::InvalidEscapeSequence { character } => todo!(),
96+
CompileErrorKind::MismatchedClosingDelimiter { close, open, open_line } => todo!(),
97+
CompileErrorKind::MixedLeadingWhitespace { whitespace } => todo!(),
98+
CompileErrorKind::ParameterFollowsVariadicParameter { parameter } => todo!(),
99+
CompileErrorKind::ParsingRecursionDepthExceeded => todo!(),
100+
CompileErrorKind::RequiredParameterFollowsDefaultParameter { parameter } => todo!(),
101+
CompileErrorKind::ShebangAndScriptAttribute { recipe } => todo!(),
102+
CompileErrorKind::ShellExpansion { err } => todo!(),
103+
CompileErrorKind::UndefinedVariable { variable } => todo!(),
104+
CompileErrorKind::UnexpectedCharacter { expected } => todo!(),
105+
CompileErrorKind::UnexpectedClosingDelimiter { close } => todo!(),
106+
CompileErrorKind::UnexpectedEndOfToken { expected } => todo!(),
107+
CompileErrorKind::UnexpectedToken { expected, found } => todo!(),
108+
CompileErrorKind::UnicodeEscapeCharacter { character } => todo!(),
109+
CompileErrorKind::UnicodeEscapeDelimiter { character } => todo!(),
110+
CompileErrorKind::UnicodeEscapeEmpty => todo!(),
111+
CompileErrorKind::UnicodeEscapeLength { hex } => todo!(),
112+
CompileErrorKind::UnicodeEscapeRange { hex } => todo!(),
113+
CompileErrorKind::UnicodeEscapeUnterminated => todo!(),
114+
CompileErrorKind::UnknownAliasTarget { alias, target } => todo!(),
115+
CompileErrorKind::UnknownAttribute { attribute } => todo!(),
116+
CompileErrorKind::UnknownDependency { recipe, unknown } => todo!(),
117+
CompileErrorKind::UnknownFunction { function } => todo!(),
118+
CompileErrorKind::UnknownSetting { setting } => todo!(),
119+
CompileErrorKind::UnknownStartOfToken => todo!(),
120+
CompileErrorKind::UnpairedCarriageReturn => todo!(),
121+
CompileErrorKind::UnterminatedBacktick => todo!(),
122+
CompileErrorKind::UnterminatedInterpolation => todo!(),
123+
CompileErrorKind::UnterminatedString => todo!(),
124+
*/
125+
};
126+
127+
report.eprint((&path, source)).unwrap();
128+
}
129+
22130
fn capitalize(s: &str) -> String {
23131
let mut chars = s.chars();
24132
match chars.next() {

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,13 @@ impl<'src> ColorDisplay for Error<'src> {
504504
}
505505
}
506506

507+
pub(crate) fn render_error(error: &Error, color: Color) {
508+
match error {
509+
Error::Compile { compile_error } => compile_error::render_compile_error(compile_error),
510+
_ => eprintln!("{}", error.color_display(color.stderr())),
511+
}
512+
}
513+
507514
fn format_cmd(binary: &OsString, arguments: &Vec<OsString>) -> String {
508515
iter::once(binary)
509516
.chain(arguments)

src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
2929
})
3030
.map_err(|error| {
3131
if !verbosity.quiet() && error.print_message() {
32-
eprintln!("{}", error.color_display(color.stderr()));
32+
crate::error::render_error(&error, color);
3333
}
3434
error.code().unwrap_or(EXIT_FAILURE)
3535
})

0 commit comments

Comments
 (0)