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::expand::{Expansion, ExpansionKind};
16 use ext::tt::macro_parser::{Success, Error, Failure};
17 use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
18 use ext::tt::macro_parser::{parse, parse_failure_msg};
20 use ext::tt::transcribe::transcribe;
21 use parse::{Directory, ParseSess};
22 use parse::parser::Parser;
23 use parse::token::{self, NtTT};
24 use parse::token::Token::*;
26 use tokenstream::{TokenStream, TokenTree};
28 use std::collections::{HashMap};
29 use std::collections::hash_map::{Entry};
32 pub struct ParserAnyMacro<'a> {
35 /// Span of the expansion site of the macro this parser is for
37 /// The ident of the macro we're parsing
38 macro_ident: ast::Ident
41 impl<'a> ParserAnyMacro<'a> {
42 pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: ExpansionKind) -> Expansion {
43 let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self;
44 let expansion = panictry!(parser.parse_expansion(kind, true));
46 // We allow semicolons at the end of expressions -- e.g. the semicolon in
47 // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
48 // but `m!()` is allowed in expression positions (c.f. issue #34706).
49 if kind == ExpansionKind::Expr && parser.token == token::Semi {
53 // Make sure we don't have any tokens left to parse so we don't silently drop anything.
54 let path = ast::Path::from_ident(site_span, macro_ident);
55 parser.ensure_complete_parse(&path, kind.name(), site_span);
60 struct MacroRulesMacroExpander {
62 lhses: Vec<quoted::TokenTree>,
63 rhses: Vec<quoted::TokenTree>,
67 impl TTMacroExpander for MacroRulesMacroExpander {
72 -> Box<MacResult+'cx> {
74 return DummyResult::any(sp);
85 /// Given `lhses` and `rhses`, this is the new macro we create
86 fn generic_extension<'cx>(cx: &'cx ExtCtxt,
90 lhses: &[quoted::TokenTree],
91 rhses: &[quoted::TokenTree])
92 -> Box<MacResult+'cx> {
93 if cx.trace_macros() {
94 println!("{}! {{ {} }}", name, arg);
97 // Which arm's failure should we report? (the one furthest along)
98 let mut best_fail_spot = DUMMY_SP;
99 let mut best_fail_tok = None;
101 for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
102 let lhs_tt = match *lhs {
103 quoted::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
104 _ => cx.span_bug(sp, "malformed macro lhs")
107 match TokenTree::parse(cx, lhs_tt, arg.clone()) {
108 Success(named_matches) => {
109 let rhs = match rhses[i] {
111 quoted::TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
112 _ => cx.span_bug(sp, "malformed macro rhs"),
114 // rhs has holes ( `$id` and `$(...)` that need filled)
115 let tts = transcribe(&cx.parse_sess.span_diagnostic, Some(named_matches), rhs);
116 let directory = Directory {
117 path: cx.current_expansion.module.directory.clone(),
118 ownership: cx.current_expansion.directory_ownership,
120 let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), false);
121 p.root_module_name = cx.current_expansion.module.mod_path.last()
122 .map(|id| (*id.name.as_str()).to_owned());
124 p.check_unknown_macro_variable();
125 // Let the context choose how to interpret the result.
126 // Weird, but useful for X-macros.
127 return Box::new(ParserAnyMacro {
130 // Pass along the original expansion site and the name of the macro
131 // so we can print a useful error message if the parse of the expanded
132 // macro leaves unparsed tokens.
137 Failure(sp, tok) => if sp.lo >= best_fail_spot.lo {
139 best_fail_tok = Some(tok);
141 Error(err_sp, ref msg) => {
142 cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..])
147 let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
148 cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
151 // Note that macro-by-example's input is also matched against a token tree:
152 // $( $lhs:tt => $rhs:tt );+
154 // Holy self-referential!
156 /// Converts a `macro_rules!` invocation into a syntax extension.
157 pub fn compile(sess: &ParseSess, def: &ast::Item) -> SyntaxExtension {
158 let lhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("lhs"));
159 let rhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("rhs"));
161 // The pattern that macro_rules matches.
162 // The grammar for macro_rules! is:
163 // $( $lhs:tt => $rhs:tt );+
164 // ...quasiquoting this would be nice.
165 // These spans won't matter, anyways
166 let argument_gram = vec![
167 quoted::TokenTree::Sequence(DUMMY_SP, Rc::new(quoted::SequenceRepetition {
169 quoted::TokenTree::MetaVarDecl(DUMMY_SP, lhs_nm, ast::Ident::from_str("tt")),
170 quoted::TokenTree::Token(DUMMY_SP, token::FatArrow),
171 quoted::TokenTree::MetaVarDecl(DUMMY_SP, rhs_nm, ast::Ident::from_str("tt")),
173 separator: Some(token::Semi),
174 op: quoted::KleeneOp::OneOrMore,
177 // to phase into semicolon-termination instead of semicolon-separation
178 quoted::TokenTree::Sequence(DUMMY_SP, Rc::new(quoted::SequenceRepetition {
179 tts: vec![quoted::TokenTree::Token(DUMMY_SP, token::Semi)],
181 op: quoted::KleeneOp::ZeroOrMore,
186 // Parse the macro_rules! invocation
187 let body = match def.node {
188 ast::ItemKind::MacroDef(ref body) => body.clone().into(),
191 let argument_map = match parse(sess, body, &argument_gram, None) {
193 Failure(sp, tok) => {
194 let s = parse_failure_msg(tok);
195 panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
198 panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
202 let mut valid = true;
204 // Extract the arguments:
205 let lhses = match **argument_map.get(&lhs_nm).unwrap() {
206 MatchedSeq(ref s, _) => {
208 if let MatchedNonterminal(ref nt) = **m {
209 if let NtTT(ref tt) = **nt {
210 let tt = quoted::parse(tt.clone().into(), true, sess).pop().unwrap();
211 valid &= check_lhs_nt_follows(sess, &tt);
215 sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
216 }).collect::<Vec<quoted::TokenTree>>()
218 _ => sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
221 let rhses = match **argument_map.get(&rhs_nm).unwrap() {
222 MatchedSeq(ref s, _) => {
224 if let MatchedNonterminal(ref nt) = **m {
225 if let NtTT(ref tt) = **nt {
226 return quoted::parse(tt.clone().into(), false, sess).pop().unwrap();
229 sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
230 }).collect::<Vec<quoted::TokenTree>>()
232 _ => sess.span_diagnostic.span_bug(def.span, "wrong-structured rhs")
236 valid &= check_rhs(sess, rhs);
239 // don't abort iteration early, so that errors for multiple lhses can be reported
241 valid &= check_lhs_no_empty_seq(sess, &[lhs.clone()])
244 let exp: Box<_> = Box::new(MacroRulesMacroExpander {
251 NormalTT(exp, Some(def.span), attr::contains_name(&def.attrs, "allow_internal_unstable"))
254 fn check_lhs_nt_follows(sess: &ParseSess, lhs: "ed::TokenTree) -> bool {
255 // lhs is going to be like TokenTree::Delimited(...), where the
256 // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
258 "ed::TokenTree::Delimited(_, ref tts) => check_matcher(sess, &tts.tts),
260 let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
261 sess.span_diagnostic.span_err(lhs.span(), msg);
265 // we don't abort on errors on rejection, the driver will do that for us
266 // after parsing/expansion. we can report every error in every macro this way.
269 /// Check that the lhs contains no repetition which could match an empty token
270 /// tree, because then the matcher would hang indefinitely.
271 fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[quoted::TokenTree]) -> bool {
272 use self::quoted::TokenTree;
275 TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => (),
276 TokenTree::Delimited(_, ref del) => if !check_lhs_no_empty_seq(sess, &del.tts) {
279 TokenTree::Sequence(span, ref seq) => {
280 if seq.separator.is_none() {
281 if seq.tts.iter().all(|seq_tt| {
283 TokenTree::Sequence(_, ref sub_seq) =>
284 sub_seq.op == quoted::KleeneOp::ZeroOrMore,
288 sess.span_diagnostic.span_err(span, "repetition matches empty token tree");
292 if !check_lhs_no_empty_seq(sess, &seq.tts) {
302 fn check_rhs(sess: &ParseSess, rhs: "ed::TokenTree) -> bool {
304 quoted::TokenTree::Delimited(..) => return true,
305 _ => sess.span_diagnostic.span_err(rhs.span(), "macro rhs must be delimited")
310 fn check_matcher(sess: &ParseSess, matcher: &[quoted::TokenTree]) -> bool {
311 let first_sets = FirstSets::new(matcher);
312 let empty_suffix = TokenSet::empty();
313 let err = sess.span_diagnostic.err_count();
314 check_matcher_core(sess, &first_sets, matcher, &empty_suffix);
315 err == sess.span_diagnostic.err_count()
318 // The FirstSets for a matcher is a mapping from subsequences in the
319 // matcher to the FIRST set for that subsequence.
321 // This mapping is partially precomputed via a backwards scan over the
322 // token trees of the matcher, which provides a mapping from each
323 // repetition sequence to its FIRST set.
325 // (Hypothetically sequences should be uniquely identifiable via their
326 // spans, though perhaps that is false e.g. for macro-generated macros
327 // that do not try to inject artificial span information. My plan is
328 // to try to catch such cases ahead of time and not include them in
329 // the precomputed mapping.)
331 // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
332 // span in the original matcher to the First set for the inner sequence `tt ...`.
334 // If two sequences have the same span in a matcher, then map that
335 // span to None (invalidating the mapping here and forcing the code to
337 first: HashMap<Span, Option<TokenSet>>,
341 fn new(tts: &[quoted::TokenTree]) -> FirstSets {
342 use self::quoted::TokenTree;
344 let mut sets = FirstSets { first: HashMap::new() };
345 build_recur(&mut sets, tts);
348 // walks backward over `tts`, returning the FIRST for `tts`
349 // and updating `sets` at the same time for all sequence
350 // substructure we find within `tts`.
351 fn build_recur(sets: &mut FirstSets, tts: &[TokenTree]) -> TokenSet {
352 let mut first = TokenSet::empty();
353 for tt in tts.iter().rev() {
355 TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
356 first.replace_with(tt.clone());
358 TokenTree::Delimited(span, ref delimited) => {
359 build_recur(sets, &delimited.tts[..]);
360 first.replace_with(delimited.open_tt(span));
362 TokenTree::Sequence(sp, ref seq_rep) => {
363 let subfirst = build_recur(sets, &seq_rep.tts[..]);
365 match sets.first.entry(sp) {
366 Entry::Vacant(vac) => {
367 vac.insert(Some(subfirst.clone()));
369 Entry::Occupied(mut occ) => {
370 // if there is already an entry, then a span must have collided.
371 // This should not happen with typical macro_rules macros,
372 // but syntax extensions need not maintain distinct spans,
373 // so distinct syntax trees can be assigned the same span.
374 // In such a case, the map cannot be trusted; so mark this
375 // entry as unusable.
380 // If the sequence contents can be empty, then the first
381 // token could be the separator token itself.
383 if let (Some(ref sep), true) = (seq_rep.separator.clone(),
384 subfirst.maybe_empty) {
385 first.add_one_maybe(TokenTree::Token(sp, sep.clone()));
388 // Reverse scan: Sequence comes before `first`.
389 if subfirst.maybe_empty || seq_rep.op == quoted::KleeneOp::ZeroOrMore {
390 // If sequence is potentially empty, then
391 // union them (preserving first emptiness).
392 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
394 // Otherwise, sequence guaranteed
395 // non-empty; replace first.
406 // walks forward over `tts` until all potential FIRST tokens are
408 fn first(&self, tts: &[quoted::TokenTree]) -> TokenSet {
409 use self::quoted::TokenTree;
411 let mut first = TokenSet::empty();
412 for tt in tts.iter() {
413 assert!(first.maybe_empty);
415 TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
416 first.add_one(tt.clone());
419 TokenTree::Delimited(span, ref delimited) => {
420 first.add_one(delimited.open_tt(span));
423 TokenTree::Sequence(sp, ref seq_rep) => {
424 match self.first.get(&sp) {
425 Some(&Some(ref subfirst)) => {
427 // If the sequence contents can be empty, then the first
428 // token could be the separator token itself.
430 if let (Some(ref sep), true) = (seq_rep.separator.clone(),
431 subfirst.maybe_empty) {
432 first.add_one_maybe(TokenTree::Token(sp, sep.clone()));
435 assert!(first.maybe_empty);
436 first.add_all(subfirst);
437 if subfirst.maybe_empty ||
438 seq_rep.op == quoted::KleeneOp::ZeroOrMore {
439 // continue scanning for more first
440 // tokens, but also make sure we
441 // restore empty-tracking state
442 first.maybe_empty = true;
450 panic!("assume all sequences have (unique) spans for now");
454 panic!("We missed a sequence during FirstSets construction");
461 // we only exit the loop if `tts` was empty or if every
462 // element of `tts` matches the empty sequence.
463 assert!(first.maybe_empty);
468 // A set of `quoted::TokenTree`s, which may include `TokenTree::Match`s
469 // (for macro-by-example syntactic variables). It also carries the
470 // `maybe_empty` flag; that is true if and only if the matcher can
471 // match an empty token sequence.
473 // The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
474 // which has corresponding FIRST = {$a:expr, c, d}.
475 // Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
477 // (Notably, we must allow for *-op to occur zero times.)
478 #[derive(Clone, Debug)]
480 tokens: Vec<quoted::TokenTree>,
485 // Returns a set for the empty sequence.
486 fn empty() -> Self { TokenSet { tokens: Vec::new(), maybe_empty: true } }
488 // Returns the set `{ tok }` for the single-token (and thus
489 // non-empty) sequence [tok].
490 fn singleton(tok: quoted::TokenTree) -> Self {
491 TokenSet { tokens: vec![tok], maybe_empty: false }
494 // Changes self to be the set `{ tok }`.
495 // Since `tok` is always present, marks self as non-empty.
496 fn replace_with(&mut self, tok: quoted::TokenTree) {
498 self.tokens.push(tok);
499 self.maybe_empty = false;
502 // Changes self to be the empty set `{}`; meant for use when
503 // the particular token does not matter, but we want to
504 // record that it occurs.
505 fn replace_with_irrelevant(&mut self) {
507 self.maybe_empty = false;
510 // Adds `tok` to the set for `self`, marking sequence as non-empy.
511 fn add_one(&mut self, tok: quoted::TokenTree) {
512 if !self.tokens.contains(&tok) {
513 self.tokens.push(tok);
515 self.maybe_empty = false;
518 // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
519 fn add_one_maybe(&mut self, tok: quoted::TokenTree) {
520 if !self.tokens.contains(&tok) {
521 self.tokens.push(tok);
525 // Adds all elements of `other` to this.
527 // (Since this is a set, we filter out duplicates.)
529 // If `other` is potentially empty, then preserves the previous
530 // setting of the empty flag of `self`. If `other` is guaranteed
531 // non-empty, then `self` is marked non-empty.
532 fn add_all(&mut self, other: &Self) {
533 for tok in &other.tokens {
534 if !self.tokens.contains(tok) {
535 self.tokens.push(tok.clone());
538 if !other.maybe_empty {
539 self.maybe_empty = false;
544 // Checks that `matcher` is internally consistent and that it
545 // can legally by followed by a token N, for all N in `follow`.
546 // (If `follow` is empty, then it imposes no constraint on
549 // Returns the set of NT tokens that could possibly come last in
550 // `matcher`. (If `matcher` matches the empty sequence, then
551 // `maybe_empty` will be set to true.)
553 // Requires that `first_sets` is pre-computed for `matcher`;
554 // see `FirstSets::new`.
555 fn check_matcher_core(sess: &ParseSess,
556 first_sets: &FirstSets,
557 matcher: &[quoted::TokenTree],
558 follow: &TokenSet) -> TokenSet {
559 use self::quoted::TokenTree;
561 let mut last = TokenSet::empty();
563 // 2. For each token and suffix [T, SUFFIX] in M:
564 // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
565 // then ensure T can also be followed by any element of FOLLOW.
566 'each_token: for i in 0..matcher.len() {
567 let token = &matcher[i];
568 let suffix = &matcher[i+1..];
570 let build_suffix_first = || {
571 let mut s = first_sets.first(suffix);
572 if s.maybe_empty { s.add_all(follow); }
576 // (we build `suffix_first` on demand below; you can tell
577 // which cases are supposed to fall through by looking for the
578 // initialization of this variable.)
581 // First, update `last` so that it corresponds to the set
582 // of NT tokens that might end the sequence `... token`.
584 TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
585 let can_be_followed_by_any;
586 if let Err(bad_frag) = has_legal_fragment_specifier(token) {
587 let msg = format!("invalid fragment specifier `{}`", bad_frag);
588 sess.span_diagnostic.struct_span_err(token.span(), &msg)
589 .help("valid fragment specifiers are `ident`, `block`, \
590 `stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
593 // (This eliminates false positives and duplicates
594 // from error messages.)
595 can_be_followed_by_any = true;
597 can_be_followed_by_any = token_can_be_followed_by_any(token);
600 if can_be_followed_by_any {
601 // don't need to track tokens that work with any,
602 last.replace_with_irrelevant();
603 // ... and don't need to check tokens that can be
604 // followed by anything against SUFFIX.
605 continue 'each_token;
607 last.replace_with(token.clone());
608 suffix_first = build_suffix_first();
611 TokenTree::Delimited(span, ref d) => {
612 let my_suffix = TokenSet::singleton(d.close_tt(span));
613 check_matcher_core(sess, first_sets, &d.tts, &my_suffix);
614 // don't track non NT tokens
615 last.replace_with_irrelevant();
617 // also, we don't need to check delimited sequences
619 continue 'each_token;
621 TokenTree::Sequence(sp, ref seq_rep) => {
622 suffix_first = build_suffix_first();
623 // The trick here: when we check the interior, we want
624 // to include the separator (if any) as a potential
625 // (but not guaranteed) element of FOLLOW. So in that
626 // case, we make a temp copy of suffix and stuff
627 // delimiter in there.
629 // FIXME: Should I first scan suffix_first to see if
630 // delimiter is already in it before I go through the
631 // work of cloning it? But then again, this way I may
632 // get a "tighter" span?
634 let my_suffix = if let Some(ref u) = seq_rep.separator {
635 new = suffix_first.clone();
636 new.add_one_maybe(TokenTree::Token(sp, u.clone()));
642 // At this point, `suffix_first` is built, and
643 // `my_suffix` is some TokenSet that we can use
644 // for checking the interior of `seq_rep`.
645 let next = check_matcher_core(sess, first_sets, &seq_rep.tts, my_suffix);
646 if next.maybe_empty {
652 // the recursive call to check_matcher_core already ran the 'each_last
653 // check below, so we can just keep going forward here.
654 continue 'each_token;
658 // (`suffix_first` guaranteed initialized once reaching here.)
660 // Now `last` holds the complete set of NT tokens that could
661 // end the sequence before SUFFIX. Check that every one works with `suffix`.
662 'each_last: for token in &last.tokens {
663 if let TokenTree::MetaVarDecl(_, ref name, ref frag_spec) = *token {
664 for next_token in &suffix_first.tokens {
665 match is_in_follow(next_token, &frag_spec.name.as_str()) {
666 Err((msg, help)) => {
667 sess.span_diagnostic.struct_span_err(next_token.span(), &msg)
669 // don't bother reporting every source of
670 // conflict for a particular element of `last`.
675 let may_be = if last.tokens.len() == 1 &&
676 suffix_first.tokens.len() == 1
683 sess.span_diagnostic.span_err(
685 &format!("`${name}:{frag}` {may_be} followed by `{next}`, which \
686 is not allowed for `{frag}` fragments",
689 next=quoted_tt_to_string(next_token),
701 fn token_can_be_followed_by_any(tok: "ed::TokenTree) -> bool {
702 if let quoted::TokenTree::MetaVarDecl(_, _, frag_spec) = *tok {
703 frag_can_be_followed_by_any(&frag_spec.name.as_str())
705 // (Non NT's can always be followed by anthing in matchers.)
710 /// True if a fragment of type `frag` can be followed by any sort of
711 /// token. We use this (among other things) as a useful approximation
712 /// for when `frag` can be followed by a repetition like `$(...)*` or
713 /// `$(...)+`. In general, these can be a bit tricky to reason about,
714 /// so we adopt a conservative position that says that any fragment
715 /// specifier which consumes at most one token tree can be followed by
716 /// a fragment specifier (indeed, these fragments can be followed by
717 /// ANYTHING without fear of future compatibility hazards).
718 fn frag_can_be_followed_by_any(frag: &str) -> bool {
720 "item" | // always terminated by `}` or `;`
721 "block" | // exactly one token tree
722 "ident" | // exactly one token tree
723 "meta" | // exactly one token tree
724 "tt" => // exactly one token tree
732 /// True if `frag` can legally be followed by the token `tok`. For
733 /// fragments that can consume an unbounded number of tokens, `tok`
734 /// must be within a well-defined follow set. This is intended to
735 /// guarantee future compatibility: for example, without this rule, if
736 /// we expanded `expr` to include a new binary operator, we might
737 /// break macros that were relying on that binary operator as a
739 // when changing this do not forget to update doc/book/macros.md!
740 fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> Result<bool, (String, &'static str)> {
741 use self::quoted::TokenTree;
743 if let TokenTree::Token(_, token::CloseDelim(_)) = *tok {
744 // closing a token tree can never be matched by any fragment;
745 // iow, we always require that `(` and `)` match, etc.
750 // since items *must* be followed by either a `;` or a `}`, we can
751 // accept anything after them
755 // anything can follow block, the braces provide an easy boundary to
759 "stmt" | "expr" => match *tok {
760 TokenTree::Token(_, ref tok) => match *tok {
761 FatArrow | Comma | Semi => Ok(true),
766 "pat" => match *tok {
767 TokenTree::Token(_, ref tok) => match *tok {
768 FatArrow | Comma | Eq | BinOp(token::Or) => Ok(true),
769 Ident(i) if i.name == "if" || i.name == "in" => Ok(true),
774 "path" | "ty" => match *tok {
775 TokenTree::Token(_, ref tok) => match *tok {
776 OpenDelim(token::DelimToken::Brace) | OpenDelim(token::DelimToken::Bracket) |
777 Comma | FatArrow | Colon | Eq | Gt | Semi | BinOp(token::Or) => Ok(true),
778 Ident(i) if i.name == "as" || i.name == "where" => Ok(true),
781 TokenTree::MetaVarDecl(_, _, frag) if frag.name == "block" => Ok(true),
785 // being a single token, idents are harmless
789 // being either a single token or a delimited sequence, tt is
793 "" => Ok(true), // keywords::Invalid
794 _ => Err((format!("invalid fragment specifier `{}`", frag),
795 "valid fragment specifiers are `ident`, `block`, \
796 `stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
802 fn has_legal_fragment_specifier(tok: "ed::TokenTree) -> Result<(), String> {
803 debug!("has_legal_fragment_specifier({:?})", tok);
804 if let quoted::TokenTree::MetaVarDecl(_, _, frag_spec) = *tok {
805 let s = &frag_spec.name.as_str();
806 if !is_legal_fragment_specifier(s) {
807 return Err(s.to_string());
813 fn is_legal_fragment_specifier(frag: &str) -> bool {
815 "item" | "block" | "stmt" | "expr" | "pat" |
816 "path" | "ty" | "ident" | "meta" | "tt" | "" => true,
821 fn quoted_tt_to_string(tt: "ed::TokenTree) -> String {
823 quoted::TokenTree::Token(_, ref tok) => ::print::pprust::token_to_string(tok),
824 quoted::TokenTree::MetaVarDecl(_, name, kind) => format!("${}:{}", name, kind),
825 _ => panic!("unexpected quoted::TokenTree::{Sequence or Delimited} in follow set checker"),