more ergonomic receive function of handler
This commit is contained in:
parent
dd79541ae9
commit
b428c64f89
|
@ -30,7 +30,7 @@ getset = "0.1.2"
|
|||
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
|
||||
path-absolutize = "3.1.1"
|
||||
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
|
||||
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "e31f9f904a5f5905e912907d988c189ffc8cf313" }
|
||||
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "7efe73eb80fb66d3d9eb67eeb7bcb10727462956" }
|
||||
strum = { version = "0.26.2", features = ["derive"] }
|
||||
strum_macros = "0.26.2"
|
||||
thiserror = "1.0.58"
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
/// Represents a trait responsible for handling diagnostics in the interpreter.
|
||||
pub trait Handler<T> {
|
||||
/// Receive an error and handles it.
|
||||
fn receive(&self, error: T);
|
||||
fn receive<E: Into<T>>(&self, error: E);
|
||||
/// Returns whether the handler has received any error.
|
||||
fn has_received(&self) -> bool;
|
||||
}
|
||||
|
||||
/// Is a struct that implements [`Handler`] trait by doing nothing with the errors.
|
||||
|
@ -9,5 +11,8 @@ pub trait Handler<T> {
|
|||
pub struct DummyHandler;
|
||||
|
||||
impl<T> Handler<T> for DummyHandler {
|
||||
fn receive(&self, _error: T) {}
|
||||
fn receive<E: Into<T>>(&self, _error: E) {}
|
||||
fn has_received(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -492,12 +492,9 @@ impl Token {
|
|||
}
|
||||
.into())
|
||||
} else {
|
||||
handler.receive(
|
||||
UnterminatedDelimitedComment {
|
||||
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
handler.receive(UnterminatedDelimitedComment {
|
||||
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
||||
});
|
||||
return Err(TokenizeError::FatalLexicalError);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ impl TokenStream {
|
|||
fn handle_popped_token(
|
||||
tokens: &mut Vec<Token>,
|
||||
popped_token: Token,
|
||||
handler: &dyn Handler<error::Error>,
|
||||
handler: &impl Handler<error::Error>,
|
||||
) -> Option<TokenTree> {
|
||||
match popped_token {
|
||||
Token::Punctuation(punc) if punc.punctuation == '{' => {
|
||||
|
@ -116,7 +116,7 @@ impl TokenStream {
|
|||
tokens: &mut Vec<Token>,
|
||||
open: Punctuation,
|
||||
delimiter: Delimiter,
|
||||
handler: &dyn Handler<error::Error>,
|
||||
handler: &impl Handler<error::Error>,
|
||||
) -> Option<Delimited> {
|
||||
let mut token_trees = Vec::new();
|
||||
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -107,9 +107,13 @@ impl Printer {
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: Display> Handler<E> for Printer {
|
||||
fn receive(&self, error: E) {
|
||||
eprintln!("{error}");
|
||||
impl<T: Display> Handler<T> for Printer {
|
||||
fn receive<E: Into<T>>(&self, error: E) {
|
||||
eprintln!("{}", error.into());
|
||||
self.printed.set(true);
|
||||
}
|
||||
|
||||
fn has_received(&self) -> bool {
|
||||
self.printed.get()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ impl<'a> Parser<'a> {
|
|||
&mut self,
|
||||
delimiter: Delimiter,
|
||||
f: impl FnOnce(&mut Self) -> Option<T>,
|
||||
handler: &dyn Handler<Error>,
|
||||
handler: &impl Handler<Error>,
|
||||
) -> Option<DelimitedTree<T>> {
|
||||
self.current_frame.stop_at_significant();
|
||||
let raw_token_tree = self
|
||||
|
|
|
@ -102,13 +102,10 @@ impl<'a> Parser<'a> {
|
|||
})
|
||||
}
|
||||
unexpected => {
|
||||
handler.receive(
|
||||
UnexpectedSyntax {
|
||||
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
||||
found: unexpected.into_token(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
handler.receive(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
||||
found: unexpected.into_token(),
|
||||
});
|
||||
None
|
||||
}
|
||||
}?;
|
||||
|
|
|
@ -775,13 +775,10 @@ impl<'a> Parser<'a> {
|
|||
handler,
|
||||
),
|
||||
unexpected => {
|
||||
handler.receive(
|
||||
UnexpectedSyntax {
|
||||
expected: SyntaxKind::Punctuation('('),
|
||||
found: unexpected.into_token(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
handler.receive(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Punctuation('('),
|
||||
found: unexpected.into_token(),
|
||||
});
|
||||
None
|
||||
}
|
||||
}?;
|
||||
|
@ -795,13 +792,10 @@ impl<'a> Parser<'a> {
|
|||
|
||||
// unexpected
|
||||
unexpected => {
|
||||
handler.receive(
|
||||
UnexpectedSyntax {
|
||||
expected: SyntaxKind::ExecuteBlock,
|
||||
found: unexpected.into_token(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
handler.receive(UnexpectedSyntax {
|
||||
expected: SyntaxKind::ExecuteBlock,
|
||||
found: unexpected.into_token(),
|
||||
});
|
||||
None
|
||||
}
|
||||
}
|
||||
|
@ -833,13 +827,10 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
unexpected => {
|
||||
handler.receive(
|
||||
UnexpectedSyntax {
|
||||
expected: SyntaxKind::ExecuteBlockTail,
|
||||
found: unexpected.into_token(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
handler.receive(UnexpectedSyntax {
|
||||
expected: SyntaxKind::ExecuteBlockTail,
|
||||
found: unexpected.into_token(),
|
||||
});
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue