2019-09-22 20:36:35 +03: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 18:28:32 +03:00
|
|
|
crate mod transcribe;
|
|
|
|
crate mod macro_check;
|
|
|
|
crate mod macro_parser;
|
|
|
|
crate mod macro_rules;
|
|
|
|
crate mod quoted;
|
2019-09-22 19:17:30 +03:00
|
|
|
|
2019-10-16 10:59:30 +02:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::parse::token::{self, Token, TokenKind};
|
|
|
|
use syntax::tokenstream::{DelimSpan};
|
2019-09-22 19:17:30 +03:00
|
|
|
|
|
|
|
use syntax_pos::{BytePos, Span};
|
|
|
|
|
|
|
|
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`.
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
|
2019-09-22 19:42:52 +03:00
|
|
|
struct Delimited {
|
|
|
|
delim: token::DelimToken,
|
|
|
|
tts: Vec<TokenTree>,
|
2019-09-22 19:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Delimited {
|
|
|
|
/// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter.
|
2019-09-22 19:42:52 +03:00
|
|
|
fn open_tt(&self, span: Span) -> TokenTree {
|
2019-09-22 19:17:30 +03:00
|
|
|
let open_span = if span.is_dummy() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
span.with_hi(span.lo() + BytePos(self.delim.len() as u32))
|
|
|
|
};
|
|
|
|
TokenTree::token(token::OpenDelim(self.delim), open_span)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
|
2019-09-22 19:42:52 +03:00
|
|
|
fn close_tt(&self, span: Span) -> TokenTree {
|
2019-09-22 19:17:30 +03:00
|
|
|
let close_span = if span.is_dummy() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
|
|
|
|
};
|
|
|
|
TokenTree::token(token::CloseDelim(self.delim), close_span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
|
2019-09-22 19:42:52 +03:00
|
|
|
struct SequenceRepetition {
|
2019-09-22 19:17:30 +03:00
|
|
|
/// The sequence of token trees
|
2019-09-22 19:42:52 +03:00
|
|
|
tts: Vec<TokenTree>,
|
2019-09-22 19:17:30 +03:00
|
|
|
/// The optional separator
|
2019-09-22 19:42:52 +03:00
|
|
|
separator: Option<Token>,
|
2019-09-22 19:17:30 +03:00
|
|
|
/// Whether the sequence can be repeated zero (*), or one or more times (+)
|
2019-09-22 19:42:52 +03:00
|
|
|
kleene: KleeneToken,
|
2019-09-22 19:17:30 +03:00
|
|
|
/// The number of `Match`s that appear in the sequence (and subsequences)
|
2019-09-22 19:42:52 +03:00
|
|
|
num_captures: usize,
|
2019-09-22 19:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
|
2019-09-22 19:42:52 +03:00
|
|
|
struct KleeneToken {
|
|
|
|
span: Span,
|
|
|
|
op: KleeneOp,
|
2019-09-22 19:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KleeneToken {
|
2019-09-22 19:42:52 +03:00
|
|
|
fn new(op: KleeneOp, span: Span) -> KleeneToken {
|
2019-09-22 19:17:30 +03:00
|
|
|
KleeneToken { span, op }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
|
|
|
|
/// for token sequences.
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2019-09-22 19:42:52 +03:00
|
|
|
enum KleeneOp {
|
2019-09-22 19:17:30 +03:00
|
|
|
/// Kleene star (`*`) for zero or more repetitions
|
|
|
|
ZeroOrMore,
|
|
|
|
/// Kleene plus (`+`) for one or more repetitions
|
|
|
|
OneOrMore,
|
|
|
|
/// Kleene optional (`?`) for zero or one reptitions
|
|
|
|
ZeroOrOne,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Similar to `tokenstream::TokenTree`, except that `$i`, `$i:ident`, and `$(...)`
|
|
|
|
/// are "first-class" token trees. Useful for parsing macros.
|
|
|
|
#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2019-09-22 19:42:52 +03:00
|
|
|
enum TokenTree {
|
2019-09-22 19:17:30 +03:00
|
|
|
Token(Token),
|
|
|
|
Delimited(DelimSpan, Lrc<Delimited>),
|
|
|
|
/// A kleene-style repetition sequence
|
|
|
|
Sequence(DelimSpan, Lrc<SequenceRepetition>),
|
|
|
|
/// e.g., `$var`
|
|
|
|
MetaVar(Span, ast::Ident),
|
|
|
|
/// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
|
|
|
|
MetaVarDecl(
|
|
|
|
Span,
|
|
|
|
ast::Ident, /* name to bind */
|
|
|
|
ast::Ident, /* kind of nonterminal */
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTree {
|
|
|
|
/// Return the number of tokens in the tree.
|
2019-09-22 19:42:52 +03:00
|
|
|
fn len(&self) -> usize {
|
2019-09-22 19:17:30 +03: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 19:42:52 +03:00
|
|
|
fn is_delimited(&self) -> bool {
|
2019-09-22 19:17:30 +03:00
|
|
|
match *self {
|
|
|
|
TokenTree::Delimited(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the given token tree is a token of the given kind.
|
2019-09-22 19:42:52 +03:00
|
|
|
fn is_token(&self, expected_kind: &TokenKind) -> bool {
|
2019-09-22 19:17:30 +03: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 19:42:52 +03:00
|
|
|
fn get_tt(&self, index: usize) -> TokenTree {
|
2019-09-22 19:17:30 +03: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 {
|
|
|
|
return delimed.open_tt(span.open);
|
|
|
|
}
|
|
|
|
if index == delimed.tts.len() + 1 {
|
|
|
|
return delimed.close_tt(span.close);
|
|
|
|
}
|
|
|
|
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 19:42:52 +03:00
|
|
|
fn span(&self) -> Span {
|
2019-09-22 19:17:30 +03: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 19:42:52 +03:00
|
|
|
fn token(kind: TokenKind, span: Span) -> TokenTree {
|
2019-09-22 19:17:30 +03:00
|
|
|
TokenTree::Token(Token::new(kind, span))
|
|
|
|
}
|
|
|
|
}
|