rust/compiler/rustc_expand/src/mbe.rs

112 lines
3.7 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".
pub(crate) mod diagnostics;
pub(crate) mod macro_check;
pub(crate) mod macro_parser;
pub(crate) mod macro_rules;
pub(crate) mod metavar_expr;
pub(crate) mod quoted;
pub(crate) mod transcribe;
2019-09-22 11:17:30 -05:00
use metavar_expr::MetaVarExpr;
use rustc_ast::token::{Delimiter, NonterminalKind, Token, TokenKind};
use rustc_ast::tokenstream::DelimSpan;
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 delimiters are not represented explicitly in the `tts` vector.
#[derive(PartialEq, Encodable, Decodable, Debug)]
2019-09-22 11:42:52 -05:00
struct Delimited {
delim: Delimiter,
/// FIXME: #67062 has details about why this is sub-optimal.
tts: Vec<TokenTree>,
2019-09-22 11:17:30 -05:00
}
#[derive(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)]
pub(crate) 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, 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).
Delimited(DelimSpan, Delimited),
/// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
Sequence(DelimSpan, 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))
}
}