Add some missing support for NtIdent
This commit is contained in:
parent
79cd224e75
commit
59261f0a7c
4 changed files with 44 additions and 36 deletions
|
|
@ -753,6 +753,12 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
|
|||
fn get_macro_name(token: &Token) -> Option<(Name, bool)> {
|
||||
match token.kind {
|
||||
token::Ident(name, is_raw) if name != kw::Underscore => Some((name, is_raw)),
|
||||
token::Interpolated(ref nt) => match **nt {
|
||||
token::NtIdent(ident, is_raw) if ident.name != kw::Underscore => {
|
||||
Some((ident.name, is_raw))
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use syntax::ast::{
|
|||
};
|
||||
use syntax::ast::{AttrVec, ItemKind, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind};
|
||||
use syntax::ptr::P;
|
||||
use syntax::token::{self, token_can_begin_expr, TokenKind};
|
||||
use syntax::token::{self, TokenKind};
|
||||
use syntax::util::parser::AssocOp;
|
||||
|
||||
use log::{debug, trace};
|
||||
|
|
@ -900,8 +900,7 @@ impl<'a> Parser<'a> {
|
|||
} else if !sm.is_multiline(self.prev_span.until(self.token.span)) {
|
||||
// The current token is in the same line as the prior token, not recoverable.
|
||||
} else if self.look_ahead(1, |t| {
|
||||
t == &token::CloseDelim(token::Brace)
|
||||
|| token_can_begin_expr(t) && t.kind != token::Colon
|
||||
t == &token::CloseDelim(token::Brace) || t.can_begin_expr() && t.kind != token::Colon
|
||||
}) && [token::Comma, token::Colon].contains(&self.token.kind)
|
||||
{
|
||||
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
|
||||
|
|
@ -919,7 +918,7 @@ impl<'a> Parser<'a> {
|
|||
} else if self.look_ahead(0, |t| {
|
||||
t == &token::CloseDelim(token::Brace)
|
||||
|| (
|
||||
token_can_begin_expr(t) && t != &token::Semi && t != &token::Pound
|
||||
t.can_begin_expr() && t != &token::Semi && t != &token::Pound
|
||||
// Avoid triggering with too many trailing `#` in raw string.
|
||||
)
|
||||
}) {
|
||||
|
|
|
|||
|
|
@ -147,36 +147,30 @@ impl Lit {
|
|||
|
||||
pub fn ident_can_begin_expr(name: ast::Name, span: Span, is_raw: bool) -> bool {
|
||||
let ident_token = Token::new(Ident(name, is_raw), span);
|
||||
token_can_begin_expr(&ident_token)
|
||||
}
|
||||
|
||||
pub fn token_can_begin_expr(ident_token: &Token) -> bool {
|
||||
!ident_token.is_reserved_ident()
|
||||
|| ident_token.is_path_segment_keyword()
|
||||
|| match ident_token.kind {
|
||||
TokenKind::Ident(ident, _) => [
|
||||
kw::Async,
|
||||
kw::Do,
|
||||
kw::Box,
|
||||
kw::Break,
|
||||
kw::Continue,
|
||||
kw::False,
|
||||
kw::For,
|
||||
kw::If,
|
||||
kw::Let,
|
||||
kw::Loop,
|
||||
kw::Match,
|
||||
kw::Move,
|
||||
kw::Return,
|
||||
kw::True,
|
||||
kw::Unsafe,
|
||||
kw::While,
|
||||
kw::Yield,
|
||||
kw::Static,
|
||||
]
|
||||
.contains(&ident),
|
||||
_ => false,
|
||||
}
|
||||
|| [
|
||||
kw::Async,
|
||||
kw::Do,
|
||||
kw::Box,
|
||||
kw::Break,
|
||||
kw::Continue,
|
||||
kw::False,
|
||||
kw::For,
|
||||
kw::If,
|
||||
kw::Let,
|
||||
kw::Loop,
|
||||
kw::Match,
|
||||
kw::Move,
|
||||
kw::Return,
|
||||
kw::True,
|
||||
kw::Unsafe,
|
||||
kw::While,
|
||||
kw::Yield,
|
||||
kw::Static,
|
||||
]
|
||||
.contains(&name)
|
||||
}
|
||||
|
||||
fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool {
|
||||
|
|
@ -369,8 +363,8 @@ impl Token {
|
|||
Lifetime(..) | // labeled loop
|
||||
Pound => true, // expression attributes
|
||||
Interpolated(ref nt) => match **nt {
|
||||
NtIdent(ident, is_raw) => ident_can_begin_expr(ident.name, ident.span, is_raw),
|
||||
NtLiteral(..) |
|
||||
NtIdent(..) |
|
||||
NtExpr(..) |
|
||||
NtBlock(..) |
|
||||
NtPath(..) |
|
||||
|
|
@ -397,7 +391,8 @@ impl Token {
|
|||
Lt | BinOp(Shl) | // associated path
|
||||
ModSep => true, // global path
|
||||
Interpolated(ref nt) => match **nt {
|
||||
NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
|
||||
NtIdent(ident, is_raw) => ident_can_begin_type(ident.name, ident.span, is_raw),
|
||||
NtTy(..) | NtPath(..) | NtLifetime(..) => true,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
|
|
@ -442,6 +437,7 @@ impl Token {
|
|||
Literal(..) | BinOp(Minus) => true,
|
||||
Ident(name, false) if name.is_bool_lit() => true,
|
||||
Interpolated(ref nt) => match &**nt {
|
||||
NtIdent(ident, false) if ident.name.is_bool_lit() => true,
|
||||
NtExpr(e) | NtLiteral(e) => matches!(e.kind, ast::ExprKind::Lit(_)),
|
||||
_ => false,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -197,10 +197,17 @@ impl Lit {
|
|||
}
|
||||
token::Literal(lit) => lit,
|
||||
token::Interpolated(ref nt) => {
|
||||
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt {
|
||||
if let ast::ExprKind::Lit(lit) = &expr.kind {
|
||||
return Ok(lit.clone());
|
||||
match &**nt {
|
||||
token::NtIdent(ident, false) if ident.name.is_bool_lit() => {
|
||||
let lit = token::Lit::new(token::Bool, ident.name, None);
|
||||
return Lit::from_lit_token(lit, ident.span);
|
||||
}
|
||||
token::NtExpr(expr) | token::NtLiteral(expr) => {
|
||||
if let ast::ExprKind::Lit(lit) = &expr.kind {
|
||||
return Ok(lit.clone());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return Err(LitError::NotLiteral);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue