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.
12 use syntax_pos::{Span, DUMMY_SP};
13 use ext::base::{DummyResult, ExtCtxt, MacResult, SyntaxExtension};
14 use ext::base::{NormalTT, TTMacroExpander};
15 use ext::tt::macro_parser::{Success, Error, Failure};
16 use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
17 use ext::tt::macro_parser::parse;
18 use parse::lexer::new_tt_reader;
19 use parse::parser::{Parser, Restrictions};
20 use parse::token::{self, gensym_ident, NtTT, Token};
21 use parse::token::Token::*;
24 use tokenstream::{self, TokenTree};
26 use util::small_vector::SmallVector;
28 use std::cell::RefCell;
29 use std::collections::{HashMap};
30 use std::collections::hash_map::{Entry};
33 struct ParserAnyMacro<'a> {
34 parser: RefCell<Parser<'a>>,
36 /// Span of the expansion site of the macro this parser is for
38 /// The ident of the macro we're parsing
39 macro_ident: ast::Ident
42 impl<'a> ParserAnyMacro<'a> {
43 /// Make sure we don't have any tokens left to parse, so we don't
44 /// silently drop anything. `allow_semi` is so that "optional"
45 /// semicolons at the end of normal expressions aren't complained
46 /// about e.g. the semicolon in `macro_rules! kapow { () => {
47 /// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
48 /// allowed to be there.
49 fn ensure_complete_parse(&self, allow_semi: bool, context: &str) {
50 let mut parser = self.parser.borrow_mut();
51 if allow_semi && parser.token == token::Semi {
54 if parser.token != token::Eof {
55 let token_str = parser.this_token_to_string();
56 let msg = format!("macro expansion ignores token `{}` and any \
59 let span = parser.span;
60 let mut err = parser.diagnostic().struct_span_err(span, &msg[..]);
61 let msg = format!("caused by the macro expansion here; the usage \
62 of `{}!` is likely invalid in {} context",
63 self.macro_ident, context);
64 err.span_note(self.site_span, &msg[..])
70 impl<'a> MacResult for ParserAnyMacro<'a> {
71 fn make_expr(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Expr>> {
72 let ret = panictry!(self.parser.borrow_mut().parse_expr());
73 self.ensure_complete_parse(true, "expression");
76 fn make_pat(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Pat>> {
77 let ret = panictry!(self.parser.borrow_mut().parse_pat());
78 self.ensure_complete_parse(false, "pattern");
81 fn make_items(self: Box<ParserAnyMacro<'a>>) -> Option<SmallVector<P<ast::Item>>> {
82 let mut ret = SmallVector::zero();
83 while let Some(item) = panictry!(self.parser.borrow_mut().parse_item()) {
86 self.ensure_complete_parse(false, "item");
90 fn make_impl_items(self: Box<ParserAnyMacro<'a>>)
91 -> Option<SmallVector<ast::ImplItem>> {
92 let mut ret = SmallVector::zero();
94 let mut parser = self.parser.borrow_mut();
97 _ => ret.push(panictry!(parser.parse_impl_item()))
100 self.ensure_complete_parse(false, "item");
104 fn make_trait_items(self: Box<ParserAnyMacro<'a>>)
105 -> Option<SmallVector<ast::TraitItem>> {
106 let mut ret = SmallVector::zero();
108 let mut parser = self.parser.borrow_mut();
111 _ => ret.push(panictry!(parser.parse_trait_item()))
114 self.ensure_complete_parse(false, "item");
119 fn make_stmts(self: Box<ParserAnyMacro<'a>>)
120 -> Option<SmallVector<ast::Stmt>> {
121 let mut ret = SmallVector::zero();
123 let mut parser = self.parser.borrow_mut();
126 _ => match parser.parse_full_stmt(true) {
127 Ok(maybe_stmt) => match maybe_stmt {
128 Some(stmt) => ret.push(stmt),
138 self.ensure_complete_parse(false, "statement");
142 fn make_ty(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Ty>> {
143 let ret = panictry!(self.parser.borrow_mut().parse_ty());
144 self.ensure_complete_parse(false, "type");
149 struct MacroRulesMacroExpander {
151 imported_from: Option<ast::Ident>,
152 lhses: Vec<TokenTree>,
153 rhses: Vec<TokenTree>,
157 impl TTMacroExpander for MacroRulesMacroExpander {
158 fn expand<'cx>(&self,
159 cx: &'cx mut ExtCtxt,
162 -> Box<MacResult+'cx> {
164 return DummyResult::any(sp);
166 generic_extension(cx,
176 /// Given `lhses` and `rhses`, this is the new macro we create
177 fn generic_extension<'cx>(cx: &'cx ExtCtxt,
180 imported_from: Option<ast::Ident>,
184 -> Box<MacResult+'cx> {
185 if cx.trace_macros() {
186 println!("{}! {{ {} }}",
188 print::pprust::tts_to_string(arg));
191 // Which arm's failure should we report? (the one furthest along)
192 let mut best_fail_spot = DUMMY_SP;
193 let mut best_fail_msg = "internal error: ran no matchers".to_string();
195 for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
196 let lhs_tt = match *lhs {
197 TokenTree::Delimited(_, ref delim) => &delim.tts[..],
198 _ => cx.span_bug(sp, "malformed macro lhs")
201 match TokenTree::parse(cx, lhs_tt, arg) {
202 Success(named_matches) => {
203 let rhs = match rhses[i] {
205 TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
206 _ => cx.span_bug(sp, "malformed macro rhs"),
208 // rhs has holes ( `$id` and `$(...)` that need filled)
209 let trncbr = new_tt_reader(&cx.parse_sess().span_diagnostic,
213 let mut p = Parser::new(cx.parse_sess(), cx.cfg(), Box::new(trncbr));
214 p.filename = cx.filename.clone();
215 p.mod_path_stack = cx.mod_path_stack.clone();
216 p.restrictions = match cx.in_block {
217 true => Restrictions::NO_NONINLINE_MOD,
218 false => Restrictions::empty(),
220 p.check_unknown_macro_variable();
221 // Let the context choose how to interpret the result.
222 // Weird, but useful for X-macros.
223 return Box::new(ParserAnyMacro {
224 parser: RefCell::new(p),
226 // Pass along the original expansion site and the name of the macro
227 // so we can print a useful error message if the parse of the expanded
228 // macro leaves unparsed tokens.
233 Failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
235 best_fail_msg = (*msg).clone();
237 Error(err_sp, ref msg) => {
238 cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..])
243 cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]);
246 // Note that macro-by-example's input is also matched against a token tree:
247 // $( $lhs:tt => $rhs:tt );+
249 // Holy self-referential!
251 /// Converts a `macro_rules!` invocation into a syntax extension.
252 pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
253 def: &ast::MacroDef) -> SyntaxExtension {
255 let lhs_nm = gensym_ident("lhs");
256 let rhs_nm = gensym_ident("rhs");
258 // The pattern that macro_rules matches.
259 // The grammar for macro_rules! is:
260 // $( $lhs:tt => $rhs:tt );+
261 // ...quasiquoting this would be nice.
262 // These spans won't matter, anyways
263 let match_lhs_tok = MatchNt(lhs_nm, token::str_to_ident("tt"));
264 let match_rhs_tok = MatchNt(rhs_nm, token::str_to_ident("tt"));
265 let argument_gram = vec![
266 TokenTree::Sequence(DUMMY_SP, Rc::new(tokenstream::SequenceRepetition {
268 TokenTree::Token(DUMMY_SP, match_lhs_tok),
269 TokenTree::Token(DUMMY_SP, token::FatArrow),
270 TokenTree::Token(DUMMY_SP, match_rhs_tok),
272 separator: Some(token::Semi),
273 op: tokenstream::KleeneOp::OneOrMore,
276 // to phase into semicolon-termination instead of semicolon-separation
277 TokenTree::Sequence(DUMMY_SP, Rc::new(tokenstream::SequenceRepetition {
278 tts: vec![TokenTree::Token(DUMMY_SP, token::Semi)],
280 op: tokenstream::KleeneOp::ZeroOrMore,
285 // Parse the macro_rules! invocation (`none` is for no interpolations):
286 let arg_reader = new_tt_reader(&cx.parse_sess().span_diagnostic,
291 let argument_map = match parse(cx.parse_sess(),
296 Failure(sp, str) | Error(sp, str) => {
297 panic!(cx.parse_sess().span_diagnostic
298 .span_fatal(sp.substitute_dummy(def.span), &str[..]));
302 let mut valid = true;
304 // Extract the arguments:
305 let lhses = match **argument_map.get(&lhs_nm).unwrap() {
306 MatchedSeq(ref s, _) => {
307 s.iter().map(|m| match **m {
308 MatchedNonterminal(NtTT(ref tt)) => {
309 valid &= check_lhs_nt_follows(cx, tt);
312 _ => cx.span_bug(def.span, "wrong-structured lhs")
315 _ => cx.span_bug(def.span, "wrong-structured lhs")
318 let rhses = match **argument_map.get(&rhs_nm).unwrap() {
319 MatchedSeq(ref s, _) => {
320 s.iter().map(|m| match **m {
321 MatchedNonterminal(NtTT(ref tt)) => (**tt).clone(),
322 _ => cx.span_bug(def.span, "wrong-structured rhs")
325 _ => cx.span_bug(def.span, "wrong-structured rhs")
329 valid &= check_rhs(cx, rhs);
332 let exp: Box<_> = Box::new(MacroRulesMacroExpander {
334 imported_from: def.imported_from,
340 NormalTT(exp, Some(def.span), def.allow_internal_unstable)
343 fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &TokenTree) -> bool {
344 // lhs is going to be like TokenTree::Delimited(...), where the
345 // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
347 &TokenTree::Delimited(_, ref tts) => check_matcher(cx, &tts.tts),
349 cx.span_err(lhs.get_span(), "invalid macro matcher; matchers must \
350 be contained in balanced delimiters");
354 // we don't abort on errors on rejection, the driver will do that for us
355 // after parsing/expansion. we can report every error in every macro this way.
358 fn check_rhs(cx: &mut ExtCtxt, rhs: &TokenTree) -> bool {
360 TokenTree::Delimited(..) => return true,
361 _ => cx.span_err(rhs.get_span(), "macro rhs must be delimited")
366 fn check_matcher(cx: &mut ExtCtxt, matcher: &[TokenTree]) -> bool {
367 let first_sets = FirstSets::new(matcher);
368 let empty_suffix = TokenSet::empty();
369 let err = cx.parse_sess.span_diagnostic.err_count();
370 check_matcher_core(cx, &first_sets, matcher, &empty_suffix);
371 err == cx.parse_sess.span_diagnostic.err_count()
374 // The FirstSets for a matcher is a mapping from subsequences in the
375 // matcher to the FIRST set for that subsequence.
377 // This mapping is partially precomputed via a backwards scan over the
378 // token trees of the matcher, which provides a mapping from each
379 // repetition sequence to its FIRST set.
381 // (Hypothetically sequences should be uniquely identifiable via their
382 // spans, though perhaps that is false e.g. for macro-generated macros
383 // that do not try to inject artificial span information. My plan is
384 // to try to catch such cases ahead of time and not include them in
385 // the precomputed mapping.)
387 // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
388 // span in the original matcher to the First set for the inner sequence `tt ...`.
390 // If two sequences have the same span in a matcher, then map that
391 // span to None (invalidating the mapping here and forcing the code to
393 first: HashMap<Span, Option<TokenSet>>,
397 fn new(tts: &[TokenTree]) -> FirstSets {
398 let mut sets = FirstSets { first: HashMap::new() };
399 build_recur(&mut sets, tts);
402 // walks backward over `tts`, returning the FIRST for `tts`
403 // and updating `sets` at the same time for all sequence
404 // substructure we find within `tts`.
405 fn build_recur(sets: &mut FirstSets, tts: &[TokenTree]) -> TokenSet {
406 let mut first = TokenSet::empty();
407 for tt in tts.iter().rev() {
409 TokenTree::Token(sp, ref tok) => {
410 first.replace_with((sp, tok.clone()));
412 TokenTree::Delimited(_, ref delimited) => {
413 build_recur(sets, &delimited.tts[..]);
414 first.replace_with((delimited.open_span,
415 Token::OpenDelim(delimited.delim)));
417 TokenTree::Sequence(sp, ref seq_rep) => {
418 let subfirst = build_recur(sets, &seq_rep.tts[..]);
420 match sets.first.entry(sp) {
421 Entry::Vacant(vac) => {
422 vac.insert(Some(subfirst.clone()));
424 Entry::Occupied(mut occ) => {
425 // if there is already an entry, then a span must have collided.
426 // This should not happen with typical macro_rules macros,
427 // but syntax extensions need not maintain distinct spans,
428 // so distinct syntax trees can be assigned the same span.
429 // In such a case, the map cannot be trusted; so mark this
430 // entry as unusable.
435 // If the sequence contents can be empty, then the first
436 // token could be the separator token itself.
438 if let (Some(ref sep), true) = (seq_rep.separator.clone(),
439 subfirst.maybe_empty) {
440 first.add_one_maybe((sp, sep.clone()));
443 // Reverse scan: Sequence comes before `first`.
444 if subfirst.maybe_empty || seq_rep.op == tokenstream::KleeneOp::ZeroOrMore {
445 // If sequence is potentially empty, then
446 // union them (preserving first emptiness).
447 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
449 // Otherwise, sequence guaranteed
450 // non-empty; replace first.
461 // walks forward over `tts` until all potential FIRST tokens are
463 fn first(&self, tts: &[TokenTree]) -> TokenSet {
464 let mut first = TokenSet::empty();
465 for tt in tts.iter() {
466 assert!(first.maybe_empty);
468 TokenTree::Token(sp, ref tok) => {
469 first.add_one((sp, tok.clone()));
472 TokenTree::Delimited(_, ref delimited) => {
473 first.add_one((delimited.open_span,
474 Token::OpenDelim(delimited.delim)));
477 TokenTree::Sequence(sp, ref seq_rep) => {
478 match self.first.get(&sp) {
479 Some(&Some(ref subfirst)) => {
481 // If the sequence contents can be empty, then the first
482 // token could be the separator token itself.
484 if let (Some(ref sep), true) = (seq_rep.separator.clone(),
485 subfirst.maybe_empty) {
486 first.add_one_maybe((sp, sep.clone()));
489 assert!(first.maybe_empty);
490 first.add_all(subfirst);
491 if subfirst.maybe_empty ||
492 seq_rep.op == tokenstream::KleeneOp::ZeroOrMore {
493 // continue scanning for more first
494 // tokens, but also make sure we
495 // restore empty-tracking state
496 first.maybe_empty = true;
504 panic!("assume all sequences have (unique) spans for now");
508 panic!("We missed a sequence during FirstSets construction");
515 // we only exit the loop if `tts` was empty or if every
516 // element of `tts` matches the empty sequence.
517 assert!(first.maybe_empty);
522 // A set of Tokens, which may include MatchNt tokens (for
523 // macro-by-example syntactic variables). It also carries the
524 // `maybe_empty` flag; that is true if and only if the matcher can
525 // match an empty token sequence.
527 // The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
528 // which has corresponding FIRST = {$a:expr, c, d}.
529 // Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
531 // (Notably, we must allow for *-op to occur zero times.)
532 #[derive(Clone, Debug)]
534 tokens: Vec<(Span, Token)>,
539 // Returns a set for the empty sequence.
540 fn empty() -> Self { TokenSet { tokens: Vec::new(), maybe_empty: true } }
542 // Returns the set `{ tok }` for the single-token (and thus
543 // non-empty) sequence [tok].
544 fn singleton(tok: (Span, Token)) -> Self {
545 TokenSet { tokens: vec![tok], maybe_empty: false }
548 // Changes self to be the set `{ tok }`.
549 // Since `tok` is always present, marks self as non-empty.
550 fn replace_with(&mut self, tok: (Span, Token)) {
552 self.tokens.push(tok);
553 self.maybe_empty = false;
556 // Changes self to be the empty set `{}`; meant for use when
557 // the particular token does not matter, but we want to
558 // record that it occurs.
559 fn replace_with_irrelevant(&mut self) {
561 self.maybe_empty = false;
564 // Adds `tok` to the set for `self`, marking sequence as non-empy.
565 fn add_one(&mut self, tok: (Span, Token)) {
566 if !self.tokens.contains(&tok) {
567 self.tokens.push(tok);
569 self.maybe_empty = false;
572 // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
573 fn add_one_maybe(&mut self, tok: (Span, Token)) {
574 if !self.tokens.contains(&tok) {
575 self.tokens.push(tok);
579 // Adds all elements of `other` to this.
581 // (Since this is a set, we filter out duplicates.)
583 // If `other` is potentially empty, then preserves the previous
584 // setting of the empty flag of `self`. If `other` is guaranteed
585 // non-empty, then `self` is marked non-empty.
586 fn add_all(&mut self, other: &Self) {
587 for tok in &other.tokens {
588 if !self.tokens.contains(tok) {
589 self.tokens.push(tok.clone());
592 if !other.maybe_empty {
593 self.maybe_empty = false;
598 // Checks that `matcher` is internally consistent and that it
599 // can legally by followed by a token N, for all N in `follow`.
600 // (If `follow` is empty, then it imposes no constraint on
603 // Returns the set of NT tokens that could possibly come last in
604 // `matcher`. (If `matcher` matches the empty sequence, then
605 // `maybe_empty` will be set to true.)
607 // Requires that `first_sets` is pre-computed for `matcher`;
608 // see `FirstSets::new`.
609 fn check_matcher_core(cx: &mut ExtCtxt,
610 first_sets: &FirstSets,
611 matcher: &[TokenTree],
612 follow: &TokenSet) -> TokenSet {
613 use print::pprust::token_to_string;
615 let mut last = TokenSet::empty();
617 // 2. For each token and suffix [T, SUFFIX] in M:
618 // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
619 // then ensure T can also be followed by any element of FOLLOW.
620 'each_token: for i in 0..matcher.len() {
621 let token = &matcher[i];
622 let suffix = &matcher[i+1..];
624 let build_suffix_first = || {
625 let mut s = first_sets.first(suffix);
626 if s.maybe_empty { s.add_all(follow); }
630 // (we build `suffix_first` on demand below; you can tell
631 // which cases are supposed to fall through by looking for the
632 // initialization of this variable.)
635 // First, update `last` so that it corresponds to the set
636 // of NT tokens that might end the sequence `... token`.
638 TokenTree::Token(sp, ref tok) => {
639 let can_be_followed_by_any;
640 if let Err(bad_frag) = has_legal_fragment_specifier(tok) {
641 cx.struct_span_err(sp, &format!("invalid fragment specifier `{}`", bad_frag))
642 .help("valid fragment specifiers are `ident`, `block`, \
643 `stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
646 // (This eliminates false positives and duplicates
647 // from error messages.)
648 can_be_followed_by_any = true;
650 can_be_followed_by_any = token_can_be_followed_by_any(tok);
653 if can_be_followed_by_any {
654 // don't need to track tokens that work with any,
655 last.replace_with_irrelevant();
656 // ... and don't need to check tokens that can be
657 // followed by anything against SUFFIX.
658 continue 'each_token;
660 last.replace_with((sp, tok.clone()));
661 suffix_first = build_suffix_first();
664 TokenTree::Delimited(_, ref d) => {
665 let my_suffix = TokenSet::singleton((d.close_span, Token::CloseDelim(d.delim)));
666 check_matcher_core(cx, first_sets, &d.tts, &my_suffix);
667 // don't track non NT tokens
668 last.replace_with_irrelevant();
670 // also, we don't need to check delimited sequences
672 continue 'each_token;
674 TokenTree::Sequence(sp, ref seq_rep) => {
675 suffix_first = build_suffix_first();
676 // The trick here: when we check the interior, we want
677 // to include the separator (if any) as a potential
678 // (but not guaranteed) element of FOLLOW. So in that
679 // case, we make a temp copy of suffix and stuff
680 // delimiter in there.
682 // FIXME: Should I first scan suffix_first to see if
683 // delimiter is already in it before I go through the
684 // work of cloning it? But then again, this way I may
685 // get a "tighter" span?
687 let my_suffix = if let Some(ref u) = seq_rep.separator {
688 new = suffix_first.clone();
689 new.add_one_maybe((sp, u.clone()));
695 // At this point, `suffix_first` is built, and
696 // `my_suffix` is some TokenSet that we can use
697 // for checking the interior of `seq_rep`.
698 let next = check_matcher_core(cx, first_sets, &seq_rep.tts, my_suffix);
699 if next.maybe_empty {
705 // the recursive call to check_matcher_core already ran the 'each_last
706 // check below, so we can just keep going forward here.
707 continue 'each_token;
711 // (`suffix_first` guaranteed initialized once reaching here.)
713 // Now `last` holds the complete set of NT tokens that could
714 // end the sequence before SUFFIX. Check that every one works with `suffix`.
715 'each_last: for &(_sp, ref t) in &last.tokens {
716 if let MatchNt(ref name, ref frag_spec) = *t {
717 for &(sp, ref next_token) in &suffix_first.tokens {
718 match is_in_follow(cx, next_token, &frag_spec.name.as_str()) {
719 Err((msg, help)) => {
720 cx.struct_span_err(sp, &msg).help(help).emit();
721 // don't bother reporting every source of
722 // conflict for a particular element of `last`.
727 let may_be = if last.tokens.len() == 1 &&
728 suffix_first.tokens.len() == 1
737 &format!("`${name}:{frag}` {may_be} followed by `{next}`, which \
738 is not allowed for `{frag}` fragments",
741 next=token_to_string(next_token),
753 fn token_can_be_followed_by_any(tok: &Token) -> bool {
754 if let &MatchNt(_, ref frag_spec) = tok {
755 frag_can_be_followed_by_any(&frag_spec.name.as_str())
757 // (Non NT's can always be followed by anthing in matchers.)
762 /// True if a fragment of type `frag` can be followed by any sort of
763 /// token. We use this (among other things) as a useful approximation
764 /// for when `frag` can be followed by a repetition like `$(...)*` or
765 /// `$(...)+`. In general, these can be a bit tricky to reason about,
766 /// so we adopt a conservative position that says that any fragment
767 /// specifier which consumes at most one token tree can be followed by
768 /// a fragment specifier (indeed, these fragments can be followed by
769 /// ANYTHING without fear of future compatibility hazards).
770 fn frag_can_be_followed_by_any(frag: &str) -> bool {
772 "item" | // always terminated by `}` or `;`
773 "block" | // exactly one token tree
774 "ident" | // exactly one token tree
775 "meta" | // exactly one token tree
776 "tt" => // exactly one token tree
784 /// True if `frag` can legally be followed by the token `tok`. For
785 /// fragments that can consume an unbounded number of tokens, `tok`
786 /// must be within a well-defined follow set. This is intended to
787 /// guarantee future compatibility: for example, without this rule, if
788 /// we expanded `expr` to include a new binary operator, we might
789 /// break macros that were relying on that binary operator as a
791 // when changing this do not forget to update doc/book/macros.md!
792 fn is_in_follow(_: &ExtCtxt, tok: &Token, frag: &str) -> Result<bool, (String, &'static str)> {
793 if let &CloseDelim(_) = tok {
794 // closing a token tree can never be matched by any fragment;
795 // iow, we always require that `(` and `)` match, etc.
800 // since items *must* be followed by either a `;` or a `}`, we can
801 // accept anything after them
805 // anything can follow block, the braces provide an easy boundary to
811 FatArrow | Comma | Semi => Ok(true),
817 FatArrow | Comma | Eq | BinOp(token::Or) => Ok(true),
818 Ident(i) if (i.name.as_str() == "if" ||
819 i.name.as_str() == "in") => Ok(true),
825 OpenDelim(token::DelimToken::Brace) | OpenDelim(token::DelimToken::Bracket) |
826 Comma | FatArrow | Colon | Eq | Gt | Semi | BinOp(token::Or) => Ok(true),
827 MatchNt(_, ref frag) if frag.name.as_str() == "block" => Ok(true),
828 Ident(i) if i.name.as_str() == "as" || i.name.as_str() == "where" => Ok(true),
833 // being a single token, idents are harmless
837 // being either a single token or a delimited sequence, tt is
841 _ => Err((format!("invalid fragment specifier `{}`", frag),
842 "valid fragment specifiers are `ident`, `block`, \
843 `stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
849 fn has_legal_fragment_specifier(tok: &Token) -> Result<(), String> {
850 debug!("has_legal_fragment_specifier({:?})", tok);
851 if let &MatchNt(_, ref frag_spec) = tok {
852 let s = &frag_spec.name.as_str();
853 if !is_legal_fragment_specifier(s) {
854 return Err(s.to_string());
860 fn is_legal_fragment_specifier(frag: &str) -> bool {
862 "item" | "block" | "stmt" | "expr" | "pat" |
863 "path" | "ty" | "ident" | "meta" | "tt" => true,