2 pub use Nonterminal::*;
10 use crate::tokenstream::TokenTree;
12 use syntax_pos::symbol::Symbol;
13 use syntax_pos::{self, Span, DUMMY_SP};
17 use rustc_data_structures::sync::Lrc;
19 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
33 /// A delimiter token.
34 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
36 /// A round parenthesis (i.e., `(` or `)`).
38 /// A square bracket (i.e., `[` or `]`).
40 /// A curly brace (i.e., `{` or `}`).
42 /// An empty delimiter.
47 pub fn len(self) -> usize {
48 if self == NoDelim { 0 } else { 1 }
51 pub fn is_empty(self) -> bool {
56 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
58 Bool, // AST only, must never appear in a `Token`
64 StrRaw(u16), // raw string delimited by `n` hash symbols
66 ByteStrRaw(u16), // raw byte string delimited by `n` hash symbols
71 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
75 pub suffix: Option<Symbol>,
78 impl fmt::Display for Lit {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 let Lit { kind, symbol, suffix } = *self;
82 Byte => write!(f, "b'{}'", symbol)?,
83 Char => write!(f, "'{}'", symbol)?,
84 Str => write!(f, "\"{}\"", symbol)?,
85 StrRaw(n) => write!(f, "r{delim}\"{string}\"{delim}",
86 delim="#".repeat(n as usize),
88 ByteStr => write!(f, "b\"{}\"", symbol)?,
89 ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}",
90 delim="#".repeat(n as usize),
95 Err => write!(f, "{}", symbol)?,
98 if let Some(suffix) = suffix {
99 write!(f, "{}", suffix)?;
107 /// An English article for the literal token kind.
108 pub fn article(self) -> &'static str {
110 Integer | Err => "an",
115 pub fn descr(self) -> &'static str {
117 Bool => panic!("literal token contains `Lit::Bool`"),
120 Integer => "integer",
122 Str | StrRaw(..) => "string",
123 ByteStr | ByteStrRaw(..) => "byte string",
128 crate fn may_have_suffix(self) -> bool {
130 Integer | Float | Err => true,
137 pub fn new(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Lit {
138 Lit { kind, symbol, suffix }
142 pub fn ident_can_begin_expr(name: ast::Name, span: Span, is_raw: bool) -> bool {
143 let ident_token = Token::new(Ident(name, is_raw), span);
144 token_can_begin_expr(&ident_token)
147 pub fn token_can_begin_expr(ident_token: &Token) -> bool {
148 !ident_token.is_reserved_ident() ||
149 ident_token.is_path_segment_keyword() ||
150 match ident_token.kind {
151 TokenKind::Ident(ident, _) => [
175 fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool {
176 let ident_token = Token::new(Ident(name, is_raw), span);
178 !ident_token.is_reserved_ident() ||
179 ident_token.is_path_segment_keyword() ||
192 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
194 /* Expression-operator symbols. */
209 /* Structural symbols */
225 /// Used by proc macros for representing lifetimes, not generated by lexer right now.
227 /// An opening delimiter (e.g., `{`).
228 OpenDelim(DelimToken),
229 /// A closing delimiter (e.g., `}`).
230 CloseDelim(DelimToken),
235 /* Name components */
236 Ident(ast::Name, /* is_raw */ bool),
239 Interpolated(Lrc<Nonterminal>),
241 // Can be expanded into several tokens.
243 DocComment(ast::Name),
245 // Junk. These carry no data because we don't really care about the data
246 // they *would* carry, and don't really want to allocate a new ident for
247 // them. Instead, users could extract that from the associated span.
254 /// A completely invalid token which should be skipped.
260 // `TokenKind` is used a lot. Make sure it doesn't unintentionally get bigger.
261 #[cfg(target_arch = "x86_64")]
262 rustc_data_structures::static_assert_size!(TokenKind, 16);
264 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
271 pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> TokenKind {
272 Literal(Lit::new(kind, symbol, suffix))
275 /// Returns tokens that are likely to be typed accidentally instead of the current token.
276 /// Enables better error recovery when the wrong token is found.
277 pub fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
279 Comma => Some(vec![Dot, Lt, Semi]),
280 Semi => Some(vec![Colon, Comma]),
287 pub fn new(kind: TokenKind, span: Span) -> Self {
291 /// Some token that will be thrown away later.
292 pub fn dummy() -> Self {
293 Token::new(TokenKind::Whitespace, DUMMY_SP)
296 /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
297 pub fn from_ast_ident(ident: ast::Ident) -> Self {
298 Token::new(Ident(ident.name, ident.is_raw_guess()), ident.span)
301 /// Return this token by value and leave a dummy token in its place.
302 pub fn take(&mut self) -> Self {
303 mem::replace(self, Token::dummy())
306 pub fn is_op(&self) -> bool {
308 OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
309 Ident(..) | Lifetime(..) | Interpolated(..) |
310 Whitespace | Comment | Shebang(..) | Eof => false,
315 pub fn is_like_plus(&self) -> bool {
317 BinOp(Plus) | BinOpEq(Plus) => true,
322 /// Returns `true` if the token can appear at the start of an expression.
323 pub fn can_begin_expr(&self) -> bool {
325 Ident(name, is_raw) =>
326 ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
327 OpenDelim(..) | // tuple, array or block
328 Literal(..) | // literal
329 Not | // operator not
330 BinOp(Minus) | // unary minus
331 BinOp(Star) | // dereference
332 BinOp(Or) | OrOr | // closure
333 BinOp(And) | // reference
334 AndAnd | // double reference
335 // DotDotDot is no longer supported, but we need some way to display the error
336 DotDot | DotDotDot | DotDotEq | // range notation
337 Lt | BinOp(Shl) | // associated path
338 ModSep | // global path
339 Lifetime(..) | // labeled loop
340 Pound => true, // expression attributes
341 Interpolated(ref nt) => match **nt {
347 NtLifetime(..) => true,
354 /// Returns `true` if the token can appear at the start of a type.
355 pub fn can_begin_type(&self) -> bool {
357 Ident(name, is_raw) =>
358 ident_can_begin_type(name, self.span, is_raw), // type name or keyword
359 OpenDelim(Paren) | // tuple
360 OpenDelim(Bracket) | // array
362 BinOp(Star) | // raw pointer
363 BinOp(And) | // reference
364 AndAnd | // double reference
365 Question | // maybe bound in trait object
366 Lifetime(..) | // lifetime bound in trait object
367 Lt | BinOp(Shl) | // associated path
368 ModSep => true, // global path
369 Interpolated(ref nt) => match **nt {
370 NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
377 /// Returns `true` if the token can appear at the start of a const param.
378 pub fn can_begin_const_arg(&self) -> bool {
380 OpenDelim(Brace) => true,
381 Interpolated(ref nt) => match **nt {
382 NtExpr(..) | NtBlock(..) | NtLiteral(..) => true,
385 _ => self.can_begin_literal_or_bool(),
389 /// Returns `true` if the token can appear at the start of a generic bound.
390 pub fn can_begin_bound(&self) -> bool {
391 self.is_path_start() || self.is_lifetime() || self.is_keyword(kw::For) ||
392 self == &Question || self == &OpenDelim(Paren)
395 /// Returns `true` if the token is any literal
396 pub fn is_lit(&self) -> bool {
403 /// Returns `true` if the token is any literal, a minus (which can prefix a literal,
404 /// for example a '-42', or one of the boolean idents).
405 pub fn can_begin_literal_or_bool(&self) -> bool {
407 Literal(..) | BinOp(Minus) => true,
408 Ident(name, false) if name.is_bool_lit() => true,
409 Interpolated(ref nt) => match **nt {
410 NtLiteral(..) => true,
417 /// Returns an identifier if this token is an identifier.
418 pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
420 Ident(name, is_raw) => Some((ast::Ident::new(name, self.span), is_raw)),
421 Interpolated(ref nt) => match **nt {
422 NtIdent(ident, is_raw) => Some((ident, is_raw)),
429 /// Returns a lifetime identifier if this token is a lifetime.
430 pub fn lifetime(&self) -> Option<ast::Ident> {
432 Lifetime(name) => Some(ast::Ident::new(name, self.span)),
433 Interpolated(ref nt) => match **nt {
434 NtLifetime(ident) => Some(ident),
441 /// Returns `true` if the token is an identifier.
442 pub fn is_ident(&self) -> bool {
443 self.ident().is_some()
446 /// Returns `true` if the token is a lifetime.
447 pub fn is_lifetime(&self) -> bool {
448 self.lifetime().is_some()
451 /// Returns `true` if the token is a identifier whose name is the given
453 pub fn is_ident_named(&self, name: Symbol) -> bool {
454 self.ident().map_or(false, |(ident, _)| ident.name == name)
457 /// Returns `true` if the token is an interpolated path.
458 fn is_path(&self) -> bool {
459 if let Interpolated(ref nt) = self.kind {
460 if let NtPath(..) = **nt {
467 /// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
468 /// That is, is this a pre-parsed expression dropped into the token stream
469 /// (which happens while parsing the result of macro expansion)?
470 pub fn is_whole_expr(&self) -> bool {
471 if let Interpolated(ref nt) = self.kind {
472 if let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtIdent(..) | NtBlock(_) = **nt {
480 /// Returns `true` if the token is either the `mut` or `const` keyword.
481 pub fn is_mutability(&self) -> bool {
482 self.is_keyword(kw::Mut) ||
483 self.is_keyword(kw::Const)
486 pub fn is_qpath_start(&self) -> bool {
487 self == &Lt || self == &BinOp(Shl)
490 pub fn is_path_start(&self) -> bool {
491 self == &ModSep || self.is_qpath_start() || self.is_path() ||
492 self.is_path_segment_keyword() || self.is_ident() && !self.is_reserved_ident()
495 /// Returns `true` if the token is a given keyword, `kw`.
496 pub fn is_keyword(&self, kw: Symbol) -> bool {
497 self.is_non_raw_ident_where(|id| id.name == kw)
500 pub fn is_path_segment_keyword(&self) -> bool {
501 self.is_non_raw_ident_where(ast::Ident::is_path_segment_keyword)
504 // Returns true for reserved identifiers used internally for elided lifetimes,
505 // unnamed method parameters, crate root module, error recovery etc.
506 pub fn is_special_ident(&self) -> bool {
507 self.is_non_raw_ident_where(ast::Ident::is_special)
510 /// Returns `true` if the token is a keyword used in the language.
511 pub fn is_used_keyword(&self) -> bool {
512 self.is_non_raw_ident_where(ast::Ident::is_used_keyword)
515 /// Returns `true` if the token is a keyword reserved for possible future use.
516 pub fn is_unused_keyword(&self) -> bool {
517 self.is_non_raw_ident_where(ast::Ident::is_unused_keyword)
520 /// Returns `true` if the token is either a special identifier or a keyword.
521 pub fn is_reserved_ident(&self) -> bool {
522 self.is_non_raw_ident_where(ast::Ident::is_reserved)
525 /// Returns `true` if the token is the identifier `true` or `false`.
526 pub fn is_bool_lit(&self) -> bool {
527 self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
530 /// Returns `true` if the token is a non-raw identifier for which `pred` holds.
531 fn is_non_raw_ident_where(&self, pred: impl FnOnce(ast::Ident) -> bool) -> bool {
533 Some((id, false)) => pred(id),
538 pub fn glue(&self, joint: &Token) -> Option<Token> {
539 let kind = match self.kind {
540 Eq => match joint.kind {
545 Lt => match joint.kind {
549 BinOp(Minus) => LArrow,
552 Gt => match joint.kind {
558 Not => match joint.kind {
562 BinOp(op) => match joint.kind {
564 BinOp(And) if op == And => AndAnd,
565 BinOp(Or) if op == Or => OrOr,
566 Gt if op == Minus => RArrow,
569 Dot => match joint.kind {
574 DotDot => match joint.kind {
579 Colon => match joint.kind {
583 SingleQuote => match joint.kind {
584 Ident(name, false) => Lifetime(Symbol::intern(&format!("'{}", name))),
588 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
589 DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
590 Question | OpenDelim(..) | CloseDelim(..) |
591 Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
592 Whitespace | Comment | Shebang(..) | Unknown(..) | Eof => return None,
595 Some(Token::new(kind, self.span.to(joint.span)))
598 // See comments in `Nonterminal::to_tokenstream` for why we care about
599 // *probably* equal here rather than actual equality
600 crate fn probably_equal_for_proc_macro(&self, other: &Token) -> bool {
601 if mem::discriminant(&self.kind) != mem::discriminant(&other.kind) {
604 match (&self.kind, &other.kind) {
619 (&DotDotDot, &DotDotDot) |
620 (&DotDotEq, &DotDotEq) |
627 (&FatArrow, &FatArrow) |
630 (&Question, &Question) |
631 (&Whitespace, &Whitespace) |
632 (&Comment, &Comment) |
633 (&Eof, &Eof) => true,
635 (&BinOp(a), &BinOp(b)) |
636 (&BinOpEq(a), &BinOpEq(b)) => a == b,
638 (&OpenDelim(a), &OpenDelim(b)) |
639 (&CloseDelim(a), &CloseDelim(b)) => a == b,
641 (&DocComment(a), &DocComment(b)) |
642 (&Shebang(a), &Shebang(b)) => a == b,
644 (&Literal(a), &Literal(b)) => a == b,
646 (&Lifetime(a), &Lifetime(b)) => a == b,
647 (&Ident(a, b), &Ident(c, d)) => b == d && (a == c ||
648 a == kw::DollarCrate ||
649 c == kw::DollarCrate),
651 (&Interpolated(_), &Interpolated(_)) => false,
653 _ => panic!("forgot to add a token?"),
658 impl PartialEq<TokenKind> for Token {
659 fn eq(&self, rhs: &TokenKind) -> bool {
664 #[derive(Clone, RustcEncodable, RustcDecodable)]
665 /// For interpolation during macro expansion.
666 pub enum Nonterminal {
667 NtItem(P<ast::Item>),
668 NtBlock(P<ast::Block>),
671 NtExpr(P<ast::Expr>),
673 NtIdent(ast::Ident, /* is_raw */ bool),
674 NtLifetime(ast::Ident),
675 NtLiteral(P<ast::Expr>),
676 /// Stuff inside brackets for attributes
677 NtMeta(ast::AttrItem),
679 NtVis(ast::Visibility),
681 // Used only for passing items to proc macro attributes (they are not
682 // strictly necessary for that, `Annotatable` can be converted into
683 // tokens directly, but doing that naively regresses pretty-printing).
684 NtTraitItem(ast::TraitItem),
685 NtImplItem(ast::ImplItem),
686 NtForeignItem(ast::ForeignItem),
689 impl PartialEq for Nonterminal {
690 fn eq(&self, rhs: &Self) -> bool {
692 (NtIdent(ident_lhs, is_raw_lhs), NtIdent(ident_rhs, is_raw_rhs)) =>
693 ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs,
694 (NtLifetime(ident_lhs), NtLifetime(ident_rhs)) => ident_lhs == ident_rhs,
695 (NtTT(tt_lhs), NtTT(tt_rhs)) => tt_lhs == tt_rhs,
696 // FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
697 // correctly based on data from AST. This will prevent them from matching each other
698 // in macros. The comparison will become possible only when each nonterminal has an
699 // attached token stream from which it was parsed.
705 impl fmt::Debug for Nonterminal {
706 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
708 NtItem(..) => f.pad("NtItem(..)"),
709 NtBlock(..) => f.pad("NtBlock(..)"),
710 NtStmt(..) => f.pad("NtStmt(..)"),
711 NtPat(..) => f.pad("NtPat(..)"),
712 NtExpr(..) => f.pad("NtExpr(..)"),
713 NtTy(..) => f.pad("NtTy(..)"),
714 NtIdent(..) => f.pad("NtIdent(..)"),
715 NtLiteral(..) => f.pad("NtLiteral(..)"),
716 NtMeta(..) => f.pad("NtMeta(..)"),
717 NtPath(..) => f.pad("NtPath(..)"),
718 NtTT(..) => f.pad("NtTT(..)"),
719 NtImplItem(..) => f.pad("NtImplItem(..)"),
720 NtTraitItem(..) => f.pad("NtTraitItem(..)"),
721 NtForeignItem(..) => f.pad("NtForeignItem(..)"),
722 NtVis(..) => f.pad("NtVis(..)"),
723 NtLifetime(..) => f.pad("NtLifetime(..)"),