Skip to content

Commit b3d6e3f

Browse files
committed
Add doc for rustc_parse's sequences
1 parent 21d88b3 commit b3d6e3f

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

+28-10
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,15 @@ impl TokenType {
315315
}
316316
}
317317

318+
/// Used by [`Parser::expect_any_with_type`].
318319
#[derive(Copy, Clone, Debug)]
319320
enum TokenExpectType {
321+
/// Unencountered tokens are inserted into [`Parser::expected_tokens`].
322+
/// See [`Parser::check`].
320323
Expect,
324+
325+
/// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
326+
/// See [`Parser::check_noexpect`].
321327
NoExpect,
322328
}
323329

@@ -761,13 +767,17 @@ impl<'a> Parser<'a> {
761767
}
762768
}
763769

770+
/// Checks if the next token is contained within `kets`, and returns `true` if so.
764771
fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
765772
kets.iter().any(|k| match expect {
766773
TokenExpectType::Expect => self.check(k),
767-
TokenExpectType::NoExpect => self.token == **k,
774+
TokenExpectType::NoExpect => self.check_noexpect(k),
768775
})
769776
}
770777

778+
/// Parses a sequence until the specified delimiters. The function
779+
/// `f` must consume tokens until reaching the next separator or
780+
/// closing bracket.
771781
fn parse_seq_to_before_tokens<T>(
772782
&mut self,
773783
kets: &[&TokenKind],
@@ -786,13 +796,15 @@ impl<'a> Parser<'a> {
786796
}
787797
if let Some(t) = &sep.sep {
788798
if first {
799+
// no separator for the first element
789800
first = false;
790801
} else {
802+
// check for separator
791803
match self.expect(t) {
792-
Ok(false) => {
804+
Ok(false) /* not recovered */ => {
793805
self.current_closure.take();
794806
}
795-
Ok(true) => {
807+
Ok(true) /* recovered */ => {
796808
self.current_closure.take();
797809
recovered = true;
798810
break;
@@ -960,19 +972,19 @@ impl<'a> Parser<'a> {
960972
Ok(())
961973
}
962974

963-
/// Parses a sequence, not including the closing delimiter. The function
975+
/// Parses a sequence, not including the delimiters. The function
964976
/// `f` must consume tokens until reaching the next separator or
965977
/// closing bracket.
966978
fn parse_seq_to_before_end<T>(
967979
&mut self,
968980
ket: &TokenKind,
969981
sep: SeqSep,
970982
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
971-
) -> PResult<'a, (ThinVec<T>, bool, bool)> {
983+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, bool /* recovered */)> {
972984
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
973985
}
974986

975-
/// Parses a sequence, including the closing delimiter. The function
987+
/// Parses a sequence, including only the closing delimiter. The function
976988
/// `f` must consume tokens until reaching the next separator or
977989
/// closing bracket.
978990
fn parse_seq_to_end<T>(
@@ -988,7 +1000,7 @@ impl<'a> Parser<'a> {
9881000
Ok((val, trailing))
9891001
}
9901002

991-
/// Parses a sequence, including the closing delimiter. The function
1003+
/// Parses a sequence, including both delimiters. The function
9921004
/// `f` must consume tokens until reaching the next separator or
9931005
/// closing bracket.
9941006
fn parse_unspanned_seq<T>(
@@ -997,16 +1009,19 @@ impl<'a> Parser<'a> {
9971009
ket: &TokenKind,
9981010
sep: SeqSep,
9991011
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1000-
) -> PResult<'a, (ThinVec<T>, bool)> {
1012+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10011013
self.expect(bra)?;
10021014
self.parse_seq_to_end(ket, sep, f)
10031015
}
10041016

1017+
/// Parses a comma-separated sequence, including both delimiters.
1018+
/// The function `f` must consume tokens until reaching the next separator or
1019+
/// closing bracket.
10051020
fn parse_delim_comma_seq<T>(
10061021
&mut self,
10071022
delim: Delimiter,
10081023
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1009-
) -> PResult<'a, (ThinVec<T>, bool)> {
1024+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10101025
self.parse_unspanned_seq(
10111026
&token::OpenDelim(delim),
10121027
&token::CloseDelim(delim),
@@ -1015,10 +1030,13 @@ impl<'a> Parser<'a> {
10151030
)
10161031
}
10171032

1033+
/// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1034+
/// The function `f` must consume tokens until reaching the next separator or
1035+
/// closing bracket.
10181036
fn parse_paren_comma_seq<T>(
10191037
&mut self,
10201038
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1021-
) -> PResult<'a, (ThinVec<T>, bool)> {
1039+
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
10221040
self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
10231041
}
10241042

0 commit comments

Comments
 (0)