Skip to content

Commit

Permalink
Improve documentation on Parser::consume_token and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Oct 5, 2023
1 parent c811e22 commit 14d6453
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2262,15 +2262,16 @@ impl<'a> Parser<'a> {
}
}

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

/// Look for an expected keyword and consume it if it exists
/// If the current token is the `expected` keyword, consume it and returns
/// true. Otherwise, no tokens are consumed and returns false [must_use].

Check failure on line 2274 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / docs

unresolved link to `must_use`
#[must_use]
pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
match self.peek_token().token {
Expand All @@ -2282,7 +2283,9 @@ impl<'a> Parser<'a> {
}
}

/// Look for an expected sequence of keywords and consume them if they exist
/// If the current and subsequent tokens exactly match the `keywords`
/// sequence, consume them and returns true. Otherwise, no tokens are
/// consumed and returns false
#[must_use]
pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
let index = self.index;
Expand All @@ -2297,7 +2300,9 @@ impl<'a> Parser<'a> {
true
}

/// Look for one of the given keywords and return the one that matches.
/// If the current token is one of the given `keywords`, consume the token
/// and return the keyword that matches. Otherwise, no tokens are consumed
/// and returns `None`.
#[must_use]
pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
match self.peek_token().token {
Expand All @@ -2314,7 +2319,8 @@ impl<'a> Parser<'a> {
}
}

/// Bail out if the current token is not one of the expected keywords, or consume it if it is
/// If the current token is one of the expected keywords, consume the token
/// and return the keyword that matches. Otherwise, return an error.
pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
if let Some(keyword) = self.parse_one_of_keywords(keywords) {
Ok(keyword)
Expand All @@ -2327,7 +2333,8 @@ impl<'a> Parser<'a> {
}
}

/// Bail out if the current token is not an expected keyword, or consume it if it is
/// If the current token is the `expected` keyword, consume the token.
/// Otherwise return an error.
pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError> {
if self.parse_keyword(expected) {
Ok(())
Expand All @@ -2336,8 +2343,8 @@ impl<'a> Parser<'a> {
}
}

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

0 comments on commit 14d6453

Please sign in to comment.