SSR: Extract error code out to a separate module

This is to make reusing it outside of parsing easier in a subsequent
change.
This commit is contained in:
David Lattimore 2020-06-23 22:03:39 +10:00
parent 57ed622ec4
commit a5ef644a16
3 changed files with 35 additions and 23 deletions

View file

@ -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<String>) -> SsrError {
SsrError(message.into())
}
}

View file

@ -6,9 +6,12 @@
mod matching; mod matching;
mod parsing; mod parsing;
mod replacing; mod replacing;
#[macro_use]
mod errors;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub use crate::errors::SsrError;
pub use crate::matching::Match; pub use crate::matching::Match;
use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason}; use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason};
use hir::Semantics; use hir::Semantics;
@ -41,9 +44,6 @@ pub struct SsrPattern {
pattern: Option<SyntaxNode>, pattern: Option<SyntaxNode>,
} }
#[derive(Debug, PartialEq)]
pub struct SsrError(String);
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct SsrMatches { pub struct SsrMatches {
pub matches: Vec<Match>, pub matches: Vec<Match>,
@ -216,12 +216,6 @@ pub struct MatchDebugInfo {
matched: Result<Match, MatchFailureReason>, matched: Result<Match, MatchFailureReason>,
} }
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 { impl std::fmt::Debug for MatchDebugInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "========= PATTERN ==========\n")?; write!(f, "========= PATTERN ==========\n")?;

View file

@ -5,17 +5,12 @@
//! search patterns, we go further and parse the pattern as each kind of thing that we can match. //! search patterns, we go further and parse the pattern as each kind of thing that we can match.
//! e.g. expressions, type references etc. //! e.g. expressions, type references etc.
use crate::errors::bail;
use crate::{SsrError, SsrPattern, SsrRule}; use crate::{SsrError, SsrPattern, SsrRule};
use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T}; use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use std::str::FromStr; 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)] #[derive(Clone, Debug)]
pub(crate) struct SsrTemplate { pub(crate) struct SsrTemplate {
pub(crate) tokens: Vec<PatternElement>, pub(crate) tokens: Vec<PatternElement>,
@ -246,7 +241,7 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placehold
} }
} }
_ => { _ => {
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<Token>, expected: &str) -> Resul
} }
bail!("Expected {} found {}", expected, t.text); bail!("Expected {} found {}", expected, t.text);
} }
bail!("Expected {} found end of stream"); bail!("Expected {} found end of stream", expected);
} }
impl NodeKind { impl NodeKind {
@ -307,12 +302,6 @@ impl Placeholder {
} }
} }
impl SsrError {
fn new(message: impl Into<String>) -> SsrError {
SsrError(message.into())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;