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 quoted;
|
2019-12-22 16:42:04 -06:00
|
|
|
crate mod transcribe;
|
2019-09-22 11:17:30 -05:00
|
|
|
|
2020-08-01 10:45:17 -05:00
|
|
|
use rustc_ast::token::{self, NonterminalKind, Token, TokenKind};
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::tokenstream::DelimSpan;
|
2019-09-22 11:17:30 -05:00
|
|
|
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2019-09-22 11:17:30 -05:00
|
|
|
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
|
|
|
|
|
|
|
/// Contains the sub-token-trees of a "delimited" token tree, such as the contents of `(`. Note
|
|
|
|
/// that the delimiter itself might be `NoDelim`.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
|
2019-09-22 11:42:52 -05:00
|
|
|
struct Delimited {
|
|
|
|
delim: token::DelimToken,
|
|
|
|
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.
|
2019-11-03 05:58:01 -06:00
|
|
|
fn open_tt(&self, span: DelimSpan) -> TokenTree {
|
|
|
|
TokenTree::token(token::OpenDelim(self.delim), span.open)
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
|
2019-11-03 05:58:01 -06:00
|
|
|
fn close_tt(&self, span: DelimSpan) -> TokenTree {
|
|
|
|
TokenTree::token(token::CloseDelim(self.delim), span.close)
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -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
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -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.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[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 `$i`, `$i:ident`, and `$(...)`
|
|
|
|
/// are "first-class" token trees. Useful for parsing macros.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[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),
|
|
|
|
Delimited(DelimSpan, Lrc<Delimited>),
|
|
|
|
/// A kleene-style repetition sequence
|
|
|
|
Sequence(DelimSpan, Lrc<SequenceRepetition>),
|
|
|
|
/// e.g., `$var`
|
2020-04-19 06:00:18 -05:00
|
|
|
MetaVar(Span, Ident),
|
2019-09-22 11:17:30 -05:00
|
|
|
/// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
|
2020-12-19 15:30:56 -06:00
|
|
|
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTree {
|
|
|
|
/// Return the number of tokens in the tree.
|
2019-09-22 11:42:52 -05:00
|
|
|
fn len(&self) -> usize {
|
2019-09-22 11:17:30 -05:00
|
|
|
match *self {
|
|
|
|
TokenTree::Delimited(_, ref delimed) => match delimed.delim {
|
|
|
|
token::NoDelim => delimed.tts.len(),
|
|
|
|
_ => delimed.tts.len() + 2,
|
|
|
|
},
|
|
|
|
TokenTree::Sequence(_, ref seq) => seq.tts.len(),
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
|
2019-09-22 11:42:52 -05:00
|
|
|
fn get_tt(&self, index: usize) -> TokenTree {
|
2019-09-22 11:17:30 -05:00
|
|
|
match (self, index) {
|
|
|
|
(&TokenTree::Delimited(_, ref delimed), _) if delimed.delim == token::NoDelim => {
|
|
|
|
delimed.tts[index].clone()
|
|
|
|
}
|
|
|
|
(&TokenTree::Delimited(span, ref delimed), _) => {
|
|
|
|
if index == 0 {
|
2019-11-03 05:58:01 -06:00
|
|
|
return delimed.open_tt(span);
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
if index == delimed.tts.len() + 1 {
|
2019-11-03 05:58:01 -06:00
|
|
|
return delimed.close_tt(span);
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
delimed.tts[index - 1].clone()
|
|
|
|
}
|
|
|
|
(&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
|
|
|
|
_ => panic!("Cannot expand a token tree"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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::Sequence(span, _) => span.entire(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|