From af0f5fe507aa6abe64b2ef57049a3256c77bdefb Mon Sep 17 00:00:00 2001 From: hexaredecimal Date: Sat, 6 Apr 2024 19:21:51 +0200 Subject: [PATCH] parser+lexer: Rust fmt formatted the code --- src/lexer/display.rs | 17 ++++++++--------- src/parser/parser.rs | 15 +++++++++++---- src/parser/rules.rs | 18 ++++++++++++------ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/lexer/display.rs b/src/lexer/display.rs index c7d171ad..d48b760e 100644 --- a/src/lexer/display.rs +++ b/src/lexer/display.rs @@ -1,8 +1,9 @@ - use crate::lexer::{Keyword, TokenKind, Value}; + +use crate::lexer::{Keyword, TokenKind, Value}; impl std::fmt::Display for Keyword { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { Keyword::Let => write!(f, "let"), Keyword::If => write!(f, "if"), Keyword::Else => write!(f, "else"), @@ -20,16 +21,16 @@ impl std::fmt::Display for Keyword { Keyword::Import => write!(f, "import"), Keyword::Selff => write!(f, "self"), // "self" Keyword::Unknown => write!(f, "unknown"), - } - } - } + } + } +} impl std::fmt::Display for TokenKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { TokenKind::Whitespace => write!(f, "whitespace"), TokenKind::CarriageReturn => write!(f, "\\n"), - TokenKind::Identifier(id) => write!(f, "{id}"), + TokenKind::Identifier(id) => write!(f, "{id}"), TokenKind::Literal(value) => write!(f, "{value}"), TokenKind::Keyword(keyword) => write!(f, "{keyword}"), TokenKind::Comment => write!(f, "comment"), @@ -69,7 +70,6 @@ impl std::fmt::Display for TokenKind { } } - impl std::fmt::Display for Value { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -78,4 +78,3 @@ impl std::fmt::Display for Value { } } } - diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 9d07da53..ac309798 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -102,8 +102,12 @@ impl Parser { match &token.kind { TokenKind::Keyword(ref k) if k == &keyword => Ok(()), _ => { - let mut error = self.make_error_msg(token.pos, format!("Expected keyword, found {}", token.raw)); - let hint = self.make_hint_msg(format!("replace the symbol `{}` with the appropriate keyword. ", token.raw)); + let mut error = self + .make_error_msg(token.pos, format!("Expected keyword, found {}", token.raw)); + let hint = self.make_hint_msg(format!( + "replace the symbol `{}` with the appropriate keyword. ", + token.raw + )); error.push_str(&hint); Err(error) } @@ -119,8 +123,11 @@ impl Parser { match &token.kind { TokenKind::Identifier(n) => Ok(n.to_string()), other => { - let mut error = self.make_error_msg(token.pos, format!("Expected Identifier, found `{other}`",)); - let hint = self.make_hint_msg(format!("replace the symbol `{other}` with an identifier. Example `Foo`")); + let mut error = self + .make_error_msg(token.pos, format!("Expected Identifier, found `{other}`",)); + let hint = self.make_hint_msg(format!( + "replace the symbol `{other}` with an identifier. Example `Foo`" + )); error.push_str(&hint); Err(error) } diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 9b0bb799..7b77e1e1 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -69,10 +69,12 @@ impl Parser { } TokenKind::Identifier(_) => fields.push(self.parse_typed_variable()?), _ => { - let mut error = self.make_error_msg(next.pos, "Expected struct field or method".into()); - let hint = self.make_hint_msg(format!("remove the following symbol `{}`", next.raw)); + let mut error = + self.make_error_msg(next.pos, "Expected struct field or method".into()); + let hint = + self.make_hint_msg(format!("remove the following symbol `{}`", next.raw)); error.push_str(&hint); - return Err(error) + return Err(error); } } } @@ -473,10 +475,14 @@ impl Parser { let last = self.peek()?; if last.kind != TokenKind::CurlyBracesClose { - let mut error = self.make_error_msg(last.pos, "Expected a struct field initialization or a closing curly brace (`}`)".into()); - let hint = self.make_hint_msg(format!("remove the following symbol `{}`", last.raw)); + let mut error = self.make_error_msg( + last.pos, + "Expected a struct field initialization or a closing curly brace (`}`)".into(), + ); + let hint = + self.make_hint_msg(format!("remove the following symbol `{}`", last.raw)); error.push_str(&hint); - return Err(error) + return Err(error); } }