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 }
|
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
|
||||||
path-absolutize = "3.1.1"
|
path-absolutize = "3.1.1"
|
||||||
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
|
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 = { version = "0.26.2", features = ["derive"] }
|
||||||
strum_macros = "0.26.2"
|
strum_macros = "0.26.2"
|
||||||
thiserror = "1.0.58"
|
thiserror = "1.0.58"
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
/// Represents a trait responsible for handling diagnostics in the interpreter.
|
/// Represents a trait responsible for handling diagnostics in the interpreter.
|
||||||
pub trait Handler<T> {
|
pub trait Handler<T> {
|
||||||
/// Receive an error and handles it.
|
/// 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.
|
/// Is a struct that implements [`Handler`] trait by doing nothing with the errors.
|
||||||
|
@ -9,5 +11,8 @@ pub trait Handler<T> {
|
||||||
pub struct DummyHandler;
|
pub struct DummyHandler;
|
||||||
|
|
||||||
impl<T> Handler<T> for 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())
|
.into())
|
||||||
} else {
|
} else {
|
||||||
handler.receive(
|
handler.receive(UnterminatedDelimitedComment {
|
||||||
UnterminatedDelimitedComment {
|
|
||||||
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
span: Span::new(iter.source_file().clone(), start, start + 2).unwrap(),
|
||||||
}
|
});
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
return Err(TokenizeError::FatalLexicalError);
|
return Err(TokenizeError::FatalLexicalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ impl TokenStream {
|
||||||
fn handle_popped_token(
|
fn handle_popped_token(
|
||||||
tokens: &mut Vec<Token>,
|
tokens: &mut Vec<Token>,
|
||||||
popped_token: Token,
|
popped_token: Token,
|
||||||
handler: &dyn Handler<error::Error>,
|
handler: &impl Handler<error::Error>,
|
||||||
) -> Option<TokenTree> {
|
) -> Option<TokenTree> {
|
||||||
match popped_token {
|
match popped_token {
|
||||||
Token::Punctuation(punc) if punc.punctuation == '{' => {
|
Token::Punctuation(punc) if punc.punctuation == '{' => {
|
||||||
|
@ -116,7 +116,7 @@ impl TokenStream {
|
||||||
tokens: &mut Vec<Token>,
|
tokens: &mut Vec<Token>,
|
||||||
open: Punctuation,
|
open: Punctuation,
|
||||||
delimiter: Delimiter,
|
delimiter: Delimiter,
|
||||||
handler: &dyn Handler<error::Error>,
|
handler: &impl Handler<error::Error>,
|
||||||
) -> Option<Delimited> {
|
) -> Option<Delimited> {
|
||||||
let mut token_trees = Vec::new();
|
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 {
|
impl<T: Display> Handler<T> for Printer {
|
||||||
fn receive(&self, error: E) {
|
fn receive<E: Into<T>>(&self, error: E) {
|
||||||
eprintln!("{error}");
|
eprintln!("{}", error.into());
|
||||||
self.printed.set(true);
|
self.printed.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn has_received(&self) -> bool {
|
||||||
|
self.printed.get()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ impl<'a> Parser<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
delimiter: Delimiter,
|
delimiter: Delimiter,
|
||||||
f: impl FnOnce(&mut Self) -> Option<T>,
|
f: impl FnOnce(&mut Self) -> Option<T>,
|
||||||
handler: &dyn Handler<Error>,
|
handler: &impl Handler<Error>,
|
||||||
) -> Option<DelimitedTree<T>> {
|
) -> Option<DelimitedTree<T>> {
|
||||||
self.current_frame.stop_at_significant();
|
self.current_frame.stop_at_significant();
|
||||||
let raw_token_tree = self
|
let raw_token_tree = self
|
||||||
|
|
|
@ -102,13 +102,10 @@ impl<'a> Parser<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(
|
handler.receive(UnexpectedSyntax {
|
||||||
UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
expected: SyntaxKind::Keyword(KeywordKind::Namespace),
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}
|
});
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
|
@ -775,13 +775,10 @@ impl<'a> Parser<'a> {
|
||||||
handler,
|
handler,
|
||||||
),
|
),
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(
|
handler.receive(UnexpectedSyntax {
|
||||||
UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::Punctuation('('),
|
expected: SyntaxKind::Punctuation('('),
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}
|
});
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
@ -795,13 +792,10 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
// unexpected
|
// unexpected
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(
|
handler.receive(UnexpectedSyntax {
|
||||||
UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::ExecuteBlock,
|
expected: SyntaxKind::ExecuteBlock,
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}
|
});
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -833,13 +827,10 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
unexpected => {
|
unexpected => {
|
||||||
handler.receive(
|
handler.receive(UnexpectedSyntax {
|
||||||
UnexpectedSyntax {
|
|
||||||
expected: SyntaxKind::ExecuteBlockTail,
|
expected: SyntaxKind::ExecuteBlockTail,
|
||||||
found: unexpected.into_token(),
|
found: unexpected.into_token(),
|
||||||
}
|
});
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue