Skip to content

Commit 8262abc

Browse files
authored
Improve documentation on Parser::consume_token and friends (#994)
1 parent c5a7d6c commit 8262abc

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/parser/mod.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,15 +2346,16 @@ impl<'a> Parser<'a> {
23462346
}
23472347
}
23482348

2349-
/// Report unexpected token
2349+
/// Report `found` was encountered instead of `expected`
23502350
pub fn expected<T>(&self, expected: &str, found: TokenWithLocation) -> Result<T, ParserError> {
23512351
parser_err!(
23522352
format!("Expected {expected}, found: {found}"),
23532353
found.location
23542354
)
23552355
}
23562356

2357-
/// Look for an expected keyword and consume it if it exists
2357+
/// If the current token is the `expected` keyword, consume it and returns
2358+
/// true. Otherwise, no tokens are consumed and returns false.
23582359
#[must_use]
23592360
pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
23602361
match self.peek_token().token {
@@ -2366,7 +2367,9 @@ impl<'a> Parser<'a> {
23662367
}
23672368
}
23682369

2369-
/// Look for an expected sequence of keywords and consume them if they exist
2370+
/// If the current and subsequent tokens exactly match the `keywords`
2371+
/// sequence, consume them and returns true. Otherwise, no tokens are
2372+
/// consumed and returns false
23702373
#[must_use]
23712374
pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
23722375
let index = self.index;
@@ -2381,7 +2384,9 @@ impl<'a> Parser<'a> {
23812384
true
23822385
}
23832386

2384-
/// Look for one of the given keywords and return the one that matches.
2387+
/// If the current token is one of the given `keywords`, consume the token
2388+
/// and return the keyword that matches. Otherwise, no tokens are consumed
2389+
/// and returns `None`.
23852390
#[must_use]
23862391
pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
23872392
match self.peek_token().token {
@@ -2398,7 +2403,8 @@ impl<'a> Parser<'a> {
23982403
}
23992404
}
24002405

2401-
/// Bail out if the current token is not one of the expected keywords, or consume it if it is
2406+
/// If the current token is one of the expected keywords, consume the token
2407+
/// and return the keyword that matches. Otherwise, return an error.
24022408
pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
24032409
if let Some(keyword) = self.parse_one_of_keywords(keywords) {
24042410
Ok(keyword)
@@ -2411,7 +2417,8 @@ impl<'a> Parser<'a> {
24112417
}
24122418
}
24132419

2414-
/// Bail out if the current token is not an expected keyword, or consume it if it is
2420+
/// If the current token is the `expected` keyword, consume the token.
2421+
/// Otherwise return an error.
24152422
pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError> {
24162423
if self.parse_keyword(expected) {
24172424
Ok(())
@@ -2420,8 +2427,8 @@ impl<'a> Parser<'a> {
24202427
}
24212428
}
24222429

2423-
/// Bail out if the following tokens are not the expected sequence of
2424-
/// keywords, or consume them if they are.
2430+
/// If the current and subsequent tokens exactly match the `keywords`
2431+
/// sequence, consume them and returns Ok. Otherwise, return an Error.
24252432
pub fn expect_keywords(&mut self, expected: &[Keyword]) -> Result<(), ParserError> {
24262433
for &kw in expected {
24272434
self.expect_keyword(kw)?;

0 commit comments

Comments
 (0)