]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/mbe.rs
Rollup merge of #104394 - oli-obk:suggest_method_call, r=lcnr
[rust.git] / compiler / rustc_expand / src / mbe.rs
1 //! This module implements declarative macros: old `macro_rules` and the newer
2 //! `macro`. Declarative macros are also known as "macro by example", and that's
3 //! why we call this module `mbe`. For external documentation, prefer the
4 //! official terminology: "declarative macros".
5
6 pub(crate) mod macro_check;
7 pub(crate) mod macro_parser;
8 pub(crate) mod macro_rules;
9 pub(crate) mod metavar_expr;
10 pub(crate) mod quoted;
11 pub(crate) mod transcribe;
12
13 use metavar_expr::MetaVarExpr;
14 use rustc_ast::token::{Delimiter, NonterminalKind, Token, TokenKind};
15 use rustc_ast::tokenstream::DelimSpan;
16 use rustc_span::symbol::Ident;
17 use rustc_span::Span;
18
19 /// Contains the sub-token-trees of a "delimited" token tree such as `(a b c)`.
20 /// The delimiters are not represented explicitly in the `tts` vector.
21 #[derive(PartialEq, Encodable, Decodable, Debug)]
22 struct Delimited {
23     delim: Delimiter,
24     /// FIXME: #67062 has details about why this is sub-optimal.
25     tts: Vec<TokenTree>,
26 }
27
28 #[derive(PartialEq, Encodable, Decodable, Debug)]
29 struct SequenceRepetition {
30     /// The sequence of token trees
31     tts: Vec<TokenTree>,
32     /// The optional separator
33     separator: Option<Token>,
34     /// Whether the sequence can be repeated zero (*), or one or more times (+)
35     kleene: KleeneToken,
36     /// The number of `Match`s that appear in the sequence (and subsequences)
37     num_captures: usize,
38 }
39
40 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
41 struct KleeneToken {
42     span: Span,
43     op: KleeneOp,
44 }
45
46 impl KleeneToken {
47     fn new(op: KleeneOp, span: Span) -> KleeneToken {
48         KleeneToken { span, op }
49     }
50 }
51
52 /// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
53 /// for token sequences.
54 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
55 pub(crate) enum KleeneOp {
56     /// Kleene star (`*`) for zero or more repetitions
57     ZeroOrMore,
58     /// Kleene plus (`+`) for one or more repetitions
59     OneOrMore,
60     /// Kleene optional (`?`) for zero or one repetitions
61     ZeroOrOne,
62 }
63
64 /// Similar to `tokenstream::TokenTree`, except that `Sequence`, `MetaVar`, `MetaVarDecl`, and
65 /// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
66 #[derive(Debug, PartialEq, Encodable, Decodable)]
67 enum TokenTree {
68     Token(Token),
69     /// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
70     Delimited(DelimSpan, Delimited),
71     /// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
72     Sequence(DelimSpan, SequenceRepetition),
73     /// e.g., `$var`.
74     MetaVar(Span, Ident),
75     /// e.g., `$var:expr`. Only appears on the LHS.
76     MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
77     /// A meta-variable expression inside `${...}`.
78     MetaVarExpr(DelimSpan, MetaVarExpr),
79 }
80
81 impl TokenTree {
82     /// Returns `true` if the given token tree is delimited.
83     fn is_delimited(&self) -> bool {
84         matches!(*self, TokenTree::Delimited(..))
85     }
86
87     /// Returns `true` if the given token tree is a token of the given kind.
88     fn is_token(&self, expected_kind: &TokenKind) -> bool {
89         match self {
90             TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
91             _ => false,
92         }
93     }
94
95     /// Retrieves the `TokenTree`'s span.
96     fn span(&self) -> Span {
97         match *self {
98             TokenTree::Token(Token { span, .. })
99             | TokenTree::MetaVar(span, _)
100             | TokenTree::MetaVarDecl(span, _, _) => span,
101             TokenTree::Delimited(span, _)
102             | TokenTree::MetaVarExpr(span, _)
103             | TokenTree::Sequence(span, _) => span.entire(),
104         }
105     }
106
107     fn token(kind: TokenKind, span: Span) -> TokenTree {
108         TokenTree::Token(Token::new(kind, span))
109     }
110 }