From a5ef644a16f6d187fb6b612d30f19bf4f20fc6c4 Mon Sep 17 00:00:00 2001 From: David Lattimore Date: Tue, 23 Jun 2020 22:03:39 +1000 Subject: [PATCH] SSR: Extract error code out to a separate module This is to make reusing it outside of parsing easier in a subsequent change. --- crates/ra_ssr/src/errors.rs | 29 +++++++++++++++++++++++++++++ crates/ra_ssr/src/lib.rs | 12 +++--------- crates/ra_ssr/src/parsing.rs | 17 +++-------------- 3 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 crates/ra_ssr/src/errors.rs diff --git a/crates/ra_ssr/src/errors.rs b/crates/ra_ssr/src/errors.rs new file mode 100644 index 0000000000..c02bacae6b --- /dev/null +++ b/crates/ra_ssr/src/errors.rs @@ -0,0 +1,29 @@ +//! Code relating to errors produced by SSR. + +/// Constructs an SsrError taking arguments like the format macro. +macro_rules! _error { + ($fmt:expr) => {$crate::SsrError::new(format!($fmt))}; + ($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))} +} +pub(crate) use _error as error; + +/// Returns from the current function with an error, supplied by arguments as for format! +macro_rules! _bail { + ($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))} +} +pub(crate) use _bail as bail; + +#[derive(Debug, PartialEq)] +pub struct SsrError(pub(crate) String); + +impl std::fmt::Display for SsrError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "Parse error: {}", self.0) + } +} + +impl SsrError { + pub(crate) fn new(message: impl Into) -> SsrError { + SsrError(message.into()) + } +} diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index 422e15ee6a..d6862356d5 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs @@ -6,9 +6,12 @@ mod matching; mod parsing; mod replacing; +#[macro_use] +mod errors; #[cfg(test)] mod tests; +pub use crate::errors::SsrError; pub use crate::matching::Match; use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason}; use hir::Semantics; @@ -41,9 +44,6 @@ pub struct SsrPattern { pattern: Option, } -#[derive(Debug, PartialEq)] -pub struct SsrError(String); - #[derive(Debug, Default)] pub struct SsrMatches { pub matches: Vec, @@ -216,12 +216,6 @@ pub struct MatchDebugInfo { matched: Result, } -impl std::fmt::Display for SsrError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "Parse error: {}", self.0) - } -} - impl std::fmt::Debug for MatchDebugInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "========= PATTERN ==========\n")?; diff --git a/crates/ra_ssr/src/parsing.rs b/crates/ra_ssr/src/parsing.rs index 5ea1256165..4aee97bb2b 100644 --- a/crates/ra_ssr/src/parsing.rs +++ b/crates/ra_ssr/src/parsing.rs @@ -5,17 +5,12 @@ //! search patterns, we go further and parse the pattern as each kind of thing that we can match. //! e.g. expressions, type references etc. +use crate::errors::bail; use crate::{SsrError, SsrPattern, SsrRule}; use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T}; use rustc_hash::{FxHashMap, FxHashSet}; use std::str::FromStr; -/// Returns from the current function with an error, supplied by arguments as for format! -macro_rules! bail { - ($e:expr) => {return Err($crate::SsrError::new($e))}; - ($fmt:expr, $($arg:tt)+) => {return Err($crate::SsrError::new(format!($fmt, $($arg)+)))} -} - #[derive(Clone, Debug)] pub(crate) struct SsrTemplate { pub(crate) tokens: Vec, @@ -246,7 +241,7 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter) -> Result { - bail!("Placeholders should either be $name or ${name:constraints}"); + bail!("Placeholders should either be $name or ${{name:constraints}}"); } } } @@ -289,7 +284,7 @@ fn expect_token(tokens: &mut std::vec::IntoIter, expected: &str) -> Resul } bail!("Expected {} found {}", expected, t.text); } - bail!("Expected {} found end of stream"); + bail!("Expected {} found end of stream", expected); } impl NodeKind { @@ -307,12 +302,6 @@ impl Placeholder { } } -impl SsrError { - fn new(message: impl Into) -> SsrError { - SsrError(message.into()) - } -} - #[cfg(test)] mod tests { use super::*;