diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-03-24 19:49:50 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-04-06 11:52:16 +0300 |
| commit | bfaf4180ae8f048634d270564692d35f64a9c130 (patch) | |
| tree | 447db804b4a7d405f2fb492434378a57bea33802 /src/libsyntax/parse/token.rs | |
| parent | b3b5ef186c1f9f57179ab1dc922f6c7f02fb9d8c (diff) | |
| download | rust-bfaf4180ae8f048634d270564692d35f64a9c130.tar.gz rust-bfaf4180ae8f048634d270564692d35f64a9c130.zip | |
Make lifetime nonterminals closer to identifier nonterminals
Diffstat (limited to 'src/libsyntax/parse/token.rs')
| -rw-r--r-- | src/libsyntax/parse/token.rs | 63 |
1 files changed, 29 insertions, 34 deletions
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index c1049beea9f..6544619af9c 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -317,7 +317,8 @@ impl Token { } } - pub fn ident(&self) -> Option<(ast::Ident, bool)> { + /// Returns an identifier if this token is an identifier. + pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> { match *self { Ident(ident, is_raw) => Some((ident, is_raw)), Interpolated(ref nt) => match nt.0 { @@ -327,11 +328,25 @@ impl Token { _ => None, } } - + /// Returns a lifetime identifier if this token is a lifetime. + pub fn lifetime(&self) -> Option<ast::Ident> { + match *self { + Lifetime(ident) => Some(ident), + Interpolated(ref nt) => match nt.0 { + NtLifetime(ident) => Some(ident), + _ => None, + }, + _ => None, + } + } /// Returns `true` if the token is an identifier. pub fn is_ident(&self) -> bool { self.ident().is_some() } + /// Returns `true` if the token is a lifetime. + pub fn is_lifetime(&self) -> bool { + self.lifetime().is_some() + } /// Returns `true` if the token is a documentation comment. pub fn is_doc_comment(&self) -> bool { @@ -359,26 +374,6 @@ impl Token { false } - /// Returns a lifetime with the span and a dummy id if it is a lifetime, - /// or the original lifetime if it is an interpolated lifetime, ignoring - /// the span. - pub fn lifetime2(&self, span: Span) -> Option<ast::Lifetime> { - match *self { - Lifetime(ident) => Some(ast::Lifetime { id: ast::DUMMY_NODE_ID, - ident: ast::Ident::new(ident.name, span) }), - Interpolated(ref nt) => match nt.0 { - NtLifetime(lifetime) => Some(lifetime), - _ => None, - }, - _ => None, - } - } - - /// Returns `true` if the token is a lifetime. - pub fn is_lifetime(&self) -> bool { - self.lifetime2(syntax_pos::DUMMY_SP).is_some() - } - /// Returns `true` if the token is either the `mut` or `const` keyword. pub fn is_mutability(&self) -> bool { self.is_keyword(keywords::Mut) || @@ -431,6 +426,14 @@ impl Token { } } + /// Returns `true` if the token is either a special identifier or a keyword. + pub fn is_reserved_ident(&self) -> bool { + match self.ident() { + Some((id, false)) => is_reserved_ident(id), + _ => false, + } + } + pub fn glue(self, joint: Token) -> Option<Token> { Some(match self { Eq => match joint { @@ -497,14 +500,6 @@ impl Token { } } - /// Returns `true` if the token is either a special identifier or a keyword. - pub fn is_reserved_ident(&self) -> bool { - match self.ident() { - Some((id, false)) => is_reserved_ident(id), - _ => false, - } - } - pub fn interpolated_to_tokenstream(&self, sess: &ParseSess, span: Span) -> TokenStream { @@ -542,9 +537,9 @@ impl Token { let token = Token::Ident(ident, is_raw); tokens = Some(TokenTree::Token(ident.span, token).into()); } - Nonterminal::NtLifetime(lifetime) => { - let token = Token::Lifetime(lifetime.ident); - tokens = Some(TokenTree::Token(lifetime.ident.span, token).into()); + Nonterminal::NtLifetime(ident) => { + let token = Token::Lifetime(ident); + tokens = Some(TokenTree::Token(ident.span, token).into()); } Nonterminal::NtTT(ref tt) => { tokens = Some(tt.clone().into()); @@ -572,6 +567,7 @@ pub enum Nonterminal { NtExpr(P<ast::Expr>), NtTy(P<ast::Ty>), NtIdent(ast::Ident, /* is_raw */ bool), + NtLifetime(ast::Ident), /// Stuff inside brackets for attributes NtMeta(ast::MetaItem), NtPath(ast::Path), @@ -585,7 +581,6 @@ pub enum Nonterminal { NtGenerics(ast::Generics), NtWhereClause(ast::WhereClause), NtArg(ast::Arg), - NtLifetime(ast::Lifetime), } impl fmt::Debug for Nonterminal { |
