From 14d6453472cde8fccc9649edd79bdc2d84c1c968 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 5 Oct 2023 15:13:02 -0400 Subject: [PATCH 1/2] Improve documentation on Parser::consume_token and friends --- src/parser/mod.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 5f6788696..d7ab603c7 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2262,7 +2262,7 @@ impl<'a> Parser<'a> { } } - /// Report unexpected token + /// Report `found` was encountered instead of `expected` pub fn expected(&self, expected: &str, found: TokenWithLocation) -> Result { parser_err!( format!("Expected {expected}, found: {found}"), @@ -2270,7 +2270,8 @@ impl<'a> Parser<'a> { ) } - /// 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]. #[must_use] pub fn parse_keyword(&mut self, expected: Keyword) -> bool { match self.peek_token().token { @@ -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; @@ -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 { match self.peek_token().token { @@ -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 { if let Some(keyword) = self.parse_one_of_keywords(keywords) { Ok(keyword) @@ -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(()) @@ -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)?; From a2c3fa43aaa0d076f0c4ba5ebd2e5ee2f48fd833 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 5 Oct 2023 15:32:25 -0400 Subject: [PATCH 2/2] fix docs --- src/parser/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f2e284eee..a40a3df1c 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2271,7 +2271,7 @@ impl<'a> Parser<'a> { } /// If the current token is the `expected` keyword, consume it and returns - /// true. Otherwise, no tokens are consumed and returns false [must_use]. + /// true. Otherwise, no tokens are consumed and returns false. #[must_use] pub fn parse_keyword(&mut self, expected: Keyword) -> bool { match self.peek_token().token {