Skip to content

Commit

Permalink
OxCraft: The #[track_caller] update
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 2, 2023
1 parent 4cc1624 commit 8db71c3
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

96 changes: 82 additions & 14 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{borrow::Cow, num::ParseIntError, ops::Range};

use oxcr_protocol::{
aott,
aott::{
self,
text::{self, CharError},
},
miette::{self, SourceSpan},
ser::any_of,
thiserror,
Expand All @@ -11,7 +14,7 @@ use oxcr_protocol::{
#[derive(miette::Diagnostic, thiserror::Error, Debug)]
pub enum ParseError {
#[error("expected {expected}, found {found}")]
#[diagnostic(code(cli::expected))]
#[diagnostic(code(aott::error::expected))]
Expected {
expected: Expectation,
found: char,
Expand All @@ -22,7 +25,7 @@ pub enum ParseError {
},
#[error("unexpected end of input{}", .expected.as_ref().map(|expectation| format!(", expected {expectation}")).unwrap_or_else(String::new))]
#[diagnostic(
code(cli::unexpected_eof),
code(aott::error::unexpected_eof),
help("try giving it more input next time, I guess?")
)]
UnexpectedEof {
Expand Down Expand Up @@ -58,6 +61,36 @@ pub enum ParseError {
#[source]
error: ParseIntError,
},
#[error("filter failed in {location}, while checking {token}")]
#[diagnostic(code(aott::error::filter_failed))]
FilterFailed {
#[label = "this is the token that didn't pass"]
at: SourceSpan,
location: &'static core::panic::Location<'static>,
token: char,
},
#[error("expected keyword {keyword}")]
#[diagnostic(code(aott::text::error::expected_keyword))]
ExpectedKeyword {
#[label = "the keyword here is {found}"]
at: SourceSpan,
keyword: String,
found: String,
},
#[error("expected digit of radix {radix}")]
#[diagnostic(code(aott::text::error::expected_digit))]
ExpectedDigit {
#[label = "here"]
at: SourceSpan,
radix: u32,
found: char,
},
#[error("expected identifier character (a-zA-Z or _), but found {found}")]
ExpectedIdent {
#[label = "here"]
at: SourceSpan,
found: char,
},
}

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

impl<'a> aott::error::Error<&'a str> for ParseError {
type Span = Range<usize>;

fn unexpected_eof(
span: Self::Span,
span: Range<usize>,
expected: Option<Vec<<&'a str as aott::prelude::InputType>::Token>>,
) -> Self {
Self::UnexpectedEof {
Expand All @@ -86,25 +117,62 @@ impl<'a> aott::error::Error<&'a str> for ParseError {
}
}

fn expected_token_found(
span: Self::Span,
expected: Vec<char>,
found: aott::MaybeRef<'_, char>,
) -> Self {
fn expected_token_found(span: Range<usize>, expected: Vec<char>, found: char) -> Self {
Self::Expected {
expected: Expectation::AnyOf(expected),
found: found.into_clone(),
found,
at: span.into(),
help: None,
}
}

fn expected_eof_found(span: Self::Span, found: aott::MaybeRef<'_, char>) -> Self {
fn expected_eof_found(span: Range<usize>, found: char) -> Self {
Self::Expected {
expected: Expectation::EndOfInput,
found: found.into_clone(),
found,
at: span.into(),
help: None,
}
}

fn filter_failed(
span: Range<usize>,
location: &'static core::panic::Location<'static>,
token: <&'a str as aott::prelude::InputType>::Token,
) -> Self {
Self::FilterFailed {
at: span.into(),
location,
token,
}
}
}

impl CharError<char> for ParseError {
fn expected_digit(span: Range<usize>, radix: u32, got: char) -> Self {
Self::ExpectedDigit {
at: span.into(),
radix,
found: got,
}
}

fn expected_ident_char(span: Range<usize>, got: char) -> Self {
Self::ExpectedIdent {
at: span.into(),
found: got,
}
}

fn expected_keyword<'a, 'b: 'a>(
span: Range<usize>,
keyword: &'b <char as text::Char>::Str,
actual: &'a <char as text::Char>::Str,
) -> Self {
Self::ExpectedKeyword {
at: span.into(),
keyword: keyword.to_owned(),
found: actual.to_owned(),
}
}
}
3 changes: 2 additions & 1 deletion protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
async_fn_in_trait,
exhaustive_patterns,
never_type,
fmt_internals
fmt_internals,
closure_track_caller
)]

pub mod error;
Expand Down
Loading

0 comments on commit 8db71c3

Please sign in to comment.