Skip to content

Commit 8db71c3

Browse files
committed
OxCraft: The #[track_caller] update
1 parent 4cc1624 commit 8db71c3

File tree

4 files changed

+197
-44
lines changed

4 files changed

+197
-44
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/error.rs

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::{borrow::Cow, num::ParseIntError, ops::Range};
22

33
use oxcr_protocol::{
4-
aott,
4+
aott::{
5+
self,
6+
text::{self, CharError},
7+
},
58
miette::{self, SourceSpan},
69
ser::any_of,
710
thiserror,
@@ -11,7 +14,7 @@ use oxcr_protocol::{
1114
#[derive(miette::Diagnostic, thiserror::Error, Debug)]
1215
pub enum ParseError {
1316
#[error("expected {expected}, found {found}")]
14-
#[diagnostic(code(cli::expected))]
17+
#[diagnostic(code(aott::error::expected))]
1518
Expected {
1619
expected: Expectation,
1720
found: char,
@@ -22,7 +25,7 @@ pub enum ParseError {
2225
},
2326
#[error("unexpected end of input{}", .expected.as_ref().map(|expectation| format!(", expected {expectation}")).unwrap_or_else(String::new))]
2427
#[diagnostic(
25-
code(cli::unexpected_eof),
28+
code(aott::error::unexpected_eof),
2629
help("try giving it more input next time, I guess?")
2730
)]
2831
UnexpectedEof {
@@ -58,6 +61,36 @@ pub enum ParseError {
5861
#[source]
5962
error: ParseIntError,
6063
},
64+
#[error("filter failed in {location}, while checking {token}")]
65+
#[diagnostic(code(aott::error::filter_failed))]
66+
FilterFailed {
67+
#[label = "this is the token that didn't pass"]
68+
at: SourceSpan,
69+
location: &'static core::panic::Location<'static>,
70+
token: char,
71+
},
72+
#[error("expected keyword {keyword}")]
73+
#[diagnostic(code(aott::text::error::expected_keyword))]
74+
ExpectedKeyword {
75+
#[label = "the keyword here is {found}"]
76+
at: SourceSpan,
77+
keyword: String,
78+
found: String,
79+
},
80+
#[error("expected digit of radix {radix}")]
81+
#[diagnostic(code(aott::text::error::expected_digit))]
82+
ExpectedDigit {
83+
#[label = "here"]
84+
at: SourceSpan,
85+
radix: u32,
86+
found: char,
87+
},
88+
#[error("expected identifier character (a-zA-Z or _), but found {found}")]
89+
ExpectedIdent {
90+
#[label = "here"]
91+
at: SourceSpan,
92+
found: char,
93+
},
6194
}
6295

6396
#[derive(Debug, thiserror::Error)]
@@ -73,10 +106,8 @@ pub enum Expectation {
73106
}
74107

75108
impl<'a> aott::error::Error<&'a str> for ParseError {
76-
type Span = Range<usize>;
77-
78109
fn unexpected_eof(
79-
span: Self::Span,
110+
span: Range<usize>,
80111
expected: Option<Vec<<&'a str as aott::prelude::InputType>::Token>>,
81112
) -> Self {
82113
Self::UnexpectedEof {
@@ -86,25 +117,62 @@ impl<'a> aott::error::Error<&'a str> for ParseError {
86117
}
87118
}
88119

89-
fn expected_token_found(
90-
span: Self::Span,
91-
expected: Vec<char>,
92-
found: aott::MaybeRef<'_, char>,
93-
) -> Self {
120+
fn expected_token_found(span: Range<usize>, expected: Vec<char>, found: char) -> Self {
94121
Self::Expected {
95122
expected: Expectation::AnyOf(expected),
96-
found: found.into_clone(),
123+
found,
97124
at: span.into(),
98125
help: None,
99126
}
100127
}
101128

102-
fn expected_eof_found(span: Self::Span, found: aott::MaybeRef<'_, char>) -> Self {
129+
fn expected_eof_found(span: Range<usize>, found: char) -> Self {
103130
Self::Expected {
104131
expected: Expectation::EndOfInput,
105-
found: found.into_clone(),
132+
found,
106133
at: span.into(),
107134
help: None,
108135
}
109136
}
137+
138+
fn filter_failed(
139+
span: Range<usize>,
140+
location: &'static core::panic::Location<'static>,
141+
token: <&'a str as aott::prelude::InputType>::Token,
142+
) -> Self {
143+
Self::FilterFailed {
144+
at: span.into(),
145+
location,
146+
token,
147+
}
148+
}
149+
}
150+
151+
impl CharError<char> for ParseError {
152+
fn expected_digit(span: Range<usize>, radix: u32, got: char) -> Self {
153+
Self::ExpectedDigit {
154+
at: span.into(),
155+
radix,
156+
found: got,
157+
}
158+
}
159+
160+
fn expected_ident_char(span: Range<usize>, got: char) -> Self {
161+
Self::ExpectedIdent {
162+
at: span.into(),
163+
found: got,
164+
}
165+
}
166+
167+
fn expected_keyword<'a, 'b: 'a>(
168+
span: Range<usize>,
169+
keyword: &'b <char as text::Char>::Str,
170+
actual: &'a <char as text::Char>::Str,
171+
) -> Self {
172+
Self::ExpectedKeyword {
173+
at: span.into(),
174+
keyword: keyword.to_owned(),
175+
found: actual.to_owned(),
176+
}
177+
}
110178
}

protocol/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
async_fn_in_trait,
1111
exhaustive_patterns,
1212
never_type,
13-
fmt_internals
13+
fmt_internals,
14+
closure_track_caller
1415
)]
1516

1617
pub mod error;

0 commit comments

Comments
 (0)