diff options
author | David Lattimore <[email protected]> | 2020-06-23 13:03:39 +0100 |
---|---|---|
committer | David Lattimore <[email protected]> | 2020-07-03 23:56:51 +0100 |
commit | a5ef644a16f6d187fb6b612d30f19bf4f20fc6c4 (patch) | |
tree | d107edd6fa6a70e53c08300b25b9605a90eb80c9 /crates | |
parent | 57ed622ec4f0f71a618f99a46aa0026e81eb2583 (diff) |
SSR: Extract error code out to a separate module
This is to make reusing it outside of parsing easier in a subsequent
change.
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ssr/src/errors.rs | 29 | ||||
-rw-r--r-- | crates/ra_ssr/src/lib.rs | 12 | ||||
-rw-r--r-- | crates/ra_ssr/src/parsing.rs | 17 |
3 files changed, 35 insertions, 23 deletions
diff --git a/crates/ra_ssr/src/errors.rs b/crates/ra_ssr/src/errors.rs new file mode 100644 index 000000000..c02bacae6 --- /dev/null +++ b/crates/ra_ssr/src/errors.rs | |||
@@ -0,0 +1,29 @@ | |||
1 | //! Code relating to errors produced by SSR. | ||
2 | |||
3 | /// Constructs an SsrError taking arguments like the format macro. | ||
4 | macro_rules! _error { | ||
5 | ($fmt:expr) => {$crate::SsrError::new(format!($fmt))}; | ||
6 | ($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))} | ||
7 | } | ||
8 | pub(crate) use _error as error; | ||
9 | |||
10 | /// Returns from the current function with an error, supplied by arguments as for format! | ||
11 | macro_rules! _bail { | ||
12 | ($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))} | ||
13 | } | ||
14 | pub(crate) use _bail as bail; | ||
15 | |||
16 | #[derive(Debug, PartialEq)] | ||
17 | pub struct SsrError(pub(crate) String); | ||
18 | |||
19 | impl std::fmt::Display for SsrError { | ||
20 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
21 | write!(f, "Parse error: {}", self.0) | ||
22 | } | ||
23 | } | ||
24 | |||
25 | impl SsrError { | ||
26 | pub(crate) fn new(message: impl Into<String>) -> SsrError { | ||
27 | SsrError(message.into()) | ||
28 | } | ||
29 | } | ||
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index 422e15ee6..d6862356d 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs | |||
@@ -6,9 +6,12 @@ | |||
6 | mod matching; | 6 | mod matching; |
7 | mod parsing; | 7 | mod parsing; |
8 | mod replacing; | 8 | mod replacing; |
9 | #[macro_use] | ||
10 | mod errors; | ||
9 | #[cfg(test)] | 11 | #[cfg(test)] |
10 | mod tests; | 12 | mod tests; |
11 | 13 | ||
14 | pub use crate::errors::SsrError; | ||
12 | pub use crate::matching::Match; | 15 | pub use crate::matching::Match; |
13 | use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason}; | 16 | use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason}; |
14 | use hir::Semantics; | 17 | use hir::Semantics; |
@@ -41,9 +44,6 @@ pub struct SsrPattern { | |||
41 | pattern: Option<SyntaxNode>, | 44 | pattern: Option<SyntaxNode>, |
42 | } | 45 | } |
43 | 46 | ||
44 | #[derive(Debug, PartialEq)] | ||
45 | pub struct SsrError(String); | ||
46 | |||
47 | #[derive(Debug, Default)] | 47 | #[derive(Debug, Default)] |
48 | pub struct SsrMatches { | 48 | pub struct SsrMatches { |
49 | pub matches: Vec<Match>, | 49 | pub matches: Vec<Match>, |
@@ -216,12 +216,6 @@ pub struct MatchDebugInfo { | |||
216 | matched: Result<Match, MatchFailureReason>, | 216 | matched: Result<Match, MatchFailureReason>, |
217 | } | 217 | } |
218 | 218 | ||
219 | impl std::fmt::Display for SsrError { | ||
220 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
221 | write!(f, "Parse error: {}", self.0) | ||
222 | } | ||
223 | } | ||
224 | |||
225 | impl std::fmt::Debug for MatchDebugInfo { | 219 | impl std::fmt::Debug for MatchDebugInfo { |
226 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 220 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
227 | write!(f, "========= PATTERN ==========\n")?; | 221 | write!(f, "========= PATTERN ==========\n")?; |
diff --git a/crates/ra_ssr/src/parsing.rs b/crates/ra_ssr/src/parsing.rs index 5ea125616..4aee97bb2 100644 --- a/crates/ra_ssr/src/parsing.rs +++ b/crates/ra_ssr/src/parsing.rs | |||
@@ -5,17 +5,12 @@ | |||
5 | //! search patterns, we go further and parse the pattern as each kind of thing that we can match. | 5 | //! search patterns, we go further and parse the pattern as each kind of thing that we can match. |
6 | //! e.g. expressions, type references etc. | 6 | //! e.g. expressions, type references etc. |
7 | 7 | ||
8 | use crate::errors::bail; | ||
8 | use crate::{SsrError, SsrPattern, SsrRule}; | 9 | use crate::{SsrError, SsrPattern, SsrRule}; |
9 | use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T}; | 10 | use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T}; |
10 | use rustc_hash::{FxHashMap, FxHashSet}; | 11 | use rustc_hash::{FxHashMap, FxHashSet}; |
11 | use std::str::FromStr; | 12 | use std::str::FromStr; |
12 | 13 | ||
13 | /// Returns from the current function with an error, supplied by arguments as for format! | ||
14 | macro_rules! bail { | ||
15 | ($e:expr) => {return Err($crate::SsrError::new($e))}; | ||
16 | ($fmt:expr, $($arg:tt)+) => {return Err($crate::SsrError::new(format!($fmt, $($arg)+)))} | ||
17 | } | ||
18 | |||
19 | #[derive(Clone, Debug)] | 14 | #[derive(Clone, Debug)] |
20 | pub(crate) struct SsrTemplate { | 15 | pub(crate) struct SsrTemplate { |
21 | pub(crate) tokens: Vec<PatternElement>, | 16 | pub(crate) tokens: Vec<PatternElement>, |
@@ -246,7 +241,7 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placehold | |||
246 | } | 241 | } |
247 | } | 242 | } |
248 | _ => { | 243 | _ => { |
249 | bail!("Placeholders should either be $name or ${name:constraints}"); | 244 | bail!("Placeholders should either be $name or ${{name:constraints}}"); |
250 | } | 245 | } |
251 | } | 246 | } |
252 | } | 247 | } |
@@ -289,7 +284,7 @@ fn expect_token(tokens: &mut std::vec::IntoIter<Token>, expected: &str) -> Resul | |||
289 | } | 284 | } |
290 | bail!("Expected {} found {}", expected, t.text); | 285 | bail!("Expected {} found {}", expected, t.text); |
291 | } | 286 | } |
292 | bail!("Expected {} found end of stream"); | 287 | bail!("Expected {} found end of stream", expected); |
293 | } | 288 | } |
294 | 289 | ||
295 | impl NodeKind { | 290 | impl NodeKind { |
@@ -307,12 +302,6 @@ impl Placeholder { | |||
307 | } | 302 | } |
308 | } | 303 | } |
309 | 304 | ||
310 | impl SsrError { | ||
311 | fn new(message: impl Into<String>) -> SsrError { | ||
312 | SsrError(message.into()) | ||
313 | } | ||
314 | } | ||
315 | |||
316 | #[cfg(test)] | 305 | #[cfg(test)] |
317 | mod tests { | 306 | mod tests { |
318 | use super::*; | 307 | use super::*; |