Skip to content

Commit b6efd14

Browse files
alambserprex
authored andcommitted
Improve documentation on Parser::consume_token and friends (apache#994)
1 parent 07e867c commit b6efd14

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/parser/mod.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -2350,15 +2350,16 @@ impl<'a> Parser<'a> {
23502350
}
23512351
}
23522352

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

2361-
/// Look for an expected keyword and consume it if it exists
2361+
/// If the current token is the `expected` keyword, consume it and returns
2362+
/// true. Otherwise, no tokens are consumed and returns false.
23622363
#[must_use]
23632364
pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
23642365
match self.peek_token().token {
@@ -2370,7 +2371,9 @@ impl<'a> Parser<'a> {
23702371
}
23712372
}
23722373

2373-
/// Look for an expected sequence of keywords and consume them if they exist
2374+
/// If the current and subsequent tokens exactly match the `keywords`
2375+
/// sequence, consume them and returns true. Otherwise, no tokens are
2376+
/// consumed and returns false
23742377
#[must_use]
23752378
pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
23762379
let index = self.index;
@@ -2385,7 +2388,9 @@ impl<'a> Parser<'a> {
23852388
true
23862389
}
23872390

2388-
/// Look for one of the given keywords and return the one that matches.
2391+
/// If the current token is one of the given `keywords`, consume the token
2392+
/// and return the keyword that matches. Otherwise, no tokens are consumed
2393+
/// and returns `None`.
23892394
#[must_use]
23902395
pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
23912396
match self.peek_token().token {
@@ -2402,7 +2407,8 @@ impl<'a> Parser<'a> {
24022407
}
24032408
}
24042409

2405-
/// Bail out if the current token is not one of the expected keywords, or consume it if it is
2410+
/// If the current token is one of the expected keywords, consume the token
2411+
/// and return the keyword that matches. Otherwise, return an error.
24062412
pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
24072413
if let Some(keyword) = self.parse_one_of_keywords(keywords) {
24082414
Ok(keyword)
@@ -2415,7 +2421,8 @@ impl<'a> Parser<'a> {
24152421
}
24162422
}
24172423

2418-
/// Bail out if the current token is not an expected keyword, or consume it if it is
2424+
/// If the current token is the `expected` keyword, consume the token.
2425+
/// Otherwise return an error.
24192426
pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError> {
24202427
if self.parse_keyword(expected) {
24212428
Ok(())
@@ -2424,8 +2431,8 @@ impl<'a> Parser<'a> {
24242431
}
24252432
}
24262433

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

0 commit comments

Comments
 (0)