quoted.rs 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

M
Mark Mansi 已提交
11
use {ast, attr};
12
use ext::tt::macro_parser;
M
Mark Mansi 已提交
13
use feature_gate::{self, emit_feature_err, Features, GateIssue};
M
Mark Mansi 已提交
14
use parse::{token, ParseSess};
15
use print::pprust;
V
Vadim Petrochenkov 已提交
16
use symbol::keywords;
M
Mark Mansi 已提交
17
use syntax_pos::{BytePos, Span, DUMMY_SP};
18 19
use tokenstream;

20
use std::iter::Peekable;
21
use rustc_data_structures::sync::Lrc;
22

M
Mark Mansi 已提交
23
/// Contains the sub-token-trees of a "delimited" token tree, such as the contents of `(`. Note
M
Mark Mansi 已提交
24
/// that the delimiter itself might be `NoDelim`.
25 26 27 28 29 30 31
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Delimited {
    pub delim: token::DelimToken,
    pub tts: Vec<TokenTree>,
}

impl Delimited {
M
Mark Mansi 已提交
32
    /// Return the opening delimiter (possibly `NoDelim`).
33 34 35 36
    pub fn open_token(&self) -> token::Token {
        token::OpenDelim(self.delim)
    }

M
Mark Mansi 已提交
37
    /// Return the closing delimiter (possibly `NoDelim`).
38 39 40 41
    pub fn close_token(&self) -> token::Token {
        token::CloseDelim(self.delim)
    }

M
Mark Mansi 已提交
42
    /// Return a `self::TokenTree` with a `Span` corresponding to the opening delimiter.
43
    pub fn open_tt(&self, span: Span) -> TokenTree {
44 45 46
        let open_span = if span == DUMMY_SP {
            DUMMY_SP
        } else {
47
            span.with_lo(span.lo() + BytePos(self.delim.len() as u32))
48 49 50 51
        };
        TokenTree::Token(open_span, self.open_token())
    }

M
Mark Mansi 已提交
52
    /// Return a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
53
    pub fn close_tt(&self, span: Span) -> TokenTree {
54 55 56
        let close_span = if span == DUMMY_SP {
            DUMMY_SP
        } else {
57
            span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
58 59 60 61 62 63 64 65 66 67 68 69 70
        };
        TokenTree::Token(close_span, self.close_token())
    }
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct SequenceRepetition {
    /// The sequence of token trees
    pub tts: Vec<TokenTree>,
    /// The optional separator
    pub separator: Option<token::Token>,
    /// Whether the sequence can be repeated zero (*), or one or more times (+)
    pub op: KleeneOp,
J
Jeffrey Seyfried 已提交
71
    /// The number of `Match`s that appear in the sequence (and subsequences)
72 73 74 75 76 77 78
    pub num_captures: usize,
}

/// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
/// for token sequences.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum KleeneOp {
M
Mark Mansi 已提交
79
    /// Kleene star (`*`) for zero or more repetitions
80
    ZeroOrMore,
M
Mark Mansi 已提交
81
    /// Kleene plus (`+`) for one or more repetitions
82
    OneOrMore,
83
    ZeroOrOne,
84 85 86
}

/// Similar to `tokenstream::TokenTree`, except that `$i`, `$i:ident`, and `$(...)`
M
Mark Mansi 已提交
87
/// are "first-class" token trees. Useful for parsing macros.
88 89 90
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub enum TokenTree {
    Token(Span, token::Token),
91
    Delimited(Span, Lrc<Delimited>),
92
    /// A kleene-style repetition sequence
93
    Sequence(Span, Lrc<SequenceRepetition>),
94 95 96
    /// E.g. `$var`
    MetaVar(Span, ast::Ident),
    /// E.g. `$var:expr`. This is only used in the left hand side of MBE macros.
M
Mark Mansi 已提交
97 98 99 100 101
    MetaVarDecl(
        Span,
        ast::Ident, /* name to bind */
        ast::Ident, /* kind of nonterminal */
    ),
102 103 104
}

impl TokenTree {
M
Mark Mansi 已提交
105
    /// Return the number of tokens in the tree.
106 107 108 109 110 111 112
    pub fn len(&self) -> usize {
        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(),
J
Jeffrey Seyfried 已提交
113
            _ => 0,
114 115 116
        }
    }

M
Mark Mansi 已提交
117 118
    /// Returns true if the given token tree contains no other tokens. This is vacuously true for
    /// single tokens or metavar/decls, but may be false for delimited trees or sequences.
119 120 121 122 123 124 125 126 127 128 129
    pub fn is_empty(&self) -> bool {
        match *self {
            TokenTree::Delimited(_, ref delimed) => match delimed.delim {
                token::NoDelim => delimed.tts.is_empty(),
                _ => false,
            },
            TokenTree::Sequence(_, ref seq) => seq.tts.is_empty(),
            _ => true,
        }
    }

M
Mark Mansi 已提交
130
    /// Get the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    pub fn get_tt(&self, index: usize) -> TokenTree {
        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);
                }
                if index == delimed.tts.len() + 1 {
                    return delimed.close_tt(span);
                }
                delimed.tts[index - 1].clone()
            }
            (&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
            _ => panic!("Cannot expand a token tree"),
        }
    }

150
    /// Retrieve the `TokenTree`'s span.
151 152
    pub fn span(&self) -> Span {
        match *self {
M
Mark Mansi 已提交
153 154 155 156 157
            TokenTree::Token(sp, _)
            | TokenTree::MetaVar(sp, _)
            | TokenTree::MetaVarDecl(sp, _, _)
            | TokenTree::Delimited(sp, _)
            | TokenTree::Sequence(sp, _) => sp,
158 159 160 161
        }
    }
}

M
Mark Mansi 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
/// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
/// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
/// collection of `TokenTree` for use in parsing a macro.
///
/// # Parameters
///
/// - `input`: a token stream to read from, the contents of which we are parsing.
/// - `expect_matchers`: `parse` can be used to parse either the "patterns" or the "body" of a
///   macro. Both take roughly the same form _except_ that in a pattern, metavars are declared with
///   their "matcher" type. For example `$var:expr` or `$id:ident`. In this example, `expr` and
///   `ident` are "matchers". They are not present in the body of a macro rule -- just in the
///   pattern, so we pass a parameter to indicate whether to expect them or not.
/// - `sess`: the parsing session. Any errors will be emitted to this session.
M
Mark Mansi 已提交
175 176
/// - `features`, `attrs`: language feature flags and attributes so that we know whether to use
///   unstable features or not.
M
Mark Mansi 已提交
177 178 179 180
///
/// # Returns
///
/// A collection of `self::TokenTree`. There may also be some errors emitted to `sess`.
M
Mark Mansi 已提交
181 182 183 184
pub fn parse(
    input: tokenstream::TokenStream,
    expect_matchers: bool,
    sess: &ParseSess,
185
    features: &Features,
M
Mark Mansi 已提交
186
    attrs: &[ast::Attribute],
M
Mark Mansi 已提交
187
) -> Vec<TokenTree> {
M
Mark Mansi 已提交
188
    // Will contain the final collection of `self::TokenTree`
189
    let mut result = Vec::new();
M
Mark Mansi 已提交
190 191 192

    // For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
    // additional trees if need be.
193
    let mut trees = input.trees().peekable();
194
    while let Some(tree) = trees.next() {
M
Mark Mansi 已提交
195 196
        // Given the parsed tree, if there is a metavar and we are expecting matchers, actually
        // parse out the matcher (i.e. in `$id:ident` this would parse the `:` and `ident`).
M
Mark Mansi 已提交
197
        let tree = parse_tree(tree, &mut trees, expect_matchers, sess, features, attrs);
198
        match tree {
199
            TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
200 201
                let span = match trees.next() {
                    Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() {
J
Jeffrey Seyfried 已提交
202 203
                        Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() {
                            Some(kind) => {
204
                                let span = end_sp.with_lo(start_sp.lo());
J
Jeffrey Seyfried 已提交
205
                                result.push(TokenTree::MetaVarDecl(span, ident, kind));
M
Mark Mansi 已提交
206
                                continue;
J
Jeffrey Seyfried 已提交
207 208 209
                            }
                            _ => end_sp,
                        },
M
Mark Mansi 已提交
210 211 212
                        tree => tree.as_ref()
                            .map(tokenstream::TokenTree::span)
                            .unwrap_or(span),
213
                    },
M
Mark Mansi 已提交
214 215 216
                    tree => tree.as_ref()
                        .map(tokenstream::TokenTree::span)
                        .unwrap_or(start_sp),
217
                };
J
Jeffrey Seyfried 已提交
218
                sess.missing_fragment_specifiers.borrow_mut().insert(span);
M
Mark Mansi 已提交
219 220 221 222 223
                result.push(TokenTree::MetaVarDecl(
                    span,
                    ident,
                    keywords::Invalid.ident(),
                ));
224
            }
M
Mark Mansi 已提交
225 226

            // Not a metavar or no matchers allowed, so just return the tree
227 228 229 230 231 232
            _ => result.push(tree),
        }
    }
    result
}

M
Mark Mansi 已提交
233 234 235 236 237 238 239 240
/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Specifically, this takes a
/// generic `TokenTree`, such as is used in the rest of the compiler, and returns a `TokenTree`
/// for use in parsing a macro.
///
/// Converting the given tree may involve reading more tokens.
///
/// # Parameters
///
M
Mark Mansi 已提交
241
/// - `tree`: the tree we wish to convert.
M
Mark Mansi 已提交
242 243 244 245
/// - `trees`: an iterator over trees. We may need to read more tokens from it in order to finish
///   converting `tree`
/// - `expect_matchers`: same as for `parse` (see above).
/// - `sess`: the parsing session. Any errors will be emitted to this session.
M
Mark Mansi 已提交
246 247
/// - `features`, `attrs`: language feature flags and attributes so that we know whether to use
///   unstable features or not.
M
Mark Mansi 已提交
248 249
fn parse_tree<I>(
    tree: tokenstream::TokenTree,
M
Mark Mansi 已提交
250
    trees: &mut Peekable<I>,
M
Mark Mansi 已提交
251 252
    expect_matchers: bool,
    sess: &ParseSess,
253
    features: &Features,
M
Mark Mansi 已提交
254
    attrs: &[ast::Attribute],
M
Mark Mansi 已提交
255 256 257
) -> TokenTree
where
    I: Iterator<Item = tokenstream::TokenTree>,
258
{
M
Mark Mansi 已提交
259
    // Depending on what `tree` is, we could be parsing different parts of a macro
260
    match tree {
M
Mark Mansi 已提交
261
        // `tree` is a `$` token. Look at the next token in `trees`
262
        tokenstream::TokenTree::Token(span, token::Dollar) => match trees.next() {
M
Mark Mansi 已提交
263 264
            // `tree` is followed by a delimited set of token trees. This indicates the beginning
            // of a repetition sequence in the macro (e.g. `$(pat)*`).
J
Jeffrey Seyfried 已提交
265
            Some(tokenstream::TokenTree::Delimited(span, delimited)) => {
M
Mark Mansi 已提交
266
                // Must have `(` not `{` or `[`
267 268 269 270 271
                if delimited.delim != token::Paren {
                    let tok = pprust::token_to_string(&token::OpenDelim(delimited.delim));
                    let msg = format!("expected `(`, found `{}`", tok);
                    sess.span_diagnostic.span_err(span, &msg);
                }
M
Mark Mansi 已提交
272
                // Parse the contents of the sequence itself
M
Mark Mansi 已提交
273
                let sequence = parse(delimited.tts.into(), expect_matchers, sess, features, attrs);
M
Mark Mansi 已提交
274
                // Get the Kleene operator and optional separator
M
Mark Mansi 已提交
275
                let (separator, op) = parse_sep_and_kleene_op(trees, span, sess, features, attrs);
M
Mark Mansi 已提交
276
                // Count the number of captured "names" (i.e. named metavars)
277
                let name_captures = macro_parser::count_names(&sequence);
M
Mark Mansi 已提交
278 279
                TokenTree::Sequence(
                    span,
280
                    Lrc::new(SequenceRepetition {
M
Mark Mansi 已提交
281 282 283 284 285 286
                        tts: sequence,
                        separator,
                        op,
                        num_captures: name_captures,
                    }),
                )
287
            }
M
Mark Mansi 已提交
288 289 290

            // `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special
            // metavariable that names the crate of the invokation.
291 292
            Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => {
                let ident = token.ident().unwrap();
293
                let span = ident_span.with_lo(span.lo());
294
                if ident.name == keywords::Crate.name() {
M
Mark Mansi 已提交
295 296 297 298
                    let ident = ast::Ident {
                        name: keywords::DollarCrate.name(),
                        ..ident
                    };
299 300
                    TokenTree::Token(span, token::Ident(ident))
                } else {
301
                    TokenTree::MetaVar(span, ident)
302 303
                }
            }
M
Mark Mansi 已提交
304 305

            // `tree` is followed by a random token. This is an error.
306
            Some(tokenstream::TokenTree::Token(span, tok)) => {
M
Mark Mansi 已提交
307 308 309 310
                let msg = format!(
                    "expected identifier, found `{}`",
                    pprust::token_to_string(&tok)
                );
311
                sess.span_diagnostic.span_err(span, &msg);
312
                TokenTree::MetaVar(span, keywords::Invalid.ident())
313
            }
M
Mark Mansi 已提交
314 315

            // There are no more tokens. Just return the `$` we already have.
316 317
            None => TokenTree::Token(span, token::Dollar),
        },
M
Mark Mansi 已提交
318 319

        // `tree` is an arbitrary token. Keep it.
320
        tokenstream::TokenTree::Token(span, tok) => TokenTree::Token(span, tok),
M
Mark Mansi 已提交
321 322 323

        // `tree` is the beginning of a delimited set of tokens (e.g. `(` or `{`). We need to
        // descend into the delimited set and further parse it.
M
Mark Mansi 已提交
324 325
        tokenstream::TokenTree::Delimited(span, delimited) => TokenTree::Delimited(
            span,
326
            Lrc::new(Delimited {
327
                delim: delimited.delim,
M
Mark Mansi 已提交
328
                tts: parse(delimited.tts.into(), expect_matchers, sess, features, attrs),
M
Mark Mansi 已提交
329 330
            }),
        ),
331 332 333
    }
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return
/// `None`.
fn kleene_op(token: &token::Token) -> Option<KleeneOp> {
    match *token {
        token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore),
        token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore),
        token::Question => Some(KleeneOp::ZeroOrOne),
        _ => None,
    }
}

/// Parse the next token tree of the input looking for a KleeneOp. Returns
///
/// - Ok(Ok(op)) if the next token tree is a KleeneOp
/// - Ok(Err(tok, span)) if the next token tree is a token but not a KleeneOp
/// - Err(span) if the next token tree is not a token
M
Mark Mansi 已提交
350 351 352 353 354 355
fn parse_kleene_op<I>(
    input: &mut I,
    span: Span,
) -> Result<Result<KleeneOp, (token::Token, Span)>, Span>
where
    I: Iterator<Item = tokenstream::TokenTree>,
356 357 358 359 360
{
    match input.next() {
        Some(tokenstream::TokenTree::Token(span, tok)) => match kleene_op(&tok) {
            Some(op) => Ok(Ok(op)),
            None => Ok(Err((tok, span))),
M
Mark Mansi 已提交
361 362 363 364
        },
        tree => Err(tree.as_ref()
            .map(tokenstream::TokenTree::span)
            .unwrap_or(span)),
365 366 367
    }
}

M
Mark Mansi 已提交
368 369 370 371 372
/// Attempt to parse a single Kleene star, possibly with a separator.
///
/// For example, in a pattern such as `$(a),*`, `a` is the pattern to be repeated, `,` is the
/// separator, and `*` is the Kleene operator. This function is specifically concerned with parsing
/// the last two tokens of such a pattern: namely, the optional separator and the Kleene operator
M
Mark Mansi 已提交
373
/// itself. Note that here we are parsing the _macro_ itself, rather than trying to match some
M
Mark Mansi 已提交
374
/// stream of tokens in an invocation of a macro.
M
Mark Mansi 已提交
375 376 377 378 379
///
/// This function will take some input iterator `input` corresponding to `span` and a parsing
/// session `sess`. If the next one (or possibly two) tokens in `input` correspond to a Kleene
/// operator and separator, then a tuple with `(separator, KleeneOp)` is returned. Otherwise, an
/// error with the appropriate span is emitted to `sess` and a dummy value is returned.
M
Mark Mansi 已提交
380 381 382 383
fn parse_sep_and_kleene_op<I>(
    input: &mut Peekable<I>,
    span: Span,
    sess: &ParseSess,
384
    features: &Features,
M
Mark Mansi 已提交
385
    attrs: &[ast::Attribute],
M
Mark Mansi 已提交
386 387 388
) -> (Option<token::Token>, KleeneOp)
where
    I: Iterator<Item = tokenstream::TokenTree>,
389
{
390 391 392 393 394 395 396 397 398 399 400 401
    // We basically look at two token trees here, denoted as #1 and #2 below
    let span = match parse_kleene_op(input, span) {
        // #1 is a `+` or `*` KleeneOp
        //
        // `?` is ambiguous: it could be a separator or a Kleene::ZeroOrOne, so we need to look
        // ahead one more token to be sure.
        Ok(Ok(op)) if op != KleeneOp::ZeroOrOne => return (None, op),

        // #1 is `?` token, but it could be a Kleene::ZeroOrOne without a separator or it could
        // be a `?` separator followed by any Kleene operator. We need to look ahead 1 token to
        // find out which.
        Ok(Ok(op)) => {
402 403
            assert_eq!(op, KleeneOp::ZeroOrOne);

404 405 406 407 408 409 410 411 412 413 414 415
            // Lookahead at #2. If it is a KleenOp, then #1 is a separator.
            let is_1_sep = if let Some(&tokenstream::TokenTree::Token(_, ref tok2)) = input.peek() {
                kleene_op(tok2).is_some()
            } else {
                false
            };

            if is_1_sep {
                // #1 is a separator and #2 should be a KleepeOp::*
                // (N.B. We need to advance the input iterator.)
                match parse_kleene_op(input, span) {
                    // #2 is a KleeneOp (this is the only valid option) :)
M
Mark Mansi 已提交
416
                    Ok(Ok(op)) if op == KleeneOp::ZeroOrOne => {
417
                        if !features.macro_at_most_once_rep
M
Mark Mansi 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
                            && !attr::contains_name(attrs, "allow_internal_unstable")
                        {
                            let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
                            emit_feature_err(
                                sess,
                                "macro_at_most_once_rep",
                                span,
                                GateIssue::Language,
                                explain,
                            );
                        }
                        return (Some(token::Question), op);
                    }
431 432 433 434 435 436 437 438 439
                    Ok(Ok(op)) => return (Some(token::Question), op),

                    // #2 is a random token (this is an error) :(
                    Ok(Err((_, span))) => span,

                    // #2 is not even a token at all :(
                    Err(span) => span,
                }
            } else {
440
                if !features.macro_at_most_once_rep
M
Mark Mansi 已提交
441 442 443 444 445 446 447 448 449 450 451 452
                    && !attr::contains_name(attrs, "allow_internal_unstable")
                {
                    let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
                    emit_feature_err(
                        sess,
                        "macro_at_most_once_rep",
                        span,
                        GateIssue::Language,
                        explain,
                    );
                }

453 454 455
                // #2 is a random tree and #1 is KleeneOp::ZeroOrOne
                return (None, op);
            }
456 457
        }

458 459 460
        // #1 is a separator followed by #2, a KleeneOp
        Ok(Err((tok, span))) => match parse_kleene_op(input, span) {
            // #2 is a KleeneOp :D
M
Mark Mansi 已提交
461
            Ok(Ok(op)) if op == KleeneOp::ZeroOrOne => {
462
                if !features.macro_at_most_once_rep
M
Mark Mansi 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475
                    && !attr::contains_name(attrs, "allow_internal_unstable")
                {
                    let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
                    emit_feature_err(
                        sess,
                        "macro_at_most_once_rep",
                        span,
                        GateIssue::Language,
                        explain,
                    );
                }
                return (Some(tok), op);
            }
476 477 478 479 480 481 482
            Ok(Ok(op)) => return (Some(tok), op),

            // #2 is a random token :(
            Ok(Err((_, span))) => span,

            // #2 is not a token at all :(
            Err(span) => span,
M
Mark Mansi 已提交
483
        },
M
Mark Mansi 已提交
484

485 486
        // #1 is not a token
        Err(span) => span,
487 488
    };

489
    if !features.macro_at_most_once_rep
M
Mark Mansi 已提交
490 491 492 493 494 495 496
        && !attr::contains_name(attrs, "allow_internal_unstable")
    {
        sess.span_diagnostic
            .span_err(span, "expected one of: `*`, `+`, or `?`");
    } else {
        sess.span_diagnostic.span_err(span, "expected `*` or `+`");
    }
497 498
    (None, KleeneOp::ZeroOrMore)
}