Skip to content

Commit 7c98342

Browse files
committed
Add doc for rustc_parse's sequences
1 parent 21cce21 commit 7c98342

File tree

1 file changed

+28
-10
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+28
-10
lines changed

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,15 @@ impl TokenType {
322322
}
323323
}
324324

325+
/// Used by [`Parser::expect_any_with_type`].
325326
#[derive(Copy, Clone, Debug)]
326327
enum TokenExpectType {
328+
/// Unencountered tokens are inserted into [`Parser::expected_tokens`].
329+
/// See [`Parser::check`].
327330
Expect,
331+
332+
/// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
333+
/// See [`Parser::check_noexpect`].
328334
NoExpect,
329335
}
330336

@@ -768,13 +774,17 @@ impl<'a> Parser<'a> {
768774
}
769775
}
770776

777+
/// Checks if the next token is contained within `kets`, and returns `true` if so.
771778
fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
772779
kets.iter().any(|k| match expect {
773780
TokenExpectType::Expect => self.check(k),
774-
TokenExpectType::NoExpect => self.token == **k,
781+
TokenExpectType::NoExpect => self.check_noexpect(k),
775782
})
776783
}
777784

785+
/// Parses a sequence until the specified delimiters. The function
786+
/// `f` must consume tokens until reaching the next separator or
787+
/// closing bracket.
778788
fn parse_seq_to_before_tokens<T>(
779789
&mut self,
780790
kets: &[&TokenKind],
@@ -793,13 +803,15 @@ impl<'a> Parser<'a> {
793803
}
794804
if let Some(t) = &sep.sep {
795805
if first {
806+
// no separator for the first element
796807
first = false;
797808
} else {
809+
// check for separator
798810
match self.expect(t) {
799-
Ok(false) => {
811+
Ok(false) /* not recovered */ => {
800812
self.current_closure.take();
801813
}
802-
Ok(true) => {
814+
Ok(true) /* recovered */ => {
803815
self.current_closure.take();
804816
recovered = true;
805817
break;
@@ -967,19 +979,19 @@ impl<'a> Parser<'a> {
967979
Ok(())
968980
}
969981

970-
/// Parses a sequence, not including the closing delimiter. The function
982+
/// Parses a sequence, not including the delimiters. The function
971983
/// `f` must consume tokens until reaching the next separator or
972984
/// closing bracket.
973985
fn parse_seq_to_before_end<T>(
974986
&mut self,
975987
ket: &TokenKind,
976988
sep: SeqSep,
977989
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
978-
) -> PResult<'a, (ThinVec<T>, bool, bool)> {
990+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, bool /* recovered */)> {
979991
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
980992
}
981993

982-
/// Parses a sequence, including the closing delimiter. The function
994+
/// Parses a sequence, including only the closing delimiter. The function
983995
/// `f` must consume tokens until reaching the next separator or
984996
/// closing bracket.
985997
fn parse_seq_to_end<T>(
@@ -995,7 +1007,7 @@ impl<'a> Parser<'a> {
9951007
Ok((val, trailing))
9961008
}
9971009

998-
/// Parses a sequence, including the closing delimiter. The function
1010+
/// Parses a sequence, including both delimiters. The function
9991011
/// `f` must consume tokens until reaching the next separator or
10001012
/// closing bracket.
10011013
fn parse_unspanned_seq<T>(
@@ -1004,16 +1016,19 @@ impl<'a> Parser<'a> {
10041016
ket: &TokenKind,
10051017
sep: SeqSep,
10061018
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1007-
) -> PResult<'a, (ThinVec<T>, bool)> {
1019+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10081020
self.expect(bra)?;
10091021
self.parse_seq_to_end(ket, sep, f)
10101022
}
10111023

1024+
/// Parses a comma-separated sequence, including both delimiters.
1025+
/// The function `f` must consume tokens until reaching the next separator or
1026+
/// closing bracket.
10121027
fn parse_delim_comma_seq<T>(
10131028
&mut self,
10141029
delim: Delimiter,
10151030
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1016-
) -> PResult<'a, (ThinVec<T>, bool)> {
1031+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10171032
self.parse_unspanned_seq(
10181033
&token::OpenDelim(delim),
10191034
&token::CloseDelim(delim),
@@ -1022,10 +1037,13 @@ impl<'a> Parser<'a> {
10221037
)
10231038
}
10241039

1040+
/// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1041+
/// The function `f` must consume tokens until reaching the next separator or
1042+
/// closing bracket.
10251043
fn parse_paren_comma_seq<T>(
10261044
&mut self,
10271045
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1028-
) -> PResult<'a, (ThinVec<T>, bool)> {
1046+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10291047
self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
10301048
}
10311049

0 commit comments

Comments
 (0)