rust/compiler/rustc_expand/src/mbe.rs

148 lines
5.0 KiB
Rust
Raw Normal View History

2019-09-22 12:36:35 -05:00
//! This module implements declarative macros: old `macro_rules` and the newer
//! `macro`. Declarative macros are also known as "macro by example", and that's
//! why we call this module `mbe`. For external documentation, prefer the
//! official terminology: "declarative macros".
2019-09-22 10:28:32 -05:00
crate mod macro_check;
crate mod macro_parser;
crate mod macro_rules;
crate mod metavar_expr;
2019-09-22 10:28:32 -05:00
crate mod quoted;
2019-12-22 16:42:04 -06:00
crate mod transcribe;
2019-09-22 11:17:30 -05:00
use metavar_expr::MetaVarExpr;
use rustc_ast::token::{self, NonterminalKind, Token, TokenKind};
use rustc_ast::tokenstream::DelimSpan;
use rustc_data_structures::sync::Lrc;
2020-04-19 06:00:18 -05:00
use rustc_span::symbol::Ident;
use rustc_span::Span;
2019-09-22 11:17:30 -05:00
/// Contains the sub-token-trees of a "delimited" token tree such as `(a b c)`. The delimiter itself
/// might be `NoDelim`.
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
2019-09-22 11:42:52 -05:00
struct Delimited {
delim: token::DelimToken,
/// Note: This contains the opening and closing delimiters tokens (e.g. `(` and `)`). Note that
/// these could be `NoDelim`. These token kinds must match `delim`, and the methods below
/// debug_assert this.
all_tts: Vec<TokenTree>,
2019-09-22 11:17:30 -05:00
}
impl Delimited {
/// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter. Panics if
/// the delimiter is `NoDelim`.
fn open_tt(&self) -> &TokenTree {
let tt = self.all_tts.first().unwrap();
debug_assert!(matches!(
tt,
&TokenTree::Token(token::Token { kind: token::OpenDelim(d), .. }) if d == self.delim
));
tt
}
/// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter. Panics if
/// the delimiter is `NoDelim`.
fn close_tt(&self) -> &TokenTree {
let tt = self.all_tts.last().unwrap();
debug_assert!(matches!(
tt,
&TokenTree::Token(token::Token { kind: token::CloseDelim(d), .. }) if d == self.delim
));
tt
2019-09-22 11:17:30 -05:00
}
/// Returns the tts excluding the outer delimiters.
///
/// FIXME: #67062 has details about why this is sub-optimal.
fn inner_tts(&self) -> &[TokenTree] {
// These functions are called for the assertions within them.
let _open_tt = self.open_tt();
let _close_tt = self.close_tt();
&self.all_tts[1..self.all_tts.len() - 1]
2019-09-22 11:17:30 -05:00
}
}
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
2019-09-22 11:42:52 -05:00
struct SequenceRepetition {
2019-09-22 11:17:30 -05:00
/// The sequence of token trees
2019-09-22 11:42:52 -05:00
tts: Vec<TokenTree>,
2019-09-22 11:17:30 -05:00
/// The optional separator
2019-09-22 11:42:52 -05:00
separator: Option<Token>,
2019-09-22 11:17:30 -05:00
/// Whether the sequence can be repeated zero (*), or one or more times (+)
2019-09-22 11:42:52 -05:00
kleene: KleeneToken,
2019-09-22 11:17:30 -05:00
/// The number of `Match`s that appear in the sequence (and subsequences)
2019-09-22 11:42:52 -05:00
num_captures: usize,
2019-09-22 11:17:30 -05:00
}
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
2019-09-22 11:42:52 -05:00
struct KleeneToken {
span: Span,
op: KleeneOp,
2019-09-22 11:17:30 -05:00
}
impl KleeneToken {
2019-09-22 11:42:52 -05:00
fn new(op: KleeneOp, span: Span) -> KleeneToken {
2019-09-22 11:17:30 -05:00
KleeneToken { span, op }
}
}
2020-08-23 05:02:42 -05:00
/// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
2019-09-22 11:17:30 -05:00
/// for token sequences.
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
2019-09-22 11:42:52 -05:00
enum KleeneOp {
2019-09-22 11:17:30 -05:00
/// Kleene star (`*`) for zero or more repetitions
ZeroOrMore,
/// Kleene plus (`+`) for one or more repetitions
OneOrMore,
2021-04-19 07:57:08 -05:00
/// Kleene optional (`?`) for zero or one repetitions
2019-09-22 11:17:30 -05:00
ZeroOrOne,
}
/// Similar to `tokenstream::TokenTree`, except that `Sequence`, `MetaVar`, `MetaVarDecl`, and
/// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
2019-09-22 11:42:52 -05:00
enum TokenTree {
2019-09-22 11:17:30 -05:00
Token(Token),
/// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
2019-09-22 11:17:30 -05:00
Delimited(DelimSpan, Lrc<Delimited>),
/// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
2019-09-22 11:17:30 -05:00
Sequence(DelimSpan, Lrc<SequenceRepetition>),
/// e.g., `$var`.
2020-04-19 06:00:18 -05:00
MetaVar(Span, Ident),
/// e.g., `$var:expr`. Only appears on the LHS.
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
/// A meta-variable expression inside `${...}`.
MetaVarExpr(DelimSpan, MetaVarExpr),
2019-09-22 11:17:30 -05:00
}
impl TokenTree {
/// Returns `true` if the given token tree is delimited.
2019-09-22 11:42:52 -05:00
fn is_delimited(&self) -> bool {
2020-10-26 19:02:06 -05:00
matches!(*self, TokenTree::Delimited(..))
2019-09-22 11:17:30 -05:00
}
/// Returns `true` if the given token tree is a token of the given kind.
2019-09-22 11:42:52 -05:00
fn is_token(&self, expected_kind: &TokenKind) -> bool {
2019-09-22 11:17:30 -05:00
match self {
TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
_ => false,
}
}
/// Retrieves the `TokenTree`'s span.
2019-09-22 11:42:52 -05:00
fn span(&self) -> Span {
2019-09-22 11:17:30 -05:00
match *self {
TokenTree::Token(Token { span, .. })
| TokenTree::MetaVar(span, _)
| TokenTree::MetaVarDecl(span, _, _) => span,
TokenTree::Delimited(span, _)
| TokenTree::MetaVarExpr(span, _)
| TokenTree::Sequence(span, _) => span.entire(),
2019-09-22 11:17:30 -05:00
}
}
2019-09-22 11:42:52 -05:00
fn token(kind: TokenKind, span: Span) -> TokenTree {
2019-09-22 11:17:30 -05:00
TokenTree::Token(Token::new(kind, span))
}
}