2 pub use Nonterminal::*;
10 use crate::tokenstream::TokenTree;
12 use syntax_pos::symbol::Symbol;
13 use syntax_pos::{self, Span, DUMMY_SP};
17 #[cfg(target_arch = "x86_64")]
18 use rustc_data_structures::static_assert_size;
19 use rustc_data_structures::sync::Lrc;
21 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
35 /// A delimiter token.
36 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
38 /// A round parenthesis (i.e., `(` or `)`).
40 /// A square bracket (i.e., `[` or `]`).
42 /// A curly brace (i.e., `{` or `}`).
44 /// An empty delimiter.
49 pub fn len(self) -> usize {
50 if self == NoDelim { 0 } else { 1 }
53 pub fn is_empty(self) -> bool {
58 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
60 Bool, // AST only, must never appear in a `Token`
66 StrRaw(u16), // raw string delimited by `n` hash symbols
68 ByteStrRaw(u16), // raw byte string delimited by `n` hash symbols
73 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
77 pub suffix: Option<Symbol>,
80 impl fmt::Display for Lit {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 let Lit { kind, symbol, suffix } = *self;
84 Byte => write!(f, "b'{}'", symbol)?,
85 Char => write!(f, "'{}'", symbol)?,
86 Str => write!(f, "\"{}\"", symbol)?,
87 StrRaw(n) => write!(f, "r{delim}\"{string}\"{delim}",
88 delim="#".repeat(n as usize),
90 ByteStr => write!(f, "b\"{}\"", symbol)?,
91 ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}",
92 delim="#".repeat(n as usize),
97 Err => write!(f, "{}", symbol)?,
100 if let Some(suffix) = suffix {
101 write!(f, "{}", suffix)?;
109 /// An English article for the literal token kind.
110 crate fn article(self) -> &'static str {
112 Integer | Err => "an",
117 crate fn descr(self) -> &'static str {
119 Bool => panic!("literal token contains `Lit::Bool`"),
122 Integer => "integer",
124 Str | StrRaw(..) => "string",
125 ByteStr | ByteStrRaw(..) => "byte string",
130 crate fn may_have_suffix(self) -> bool {
132 Integer | Float | Err => true,
139 pub fn new(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Lit {
140 Lit { kind, symbol, suffix }
144 pub(crate) fn ident_can_begin_expr(name: ast::Name, span: Span, is_raw: bool) -> bool {
145 let ident_token = Token::new(Ident(name, is_raw), span);
146 token_can_begin_expr(&ident_token)
149 pub(crate) fn token_can_begin_expr(ident_token: &Token) -> bool {
150 !ident_token.is_reserved_ident() ||
151 ident_token.is_path_segment_keyword() ||
152 match ident_token.kind {
153 TokenKind::Ident(ident, _) => [
177 fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool {
178 let ident_token = Token::new(Ident(name, is_raw), span);
180 !ident_token.is_reserved_ident() ||
181 ident_token.is_path_segment_keyword() ||
194 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
196 /* Expression-operator symbols. */
211 /* Structural symbols */
227 /// Used by proc macros for representing lifetimes, not generated by lexer right now.
229 /// An opening delimiter (e.g., `{`).
230 OpenDelim(DelimToken),
231 /// A closing delimiter (e.g., `}`).
232 CloseDelim(DelimToken),
237 /* Name components */
238 Ident(ast::Name, /* is_raw */ bool),
241 Interpolated(Lrc<Nonterminal>),
243 // Can be expanded into several tokens.
245 DocComment(ast::Name),
247 // Junk. These carry no data because we don't really care about the data
248 // they *would* carry, and don't really want to allocate a new ident for
249 // them. Instead, users could extract that from the associated span.
256 /// A completely invalid token which should be skipped.
262 // `TokenKind` is used a lot. Make sure it doesn't unintentionally get bigger.
263 #[cfg(target_arch = "x86_64")]
264 static_assert_size!(TokenKind, 16);
266 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
273 pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> TokenKind {
274 Literal(Lit::new(kind, symbol, suffix))
277 /// Returns tokens that are likely to be typed accidentally instead of the current token.
278 /// Enables better error recovery when the wrong token is found.
279 crate fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
281 Comma => Some(vec![Dot, Lt, Semi]),
282 Semi => Some(vec![Colon, Comma]),
289 pub fn new(kind: TokenKind, span: Span) -> Self {
293 /// Some token that will be thrown away later.
294 crate fn dummy() -> Self {
295 Token::new(TokenKind::Whitespace, DUMMY_SP)
298 /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
299 pub fn from_ast_ident(ident: ast::Ident) -> Self {
300 Token::new(Ident(ident.name, ident.is_raw_guess()), ident.span)
303 /// Return this token by value and leave a dummy token in its place.
304 pub fn take(&mut self) -> Self {
305 mem::replace(self, Token::dummy())
308 crate fn is_op(&self) -> bool {
310 OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
311 Ident(..) | Lifetime(..) | Interpolated(..) |
312 Whitespace | Comment | Shebang(..) | Eof => false,
317 crate fn is_like_plus(&self) -> bool {
319 BinOp(Plus) | BinOpEq(Plus) => true,
324 /// Returns `true` if the token can appear at the start of an expression.
325 pub fn can_begin_expr(&self) -> bool {
327 Ident(name, is_raw) =>
328 ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
329 OpenDelim(..) | // tuple, array or block
330 Literal(..) | // literal
331 Not | // operator not
332 BinOp(Minus) | // unary minus
333 BinOp(Star) | // dereference
334 BinOp(Or) | OrOr | // closure
335 BinOp(And) | // reference
336 AndAnd | // double reference
337 // DotDotDot is no longer supported, but we need some way to display the error
338 DotDot | DotDotDot | DotDotEq | // range notation
339 Lt | BinOp(Shl) | // associated path
340 ModSep | // global path
341 Lifetime(..) | // labeled loop
342 Pound => true, // expression attributes
343 Interpolated(ref nt) => match **nt {
349 NtLifetime(..) => true,
356 /// Returns `true` if the token can appear at the start of a type.
357 pub fn can_begin_type(&self) -> bool {
359 Ident(name, is_raw) =>
360 ident_can_begin_type(name, self.span, is_raw), // type name or keyword
361 OpenDelim(Paren) | // tuple
362 OpenDelim(Bracket) | // array
364 BinOp(Star) | // raw pointer
365 BinOp(And) | // reference
366 AndAnd | // double reference
367 Question | // maybe bound in trait object
368 Lifetime(..) | // lifetime bound in trait object
369 Lt | BinOp(Shl) | // associated path
370 ModSep => true, // global path
371 Interpolated(ref nt) => match **nt {
372 NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
379 /// Returns `true` if the token can appear at the start of a const param.
380 crate fn can_begin_const_arg(&self) -> bool {
382 OpenDelim(Brace) => true,
383 Interpolated(ref nt) => match **nt {
386 NtLiteral(..) => true,
389 _ => self.can_begin_literal_or_bool(),
393 /// Returns `true` if the token can appear at the start of a generic bound.
394 crate fn can_begin_bound(&self) -> bool {
395 self.is_path_start() || self.is_lifetime() || self.is_keyword(kw::For) ||
396 self == &Question || self == &OpenDelim(Paren)
399 /// Returns `true` if the token is any literal
400 pub fn is_lit(&self) -> bool {
407 crate fn expect_lit(&self) -> Lit {
410 _ => panic!("`expect_lit` called on non-literal"),
414 /// Returns `true` if the token is any literal, a minus (which can prefix a literal,
415 /// for example a '-42', or one of the boolean idents).
416 pub fn can_begin_literal_or_bool(&self) -> bool {
418 Literal(..) | BinOp(Minus) => true,
419 Ident(name, false) if name.is_bool_lit() => true,
420 Interpolated(ref nt) => match **nt {
421 NtLiteral(..) => true,
428 /// Returns an identifier if this token is an identifier.
429 pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
431 Ident(name, is_raw) => Some((ast::Ident::new(name, self.span), is_raw)),
432 Interpolated(ref nt) => match **nt {
433 NtIdent(ident, is_raw) => Some((ident, is_raw)),
440 /// Returns a lifetime identifier if this token is a lifetime.
441 pub fn lifetime(&self) -> Option<ast::Ident> {
443 Lifetime(name) => Some(ast::Ident::new(name, self.span)),
444 Interpolated(ref nt) => match **nt {
445 NtLifetime(ident) => Some(ident),
452 /// Returns `true` if the token is an identifier.
453 pub fn is_ident(&self) -> bool {
454 self.ident().is_some()
457 /// Returns `true` if the token is a lifetime.
458 crate fn is_lifetime(&self) -> bool {
459 self.lifetime().is_some()
462 /// Returns `true` if the token is a identifier whose name is the given
464 crate fn is_ident_named(&self, name: Symbol) -> bool {
465 self.ident().map_or(false, |(ident, _)| ident.name == name)
468 /// Returns `true` if the token is an interpolated path.
469 fn is_path(&self) -> bool {
470 if let Interpolated(ref nt) = self.kind {
471 if let NtPath(..) = **nt {
478 /// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
479 /// That is, is this a pre-parsed expression dropped into the token stream
480 /// (which happens while parsing the result of macro expansion)?
481 crate fn is_whole_expr(&self) -> bool {
482 if let Interpolated(ref nt) = self.kind {
483 if let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtIdent(..) | NtBlock(_) = **nt {
491 /// Returns `true` if the token is either the `mut` or `const` keyword.
492 crate fn is_mutability(&self) -> bool {
493 self.is_keyword(kw::Mut) ||
494 self.is_keyword(kw::Const)
497 crate fn is_qpath_start(&self) -> bool {
498 self == &Lt || self == &BinOp(Shl)
501 crate fn is_path_start(&self) -> bool {
502 self == &ModSep || self.is_qpath_start() || self.is_path() ||
503 self.is_path_segment_keyword() || self.is_ident() && !self.is_reserved_ident()
506 /// Returns `true` if the token is a given keyword, `kw`.
507 pub fn is_keyword(&self, kw: Symbol) -> bool {
508 self.is_non_raw_ident_where(|id| id.name == kw)
511 crate fn is_path_segment_keyword(&self) -> bool {
512 self.is_non_raw_ident_where(ast::Ident::is_path_segment_keyword)
515 // Returns true for reserved identifiers used internally for elided lifetimes,
516 // unnamed method parameters, crate root module, error recovery etc.
517 crate fn is_special_ident(&self) -> bool {
518 self.is_non_raw_ident_where(ast::Ident::is_special)
521 /// Returns `true` if the token is a keyword used in the language.
522 crate fn is_used_keyword(&self) -> bool {
523 self.is_non_raw_ident_where(ast::Ident::is_used_keyword)
526 /// Returns `true` if the token is a keyword reserved for possible future use.
527 crate fn is_unused_keyword(&self) -> bool {
528 self.is_non_raw_ident_where(ast::Ident::is_unused_keyword)
531 /// Returns `true` if the token is either a special identifier or a keyword.
532 pub fn is_reserved_ident(&self) -> bool {
533 self.is_non_raw_ident_where(ast::Ident::is_reserved)
536 /// Returns `true` if the token is the identifier `true` or `false`.
537 crate fn is_bool_lit(&self) -> bool {
538 self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
541 /// Returns `true` if the token is a non-raw identifier for which `pred` holds.
542 fn is_non_raw_ident_where(&self, pred: impl FnOnce(ast::Ident) -> bool) -> bool {
544 Some((id, false)) => pred(id),
549 crate fn glue(&self, joint: &Token) -> Option<Token> {
550 let kind = match self.kind {
551 Eq => match joint.kind {
556 Lt => match joint.kind {
560 BinOp(Minus) => LArrow,
563 Gt => match joint.kind {
569 Not => match joint.kind {
573 BinOp(op) => match joint.kind {
575 BinOp(And) if op == And => AndAnd,
576 BinOp(Or) if op == Or => OrOr,
577 Gt if op == Minus => RArrow,
580 Dot => match joint.kind {
585 DotDot => match joint.kind {
590 Colon => match joint.kind {
594 SingleQuote => match joint.kind {
595 Ident(name, false) => Lifetime(Symbol::intern(&format!("'{}", name))),
599 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
600 DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
601 Question | OpenDelim(..) | CloseDelim(..) |
602 Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
603 Whitespace | Comment | Shebang(..) | Unknown(..) | Eof => return None,
606 Some(Token::new(kind, self.span.to(joint.span)))
609 // See comments in `Nonterminal::to_tokenstream` for why we care about
610 // *probably* equal here rather than actual equality
611 crate fn probably_equal_for_proc_macro(&self, other: &Token) -> bool {
612 if mem::discriminant(&self.kind) != mem::discriminant(&other.kind) {
615 match (&self.kind, &other.kind) {
630 (&DotDotDot, &DotDotDot) |
631 (&DotDotEq, &DotDotEq) |
638 (&FatArrow, &FatArrow) |
641 (&Question, &Question) |
642 (&Whitespace, &Whitespace) |
643 (&Comment, &Comment) |
644 (&Eof, &Eof) => true,
646 (&BinOp(a), &BinOp(b)) |
647 (&BinOpEq(a), &BinOpEq(b)) => a == b,
649 (&OpenDelim(a), &OpenDelim(b)) |
650 (&CloseDelim(a), &CloseDelim(b)) => a == b,
652 (&DocComment(a), &DocComment(b)) |
653 (&Shebang(a), &Shebang(b)) => a == b,
655 (&Literal(a), &Literal(b)) => a == b,
657 (&Lifetime(a), &Lifetime(b)) => a == b,
658 (&Ident(a, b), &Ident(c, d)) => b == d && (a == c ||
659 a == kw::DollarCrate ||
660 c == kw::DollarCrate),
662 (&Interpolated(_), &Interpolated(_)) => false,
664 _ => panic!("forgot to add a token?"),
669 impl PartialEq<TokenKind> for Token {
670 fn eq(&self, rhs: &TokenKind) -> bool {
675 #[derive(Clone, RustcEncodable, RustcDecodable)]
676 /// For interpolation during macro expansion.
677 pub enum Nonterminal {
678 NtItem(P<ast::Item>),
679 NtBlock(P<ast::Block>),
682 NtExpr(P<ast::Expr>),
684 NtIdent(ast::Ident, /* is_raw */ bool),
685 NtLifetime(ast::Ident),
686 NtLiteral(P<ast::Expr>),
687 /// Stuff inside brackets for attributes
688 NtMeta(ast::AttrItem),
690 NtVis(ast::Visibility),
692 // Used only for passing items to proc macro attributes (they are not
693 // strictly necessary for that, `Annotatable` can be converted into
694 // tokens directly, but doing that naively regresses pretty-printing).
695 NtTraitItem(ast::TraitItem),
696 NtImplItem(ast::ImplItem),
697 NtForeignItem(ast::ForeignItem),
700 impl PartialEq for Nonterminal {
701 fn eq(&self, rhs: &Self) -> bool {
703 (NtIdent(ident_lhs, is_raw_lhs), NtIdent(ident_rhs, is_raw_rhs)) =>
704 ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs,
705 (NtLifetime(ident_lhs), NtLifetime(ident_rhs)) => ident_lhs == ident_rhs,
706 (NtTT(tt_lhs), NtTT(tt_rhs)) => tt_lhs == tt_rhs,
707 // FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
708 // correctly based on data from AST. This will prevent them from matching each other
709 // in macros. The comparison will become possible only when each nonterminal has an
710 // attached token stream from which it was parsed.
716 impl fmt::Debug for Nonterminal {
717 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
719 NtItem(..) => f.pad("NtItem(..)"),
720 NtBlock(..) => f.pad("NtBlock(..)"),
721 NtStmt(..) => f.pad("NtStmt(..)"),
722 NtPat(..) => f.pad("NtPat(..)"),
723 NtExpr(..) => f.pad("NtExpr(..)"),
724 NtTy(..) => f.pad("NtTy(..)"),
725 NtIdent(..) => f.pad("NtIdent(..)"),
726 NtLiteral(..) => f.pad("NtLiteral(..)"),
727 NtMeta(..) => f.pad("NtMeta(..)"),
728 NtPath(..) => f.pad("NtPath(..)"),
729 NtTT(..) => f.pad("NtTT(..)"),
730 NtImplItem(..) => f.pad("NtImplItem(..)"),
731 NtTraitItem(..) => f.pad("NtTraitItem(..)"),
732 NtForeignItem(..) => f.pad("NtForeignItem(..)"),
733 NtVis(..) => f.pad("NtVis(..)"),
734 NtLifetime(..) => f.pad("NtLifetime(..)"),