aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorDavid Lattimore <[email protected]>2020-06-23 13:03:39 +0100
committerDavid Lattimore <[email protected]>2020-07-03 23:56:51 +0100
commita5ef644a16f6d187fb6b612d30f19bf4f20fc6c4 (patch)
treed107edd6fa6a70e53c08300b25b9605a90eb80c9 /crates
parent57ed622ec4f0f71a618f99a46aa0026e81eb2583 (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.rs29
-rw-r--r--crates/ra_ssr/src/lib.rs12
-rw-r--r--crates/ra_ssr/src/parsing.rs17
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.
4macro_rules! _error {
5 ($fmt:expr) => {$crate::SsrError::new(format!($fmt))};
6 ($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))}
7}
8pub(crate) use _error as error;
9
10/// Returns from the current function with an error, supplied by arguments as for format!
11macro_rules! _bail {
12 ($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))}
13}
14pub(crate) use _bail as bail;
15
16#[derive(Debug, PartialEq)]
17pub struct SsrError(pub(crate) String);
18
19impl 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
25impl 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 @@
6mod matching; 6mod matching;
7mod parsing; 7mod parsing;
8mod replacing; 8mod replacing;
9#[macro_use]
10mod errors;
9#[cfg(test)] 11#[cfg(test)]
10mod tests; 12mod tests;
11 13
14pub use crate::errors::SsrError;
12pub use crate::matching::Match; 15pub use crate::matching::Match;
13use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason}; 16use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason};
14use hir::Semantics; 17use 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)]
45pub struct SsrError(String);
46
47#[derive(Debug, Default)] 47#[derive(Debug, Default)]
48pub struct SsrMatches { 48pub 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
219impl 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
225impl std::fmt::Debug for MatchDebugInfo { 219impl 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
8use crate::errors::bail;
8use crate::{SsrError, SsrPattern, SsrRule}; 9use crate::{SsrError, SsrPattern, SsrRule};
9use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T}; 10use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T};
10use rustc_hash::{FxHashMap, FxHashSet}; 11use rustc_hash::{FxHashMap, FxHashSet};
11use std::str::FromStr; 12use std::str::FromStr;
12 13
13/// Returns from the current function with an error, supplied by arguments as for format!
14macro_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)]
20pub(crate) struct SsrTemplate { 15pub(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
295impl NodeKind { 290impl NodeKind {
@@ -307,12 +302,6 @@ impl Placeholder {
307 } 302 }
308} 303}
309 304
310impl SsrError {
311 fn new(message: impl Into<String>) -> SsrError {
312 SsrError(message.into())
313 }
314}
315
316#[cfg(test)] 305#[cfg(test)]
317mod tests { 306mod tests {
318 use super::*; 307 use super::*;