X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fmbe%2Fsrc%2Flib.rs;h=13fec269faba764d22878568f26ce33bf2340a7e;hb=dbb702cfc1419850e7d208a1b375e4a69dbe8e87;hp=380a5074431c918f6ca180f89ef0a64bbe5147cc;hpb=48ea50bf04a1bd12999bd9e27558ac31988c7228;p=rust.git diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 380a5074431..13fec269fab 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -18,13 +18,15 @@ use std::fmt; -pub use tt::{Delimiter, DelimiterKind, Punct}; - use crate::{ parser::{parse_pattern, parse_template, MetaTemplate, Op}, tt_iter::TtIter, }; +// FIXME: we probably should re-think `token_tree_to_syntax_node` interfaces +pub use ::parser::FragmentKind; +pub use tt::{Delimiter, DelimiterKind, Punct}; + #[derive(Debug, PartialEq, Eq)] pub enum ParseError { UnexpectedToken(String), @@ -39,17 +41,11 @@ pub enum ExpandError { UnexpectedToken, BindingError(String), ConversionError, - ProcMacroError(tt::ExpansionError), + // FIXME: no way mbe should know about proc macros. UnresolvedProcMacro, Other(String), } -impl From for ExpandError { - fn from(it: tt::ExpansionError) -> Self { - ExpandError::ProcMacroError(it) - } -} - impl fmt::Display for ExpandError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -57,7 +53,6 @@ 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::ProcMacroError(e) => e.fmt(f), ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"), ExpandError::Other(e) => f.write_str(e), } @@ -66,8 +61,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { pub use crate::{ syntax_bridge::{ - ast_to_token_tree, parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, - token_tree_to_syntax_node, + parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, + syntax_node_to_token_tree_censored, token_tree_to_syntax_node, }, token_map::TokenMap, }; @@ -97,11 +92,11 @@ struct Rule { rhs: MetaTemplate, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct Shift(u32); +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct Shift(u32); impl Shift { - fn new(tt: &tt::Subtree) -> Shift { + pub fn new(tt: &tt::Subtree) -> Shift { // Note that TokenId is started from zero, // We have to add 1 to prevent duplication. let value = max_id(tt).map_or(0, |it| it + 1); @@ -134,8 +129,8 @@ fn max_id(subtree: &tt::Subtree) -> Option { } /// Shift given TokenTree token id - fn shift_all(self, tt: &mut tt::Subtree) { - for t in tt.token_trees.iter_mut() { + pub fn shift_all(self, tt: &mut tt::Subtree) { + for t in &mut tt.token_trees { match t { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id), @@ -152,14 +147,14 @@ fn shift_all(self, tt: &mut tt::Subtree) { } } - fn shift(self, id: tt::TokenId) -> tt::TokenId { + pub fn shift(self, id: tt::TokenId) -> tt::TokenId { if id == tt::TokenId::unspecified() { return id; } tt::TokenId(id.0 + self.0) } - fn unshift(self, id: tt::TokenId) -> Option { + pub fn unshift(self, id: tt::TokenId) -> Option { id.0.checked_sub(self.0).map(tt::TokenId) } } @@ -188,7 +183,7 @@ pub fn parse(tt: &tt::Subtree) -> Result { } } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -241,7 +236,7 @@ pub fn parse(tt: &tt::Subtree) -> Result { } rules.push(rule); } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -268,7 +263,7 @@ pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) { } impl Rule { - fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { + fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { let lhs = src .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; @@ -280,8 +275,8 @@ fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; - let lhs = MetaTemplate(parse_pattern(&lhs)?); - let rhs = MetaTemplate(parse_template(&rhs)?); + let lhs = MetaTemplate(parse_pattern(lhs)?); + let rhs = MetaTemplate(parse_template(rhs)?); Ok(crate::Rule { lhs, rhs }) } @@ -290,7 +285,7 @@ fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { for op in pattern.iter() { match op { - Op::Subtree { tokens, .. } => validate(&tokens)?, + Op::Subtree { tokens, .. } => validate(tokens)?, Op::Repeat { tokens: subtree, separator, .. } => { // Checks that no repetition which could match an empty token // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558 @@ -356,7 +351,7 @@ pub fn map(self, f: impl FnOnce(T) -> U) -> ExpandResult { } pub fn result(self) -> Result { - self.err.map(Err).unwrap_or(Ok(self.value)) + self.err.map_or(Ok(self.value), Err) } }