]> git.lizzy.rs Git - rust.git/blob - crates/mbe/src/lib.rs
Merge #6635
[rust.git] / crates / mbe / src / lib.rs
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 mod parser;
7 mod mbe_expander;
8 mod syntax_bridge;
9 mod tt_iter;
10 mod subtree_source;
11
12 #[cfg(test)]
13 mod tests;
14
15 use std::fmt;
16
17 pub use tt::{Delimiter, Punct};
18
19 use crate::{
20     parser::{parse_pattern, Op},
21     tt_iter::TtIter,
22 };
23
24 #[derive(Debug, PartialEq, Eq)]
25 pub enum ParseError {
26     Expected(String),
27     RepetitionEmtpyTokenTree,
28 }
29
30 #[derive(Debug, PartialEq, Eq, Clone)]
31 pub enum ExpandError {
32     NoMatchingRule,
33     UnexpectedToken,
34     BindingError(String),
35     ConversionError,
36     InvalidRepeat,
37     ProcMacroError(tt::ExpansionError),
38     Other(String),
39 }
40
41 impl From<tt::ExpansionError> for ExpandError {
42     fn from(it: tt::ExpansionError) -> Self {
43         ExpandError::ProcMacroError(it)
44     }
45 }
46
47 impl fmt::Display for ExpandError {
48     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49         match self {
50             ExpandError::NoMatchingRule => f.write_str("no rule matches input tokens"),
51             ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
52             ExpandError::BindingError(e) => f.write_str(e),
53             ExpandError::ConversionError => f.write_str("could not convert tokens"),
54             ExpandError::InvalidRepeat => f.write_str("invalid repeat expression"),
55             ExpandError::ProcMacroError(e) => e.fmt(f),
56             ExpandError::Other(e) => f.write_str(e),
57         }
58     }
59 }
60
61 pub use crate::syntax_bridge::{
62     ast_to_token_tree, parse_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node,
63     TokenMap,
64 };
65
66 /// This struct contains AST for a single `macro_rules` definition. What might
67 /// be very confusing is that AST has almost exactly the same shape as
68 /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
69 /// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
70 #[derive(Clone, Debug, PartialEq, Eq)]
71 pub struct MacroRules {
72     rules: Vec<Rule>,
73     /// Highest id of the token we have in TokenMap
74     shift: Shift,
75 }
76
77 #[derive(Clone, Debug, PartialEq, Eq)]
78 struct Rule {
79     lhs: tt::Subtree,
80     rhs: tt::Subtree,
81 }
82
83 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
84 struct Shift(u32);
85
86 impl Shift {
87     fn new(tt: &tt::Subtree) -> Shift {
88         // Note that TokenId is started from zero,
89         // We have to add 1 to prevent duplication.
90         let value = max_id(tt).map_or(0, |it| it + 1);
91         return Shift(value);
92
93         // Find the max token id inside a subtree
94         fn max_id(subtree: &tt::Subtree) -> Option<u32> {
95             subtree
96                 .token_trees
97                 .iter()
98                 .filter_map(|tt| match tt {
99                     tt::TokenTree::Subtree(subtree) => {
100                         let tree_id = max_id(subtree);
101                         match subtree.delimiter {
102                             Some(it) if it.id != tt::TokenId::unspecified() => {
103                                 Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0)))
104                             }
105                             _ => tree_id,
106                         }
107                     }
108                     tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
109                         if ident.id != tt::TokenId::unspecified() =>
110                     {
111                         Some(ident.id.0)
112                     }
113                     _ => None,
114                 })
115                 .max()
116         }
117     }
118
119     /// Shift given TokenTree token id
120     fn shift_all(self, tt: &mut tt::Subtree) {
121         for t in tt.token_trees.iter_mut() {
122             match t {
123                 tt::TokenTree::Leaf(leaf) => match leaf {
124                     tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id),
125                     tt::Leaf::Punct(punct) => punct.id = self.shift(punct.id),
126                     tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id),
127                 },
128                 tt::TokenTree::Subtree(tt) => {
129                     if let Some(it) = tt.delimiter.as_mut() {
130                         it.id = self.shift(it.id);
131                     };
132                     self.shift_all(tt)
133                 }
134             }
135         }
136     }
137
138     fn shift(self, id: tt::TokenId) -> tt::TokenId {
139         if id == tt::TokenId::unspecified() {
140             return id;
141         }
142         tt::TokenId(id.0 + self.0)
143     }
144
145     fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
146         id.0.checked_sub(self.0).map(tt::TokenId)
147     }
148 }
149
150 #[derive(Debug, Eq, PartialEq)]
151 pub enum Origin {
152     Def,
153     Call,
154 }
155
156 impl MacroRules {
157     pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
158         // Note: this parsing can be implemented using mbe machinery itself, by
159         // matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
160         // manually seems easier.
161         let mut src = TtIter::new(tt);
162         let mut rules = Vec::new();
163         while src.len() > 0 {
164             let rule = Rule::parse(&mut src)?;
165             rules.push(rule);
166             if let Err(()) = src.expect_char(';') {
167                 if src.len() > 0 {
168                     return Err(ParseError::Expected("expected `:`".to_string()));
169                 }
170                 break;
171             }
172         }
173
174         for rule in rules.iter() {
175             validate(&rule.lhs)?;
176         }
177
178         Ok(MacroRules { rules, shift: Shift::new(tt) })
179     }
180
181     pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
182         // apply shift
183         let mut tt = tt.clone();
184         self.shift.shift_all(&mut tt);
185         mbe_expander::expand(self, &tt)
186     }
187
188     pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
189         self.shift.shift(id)
190     }
191
192     pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
193         match self.shift.unshift(id) {
194             Some(id) => (id, Origin::Call),
195             None => (id, Origin::Def),
196         }
197     }
198 }
199
200 impl Rule {
201     fn parse(src: &mut TtIter) -> Result<Rule, ParseError> {
202         let mut lhs = src
203             .expect_subtree()
204             .map_err(|()| ParseError::Expected("expected subtree".to_string()))?
205             .clone();
206         lhs.delimiter = None;
207         src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?;
208         src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?;
209         let mut rhs = src
210             .expect_subtree()
211             .map_err(|()| ParseError::Expected("expected subtree".to_string()))?
212             .clone();
213         rhs.delimiter = None;
214         Ok(crate::Rule { lhs, rhs })
215     }
216 }
217
218 fn to_parse_error(e: ExpandError) -> ParseError {
219     let msg = match e {
220         ExpandError::InvalidRepeat => "invalid repeat".to_string(),
221         _ => "invalid macro definition".to_string(),
222     };
223     ParseError::Expected(msg)
224 }
225
226 fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
227     for op in parse_pattern(pattern) {
228         let op = op.map_err(to_parse_error)?;
229
230         match op {
231             Op::TokenTree(tt::TokenTree::Subtree(subtree)) => validate(subtree)?,
232             Op::Repeat { subtree, separator, .. } => {
233                 // Checks that no repetition which could match an empty token
234                 // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
235
236                 if separator.is_none() {
237                     if parse_pattern(subtree).all(|child_op| {
238                         match child_op.map_err(to_parse_error) {
239                             Ok(Op::Var { kind, .. }) => {
240                                 // vis is optional
241                                 if kind.map_or(false, |it| it == "vis") {
242                                     return true;
243                                 }
244                             }
245                             Ok(Op::Repeat { kind, .. }) => {
246                                 return matches!(
247                                     kind,
248                                     parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne
249                                 )
250                             }
251                             _ => {}
252                         }
253                         false
254                     }) {
255                         return Err(ParseError::RepetitionEmtpyTokenTree);
256                     }
257                 }
258                 validate(subtree)?
259             }
260             _ => (),
261         }
262     }
263     Ok(())
264 }
265
266 #[derive(Debug, Clone, Eq, PartialEq)]
267 pub struct ExpandResult<T> {
268     pub value: T,
269     pub err: Option<ExpandError>,
270 }
271
272 impl<T> ExpandResult<T> {
273     pub fn ok(value: T) -> Self {
274         Self { value, err: None }
275     }
276
277     pub fn only_err(err: ExpandError) -> Self
278     where
279         T: Default,
280     {
281         Self { value: Default::default(), err: Some(err) }
282     }
283
284     pub fn str_err(err: String) -> Self
285     where
286         T: Default,
287     {
288         Self::only_err(ExpandError::Other(err))
289     }
290
291     pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
292         ExpandResult { value: f(self.value), err: self.err }
293     }
294
295     pub fn result(self) -> Result<T, ExpandError> {
296         self.err.map(Err).unwrap_or(Ok(self.value))
297     }
298 }
299
300 impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> {
301     fn from(result: Result<T, ExpandError>) -> Self {
302         result.map_or_else(|e| Self::only_err(e), |it| Self::ok(it))
303     }
304 }