From f00302c8afbe58669efdb67c529607ecd894f2db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:41:24 +0200 Subject: [PATCH] Fix unexpected whitespace in condition error --- src/lexical/token_stream.rs | 22 ++++++++++++++++++++++ src/syntax/parser.rs | 14 +++----------- src/syntax/syntax_tree/condition.rs | 6 +++++- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/lexical/token_stream.rs b/src/lexical/token_stream.rs index cb5189b..bc2cb9b 100644 --- a/src/lexical/token_stream.rs +++ b/src/lexical/token_stream.rs @@ -200,6 +200,28 @@ pub enum Delimiter { Bracket, } +impl Delimiter { + /// Returns the opening delimiter. + #[must_use] + pub fn opening_char(&self) -> char { + match self { + Self::Parenthesis => '(', + Self::Brace => '{', + Self::Bracket => '[', + } + } + + /// Returns the closing delimiter. + #[must_use] + pub fn closing_char(&self) -> char { + match self { + Self::Parenthesis => ')', + Self::Brace => '}', + Self::Bracket => ']', + } + } +} + /// Represents a list of tokens enclosed by a pair of delimiters. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index 6ae3108..6e3f1c4 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -54,11 +54,7 @@ impl<'a> Parser<'a> { // move after the whole delimited list self.current_frame.forward(); - let expected = match delimiter { - Delimiter::Parenthesis => '(', - Delimiter::Brace => '{', - Delimiter::Bracket => '[', - }; + let expected = delimiter.opening_char(); let delimited_stream = if let Some(token_tree) = raw_token_tree { match token_tree { @@ -107,17 +103,13 @@ impl<'a> Parser<'a> { // the current frame must be at the end if !self.current_frame.is_exhausted() { - let expected = match self + let expected = self .current_frame .token_provider .as_delimited() .unwrap() .delimiter - { - Delimiter::Parenthesis => ')', - Delimiter::Brace => '}', - Delimiter::Bracket => ']', - }; + .closing_char(); handler.receive(Error::UnexpectedSyntax(UnexpectedSyntax { expected: SyntaxKind::Punctuation(expected), diff --git a/src/syntax/syntax_tree/condition.rs b/src/syntax/syntax_tree/condition.rs index 8a7f41a..c0bfd99 100644 --- a/src/syntax/syntax_tree/condition.rs +++ b/src/syntax/syntax_tree/condition.rs @@ -358,7 +358,11 @@ impl<'a> Parser<'a> { ) -> Option { let token_tree = self.step_into( Delimiter::Parenthesis, - |parser| parser.parse_condition(handler), + |parser| { + let cond = parser.parse_condition(handler)?; + parser.stop_at_significant(); + Some(cond) + }, handler, )?;