1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use ast
::{self, TokenTree, TtDelimited, TtSequence, TtToken}
;
12 use codemap
::{Span, DUMMY_SP}
;
13 use ext
::base
::{ExtCtxt, MacResult, SyntaxExtension}
;
14 use ext
::base
::{NormalTT, TTMacroExpander}
;
15 use ext
::tt
::macro_parser
::{Success, Error, Failure}
;
16 use ext
::tt
::macro_parser
::{NamedMatch, MatchedSeq, MatchedNonterminal}
;
17 use ext
::tt
::macro_parser
::{parse, parse_or_else}
;
18 use parse
::lexer
::new_tt_reader
;
19 use parse
::parser
::Parser
;
20 use parse
::token
::{self, special_idents, gensym_ident, NtTT, Token}
;
21 use parse
::token
::Token
::*;
25 use util
::small_vector
::SmallVector
;
27 use std
::cell
::RefCell
;
30 struct ParserAnyMacro
<'a
> {
31 parser
: RefCell
<Parser
<'a
>>,
33 /// Span of the expansion site of the macro this parser is for
35 /// The ident of the macro we're parsing
36 macro_ident
: ast
::Ident
39 impl<'a
> ParserAnyMacro
<'a
> {
40 /// Make sure we don't have any tokens left to parse, so we don't
41 /// silently drop anything. `allow_semi` is so that "optional"
42 /// semicolons at the end of normal expressions aren't complained
43 /// about e.g. the semicolon in `macro_rules! kapow { () => {
44 /// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
45 /// allowed to be there.
46 fn ensure_complete_parse(&self, allow_semi
: bool
) {
47 let mut parser
= self.parser
.borrow_mut();
48 if allow_semi
&& parser
.token
== token
::Semi
{
49 panictry
!(parser
.bump())
51 if parser
.token
!= token
::Eof
{
52 let token_str
= parser
.this_token_to_string();
53 let msg
= format
!("macro expansion ignores token `{}` and any \
56 let span
= parser
.span
;
57 parser
.span_err(span
, &msg
[..]);
59 let name
= token
::get_ident(self.macro_ident
);
60 let msg
= format
!("caused by the macro expansion here; the usage \
61 of `{}` is likely invalid in this context",
63 parser
.span_note(self.site_span
, &msg
[..]);
68 impl<'a
> MacResult
for ParserAnyMacro
<'a
> {
69 fn make_expr(self: Box
<ParserAnyMacro
<'a
>>) -> Option
<P
<ast
::Expr
>> {
70 let ret
= self.parser
.borrow_mut().parse_expr();
71 self.ensure_complete_parse(true);
74 fn make_pat(self: Box
<ParserAnyMacro
<'a
>>) -> Option
<P
<ast
::Pat
>> {
75 let ret
= self.parser
.borrow_mut().parse_pat();
76 self.ensure_complete_parse(false);
79 fn make_items(self: Box
<ParserAnyMacro
<'a
>>) -> Option
<SmallVector
<P
<ast
::Item
>>> {
80 let mut ret
= SmallVector
::zero();
81 while let Some(item
) = self.parser
.borrow_mut().parse_item() {
84 self.ensure_complete_parse(false);
88 fn make_impl_items(self: Box
<ParserAnyMacro
<'a
>>)
89 -> Option
<SmallVector
<P
<ast
::ImplItem
>>> {
90 let mut ret
= SmallVector
::zero();
92 let mut parser
= self.parser
.borrow_mut();
95 _
=> ret
.push(panictry
!(parser
.parse_impl_item()))
98 self.ensure_complete_parse(false);
102 fn make_stmts(self: Box
<ParserAnyMacro
<'a
>>)
103 -> Option
<SmallVector
<P
<ast
::Stmt
>>> {
104 let mut ret
= SmallVector
::zero();
106 let mut parser
= self.parser
.borrow_mut();
109 _
=> match parser
.parse_stmt_nopanic() {
110 Ok(maybe_stmt
) => match maybe_stmt
{
111 Some(stmt
) => ret
.push(stmt
),
118 self.ensure_complete_parse(false);
123 struct MacroRulesMacroExpander
{
125 imported_from
: Option
<ast
::Ident
>,
126 lhses
: Vec
<Rc
<NamedMatch
>>,
127 rhses
: Vec
<Rc
<NamedMatch
>>,
130 impl TTMacroExpander
for MacroRulesMacroExpander
{
131 fn expand
<'cx
>(&self,
132 cx
: &'cx
mut ExtCtxt
,
134 arg
: &[ast
::TokenTree
])
135 -> Box
<MacResult
+'cx
> {
136 generic_extension(cx
,
146 /// Given `lhses` and `rhses`, this is the new macro we create
147 fn generic_extension
<'cx
>(cx
: &'cx ExtCtxt
,
150 imported_from
: Option
<ast
::Ident
>,
151 arg
: &[ast
::TokenTree
],
152 lhses
: &[Rc
<NamedMatch
>],
153 rhses
: &[Rc
<NamedMatch
>])
154 -> Box
<MacResult
+'cx
> {
155 if cx
.trace_macros() {
156 println
!("{}! {{ {} }}",
157 token
::get_ident(name
),
158 print
::pprust
::tts_to_string(arg
));
161 // Which arm's failure should we report? (the one furthest along)
162 let mut best_fail_spot
= DUMMY_SP
;
163 let mut best_fail_msg
= "internal error: ran no matchers".to_string();
165 for (i
, lhs
) in lhses
.iter().enumerate() { // try each arm's matchers
167 MatchedNonterminal(NtTT(ref lhs_tt
)) => {
168 let lhs_tt
= match **lhs_tt
{
169 TtDelimited(_
, ref delim
) => &delim
.tts
[..],
170 _
=> panic
!(cx
.span_fatal(sp
, "malformed macro lhs"))
173 match TokenTree
::parse(cx
, lhs_tt
, arg
) {
174 Success(named_matches
) => {
175 let rhs
= match *rhses
[i
] {
176 // okay, what's your transcriber?
177 MatchedNonterminal(NtTT(ref tt
)) => {
180 TtDelimited(_
, ref delimed
) => delimed
.tts
.clone(),
181 _
=> panic
!(cx
.span_fatal(sp
, "macro rhs must be delimited")),
184 _
=> cx
.span_bug(sp
, "bad thing in rhs")
186 // rhs has holes ( `$id` and `$(...)` that need filled)
187 let trncbr
= new_tt_reader(&cx
.parse_sess().span_diagnostic
,
191 let mut p
= Parser
::new(cx
.parse_sess(), cx
.cfg(), Box
::new(trncbr
));
192 panictry
!(p
.check_unknown_macro_variable());
193 // Let the context choose how to interpret the result.
194 // Weird, but useful for X-macros.
195 return Box
::new(ParserAnyMacro
{
196 parser
: RefCell
::new(p
),
198 // Pass along the original expansion site and the name of the macro
199 // so we can print a useful error message if the parse of the expanded
200 // macro leaves unparsed tokens.
205 Failure(sp
, ref msg
) => if sp
.lo
>= best_fail_spot
.lo
{
207 best_fail_msg
= (*msg
).clone();
209 Error(sp
, ref msg
) => panic
!(cx
.span_fatal(sp
, &msg
[..]))
212 _
=> cx
.bug("non-matcher found in parsed lhses")
215 panic
!(cx
.span_fatal(best_fail_spot
, &best_fail_msg
[..]));
218 // Note that macro-by-example's input is also matched against a token tree:
219 // $( $lhs:tt => $rhs:tt );+
221 // Holy self-referential!
223 /// Converts a `macro_rules!` invocation into a syntax extension.
224 pub fn compile
<'cx
>(cx
: &'cx
mut ExtCtxt
,
225 def
: &ast
::MacroDef
) -> SyntaxExtension
{
227 let lhs_nm
= gensym_ident("lhs");
228 let rhs_nm
= gensym_ident("rhs");
230 // The pattern that macro_rules matches.
231 // The grammar for macro_rules! is:
232 // $( $lhs:tt => $rhs:tt );+
233 // ...quasiquoting this would be nice.
234 // These spans won't matter, anyways
235 let match_lhs_tok
= MatchNt(lhs_nm
, special_idents
::tt
, token
::Plain
, token
::Plain
);
236 let match_rhs_tok
= MatchNt(rhs_nm
, special_idents
::tt
, token
::Plain
, token
::Plain
);
237 let argument_gram
= vec
!(
239 Rc
::new(ast
::SequenceRepetition
{
241 TtToken(DUMMY_SP
, match_lhs_tok
),
242 TtToken(DUMMY_SP
, token
::FatArrow
),
243 TtToken(DUMMY_SP
, match_rhs_tok
)],
244 separator
: Some(token
::Semi
),
248 //to phase into semicolon-termination instead of
249 //semicolon-separation
251 Rc
::new(ast
::SequenceRepetition
{
252 tts
: vec
![TtToken(DUMMY_SP
, token
::Semi
)],
259 // Parse the macro_rules! invocation (`none` is for no interpolations):
260 let arg_reader
= new_tt_reader(&cx
.parse_sess().span_diagnostic
,
264 let argument_map
= parse_or_else(cx
.parse_sess(),
269 // Extract the arguments:
270 let lhses
= match **argument_map
.get(&lhs_nm
).unwrap() {
271 MatchedSeq(ref s
, _
) => /* FIXME (#2543) */ (*s
).clone(),
272 _
=> cx
.span_bug(def
.span
, "wrong-structured lhs")
276 check_lhs_nt_follows(cx
, &**lhs
, def
.span
);
279 let rhses
= match **argument_map
.get(&rhs_nm
).unwrap() {
280 MatchedSeq(ref s
, _
) => /* FIXME (#2543) */ (*s
).clone(),
281 _
=> cx
.span_bug(def
.span
, "wrong-structured rhs")
284 let exp
: Box
<_
> = Box
::new(MacroRulesMacroExpander
{
286 imported_from
: def
.imported_from
,
291 NormalTT(exp
, Some(def
.span
), def
.allow_internal_unstable
)
294 fn check_lhs_nt_follows(cx
: &mut ExtCtxt
, lhs
: &NamedMatch
, sp
: Span
) {
295 // lhs is going to be like MatchedNonterminal(NtTT(TtDelimited(...))), where the entire lhs is
296 // those tts. Or, it can be a "bare sequence", not wrapped in parens.
298 &MatchedNonterminal(NtTT(ref inner
)) => match &**inner
{
299 &TtDelimited(_
, ref tts
) => {
300 check_matcher(cx
, tts
.tts
.iter(), &Eof
);
302 tt @
&TtSequence(..) => {
303 check_matcher(cx
, Some(tt
).into_iter(), &Eof
);
305 _
=> cx
.span_bug(sp
, "wrong-structured lhs for follow check (didn't find \
306 a TtDelimited or TtSequence)")
308 _
=> cx
.span_bug(sp
, "wrong-structured lhs for follow check (didn't find a \
309 MatchedNonterminal)")
311 // we don't abort on errors on rejection, the driver will do that for us
312 // after parsing/expansion. we can report every error in every macro this way.
315 // returns the last token that was checked, for TtSequence. this gets used later on.
316 fn check_matcher
<'a
, I
>(cx
: &mut ExtCtxt
, matcher
: I
, follow
: &Token
)
317 -> Option
<(Span
, Token
)> where I
: Iterator
<Item
=&'a TokenTree
> {
318 use print
::pprust
::token_to_string
;
322 // 2. For each token T in M:
323 let mut tokens
= matcher
.peekable();
324 while let Some(token
) = tokens
.next() {
325 last
= match *token
{
326 TtToken(sp
, MatchNt(ref name
, ref frag_spec
, _
, _
)) => {
327 // ii. If T is a simple NT, look ahead to the next token T' in
328 // M. If T' is in the set FOLLOW(NT), continue. Else; reject.
329 if can_be_followed_by_any(frag_spec
.as_str()) {
332 let next_token
= match tokens
.peek() {
333 // If T' closes a complex NT, replace T' with F
334 Some(&&TtToken(_
, CloseDelim(_
))) => follow
.clone(),
335 Some(&&TtToken(_
, ref tok
)) => tok
.clone(),
336 Some(&&TtSequence(sp
, _
)) => {
337 // Be conservative around sequences: to be
338 // more specific, we would need to
339 // consider FIRST sets, but also the
340 // possibility that the sequence occurred
341 // zero times (in which case we need to
342 // look at the token that follows the
343 // sequence, which may itself a sequence,
346 &format
!("`${0}:{1}` is followed by a \
347 sequence repetition, which is not \
348 allowed for `{1}` fragments",
349 name
.as_str(), frag_spec
.as_str())
353 // die next iteration
354 Some(&&TtDelimited(_
, ref delim
)) => delim
.close_token(),
355 // else, we're at the end of the macro or sequence
356 None
=> follow
.clone()
359 let tok
= if let TtToken(_
, ref tok
) = *token { tok }
else { unreachable!() }
;
361 // If T' is in the set FOLLOW(NT), continue. Else, reject.
362 match (&next_token
, is_in_follow(cx
, &next_token
, frag_spec
.as_str())) {
364 cx
.span_err(sp
, &msg
);
367 (&Eof
, _
) => return Some((sp
, tok
.clone())),
368 (_
, Ok(true)) => continue,
369 (next
, Ok(false)) => {
370 cx
.span_err(sp
, &format
!("`${0}:{1}` is followed by `{2}`, which \
371 is not allowed for `{1}` fragments",
372 name
.as_str(), frag_spec
.as_str(),
373 token_to_string(next
)));
379 TtSequence(sp
, ref seq
) => {
380 // iii. Else, T is a complex NT.
381 match seq
.separator
{
382 // If T has the form $(...)U+ or $(...)U* for some token U,
383 // run the algorithm on the contents with F set to U. If it
384 // accepts, continue, else, reject.
386 let last
= check_matcher(cx
, seq
.tts
.iter(), u
);
388 // Since the delimiter isn't required after the last
389 // repetition, make sure that the *next* token is
390 // sane. This doesn't actually compute the FIRST of
391 // the rest of the matcher yet, it only considers
392 // single tokens and simple NTs. This is imprecise,
393 // but conservatively correct.
394 Some((span
, tok
)) => {
395 let fol
= match tokens
.peek() {
396 Some(&&TtToken(_
, ref tok
)) => tok
.clone(),
397 Some(&&TtDelimited(_
, ref delim
)) => delim
.close_token(),
399 cx
.span_err(sp
, "sequence repetition followed by \
400 another sequence repetition, which is not allowed");
405 check_matcher(cx
, Some(&TtToken(span
, tok
.clone())).into_iter(),
411 // If T has the form $(...)+ or $(...)*, run the algorithm
412 // on the contents with F set to the token following the
413 // sequence. If it accepts, continue, else, reject.
415 let fol
= match tokens
.peek() {
416 Some(&&TtToken(_
, ref tok
)) => tok
.clone(),
417 Some(&&TtDelimited(_
, ref delim
)) => delim
.close_token(),
419 cx
.span_err(sp
, "sequence repetition followed by another \
420 sequence repetition, which is not allowed");
425 check_matcher(cx
, seq
.tts
.iter(), &fol
)
430 // i. If T is not an NT, continue.
433 TtDelimited(_
, ref tts
) => {
434 // if we don't pass in that close delimiter, we'll incorrectly consider the matcher
435 // `{ $foo:ty }` as having a follow that isn't `RBrace`
436 check_matcher(cx
, tts
.tts
.iter(), &tts
.close_token())
443 /// True if a fragment of type `frag` can be followed by any sort of
444 /// token. We use this (among other things) as a useful approximation
445 /// for when `frag` can be followed by a repetition like `$(...)*` or
446 /// `$(...)+`. In general, these can be a bit tricky to reason about,
447 /// so we adopt a conservative position that says that any fragment
448 /// specifier which consumes at most one token tree can be followed by
449 /// a fragment specifier (indeed, these fragments can be followed by
450 /// ANYTHING without fear of future compatibility hazards).
451 fn can_be_followed_by_any(frag
: &str) -> bool
{
453 "item" | // always terminated by `}` or `;`
454 "block" | // exactly one token tree
455 "ident" | // exactly one token tree
456 "meta" | // exactly one token tree
457 "tt" => // exactly one token tree
465 /// True if `frag` can legally be followed by the token `tok`. For
466 /// fragments that can consume an unbounded numbe of tokens, `tok`
467 /// must be within a well-defined follow set. This is intended to
468 /// guarantee future compatibility: for example, without this rule, if
469 /// we expanded `expr` to include a new binary operator, we might
470 /// break macros that were relying on that binary operator as a
472 fn is_in_follow(_
: &ExtCtxt
, tok
: &Token
, frag
: &str) -> Result
<bool
, String
> {
473 if let &CloseDelim(_
) = tok
{
474 // closing a token tree can never be matched by any fragment;
475 // iow, we always require that `(` and `)` match, etc.
480 // since items *must* be followed by either a `;` or a `}`, we can
481 // accept anything after them
485 // anything can follow block, the braces provide a easy boundary to
491 FatArrow
| Comma
| Semi
=> Ok(true),
497 FatArrow
| Comma
| Eq
=> Ok(true),
498 Ident(i
, _
) if i
.as_str() == "if" || i
.as_str() == "in" => Ok(true),
504 Comma
| FatArrow
| Colon
| Eq
| Gt
=> Ok(true),
505 Ident(i
, _
) if i
.as_str() == "as" => Ok(true),
510 // being a single token, idents are harmless
514 // being either a single token or a delimited sequence, tt is
518 _
=> Err(format
!("invalid fragment specifier `{}`", frag
))