mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
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:
parent
57ed622ec4
commit
a5ef644a16
3 changed files with 35 additions and 23 deletions
29
crates/ra_ssr/src/errors.rs
Normal file
29
crates/ra_ssr/src/errors.rs
Normal 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())
|
||||
}
|
||||
}
|
|
@ -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<SyntaxNode>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct SsrError(String);
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SsrMatches {
|
||||
pub matches: Vec<Match>,
|
||||
|
@ -216,12 +216,6 @@ pub struct MatchDebugInfo {
|
|||
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 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "========= PATTERN ==========\n")?;
|
||||
|
|
|
@ -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<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 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<String>) -> SsrError {
|
||||
SsrError(message.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in a new issue