• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! `mbe` (short for Macro By Example) crate contains code for handling
2 //! `macro_rules` macros. It uses `TokenTree` (from `tt` package) as the
3 //! interface, although it contains some code to bridge `SyntaxNode`s and
4 //! `TokenTree`s as well!
5 //!
6 //! The tes for this functionality live in another crate:
7 //! `hir_def::macro_expansion_tests::mbe`.
8 
9 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
10 
11 mod parser;
12 mod expander;
13 mod syntax_bridge;
14 mod tt_iter;
15 mod to_parser_input;
16 
17 #[cfg(test)]
18 mod benchmark;
19 mod token_map;
20 
21 use ::tt::token_id as tt;
22 use stdx::impl_from;
23 
24 use std::fmt;
25 
26 use crate::{
27     parser::{MetaTemplate, MetaVarKind, Op},
28     tt_iter::TtIter,
29 };
30 
31 // FIXME: we probably should re-think  `token_tree_to_syntax_node` interfaces
32 pub use self::tt::{Delimiter, DelimiterKind, Punct};
33 pub use ::parser::TopEntryPoint;
34 
35 pub use crate::{
36     syntax_bridge::{
37         parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
38         syntax_node_to_token_tree_with_modifications, token_tree_to_syntax_node, SyntheticToken,
39         SyntheticTokenId,
40     },
41     token_map::TokenMap,
42 };
43 
44 #[derive(Debug, PartialEq, Eq, Clone)]
45 pub enum ParseError {
46     UnexpectedToken(Box<str>),
47     Expected(Box<str>),
48     InvalidRepeat,
49     RepetitionEmptyTokenTree,
50 }
51 
52 impl ParseError {
expected(e: &str) -> ParseError53     fn expected(e: &str) -> ParseError {
54         ParseError::Expected(e.into())
55     }
56 
unexpected(e: &str) -> ParseError57     fn unexpected(e: &str) -> ParseError {
58         ParseError::UnexpectedToken(e.into())
59     }
60 }
61 
62 impl fmt::Display for ParseError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result63     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64         match self {
65             ParseError::UnexpectedToken(it) => f.write_str(it),
66             ParseError::Expected(it) => f.write_str(it),
67             ParseError::InvalidRepeat => f.write_str("invalid repeat"),
68             ParseError::RepetitionEmptyTokenTree => f.write_str("empty token tree in repetition"),
69         }
70     }
71 }
72 
73 #[derive(Debug, PartialEq, Eq, Clone, Hash)]
74 pub enum ExpandError {
75     BindingError(Box<Box<str>>),
76     LeftoverTokens,
77     ConversionError,
78     LimitExceeded,
79     NoMatchingRule,
80     UnexpectedToken,
81     CountError(CountError),
82 }
83 
84 impl_from!(CountError for ExpandError);
85 
86 impl ExpandError {
binding_error(e: impl Into<Box<str>>) -> ExpandError87     fn binding_error(e: impl Into<Box<str>>) -> ExpandError {
88         ExpandError::BindingError(Box::new(e.into()))
89     }
90 }
91 
92 impl fmt::Display for ExpandError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result93     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94         match self {
95             ExpandError::NoMatchingRule => f.write_str("no rule matches input tokens"),
96             ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
97             ExpandError::BindingError(e) => f.write_str(e),
98             ExpandError::ConversionError => f.write_str("could not convert tokens"),
99             ExpandError::LimitExceeded => f.write_str("Expand exceed limit"),
100             ExpandError::LeftoverTokens => f.write_str("leftover tokens"),
101             ExpandError::CountError(e) => e.fmt(f),
102         }
103     }
104 }
105 
106 // FIXME: Showing these errors could be nicer.
107 #[derive(Debug, PartialEq, Eq, Clone, Hash)]
108 pub enum CountError {
109     OutOfBounds,
110     Misplaced,
111 }
112 
113 impl fmt::Display for CountError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result114     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115         match self {
116             CountError::OutOfBounds => f.write_str("${count} out of bounds"),
117             CountError::Misplaced => f.write_str("${count} misplaced"),
118         }
119     }
120 }
121 
122 /// This struct contains AST for a single `macro_rules` definition. What might
123 /// be very confusing is that AST has almost exactly the same shape as
124 /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
125 /// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
126 #[derive(Clone, Debug, PartialEq, Eq)]
127 pub struct DeclarativeMacro {
128     rules: Box<[Rule]>,
129     /// Highest id of the token we have in TokenMap
130     shift: Shift,
131     // This is used for correctly determining the behavior of the pat fragment
132     // FIXME: This should be tracked by hygiene of the fragment identifier!
133     is_2021: bool,
134 }
135 
136 #[derive(Clone, Debug, PartialEq, Eq)]
137 struct Rule {
138     lhs: MetaTemplate,
139     rhs: MetaTemplate,
140 }
141 
142 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
143 pub struct Shift(u32);
144 
145 impl Shift {
new(tt: &tt::Subtree) -> Shift146     pub fn new(tt: &tt::Subtree) -> Shift {
147         // Note that TokenId is started from zero,
148         // We have to add 1 to prevent duplication.
149         let value = max_id(tt).map_or(0, |it| it + 1);
150         return Shift(value);
151 
152         // Find the max token id inside a subtree
153         fn max_id(subtree: &tt::Subtree) -> Option<u32> {
154             let filter =
155                 |tt: &_| match tt {
156                     tt::TokenTree::Subtree(subtree) => {
157                         let tree_id = max_id(subtree);
158                         if subtree.delimiter.open != tt::TokenId::unspecified() {
159                             Some(tree_id.map_or(subtree.delimiter.open.0, |t| {
160                                 t.max(subtree.delimiter.open.0)
161                             }))
162                         } else {
163                             tree_id
164                         }
165                     }
166                     tt::TokenTree::Leaf(leaf) => {
167                         let &(tt::Leaf::Ident(tt::Ident { span, .. })
168                         | tt::Leaf::Punct(tt::Punct { span, .. })
169                         | tt::Leaf::Literal(tt::Literal { span, .. })) = leaf;
170 
171                         (span != tt::TokenId::unspecified()).then_some(span.0)
172                     }
173                 };
174             subtree.token_trees.iter().filter_map(filter).max()
175         }
176     }
177 
178     /// Shift given TokenTree token id
shift_all(self, tt: &mut tt::Subtree)179     pub fn shift_all(self, tt: &mut tt::Subtree) {
180         for t in &mut tt.token_trees {
181             match t {
182                 tt::TokenTree::Leaf(
183                     tt::Leaf::Ident(tt::Ident { span, .. })
184                     | tt::Leaf::Punct(tt::Punct { span, .. })
185                     | tt::Leaf::Literal(tt::Literal { span, .. }),
186                 ) => *span = self.shift(*span),
187                 tt::TokenTree::Subtree(tt) => {
188                     tt.delimiter.open = self.shift(tt.delimiter.open);
189                     tt.delimiter.close = self.shift(tt.delimiter.close);
190                     self.shift_all(tt)
191                 }
192             }
193         }
194     }
195 
shift(self, id: tt::TokenId) -> tt::TokenId196     pub fn shift(self, id: tt::TokenId) -> tt::TokenId {
197         if id == tt::TokenId::unspecified() {
198             id
199         } else {
200             tt::TokenId(id.0 + self.0)
201         }
202     }
203 
unshift(self, id: tt::TokenId) -> Option<tt::TokenId>204     pub fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
205         id.0.checked_sub(self.0).map(tt::TokenId)
206     }
207 }
208 
209 #[derive(Debug, Eq, PartialEq)]
210 pub enum Origin {
211     Def,
212     Call,
213 }
214 
215 impl DeclarativeMacro {
216     /// The old, `macro_rules! m {}` flavor.
parse_macro_rules( tt: &tt::Subtree, is_2021: bool, ) -> Result<DeclarativeMacro, ParseError>217     pub fn parse_macro_rules(
218         tt: &tt::Subtree,
219         is_2021: bool,
220     ) -> Result<DeclarativeMacro, ParseError> {
221         // Note: this parsing can be implemented using mbe machinery itself, by
222         // matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
223         // manually seems easier.
224         let mut src = TtIter::new(tt);
225         let mut rules = Vec::new();
226         while src.len() > 0 {
227             let rule = Rule::parse(&mut src, true)?;
228             rules.push(rule);
229             if let Err(()) = src.expect_char(';') {
230                 if src.len() > 0 {
231                     return Err(ParseError::expected("expected `;`"));
232                 }
233                 break;
234             }
235         }
236 
237         for Rule { lhs, .. } in &rules {
238             validate(lhs)?;
239         }
240 
241         Ok(DeclarativeMacro { rules: rules.into_boxed_slice(), shift: Shift::new(tt), is_2021 })
242     }
243 
244     /// The new, unstable `macro m {}` flavor.
parse_macro2(tt: &tt::Subtree, is_2021: bool) -> Result<DeclarativeMacro, ParseError>245     pub fn parse_macro2(tt: &tt::Subtree, is_2021: bool) -> Result<DeclarativeMacro, ParseError> {
246         let mut src = TtIter::new(tt);
247         let mut rules = Vec::new();
248 
249         if tt::DelimiterKind::Brace == tt.delimiter.kind {
250             cov_mark::hit!(parse_macro_def_rules);
251             while src.len() > 0 {
252                 let rule = Rule::parse(&mut src, true)?;
253                 rules.push(rule);
254                 if let Err(()) = src.expect_any_char(&[';', ',']) {
255                     if src.len() > 0 {
256                         return Err(ParseError::expected("expected `;` or `,` to delimit rules"));
257                     }
258                     break;
259                 }
260             }
261         } else {
262             cov_mark::hit!(parse_macro_def_simple);
263             let rule = Rule::parse(&mut src, false)?;
264             if src.len() != 0 {
265                 return Err(ParseError::expected("remaining tokens in macro def"));
266             }
267             rules.push(rule);
268         }
269 
270         for Rule { lhs, .. } in &rules {
271             validate(lhs)?;
272         }
273 
274         Ok(DeclarativeMacro { rules: rules.into_boxed_slice(), shift: Shift::new(tt), is_2021 })
275     }
276 
expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree>277     pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
278         // apply shift
279         let mut tt = tt.clone();
280         self.shift.shift_all(&mut tt);
281         expander::expand_rules(&self.rules, &tt, self.is_2021)
282     }
283 
map_id_down(&self, id: tt::TokenId) -> tt::TokenId284     pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
285         self.shift.shift(id)
286     }
287 
map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin)288     pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
289         match self.shift.unshift(id) {
290             Some(id) => (id, Origin::Call),
291             None => (id, Origin::Def),
292         }
293     }
294 
shift(&self) -> Shift295     pub fn shift(&self) -> Shift {
296         self.shift
297     }
298 }
299 
300 impl Rule {
parse(src: &mut TtIter<'_>, expect_arrow: bool) -> Result<Self, ParseError>301     fn parse(src: &mut TtIter<'_>, expect_arrow: bool) -> Result<Self, ParseError> {
302         let lhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?;
303         if expect_arrow {
304             src.expect_char('=').map_err(|()| ParseError::expected("expected `=`"))?;
305             src.expect_char('>').map_err(|()| ParseError::expected("expected `>`"))?;
306         }
307         let rhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?;
308 
309         let lhs = MetaTemplate::parse_pattern(lhs)?;
310         let rhs = MetaTemplate::parse_template(rhs)?;
311 
312         Ok(crate::Rule { lhs, rhs })
313     }
314 }
315 
validate(pattern: &MetaTemplate) -> Result<(), ParseError>316 fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
317     for op in pattern.iter() {
318         match op {
319             Op::Subtree { tokens, .. } => validate(tokens)?,
320             Op::Repeat { tokens: subtree, separator, .. } => {
321                 // Checks that no repetition which could match an empty token
322                 // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
323                 let lsh_is_empty_seq = separator.is_none() && subtree.iter().all(|child_op| {
324                     match *child_op {
325                         // vis is optional
326                         Op::Var { kind: Some(kind), .. } => kind == MetaVarKind::Vis,
327                         Op::Repeat {
328                             kind: parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne,
329                             ..
330                         } => true,
331                         _ => false,
332                     }
333                 });
334                 if lsh_is_empty_seq {
335                     return Err(ParseError::RepetitionEmptyTokenTree);
336                 }
337                 validate(subtree)?
338             }
339             _ => (),
340         }
341     }
342     Ok(())
343 }
344 
345 pub type ExpandResult<T> = ValueResult<T, ExpandError>;
346 
347 #[derive(Debug, Clone, Eq, PartialEq)]
348 pub struct ValueResult<T, E> {
349     pub value: T,
350     pub err: Option<E>,
351 }
352 
353 impl<T, E> ValueResult<T, E> {
new(value: T, err: E) -> Self354     pub fn new(value: T, err: E) -> Self {
355         Self { value, err: Some(err) }
356     }
357 
ok(value: T) -> Self358     pub fn ok(value: T) -> Self {
359         Self { value, err: None }
360     }
361 
only_err(err: E) -> Self where T: Default,362     pub fn only_err(err: E) -> Self
363     where
364         T: Default,
365     {
366         Self { value: Default::default(), err: Some(err) }
367     }
368 
map<U>(self, f: impl FnOnce(T) -> U) -> ValueResult<U, E>369     pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ValueResult<U, E> {
370         ValueResult { value: f(self.value), err: self.err }
371     }
372 
map_err<E2>(self, f: impl FnOnce(E) -> E2) -> ValueResult<T, E2>373     pub fn map_err<E2>(self, f: impl FnOnce(E) -> E2) -> ValueResult<T, E2> {
374         ValueResult { value: self.value, err: self.err.map(f) }
375     }
376 
result(self) -> Result<T, E>377     pub fn result(self) -> Result<T, E> {
378         self.err.map_or(Ok(self.value), Err)
379     }
380 }
381 
382 impl<T: Default, E> From<Result<T, E>> for ValueResult<T, E> {
from(result: Result<T, E>) -> Self383     fn from(result: Result<T, E>) -> Self {
384         result.map_or_else(Self::only_err, Self::ok)
385     }
386 }
387