Skip to content

Commit bfaf418

Browse files
committed
Make lifetime nonterminals closer to identifier nonterminals
1 parent b3b5ef1 commit bfaf418

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ pub fn parse_failure_msg(tok: Token) -> String {
365365
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
366366
if let (Some((id1, is_raw1)), Some((id2, is_raw2))) = (t1.ident(), t2.ident()) {
367367
id1.name == id2.name && is_raw1 == is_raw2
368-
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
368+
} else if let (Some(id1), Some(id2)) = (t1.lifetime(), t2.lifetime()) {
369369
id1.name == id2.name
370370
} else {
371371
*t1 == *t2
@@ -835,7 +835,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
835835
"path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))),
836836
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
837837
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
838-
"lifetime" => token::NtLifetime(p.expect_lifetime()),
838+
"lifetime" => token::NtLifetime(p.expect_lifetime().ident),
839839
// this is not supposed to happen, since it has been checked
840840
// when compiling the macro.
841841
_ => p.span_bug(sp, "invalid fragment specifier"),

src/libsyntax/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
633633
token::NtPat(pat) => token::NtPat(fld.fold_pat(pat)),
634634
token::NtExpr(expr) => token::NtExpr(fld.fold_expr(expr)),
635635
token::NtTy(ty) => token::NtTy(fld.fold_ty(ty)),
636-
token::NtIdent(id, is_raw) => token::NtIdent(fld.fold_ident(id), is_raw),
636+
token::NtIdent(ident, is_raw) => token::NtIdent(fld.fold_ident(ident), is_raw),
637+
token::NtLifetime(ident) => token::NtLifetime(fld.fold_ident(ident)),
637638
token::NtMeta(meta) => token::NtMeta(fld.fold_meta_item(meta)),
638639
token::NtPath(path) => token::NtPath(fld.fold_path(path)),
639640
token::NtTT(tt) => token::NtTT(fld.fold_tt(tt)),
@@ -649,7 +650,6 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
649650
token::NtWhereClause(fld.fold_where_clause(where_clause)),
650651
token::NtArg(arg) => token::NtArg(fld.fold_arg(arg)),
651652
token::NtVis(vis) => token::NtVis(fld.fold_vis(vis)),
652-
token::NtLifetime(lifetime) => token::NtLifetime(fld.fold_lifetime(lifetime)),
653653
token::NtForeignItem(ni) =>
654654
token::NtForeignItem(fld.fold_foreign_item(ni)
655655
// see reasoning above

src/libsyntax/parse/parser.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,18 +2048,20 @@ impl<'a> Parser<'a> {
20482048

20492049
/// Parse single lifetime 'a or panic.
20502050
pub fn expect_lifetime(&mut self) -> Lifetime {
2051-
if let Some(lifetime) = self.token.lifetime2(self.span) {
2051+
if let Some(ident) = self.token.lifetime() {
2052+
let span = self.span;
20522053
self.bump();
2053-
lifetime
2054+
Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID }
20542055
} else {
20552056
self.span_bug(self.span, "not a lifetime")
20562057
}
20572058
}
20582059

20592060
fn eat_label(&mut self) -> Option<Label> {
2060-
if let Some(lifetime) = self.token.lifetime2(self.span) {
2061+
if let Some(ident) = self.token.lifetime() {
2062+
let span = self.span;
20612063
self.bump();
2062-
Some(Label { ident: lifetime.ident })
2064+
Some(Label { ident: Ident::new(ident.name, span) })
20632065
} else {
20642066
None
20652067
}
@@ -2703,7 +2705,7 @@ impl<'a> Parser<'a> {
27032705
}
27042706

27052707
pub fn process_potential_macro_variable(&mut self) {
2706-
let (ident, is_raw) = match self.token {
2708+
let (token, span) = match self.token {
27072709
token::Dollar if self.span.ctxt() != syntax_pos::hygiene::SyntaxContext::empty() &&
27082710
self.look_ahead(1, |t| t.is_ident()) => {
27092711
self.bump();
@@ -2718,15 +2720,18 @@ impl<'a> Parser<'a> {
27182720
}
27192721
token::Interpolated(ref nt) => {
27202722
self.meta_var_span = Some(self.span);
2723+
// Interpolated identifier and lifetime tokens are replaced with usual identifier
2724+
// and lifetime tokens, so the former are never encountered during normal parsing.
27212725
match nt.0 {
2722-
token::NtIdent(ident, is_raw) => (ident, is_raw),
2726+
token::NtIdent(ident, is_raw) => (token::Ident(ident, is_raw), ident.span),
2727+
token::NtLifetime(ident) => (token::Lifetime(ident), ident.span),
27232728
_ => return,
27242729
}
27252730
}
27262731
_ => return,
27272732
};
2728-
self.token = token::Ident(ident, is_raw);
2729-
self.span = ident.span;
2733+
self.token = token;
2734+
self.span = span;
27302735
}
27312736

27322737
/// parse a single token tree from the input.

src/libsyntax/parse/token.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ impl Token {
317317
}
318318
}
319319

320-
pub fn ident(&self) -> Option<(ast::Ident, bool)> {
320+
/// Returns an identifier if this token is an identifier.
321+
pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
321322
match *self {
322323
Ident(ident, is_raw) => Some((ident, is_raw)),
323324
Interpolated(ref nt) => match nt.0 {
@@ -327,11 +328,25 @@ impl Token {
327328
_ => None,
328329
}
329330
}
330-
331+
/// Returns a lifetime identifier if this token is a lifetime.
332+
pub fn lifetime(&self) -> Option<ast::Ident> {
333+
match *self {
334+
Lifetime(ident) => Some(ident),
335+
Interpolated(ref nt) => match nt.0 {
336+
NtLifetime(ident) => Some(ident),
337+
_ => None,
338+
},
339+
_ => None,
340+
}
341+
}
331342
/// Returns `true` if the token is an identifier.
332343
pub fn is_ident(&self) -> bool {
333344
self.ident().is_some()
334345
}
346+
/// Returns `true` if the token is a lifetime.
347+
pub fn is_lifetime(&self) -> bool {
348+
self.lifetime().is_some()
349+
}
335350

336351
/// Returns `true` if the token is a documentation comment.
337352
pub fn is_doc_comment(&self) -> bool {
@@ -359,26 +374,6 @@ impl Token {
359374
false
360375
}
361376

362-
/// Returns a lifetime with the span and a dummy id if it is a lifetime,
363-
/// or the original lifetime if it is an interpolated lifetime, ignoring
364-
/// the span.
365-
pub fn lifetime2(&self, span: Span) -> Option<ast::Lifetime> {
366-
match *self {
367-
Lifetime(ident) => Some(ast::Lifetime { id: ast::DUMMY_NODE_ID,
368-
ident: ast::Ident::new(ident.name, span) }),
369-
Interpolated(ref nt) => match nt.0 {
370-
NtLifetime(lifetime) => Some(lifetime),
371-
_ => None,
372-
},
373-
_ => None,
374-
}
375-
}
376-
377-
/// Returns `true` if the token is a lifetime.
378-
pub fn is_lifetime(&self) -> bool {
379-
self.lifetime2(syntax_pos::DUMMY_SP).is_some()
380-
}
381-
382377
/// Returns `true` if the token is either the `mut` or `const` keyword.
383378
pub fn is_mutability(&self) -> bool {
384379
self.is_keyword(keywords::Mut) ||
@@ -431,6 +426,14 @@ impl Token {
431426
}
432427
}
433428

429+
/// Returns `true` if the token is either a special identifier or a keyword.
430+
pub fn is_reserved_ident(&self) -> bool {
431+
match self.ident() {
432+
Some((id, false)) => is_reserved_ident(id),
433+
_ => false,
434+
}
435+
}
436+
434437
pub fn glue(self, joint: Token) -> Option<Token> {
435438
Some(match self {
436439
Eq => match joint {
@@ -497,14 +500,6 @@ impl Token {
497500
}
498501
}
499502

500-
/// Returns `true` if the token is either a special identifier or a keyword.
501-
pub fn is_reserved_ident(&self) -> bool {
502-
match self.ident() {
503-
Some((id, false)) => is_reserved_ident(id),
504-
_ => false,
505-
}
506-
}
507-
508503
pub fn interpolated_to_tokenstream(&self, sess: &ParseSess, span: Span)
509504
-> TokenStream
510505
{
@@ -542,9 +537,9 @@ impl Token {
542537
let token = Token::Ident(ident, is_raw);
543538
tokens = Some(TokenTree::Token(ident.span, token).into());
544539
}
545-
Nonterminal::NtLifetime(lifetime) => {
546-
let token = Token::Lifetime(lifetime.ident);
547-
tokens = Some(TokenTree::Token(lifetime.ident.span, token).into());
540+
Nonterminal::NtLifetime(ident) => {
541+
let token = Token::Lifetime(ident);
542+
tokens = Some(TokenTree::Token(ident.span, token).into());
548543
}
549544
Nonterminal::NtTT(ref tt) => {
550545
tokens = Some(tt.clone().into());
@@ -572,6 +567,7 @@ pub enum Nonterminal {
572567
NtExpr(P<ast::Expr>),
573568
NtTy(P<ast::Ty>),
574569
NtIdent(ast::Ident, /* is_raw */ bool),
570+
NtLifetime(ast::Ident),
575571
/// Stuff inside brackets for attributes
576572
NtMeta(ast::MetaItem),
577573
NtPath(ast::Path),
@@ -585,7 +581,6 @@ pub enum Nonterminal {
585581
NtGenerics(ast::Generics),
586582
NtWhereClause(ast::WhereClause),
587583
NtArg(ast::Arg),
588-
NtLifetime(ast::Lifetime),
589584
}
590585

591586
impl fmt::Debug for Nonterminal {

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ pub fn token_to_string(tok: &Token) -> String {
272272
token::NtPat(ref e) => pat_to_string(e),
273273
token::NtIdent(e, false) => ident_to_string(e),
274274
token::NtIdent(e, true) => format!("r#{}", ident_to_string(e)),
275+
token::NtLifetime(e) => ident_to_string(e),
275276
token::NtTT(ref tree) => tt_to_string(tree.clone()),
276277
token::NtArm(ref e) => arm_to_string(e),
277278
token::NtImplItem(ref e) => impl_item_to_string(e),
@@ -280,7 +281,6 @@ pub fn token_to_string(tok: &Token) -> String {
280281
token::NtWhereClause(ref e) => where_clause_to_string(e),
281282
token::NtArg(ref e) => arg_to_string(e),
282283
token::NtVis(ref e) => vis_to_string(e),
283-
token::NtLifetime(ref e) => lifetime_to_string(e),
284284
token::NtForeignItem(ref e) => foreign_item_to_string(e),
285285
}
286286
}

0 commit comments

Comments
 (0)