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!
22 parser::{parse_pattern, parse_template, MetaTemplate, Op},
26 // FIXME: we probably should re-think `token_tree_to_syntax_node` interfaces
27 pub use ::parser::ParserEntryPoint;
28 pub use tt::{Delimiter, DelimiterKind, Punct};
30 #[derive(Debug, PartialEq, Eq)]
32 UnexpectedToken(String),
35 RepetitionEmptyTokenTree,
38 #[derive(Debug, PartialEq, Eq, Clone)]
39 pub enum ExpandError {
44 // FIXME: no way mbe should know about proc macros.
49 impl fmt::Display for ExpandError {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 ExpandError::NoMatchingRule => f.write_str("no rule matches input tokens"),
53 ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
54 ExpandError::BindingError(e) => f.write_str(e),
55 ExpandError::ConversionError => f.write_str("could not convert tokens"),
56 ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"),
57 ExpandError::Other(e) => f.write_str(e),
64 parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
65 syntax_node_to_token_tree_censored, token_tree_to_syntax_node,
70 /// This struct contains AST for a single `macro_rules` definition. What might
71 /// be very confusing is that AST has almost exactly the same shape as
72 /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
73 /// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
74 #[derive(Clone, Debug, PartialEq, Eq)]
75 pub struct MacroRules {
77 /// Highest id of the token we have in TokenMap
82 #[derive(Clone, Debug, PartialEq, Eq)]
85 /// Highest id of the token we have in TokenMap
89 #[derive(Clone, Debug, PartialEq, Eq)]
95 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
96 pub struct Shift(u32);
99 pub fn new(tt: &tt::Subtree) -> Shift {
100 // Note that TokenId is started from zero,
101 // We have to add 1 to prevent duplication.
102 let value = max_id(tt).map_or(0, |it| it + 1);
105 // Find the max token id inside a subtree
106 fn max_id(subtree: &tt::Subtree) -> Option<u32> {
110 .filter_map(|tt| match tt {
111 tt::TokenTree::Subtree(subtree) => {
112 let tree_id = max_id(subtree);
113 match subtree.delimiter {
114 Some(it) if it.id != tt::TokenId::unspecified() => {
115 Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0)))
120 tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
121 if ident.id != tt::TokenId::unspecified() =>
131 /// Shift given TokenTree token id
132 pub fn shift_all(self, tt: &mut tt::Subtree) {
133 for t in &mut tt.token_trees {
135 tt::TokenTree::Leaf(leaf) => match leaf {
136 tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id),
137 tt::Leaf::Punct(punct) => punct.id = self.shift(punct.id),
138 tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id),
140 tt::TokenTree::Subtree(tt) => {
141 if let Some(it) = tt.delimiter.as_mut() {
142 it.id = self.shift(it.id);
150 pub fn shift(self, id: tt::TokenId) -> tt::TokenId {
151 if id == tt::TokenId::unspecified() {
154 tt::TokenId(id.0 + self.0)
157 pub fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
158 id.0.checked_sub(self.0).map(tt::TokenId)
162 #[derive(Debug, Eq, PartialEq)]
169 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
170 // Note: this parsing can be implemented using mbe machinery itself, by
171 // matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
172 // manually seems easier.
173 let mut src = TtIter::new(tt);
174 let mut rules = Vec::new();
175 while src.len() > 0 {
176 let rule = Rule::parse(&mut src, true)?;
178 if let Err(()) = src.expect_char(';') {
180 return Err(ParseError::Expected("expected `;`".to_string()));
187 validate(&rule.lhs)?;
190 Ok(MacroRules { rules, shift: Shift::new(tt) })
193 pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
195 let mut tt = tt.clone();
196 self.shift.shift_all(&mut tt);
197 expander::expand_rules(&self.rules, &tt)
200 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
204 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
205 match self.shift.unshift(id) {
206 Some(id) => (id, Origin::Call),
207 None => (id, Origin::Def),
213 pub fn parse(tt: &tt::Subtree) -> Result<MacroDef, ParseError> {
214 let mut src = TtIter::new(tt);
215 let mut rules = Vec::new();
217 if Some(tt::DelimiterKind::Brace) == tt.delimiter_kind() {
218 cov_mark::hit!(parse_macro_def_rules);
219 while src.len() > 0 {
220 let rule = Rule::parse(&mut src, true)?;
222 if let Err(()) = src.expect_any_char(&[';', ',']) {
224 return Err(ParseError::Expected(
225 "expected `;` or `,` to delimit rules".to_string(),
232 cov_mark::hit!(parse_macro_def_simple);
233 let rule = Rule::parse(&mut src, false)?;
235 return Err(ParseError::Expected("remain tokens in macro def".to_string()));
240 validate(&rule.lhs)?;
243 Ok(MacroDef { rules, shift: Shift::new(tt) })
246 pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
248 let mut tt = tt.clone();
249 self.shift.shift_all(&mut tt);
250 expander::expand_rules(&self.rules, &tt)
253 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
257 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
258 match self.shift.unshift(id) {
259 Some(id) => (id, Origin::Call),
260 None => (id, Origin::Def),
266 fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Self, ParseError> {
269 .map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
271 src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?;
272 src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?;
276 .map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
278 let lhs = MetaTemplate(parse_pattern(lhs)?);
279 let rhs = MetaTemplate(parse_template(rhs)?);
281 Ok(crate::Rule { lhs, rhs })
285 fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
286 for op in pattern.iter() {
288 Op::Subtree { tokens, .. } => validate(tokens)?,
289 Op::Repeat { tokens: subtree, separator, .. } => {
290 // Checks that no repetition which could match an empty token
291 // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
293 if separator.is_none()
294 && subtree.iter().all(|child_op| {
296 Op::Var { kind, .. } => {
298 if kind.as_ref().map_or(false, |it| it == "vis") {
302 Op::Repeat { kind, .. } => {
305 parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne
309 Op::Subtree { .. } => {}
314 return Err(ParseError::RepetitionEmptyTokenTree);
324 #[derive(Debug, Clone, Eq, PartialEq)]
325 pub struct ExpandResult<T> {
327 pub err: Option<ExpandError>,
330 impl<T> ExpandResult<T> {
331 pub fn ok(value: T) -> Self {
332 Self { value, err: None }
335 pub fn only_err(err: ExpandError) -> Self
339 Self { value: Default::default(), err: Some(err) }
342 pub fn str_err(err: String) -> Self
346 Self::only_err(ExpandError::Other(err))
349 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
350 ExpandResult { value: f(self.value), err: self.err }
353 pub fn result(self) -> Result<T, ExpandError> {
354 self.err.map_or(Ok(self.value), Err)
358 impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> {
359 fn from(result: Result<T, ExpandError>) -> Self {
360 result.map_or_else(Self::only_err, Self::ok)