X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fmbe%2Fsrc%2Flib.rs;h=6402ceadaaae6d824e29bf2a95c344ae86fd3a4f;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=3633624c6415cc2f652b1ac47e9fcc3e46481669;hpb=cff209f1520e8ac0270a71814f993bda74668203;p=rust.git diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 3633624c641..6402ceadaaa 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -30,7 +30,8 @@ pub use crate::{ syntax_bridge::{ parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, - syntax_node_to_token_tree_censored, token_tree_to_syntax_node, SyntheticToken, + syntax_node_to_token_tree_with_modifications, token_tree_to_syntax_node, SyntheticToken, + SyntheticTokenId, }, token_map::TokenMap, }; @@ -66,18 +67,17 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { #[derive(Debug, PartialEq, Eq, Clone)] pub enum ExpandError { + BindingError(Box>), + LeftoverTokens, + ConversionError, + LimitExceeded, NoMatchingRule, UnexpectedToken, - BindingError(Box), - ConversionError, - // FIXME: no way mbe should know about proc macros. - UnresolvedProcMacro, - Other(Box), } impl ExpandError { - fn binding_error(e: &str) -> ExpandError { - ExpandError::BindingError(e.into()) + fn binding_error(e: impl Into>) -> ExpandError { + ExpandError::BindingError(Box::new(e.into())) } } @@ -88,8 +88,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ExpandError::UnexpectedToken => f.write_str("unexpected token in input"), ExpandError::BindingError(e) => f.write_str(e), ExpandError::ConversionError => f.write_str("could not convert tokens"), - ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"), - ExpandError::Other(e) => f.write_str(e), + ExpandError::LimitExceeded => f.write_str("Expand exceed limit"), + ExpandError::LeftoverTokens => f.write_str("leftover tokens"), } } } @@ -310,42 +310,41 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { Ok(()) } +pub type ExpandResult = ValueResult; + #[derive(Debug, Clone, Eq, PartialEq)] -pub struct ExpandResult { +pub struct ValueResult { pub value: T, - pub err: Option, + pub err: Option, } -impl ExpandResult { +impl ValueResult { pub fn ok(value: T) -> Self { Self { value, err: None } } - pub fn only_err(err: ExpandError) -> Self + pub fn only_err(err: E) -> Self where T: Default, { Self { value: Default::default(), err: Some(err) } } - pub fn str_err(err: String) -> Self - where - T: Default, - { - Self::only_err(ExpandError::Other(err.into())) + pub fn map(self, f: impl FnOnce(T) -> U) -> ValueResult { + ValueResult { value: f(self.value), err: self.err } } - pub fn map(self, f: impl FnOnce(T) -> U) -> ExpandResult { - ExpandResult { value: f(self.value), err: self.err } + pub fn map_err(self, f: impl FnOnce(E) -> E2) -> ValueResult { + ValueResult { value: self.value, err: self.err.map(f) } } - pub fn result(self) -> Result { + pub fn result(self) -> Result { self.err.map_or(Ok(self.value), Err) } } -impl From> for ExpandResult { - fn from(result: Result) -> Self { +impl From> for ValueResult { + fn from(result: Result) -> Self { result.map_or_else(Self::only_err, Self::ok) } }