rustc_parse: Use Token::ident
where possible
This commit is contained in:
parent
2cb0b8582e
commit
925e9a2188
7 changed files with 88 additions and 94 deletions
|
@ -156,44 +156,43 @@ fn parse_args<'a>(
|
|||
if p.token == token::Eof {
|
||||
break;
|
||||
} // accept trailing commas
|
||||
if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
|
||||
named = true;
|
||||
let name = if let token::Ident(name, _) = p.normalized_token.kind {
|
||||
match p.token.ident() {
|
||||
Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
|
||||
named = true;
|
||||
p.bump();
|
||||
name
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
|
||||
p.expect(&token::Eq)?;
|
||||
let e = p.parse_expr()?;
|
||||
if let Some(prev) = names.get(&name) {
|
||||
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", name))
|
||||
.span_label(args[*prev].span, "previously here")
|
||||
.span_label(e.span, "duplicate argument")
|
||||
.emit();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Resolve names into slots early.
|
||||
// Since all the positional args are already seen at this point
|
||||
// if the input is valid, we can simply append to the positional
|
||||
// args. And remember the names.
|
||||
let slot = args.len();
|
||||
names.insert(name, slot);
|
||||
args.push(e);
|
||||
} else {
|
||||
let e = p.parse_expr()?;
|
||||
if named {
|
||||
let mut err = ecx
|
||||
.struct_span_err(e.span, "positional arguments cannot follow named arguments");
|
||||
err.span_label(e.span, "positional arguments must be before named arguments");
|
||||
for pos in names.values() {
|
||||
err.span_label(args[*pos].span, "named argument");
|
||||
p.expect(&token::Eq)?;
|
||||
let e = p.parse_expr()?;
|
||||
if let Some(prev) = names.get(&ident.name) {
|
||||
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", ident))
|
||||
.span_label(args[*prev].span, "previously here")
|
||||
.span_label(e.span, "duplicate argument")
|
||||
.emit();
|
||||
continue;
|
||||
}
|
||||
err.emit();
|
||||
|
||||
// Resolve names into slots early.
|
||||
// Since all the positional args are already seen at this point
|
||||
// if the input is valid, we can simply append to the positional
|
||||
// args. And remember the names.
|
||||
let slot = args.len();
|
||||
names.insert(ident.name, slot);
|
||||
args.push(e);
|
||||
}
|
||||
_ => {
|
||||
let e = p.parse_expr()?;
|
||||
if named {
|
||||
let mut err = ecx.struct_span_err(
|
||||
e.span,
|
||||
"positional arguments cannot follow named arguments",
|
||||
);
|
||||
err.span_label(e.span, "positional arguments must be before named arguments");
|
||||
for pos in names.values() {
|
||||
err.span_label(args[*pos].span, "named argument");
|
||||
}
|
||||
err.emit();
|
||||
}
|
||||
args.push(e);
|
||||
}
|
||||
args.push(e);
|
||||
}
|
||||
}
|
||||
Ok((fmtstr, args, names))
|
||||
|
|
|
@ -750,15 +750,9 @@ 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_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,
|
||||
},
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
@ -783,7 +777,7 @@ fn may_begin_with(token: &Token, name: Name) -> bool {
|
|||
&& !token.is_keyword(kw::Let)
|
||||
}
|
||||
sym::ty => token.can_begin_type(),
|
||||
sym::ident => get_macro_name(token).is_some(),
|
||||
sym::ident => get_macro_ident(token).is_some(),
|
||||
sym::literal => token.can_begin_literal_or_bool(),
|
||||
sym::vis => match token.kind {
|
||||
// The follow-set of :vis + "priv" keyword + interpolated
|
||||
|
@ -888,9 +882,9 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
|
|||
sym::ty => token::NtTy(p.parse_ty()?),
|
||||
// this could be handled like a token, since it is one
|
||||
sym::ident => {
|
||||
if let Some((name, is_raw)) = get_macro_name(&p.token) {
|
||||
if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
|
||||
p.bump();
|
||||
token::NtIdent(Ident::new(name, p.normalized_prev_token.span), is_raw)
|
||||
token::NtIdent(ident, is_raw)
|
||||
} else {
|
||||
let token_str = pprust::token_to_string(&p.token);
|
||||
let msg = &format!("expected ident, found {}", &token_str);
|
||||
|
|
|
@ -192,17 +192,19 @@ impl<'a> Parser<'a> {
|
|||
TokenKind::CloseDelim(token::DelimToken::Brace),
|
||||
TokenKind::CloseDelim(token::DelimToken::Paren),
|
||||
];
|
||||
if let token::Ident(name, false) = self.normalized_token.kind {
|
||||
if Ident::new(name, self.normalized_token.span).is_raw_guess()
|
||||
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind))
|
||||
match self.token.ident() {
|
||||
Some((ident, false))
|
||||
if ident.is_raw_guess()
|
||||
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
|
||||
{
|
||||
err.span_suggestion(
|
||||
self.normalized_token.span,
|
||||
ident.span,
|
||||
"you can escape reserved keywords to use them as identifiers",
|
||||
format!("r#{}", name),
|
||||
format!("r#{}", ident.name),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(token_descr) = super::token_descr_opt(&self.token) {
|
||||
err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));
|
||||
|
|
|
@ -97,9 +97,10 @@ impl<'a> Parser<'a> {
|
|||
fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
|
||||
match self.parse_expr() {
|
||||
Ok(expr) => Ok(expr),
|
||||
Err(mut err) => match self.normalized_token.kind {
|
||||
token::Ident(name, false)
|
||||
if name == kw::Underscore && self.look_ahead(1, |t| t == &token::Comma) =>
|
||||
Err(mut err) => match self.token.ident() {
|
||||
Some((ident, false))
|
||||
if ident.name == kw::Underscore
|
||||
&& self.look_ahead(1, |t| t == &token::Comma) =>
|
||||
{
|
||||
// Special-case handling of `foo(_, _, _)`
|
||||
err.emit();
|
||||
|
@ -331,21 +332,19 @@ impl<'a> Parser<'a> {
|
|||
///
|
||||
/// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
|
||||
fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
|
||||
Some(Spanned {
|
||||
node: match (AssocOp::from_token(&self.token), &self.normalized_token.kind) {
|
||||
(Some(op), _) => op,
|
||||
(None, token::Ident(sym::and, false)) => {
|
||||
self.error_bad_logical_op("and", "&&", "conjunction");
|
||||
AssocOp::LAnd
|
||||
}
|
||||
(None, token::Ident(sym::or, false)) => {
|
||||
self.error_bad_logical_op("or", "||", "disjunction");
|
||||
AssocOp::LOr
|
||||
}
|
||||
_ => return None,
|
||||
},
|
||||
span: self.normalized_token.span,
|
||||
})
|
||||
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 => {
|
||||
self.error_bad_logical_op("and", "&&", "conjunction");
|
||||
(AssocOp::LAnd, ident.span)
|
||||
}
|
||||
(None, Some((ident, false))) if ident.name == sym::or => {
|
||||
self.error_bad_logical_op("or", "||", "disjunction");
|
||||
(AssocOp::LOr, ident.span)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some(source_map::respan(span, op))
|
||||
}
|
||||
|
||||
/// Error on `and` and `or` suggesting `&&` and `||` respectively.
|
||||
|
@ -1907,20 +1906,23 @@ impl<'a> Parser<'a> {
|
|||
|
||||
/// Use in case of error after field-looking code: `S { foo: () with a }`.
|
||||
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
|
||||
if let token::Ident(name, _) = self.normalized_token.kind {
|
||||
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
|
||||
return Some(ast::Field {
|
||||
ident: Ident::new(name, self.normalized_token.span),
|
||||
match self.token.ident() {
|
||||
Some((ident, is_raw))
|
||||
if (is_raw || !ident.is_reserved())
|
||||
&& self.look_ahead(1, |t| *t == token::Colon) =>
|
||||
{
|
||||
Some(ast::Field {
|
||||
ident,
|
||||
span: self.token.span,
|
||||
expr: self.mk_expr_err(self.token.span),
|
||||
is_shorthand: false,
|
||||
attrs: AttrVec::new(),
|
||||
id: DUMMY_NODE_ID,
|
||||
is_placeholder: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn recover_struct_comma_after_dotdot(&mut self, span: Span) {
|
||||
|
|
|
@ -750,10 +750,10 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name @ kw::Underscore, false) => {
|
||||
match self.token.ident() {
|
||||
Some((ident, false)) if ident.name == kw::Underscore => {
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => self.parse_ident(),
|
||||
}
|
||||
|
@ -1609,15 +1609,12 @@ impl<'a> Parser<'a> {
|
|||
/// Returns the parsed optional self parameter and whether a self shortcut was used.
|
||||
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
|
||||
// Extract an identifier *after* having confirmed that the token is one.
|
||||
let expect_self_ident = |this: &mut Self| {
|
||||
match this.normalized_token.kind {
|
||||
// Preserve hygienic context.
|
||||
token::Ident(name, _) => {
|
||||
this.bump();
|
||||
Ident::new(name, this.normalized_prev_token.span)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
let expect_self_ident = |this: &mut Self| match this.token.ident() {
|
||||
Some((ident, false)) => {
|
||||
this.bump();
|
||||
ident
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
// Is `self` `n` tokens ahead?
|
||||
let is_isolated_self = |this: &Self, n| {
|
||||
|
|
|
@ -480,9 +480,9 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name, _) => {
|
||||
if self.token.is_reserved_ident() {
|
||||
match self.token.ident() {
|
||||
Some((ident, is_raw)) => {
|
||||
if !is_raw && ident.is_reserved() {
|
||||
let mut err = self.expected_ident_found();
|
||||
if recover {
|
||||
err.emit();
|
||||
|
@ -491,7 +491,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => Err(match self.prev_token.kind {
|
||||
TokenKind::DocComment(..) => {
|
||||
|
|
|
@ -240,10 +240,10 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name, _) if name.is_path_segment_keyword() => {
|
||||
match self.token.ident() {
|
||||
Some((ident, false)) if ident.is_path_segment_keyword() => {
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => self.parse_ident(),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue