X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fmbe%2Fsrc%2Flib.rs;h=6402ceadaaae6d824e29bf2a95c344ae86fd3a4f;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=8d223c375a33284d5fabe1b269a2423026c5b1f2;hpb=40009e07d002bf676b4b32e90a858aed37ea4cc2;p=rust.git diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 8d223c375a3..6402ceadaaa 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -30,19 +30,30 @@ 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, + syntax_node_to_token_tree_with_modifications, token_tree_to_syntax_node, SyntheticToken, + SyntheticTokenId, }, token_map::TokenMap, }; #[derive(Debug, PartialEq, Eq, Clone)] pub enum ParseError { - UnexpectedToken(String), - Expected(String), + UnexpectedToken(Box), + Expected(Box), InvalidRepeat, RepetitionEmptyTokenTree, } +impl ParseError { + fn expected(e: &str) -> ParseError { + ParseError::Expected(e.into()) + } + + fn unexpected(e: &str) -> ParseError { + ParseError::UnexpectedToken(e.into()) + } +} + impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -56,13 +67,18 @@ 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(String), - ConversionError, - // FIXME: no way mbe should know about proc macros. - UnresolvedProcMacro, - Other(String), +} + +impl ExpandError { + fn binding_error(e: impl Into>) -> ExpandError { + ExpandError::BindingError(Box::new(e.into())) + } } impl fmt::Display for ExpandError { @@ -72,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"), } } } @@ -107,28 +123,25 @@ pub fn new(tt: &tt::Subtree) -> Shift { // Find the max token id inside a subtree fn max_id(subtree: &tt::Subtree) -> Option { - subtree - .token_trees - .iter() - .filter_map(|tt| match tt { - tt::TokenTree::Subtree(subtree) => { - let tree_id = max_id(subtree); - match subtree.delimiter { - Some(it) if it.id != tt::TokenId::unspecified() => { - Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0))) - } - _ => tree_id, + let filter = |tt: &_| match tt { + tt::TokenTree::Subtree(subtree) => { + let tree_id = max_id(subtree); + match subtree.delimiter { + Some(it) if it.id != tt::TokenId::unspecified() => { + Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0))) } + _ => tree_id, } - tt::TokenTree::Leaf(leaf) => { - let &(tt::Leaf::Ident(tt::Ident { id, .. }) - | tt::Leaf::Punct(tt::Punct { id, .. }) - | tt::Leaf::Literal(tt::Literal { id, .. })) = leaf; + } + tt::TokenTree::Leaf(leaf) => { + let &(tt::Leaf::Ident(tt::Ident { id, .. }) + | tt::Leaf::Punct(tt::Punct { id, .. }) + | tt::Leaf::Literal(tt::Literal { id, .. })) = leaf; - (id != tt::TokenId::unspecified()).then(|| id.0) - } - }) - .max() + (id != tt::TokenId::unspecified()).then(|| id.0) + } + }; + subtree.token_trees.iter().filter_map(filter).max() } } @@ -183,7 +196,7 @@ pub fn parse_macro_rules(tt: &tt::Subtree) -> Result 0 { - return Err(ParseError::Expected("expected `;`".to_string())); + return Err(ParseError::expected("expected `;`")); } break; } @@ -208,9 +221,7 @@ pub fn parse_macro2(tt: &tt::Subtree) -> Result { rules.push(rule); if let Err(()) = src.expect_any_char(&[';', ',']) { if src.len() > 0 { - return Err(ParseError::Expected( - "expected `;` or `,` to delimit rules".to_string(), - )); + return Err(ParseError::expected("expected `;` or `,` to delimit rules")); } break; } @@ -219,7 +230,7 @@ pub fn parse_macro2(tt: &tt::Subtree) -> Result { cov_mark::hit!(parse_macro_def_simple); let rule = Rule::parse(&mut src, false)?; if src.len() != 0 { - return Err(ParseError::Expected("remaining tokens in macro def".to_string())); + return Err(ParseError::expected("remaining tokens in macro def")); } rules.push(rule); } @@ -256,16 +267,12 @@ pub fn shift(&self) -> Shift { impl Rule { fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { - let lhs = src - .expect_subtree() - .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; + let lhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?; if expect_arrow { - src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?; - src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?; + src.expect_char('=').map_err(|()| ParseError::expected("expected `=`"))?; + src.expect_char('>').map_err(|()| ParseError::expected("expected `>`"))?; } - let rhs = src - .expect_subtree() - .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; + let rhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?; let lhs = MetaTemplate::parse_pattern(lhs)?; let rhs = MetaTemplate::parse_template(rhs)?; @@ -303,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)) + 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) } }