2 pub use Nonterminal::*;
7 use crate::ast::{self};
8 use crate::parse::{parse_stream_from_source_str, ParseSess};
9 use crate::print::pprust;
11 use crate::symbol::kw;
12 use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
14 use syntax_pos::symbol::Symbol;
15 use syntax_pos::{self, Span, FileName, DUMMY_SP};
20 #[cfg(target_arch = "x86_64")]
21 use rustc_data_structures::static_assert_size;
22 use rustc_data_structures::sync::Lrc;
24 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
38 /// A delimiter token.
39 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
41 /// A round parenthesis (i.e., `(` or `)`).
43 /// A square bracket (i.e., `[` or `]`).
45 /// A curly brace (i.e., `{` or `}`).
47 /// An empty delimiter.
52 pub fn len(self) -> usize {
53 if self == NoDelim { 0 } else { 1 }
56 pub fn is_empty(self) -> bool {
61 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
63 Bool, // AST only, must never appear in a `Token`
69 StrRaw(u16), // raw string delimited by `n` hash symbols
71 ByteStrRaw(u16), // raw byte string delimited by `n` hash symbols
76 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
80 pub suffix: Option<Symbol>,
84 /// An English article for the literal token kind.
85 crate fn article(self) -> &'static str {
87 Integer | Err => "an",
92 crate fn descr(self) -> &'static str {
94 Bool => panic!("literal token contains `Lit::Bool`"),
99 Str | StrRaw(..) => "string",
100 ByteStr | ByteStrRaw(..) => "byte string",
105 crate fn may_have_suffix(self) -> bool {
107 Integer | Float | Err => true,
114 pub fn new(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Lit {
115 Lit { kind, symbol, suffix }
119 pub(crate) fn ident_can_begin_expr(name: ast::Name, span: Span, is_raw: bool) -> bool {
120 let ident_token = Token::new(Ident(name, is_raw), span);
122 !ident_token.is_reserved_ident() ||
123 ident_token.is_path_segment_keyword() ||
127 // FIXME: remove when `await!(..)` syntax is removed
128 // https://github.com/rust-lang/rust/issues/60610
150 fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool {
151 let ident_token = Token::new(Ident(name, is_raw), span);
153 !ident_token.is_reserved_ident() ||
154 ident_token.is_path_segment_keyword() ||
167 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
169 /* Expression-operator symbols. */
184 /* Structural symbols */
200 /// Used by proc macros for representing lifetimes, not generated by lexer right now.
202 /// An opening delimiter (e.g., `{`).
203 OpenDelim(DelimToken),
204 /// A closing delimiter (e.g., `}`).
205 CloseDelim(DelimToken),
210 /* Name components */
211 Ident(ast::Name, /* is_raw */ bool),
214 Interpolated(Lrc<Nonterminal>),
216 // Can be expanded into several tokens.
218 DocComment(ast::Name),
220 // Junk. These carry no data because we don't really care about the data
221 // they *would* carry, and don't really want to allocate a new ident for
222 // them. Instead, users could extract that from the associated span.
233 // `TokenKind` is used a lot. Make sure it doesn't unintentionally get bigger.
234 #[cfg(target_arch = "x86_64")]
235 static_assert_size!(TokenKind, 16);
237 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
244 /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
245 crate fn from_ast_ident(ident: ast::Ident) -> Token {
246 Token::new(Ident(ident.name, ident.is_raw_guess()), ident.span)
249 crate fn is_like_plus(&self) -> bool {
251 BinOp(Plus) | BinOpEq(Plus) => true,
256 /// Returns `true` if the token can appear at the start of an expression.
257 crate fn can_begin_expr(&self) -> bool {
259 Ident(name, is_raw) =>
260 ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
261 OpenDelim(..) | // tuple, array or block
262 Literal(..) | // literal
263 Not | // operator not
264 BinOp(Minus) | // unary minus
265 BinOp(Star) | // dereference
266 BinOp(Or) | OrOr | // closure
267 BinOp(And) | // reference
268 AndAnd | // double reference
269 // DotDotDot is no longer supported, but we need some way to display the error
270 DotDot | DotDotDot | DotDotEq | // range notation
271 Lt | BinOp(Shl) | // associated path
272 ModSep | // global path
273 Lifetime(..) | // labeled loop
274 Pound => true, // expression attributes
275 Interpolated(ref nt) => match **nt {
281 NtLifetime(..) => true,
288 /// Returns `true` if the token can appear at the start of a type.
289 crate fn can_begin_type(&self) -> bool {
291 Ident(name, is_raw) =>
292 ident_can_begin_type(name, self.span, is_raw), // type name or keyword
293 OpenDelim(Paren) | // tuple
294 OpenDelim(Bracket) | // array
296 BinOp(Star) | // raw pointer
297 BinOp(And) | // reference
298 AndAnd | // double reference
299 Question | // maybe bound in trait object
300 Lifetime(..) | // lifetime bound in trait object
301 Lt | BinOp(Shl) | // associated path
302 ModSep => true, // global path
303 Interpolated(ref nt) => match **nt {
304 NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
311 /// Returns `true` if the token can appear at the start of a const param.
312 crate fn can_begin_const_arg(&self) -> bool {
314 OpenDelim(Brace) => true,
315 Interpolated(ref nt) => match **nt {
318 NtLiteral(..) => true,
321 _ => self.can_begin_literal_or_bool(),
325 /// Returns `true` if the token can appear at the start of a generic bound.
326 crate fn can_begin_bound(&self) -> bool {
327 self.is_path_start() || self.is_lifetime() || self.is_keyword(kw::For) ||
328 self == &Question || self == &OpenDelim(Paren)
333 pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> TokenKind {
334 Literal(Lit::new(kind, symbol, suffix))
339 /// Returns `true` if the token is any literal
340 crate fn is_lit(&self) -> bool {
347 crate fn expect_lit(&self) -> Lit {
350 _=> panic!("`expect_lit` called on non-literal"),
354 /// Returns `true` if the token is any literal, a minus (which can prefix a literal,
355 /// for example a '-42', or one of the boolean idents).
356 crate fn can_begin_literal_or_bool(&self) -> bool {
359 BinOp(Minus) => true,
360 Ident(name, false) if name == kw::True => true,
361 Ident(name, false) if name == kw::False => true,
362 Interpolated(ref nt) => match **nt {
363 NtLiteral(..) => true,
370 /// Returns an identifier if this token is an identifier.
371 pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
373 Ident(name, is_raw) => Some((ast::Ident::new(name, self.span), is_raw)),
374 Interpolated(ref nt) => match **nt {
375 NtIdent(ident, is_raw) => Some((ident, is_raw)),
382 /// Returns a lifetime identifier if this token is a lifetime.
383 pub fn lifetime(&self) -> Option<ast::Ident> {
385 Lifetime(name) => Some(ast::Ident::new(name, self.span)),
386 Interpolated(ref nt) => match **nt {
387 NtLifetime(ident) => Some(ident),
394 /// Returns `true` if the token is an identifier.
395 pub fn is_ident(&self) -> bool {
396 self.ident().is_some()
398 /// Returns `true` if the token is a lifetime.
399 crate fn is_lifetime(&self) -> bool {
400 self.lifetime().is_some()
403 /// Returns `true` if the token is a identifier whose name is the given
405 crate fn is_ident_named(&self, name: Symbol) -> bool {
406 self.ident().map_or(false, |(ident, _)| ident.name == name)
409 /// Returns `true` if the token is an interpolated path.
410 fn is_path(&self) -> bool {
411 if let Interpolated(ref nt) = self.kind {
412 if let NtPath(..) = **nt {
419 /// Returns `true` if the token is either the `mut` or `const` keyword.
420 crate fn is_mutability(&self) -> bool {
421 self.is_keyword(kw::Mut) ||
422 self.is_keyword(kw::Const)
425 crate fn is_qpath_start(&self) -> bool {
426 self == &Lt || self == &BinOp(Shl)
429 crate fn is_path_start(&self) -> bool {
430 self == &ModSep || self.is_qpath_start() || self.is_path() ||
431 self.is_path_segment_keyword() || self.is_ident() && !self.is_reserved_ident()
434 /// Returns `true` if the token is a given keyword, `kw`.
435 pub fn is_keyword(&self, kw: Symbol) -> bool {
436 self.ident().map(|(id, is_raw)| id.name == kw && !is_raw).unwrap_or(false)
439 crate fn is_path_segment_keyword(&self) -> bool {
441 Some((id, false)) => id.is_path_segment_keyword(),
446 // Returns true for reserved identifiers used internally for elided lifetimes,
447 // unnamed method parameters, crate root module, error recovery etc.
448 crate fn is_special_ident(&self) -> bool {
450 Some((id, false)) => id.is_special(),
455 /// Returns `true` if the token is a keyword used in the language.
456 crate fn is_used_keyword(&self) -> bool {
458 Some((id, false)) => id.is_used_keyword(),
463 /// Returns `true` if the token is a keyword reserved for possible future use.
464 crate fn is_unused_keyword(&self) -> bool {
466 Some((id, false)) => id.is_unused_keyword(),
471 /// Returns `true` if the token is either a special identifier or a keyword.
472 pub fn is_reserved_ident(&self) -> bool {
474 Some((id, false)) => id.is_reserved(),
479 crate fn glue(self, joint: Token) -> Option<Token> {
480 let kind = match self.kind {
481 Eq => match joint.kind {
486 Lt => match joint.kind {
490 BinOp(Minus) => LArrow,
493 Gt => match joint.kind {
499 Not => match joint.kind {
503 BinOp(op) => match joint.kind {
505 BinOp(And) if op == And => AndAnd,
506 BinOp(Or) if op == Or => OrOr,
507 Gt if op == Minus => RArrow,
510 Dot => match joint.kind {
515 DotDot => match joint.kind {
520 Colon => match joint.kind {
524 SingleQuote => match joint.kind {
525 Ident(name, false) => Lifetime(Symbol::intern(&format!("'{}", name))),
529 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
530 DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
531 Question | OpenDelim(..) | CloseDelim(..) |
532 Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
533 Whitespace | Comment | Shebang(..) | Eof => return None,
536 Some(Token::new(kind, self.span.to(joint.span)))
541 /// Returns tokens that are likely to be typed accidentally instead of the current token.
542 /// Enables better error recovery when the wrong token is found.
543 crate fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
545 Comma => Some(vec![Dot, Lt, Semi]),
546 Semi => Some(vec![Colon, Comma]),
553 // See comments in `Nonterminal::to_tokenstream` for why we care about
554 // *probably* equal here rather than actual equality
555 crate fn probably_equal_for_proc_macro(&self, other: &Token) -> bool {
556 if mem::discriminant(&self.kind) != mem::discriminant(&other.kind) {
559 match (&self.kind, &other.kind) {
574 (&DotDotDot, &DotDotDot) |
575 (&DotDotEq, &DotDotEq) |
582 (&FatArrow, &FatArrow) |
585 (&Question, &Question) |
586 (&Whitespace, &Whitespace) |
587 (&Comment, &Comment) |
588 (&Eof, &Eof) => true,
590 (&BinOp(a), &BinOp(b)) |
591 (&BinOpEq(a), &BinOpEq(b)) => a == b,
593 (&OpenDelim(a), &OpenDelim(b)) |
594 (&CloseDelim(a), &CloseDelim(b)) => a == b,
596 (&DocComment(a), &DocComment(b)) |
597 (&Shebang(a), &Shebang(b)) => a == b,
599 (&Literal(a), &Literal(b)) => a == b,
601 (&Lifetime(a), &Lifetime(b)) => a == b,
602 (&Ident(a, b), &Ident(c, d)) => b == d && (a == c ||
603 a == kw::DollarCrate ||
604 c == kw::DollarCrate),
606 (&Interpolated(_), &Interpolated(_)) => false,
608 _ => panic!("forgot to add a token?"),
612 crate fn new(kind: TokenKind, span: Span) -> Self {
616 /// Some token that will be thrown away later.
617 crate fn dummy() -> Self {
618 Token::new(TokenKind::Whitespace, DUMMY_SP)
621 /// Return this token by value and leave a dummy token in its place.
622 crate fn take(&mut self) -> Self {
623 mem::replace(self, Token::dummy())
627 impl PartialEq<TokenKind> for Token {
628 fn eq(&self, rhs: &TokenKind) -> bool {
633 #[derive(Clone, RustcEncodable, RustcDecodable)]
634 /// For interpolation during macro expansion.
635 pub enum Nonterminal {
636 NtItem(P<ast::Item>),
637 NtBlock(P<ast::Block>),
640 NtExpr(P<ast::Expr>),
642 NtIdent(ast::Ident, /* is_raw */ bool),
643 NtLifetime(ast::Ident),
644 NtLiteral(P<ast::Expr>),
645 /// Stuff inside brackets for attributes
646 NtMeta(ast::MetaItem),
648 NtVis(ast::Visibility),
650 // Used only for passing items to proc macro attributes (they are not
651 // strictly necessary for that, `Annotatable` can be converted into
652 // tokens directly, but doing that naively regresses pretty-printing).
653 NtTraitItem(ast::TraitItem),
654 NtImplItem(ast::ImplItem),
655 NtForeignItem(ast::ForeignItem),
658 impl PartialEq for Nonterminal {
659 fn eq(&self, rhs: &Self) -> bool {
661 (NtIdent(ident_lhs, is_raw_lhs), NtIdent(ident_rhs, is_raw_rhs)) =>
662 ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs,
663 (NtLifetime(ident_lhs), NtLifetime(ident_rhs)) => ident_lhs == ident_rhs,
664 (NtTT(tt_lhs), NtTT(tt_rhs)) => tt_lhs == tt_rhs,
665 // FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
666 // correctly based on data from AST. This will prevent them from matching each other
667 // in macros. The comparison will become possible only when each nonterminal has an
668 // attached token stream from which it was parsed.
674 impl fmt::Debug for Nonterminal {
675 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
677 NtItem(..) => f.pad("NtItem(..)"),
678 NtBlock(..) => f.pad("NtBlock(..)"),
679 NtStmt(..) => f.pad("NtStmt(..)"),
680 NtPat(..) => f.pad("NtPat(..)"),
681 NtExpr(..) => f.pad("NtExpr(..)"),
682 NtTy(..) => f.pad("NtTy(..)"),
683 NtIdent(..) => f.pad("NtIdent(..)"),
684 NtLiteral(..) => f.pad("NtLiteral(..)"),
685 NtMeta(..) => f.pad("NtMeta(..)"),
686 NtPath(..) => f.pad("NtPath(..)"),
687 NtTT(..) => f.pad("NtTT(..)"),
688 NtImplItem(..) => f.pad("NtImplItem(..)"),
689 NtTraitItem(..) => f.pad("NtTraitItem(..)"),
690 NtForeignItem(..) => f.pad("NtForeignItem(..)"),
691 NtVis(..) => f.pad("NtVis(..)"),
692 NtLifetime(..) => f.pad("NtLifetime(..)"),
698 pub fn to_tokenstream(&self, sess: &ParseSess, span: Span) -> TokenStream {
699 // A `Nonterminal` is often a parsed AST item. At this point we now
700 // need to convert the parsed AST to an actual token stream, e.g.
701 // un-parse it basically.
703 // Unfortunately there's not really a great way to do that in a
704 // guaranteed lossless fashion right now. The fallback here is to just
705 // stringify the AST node and reparse it, but this loses all span
708 // As a result, some AST nodes are annotated with the token stream they
709 // came from. Here we attempt to extract these lossless token streams
710 // before we fall back to the stringification.
711 let tokens = match *self {
712 Nonterminal::NtItem(ref item) => {
713 prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
715 Nonterminal::NtTraitItem(ref item) => {
716 prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
718 Nonterminal::NtImplItem(ref item) => {
719 prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
721 Nonterminal::NtIdent(ident, is_raw) => {
722 Some(TokenTree::token(Ident(ident.name, is_raw), ident.span).into())
724 Nonterminal::NtLifetime(ident) => {
725 Some(TokenTree::token(Lifetime(ident.name), ident.span).into())
727 Nonterminal::NtTT(ref tt) => {
728 Some(tt.clone().into())
733 // FIXME(#43081): Avoid this pretty-print + reparse hack
734 let source = pprust::nonterminal_to_string(self);
735 let filename = FileName::macro_expansion_source_code(&source);
736 let tokens_for_real = parse_stream_from_source_str(filename, source, sess, Some(span));
738 // During early phases of the compiler the AST could get modified
739 // directly (e.g., attributes added or removed) and the internal cache
740 // of tokens my not be invalidated or updated. Consequently if the
741 // "lossless" token stream disagrees with our actual stringification
742 // (which has historically been much more battle-tested) then we go
743 // with the lossy stream anyway (losing span information).
745 // Note that the comparison isn't `==` here to avoid comparing spans,
746 // but it *also* is a "probable" equality which is a pretty weird
747 // definition. We mostly want to catch actual changes to the AST
748 // like a `#[cfg]` being processed or some weird `macro_rules!`
751 // What we *don't* want to catch is the fact that a user-defined
752 // literal like `0xf` is stringified as `15`, causing the cached token
753 // stream to not be literal `==` token-wise (ignoring spans) to the
754 // token stream we got from stringification.
756 // Instead the "probably equal" check here is "does each token
757 // recursively have the same discriminant?" We basically don't look at
758 // the token values here and assume that such fine grained token stream
759 // modifications, including adding/removing typically non-semantic
760 // tokens such as extra braces and commas, don't happen.
761 if let Some(tokens) = tokens {
762 if tokens.probably_equal_for_proc_macro(&tokens_for_real) {
765 info!("cached tokens found, but they're not \"probably equal\", \
766 going with stringified version");
768 return tokens_for_real
773 crate fn is_op(&self) -> bool {
775 OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
776 Ident(..) | Lifetime(..) | Interpolated(..) |
777 Whitespace | Comment | Shebang(..) | Eof => false,
783 fn prepend_attrs(sess: &ParseSess,
784 attrs: &[ast::Attribute],
785 tokens: Option<&tokenstream::TokenStream>,
786 span: syntax_pos::Span)
787 -> Option<tokenstream::TokenStream>
789 let tokens = tokens?;
790 if attrs.len() == 0 {
791 return Some(tokens.clone())
793 let mut builder = tokenstream::TokenStreamBuilder::new();
795 assert_eq!(attr.style, ast::AttrStyle::Outer,
796 "inner attributes should prevent cached tokens from existing");
798 let source = pprust::attr_to_string(attr);
799 let macro_filename = FileName::macro_expansion_source_code(&source);
800 if attr.is_sugared_doc {
801 let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span));
802 builder.push(stream);
806 // synthesize # [ $path $tokens ] manually here
807 let mut brackets = tokenstream::TokenStreamBuilder::new();
809 // For simple paths, push the identifier directly
810 if attr.path.segments.len() == 1 && attr.path.segments[0].args.is_none() {
811 let ident = attr.path.segments[0].ident;
812 let token = Ident(ident.name, ident.as_str().starts_with("r#"));
813 brackets.push(tokenstream::TokenTree::token(token, ident.span));
815 // ... and for more complicated paths, fall back to a reparse hack that
816 // should eventually be removed.
818 let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span));
819 brackets.push(stream);
822 brackets.push(attr.tokens.clone());
824 // The span we list here for `#` and for `[ ... ]` are both wrong in
825 // that it encompasses more than each token, but it hopefully is "good
826 // enough" for now at least.
827 builder.push(tokenstream::TokenTree::token(Pound, attr.span));
828 let delim_span = DelimSpan::from_single(attr.span);
829 builder.push(tokenstream::TokenTree::Delimited(
830 delim_span, DelimToken::Bracket, brackets.build().into()));
832 builder.push(tokens.clone());
833 Some(builder.build())