Add newtype for trailing in parser

This commit is contained in:
clubby789 2024-02-13 23:48:23 +00:00
parent 4850ae8442
commit acb2cee618
5 changed files with 30 additions and 21 deletions

View File

@ -4,7 +4,7 @@
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{ use super::{
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Recovered, Restrictions, AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Recovered, Restrictions,
SemiColonMode, SeqSep, TokenExpectType, TokenType, TrailingToken, SemiColonMode, SeqSep, TokenExpectType, TokenType, Trailing, TrailingToken,
}; };
use crate::errors; use crate::errors;
@ -1561,7 +1561,7 @@ fn parse_expr_tuple_parens(&mut self, restrictions: Restrictions) -> PResult<'a,
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err)); return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err));
} }
}; };
let kind = if es.len() == 1 && !trailing_comma { let kind = if es.len() == 1 && matches!(trailing_comma, Trailing::No) {
// `(e)` is parenthesized `e`. // `(e)` is parenthesized `e`.
ExprKind::Paren(es.into_iter().next().unwrap()) ExprKind::Paren(es.into_iter().next().unwrap())
} else { } else {

View File

@ -1,7 +1,8 @@
use super::diagnostics::{dummy_arg, ConsumeClosingDelim}; use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{ use super::{
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, TrailingToken, AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, Trailing,
TrailingToken,
}; };
use crate::errors::{self, MacroExpandsToAdtField}; use crate::errors::{self, MacroExpandsToAdtField};
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
@ -1459,7 +1460,7 @@ fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
let (variants, _) = if self.token == TokenKind::Semi { let (variants, _) = if self.token == TokenKind::Semi {
self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span }); self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
self.bump(); self.bump();
(thin_vec![], false) (thin_vec![], Trailing::No)
} else { } else {
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant(id.span)) self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant(id.span))
.map_err(|mut err| { .map_err(|mut err| {

View File

@ -371,6 +371,12 @@ fn from(r: Recovered) -> bool {
} }
} }
#[derive(Copy, Clone, Debug)]
pub enum Trailing {
No,
Yes,
}
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub enum TokenDescription { pub enum TokenDescription {
ReservedIdentifier, ReservedIdentifier,
@ -797,10 +803,10 @@ fn parse_seq_to_before_tokens<T>(
sep: SeqSep, sep: SeqSep,
expect: TokenExpectType, expect: TokenExpectType,
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> { ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
let mut first = true; let mut first = true;
let mut recovered = Recovered::No; let mut recovered = Recovered::No;
let mut trailing = false; let mut trailing = Trailing::No;
let mut v = ThinVec::new(); let mut v = ThinVec::new();
while !self.expect_any_with_type(kets, expect) { while !self.expect_any_with_type(kets, expect) {
@ -914,7 +920,7 @@ fn parse_seq_to_before_tokens<T>(
} }
} }
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) { if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
trailing = true; trailing = Trailing::Yes;
break; break;
} }
@ -992,7 +998,7 @@ fn parse_seq_to_before_end<T>(
ket: &TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> { ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f) self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
} }
@ -1004,7 +1010,7 @@ fn parse_seq_to_end<T>(
ket: &TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> { ) -> PResult<'a, (ThinVec<T>, Trailing)> {
let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?; let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
if matches!(recovered, Recovered::No) { if matches!(recovered, Recovered::No) {
self.eat(ket); self.eat(ket);
@ -1021,7 +1027,7 @@ fn parse_unspanned_seq<T>(
ket: &TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> { ) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.expect(bra)?; self.expect(bra)?;
self.parse_seq_to_end(ket, sep, f) self.parse_seq_to_end(ket, sep, f)
} }
@ -1033,7 +1039,7 @@ fn parse_delim_comma_seq<T>(
&mut self, &mut self,
delim: Delimiter, delim: Delimiter,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> { ) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.parse_unspanned_seq( self.parse_unspanned_seq(
&token::OpenDelim(delim), &token::OpenDelim(delim),
&token::CloseDelim(delim), &token::CloseDelim(delim),
@ -1048,7 +1054,7 @@ fn parse_delim_comma_seq<T>(
fn parse_paren_comma_seq<T>( fn parse_paren_comma_seq<T>(
&mut self, &mut self,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>, f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> { ) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.parse_delim_comma_seq(Delimiter::Parenthesis, f) self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
} }

View File

@ -1,4 +1,4 @@
use super::{ForceCollect, Parser, PathStyle, Restrictions, TrailingToken}; use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
use crate::errors::{ use crate::errors::{
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed, self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt, DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@ -696,7 +696,9 @@ fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
// Here, `(pat,)` is a tuple pattern. // Here, `(pat,)` is a tuple pattern.
// For backward compatibility, `(..)` is a tuple pattern as well. // For backward compatibility, `(..)` is a tuple pattern as well.
Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) { let paren_pattern =
fields.len() == 1 && !(matches!(trailing_comma, Trailing::Yes) || fields[0].is_rest());
if paren_pattern {
let pat = fields.into_iter().next().unwrap(); let pat = fields.into_iter().next().unwrap();
let close_paren = self.prev_token.span; let close_paren = self.prev_token.span;
@ -714,7 +716,7 @@ fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
}, },
}); });
self.parse_pat_range_begin_with(begin.clone(), form)? self.parse_pat_range_begin_with(begin.clone(), form)
} }
// recover ranges with parentheses around the `(start)..` // recover ranges with parentheses around the `(start)..`
PatKind::Err(_) PatKind::Err(_)
@ -729,15 +731,15 @@ fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
}, },
}); });
self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)? self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)
} }
// (pat) with optional parentheses // (pat) with optional parentheses
_ => PatKind::Paren(pat), _ => Ok(PatKind::Paren(pat)),
} }
} else { } else {
PatKind::Tuple(fields) Ok(PatKind::Tuple(fields))
}) }
} }
/// Parse a mutable binding with the `mut` token already eaten. /// Parse a mutable binding with the `mut` token already eaten.

View File

@ -1,4 +1,4 @@
use super::{Parser, PathStyle, TokenType}; use super::{Parser, PathStyle, TokenType, Trailing};
use crate::errors::{ use crate::errors::{
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType, self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
@ -415,7 +415,7 @@ fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: AllowPlus) -> PResu
Ok(ty) Ok(ty)
})?; })?;
if ts.len() == 1 && !trailing { if ts.len() == 1 && matches!(trailing, Trailing::No) {
let ty = ts.into_iter().next().unwrap().into_inner(); let ty = ts.into_iter().next().unwrap().into_inner();
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus(); let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
match ty.kind { match ty.kind {