]> git.lizzy.rs Git - rust.git/blobdiff - crates/mbe/src/lib.rs
internal: remove accidental code re-use
[rust.git] / crates / mbe / src / lib.rs
index 3af5bc18b8b0e547cf680043cccf2b2a5cb4a247..13fec269faba764d22878568f26ce33bf2340a7e 100644 (file)
 
 #[cfg(test)]
 mod benchmark;
+mod token_map;
 
 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),
@@ -38,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<tt::ExpansionError> 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 {
@@ -56,16 +53,18 @@ 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),
         }
     }
 }
 
-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, TokenMap,
+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,
+    },
+    token_map::TokenMap,
 };
 
 /// This struct contains AST for a single `macro_rules` definition. What might
@@ -93,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);
@@ -130,8 +129,8 @@ fn max_id(subtree: &tt::Subtree) -> Option<u32> {
     }
 
     /// 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),
@@ -148,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<tt::TokenId> {
+    pub fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
         id.0.checked_sub(self.0).map(tt::TokenId)
     }
 }
@@ -184,7 +183,7 @@ pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
             }
         }
 
-        for rule in rules.iter() {
+        for rule in &rules {
             validate(&rule.lhs)?;
         }
 
@@ -237,7 +236,7 @@ pub fn parse(tt: &tt::Subtree) -> Result<MacroDef, ParseError> {
             }
             rules.push(rule);
         }
-        for rule in rules.iter() {
+        for rule in &rules {
             validate(&rule.lhs)?;
         }
 
@@ -264,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<Rule, ParseError> {
+    fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Self, ParseError> {
         let lhs = src
             .expect_subtree()
             .map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
@@ -276,8 +275,8 @@ fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Rule, ParseError> {
             .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 })
     }
@@ -286,13 +285,13 @@ fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Rule, ParseError> {
 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
 
-                if separator.is_none() {
-                    if subtree.iter().all(|child_op| {
+                if separator.is_none()
+                    && subtree.iter().all(|child_op| {
                         match child_op {
                             Op::Var { kind, .. } => {
                                 // vis is optional
@@ -310,9 +309,9 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
                             Op::Subtree { .. } => {}
                         }
                         false
-                    }) {
-                        return Err(ParseError::RepetitionEmptyTokenTree);
-                    }
+                    })
+                {
+                    return Err(ParseError::RepetitionEmptyTokenTree);
                 }
                 validate(subtree)?
             }
@@ -352,7 +351,7 @@ pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
     }
 
     pub fn result(self) -> Result<T, ExpandError> {
-        self.err.map(Err).unwrap_or(Ok(self.value))
+        self.err.map_or(Ok(self.value), Err)
     }
 }