Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 9, 2020
1 parent 9be233c commit 7a30bb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
19 changes: 14 additions & 5 deletions src/librustc_ast/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,15 @@ pub enum TokenKind {
/* Literals */
Literal(Lit),

/* Name components */
/// Identifier token.
/// Do not forget about `NtIdent` when you want to match on identifiers.
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
/// treat regular and interpolated identifiers in the same way.
Ident(ast::Name, /* is_raw */ bool),
/// Lifetime identifier token.
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
/// treat regular and interpolated lifetime identifiers in the same way.
Lifetime(ast::Name),

Interpolated(Lrc<Nonterminal>),
Expand Down Expand Up @@ -328,11 +335,12 @@ impl Token {
mem::replace(self, Token::dummy())
}

/// For interpolated tokens returns a span of the fragment to which the interpolated
/// token refers, for all other tokens this is just a regular span.
/// For interpolated tokens, returns a span of the fragment to which the interpolated
/// token refers. For all other tokens this is just a regular span.
/// It is particularly important to use this for identifiers and lifetimes
/// for which spans affect name resolution. This also includes edition checks
/// for edition-specific keyword identifiers.
/// for which spans affect name resolution and edition checks.
/// Note that keywords are also identifiers, so they should use this
/// if they keep spans or perform edition checks.
pub fn uninterpolated_span(&self) -> Span {
match &self.kind {
Interpolated(nt) => nt.span(),
Expand Down Expand Up @@ -453,6 +461,7 @@ impl Token {
}
}

// A convenience function for matching on identifiers during parsing.
// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
// into the regular identifier or lifetime token it refers to,
// otherwise returns the original token.
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
/// The token is an identifier, but not `_`.
/// We prohibit passing `_` to macros expecting `ident` for now.
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
match token.ident() {
Some((ident, is_raw)) if ident.name != kw::Underscore => Some((ident, is_raw)),
_ => None,
}
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
}

/// Checks whether a non-terminal may begin with a particular token.
Expand Down
13 changes: 6 additions & 7 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ impl<'a> Parser<'a> {
match self.parse_expr() {
Ok(expr) => Ok(expr),
Err(mut err) => match self.token.ident() {
Some((ident, false))
if ident.name == kw::Underscore
&& self.look_ahead(1, |t| t == &token::Comma) =>
Some((Ident { name: kw::Underscore, .. }, false))
if self.look_ahead(1, |t| t == &token::Comma) =>
{
// Special-case handling of `foo(_, _, _)`
err.emit();
Expand Down Expand Up @@ -333,13 +332,13 @@ impl<'a> Parser<'a> {
fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
(Some(op), _) => (op, self.token.span),
(None, Some((ident, false))) if ident.name == sym::and => {
(None, Some((Ident { name: sym::and, span }, false))) => {
self.error_bad_logical_op("and", "&&", "conjunction");
(AssocOp::LAnd, ident.span)
(AssocOp::LAnd, span)
}
(None, Some((ident, false))) if ident.name == sym::or => {
(None, Some((Ident { name: sym::or, span }, false))) => {
self.error_bad_logical_op("or", "||", "disjunction");
(AssocOp::LOr, ident.span)
(AssocOp::LOr, span)
}
_ => return None,
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ impl<'a> Parser<'a> {

fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
match self.token.ident() {
Some((ident, false)) if ident.name == kw::Underscore => {
Some((ident @ Ident { name: kw::Underscore, .. }, false)) => {
self.bump();
Ok(ident)
}
Expand Down

0 comments on commit 7a30bb1

Please sign in to comment.