From d95b26655f190d7f1cbf05459bf9ae94fd65d5b4 Mon Sep 17 00:00:00 2001 From: Soispha Date: Fri, 22 Dec 2023 20:56:36 +0100 Subject: [PATCH] chore(trixy-lang_parser): Apply clippy's suggestions --- trixy/trixy-lang_parser/src/error.rs | 24 +++++++++---------- trixy/trixy-lang_parser/src/lexing/error.rs | 5 ++-- trixy/trixy-lang_parser/src/lexing/mod.rs | 10 ++------ .../trixy-lang_parser/src/lexing/tokenizer.rs | 4 ++-- trixy/trixy-lang_parser/src/lib.rs | 7 ++++-- .../src/parsing/checked/error.rs | 2 +- .../src/parsing/checked/mod.rs | 4 ++-- .../src/parsing/checked/test.rs | 2 +- .../src/parsing/unchecked/error.rs | 2 +- .../src/parsing/unchecked/mod.rs | 18 +++++++------- .../src/parsing/unchecked/test.rs | 2 +- 11 files changed, 36 insertions(+), 44 deletions(-) diff --git a/trixy/trixy-lang_parser/src/error.rs b/trixy/trixy-lang_parser/src/error.rs index 7e9a92e..a98f518 100644 --- a/trixy/trixy-lang_parser/src/error.rs +++ b/trixy/trixy-lang_parser/src/error.rs @@ -51,7 +51,7 @@ impl ErrorContext { let contexted_span = { let matched_line: Vec<_> = original_file.match_indices(&line).collect(); - let (index, matched_line) = matched_line.get(0).expect("This first index should always match, as we took the line from the string in the first place"); + let (index, matched_line) = matched_line.first().expect("This first index should always match, as we took the line from the string in the first place"); debug_assert_eq!(matched_line, &&line); TokenSpan { start: span.start - index, @@ -59,27 +59,25 @@ impl ErrorContext { } }; - let line_above; - if line_number == 1 { + let line_above = if line_number == 1 { // We only have one line, so no line above - line_above = "".to_owned(); + "".to_owned() } else { - line_above = (*lines + (*lines .get((line_number - 1) - 1) .expect("We checked that this should work")) - .to_owned(); - } + .to_owned() + }; - let line_below; - if lines.len() - 1 > line_number { + let line_below = if lines.len() - 1 > line_number { // We have a line after the current line - line_below = (*lines + (*lines .get((line_number + 1) - 1) .expect("We checked that this should work")) - .to_owned(); + .to_owned() } else { - line_below = "".to_owned(); - } + "".to_owned() + }; Self { span, diff --git a/trixy/trixy-lang_parser/src/lexing/error.rs b/trixy/trixy-lang_parser/src/lexing/error.rs index fed9d09..ecaf92e 100644 --- a/trixy/trixy-lang_parser/src/lexing/error.rs +++ b/trixy/trixy-lang_parser/src/lexing/error.rs @@ -19,7 +19,7 @@ pub enum LexingError { impl AdditionalHelp for LexingError { fn additional_help(&self) -> String { - let out = match self { + match self { LexingError::NoMatchesTaken => "This token does not produce a possible match".to_owned(), LexingError::UnexpectedEOF => "This eof was completely unexpected".to_owned(), LexingError::ExpectedArrow => "The `-` token is interpretet as a started arrow (`->`), but we could not find the arrow tip (`>`)".to_owned(), @@ -27,8 +27,7 @@ impl AdditionalHelp for LexingError { format!("This char: `{char}`; is not a valid token") }, LexingError::ExpectedComment => "The '/' started comment parsing, but I could not find a matching '/'".to_owned(), - }; - out + } } } diff --git a/trixy/trixy-lang_parser/src/lexing/mod.rs b/trixy/trixy-lang_parser/src/lexing/mod.rs index cbaec89..77fd918 100644 --- a/trixy/trixy-lang_parser/src/lexing/mod.rs +++ b/trixy/trixy-lang_parser/src/lexing/mod.rs @@ -31,13 +31,7 @@ impl TokenStream { // filter out comments let tokens = tokens .into_iter() - .filter(|token| { - if let TokenKind::Comment(_) = token.kind { - false - } else { - true - } - }) + .filter(|token| !matches!(token.kind, TokenKind::Comment(_))) .collect(); Ok(Self { @@ -179,7 +173,7 @@ impl Display for TokenKind { match self { TokenKind::Keyword(word) => write!(f, "KEYWORD({})", word), TokenKind::Identifier(ident) => { - if ident == "" { + if ident.is_empty() { write!(f, "IDENTIFIER") } else { write!(f, "IDENTIFIER({})", ident) diff --git a/trixy/trixy-lang_parser/src/lexing/tokenizer.rs b/trixy/trixy-lang_parser/src/lexing/tokenizer.rs index c7a9882..6662f07 100644 --- a/trixy/trixy-lang_parser/src/lexing/tokenizer.rs +++ b/trixy/trixy-lang_parser/src/lexing/tokenizer.rs @@ -27,7 +27,7 @@ impl<'a> Tokenizer<'a> { pub(super) fn next_token(&mut self) -> Result, SpannedLexingError> { self.skip_ignored_tokens(); if self.remaining_text.is_empty() { - return Ok(None); + Ok(None) } else { let start = self.current_index; @@ -153,7 +153,7 @@ fn tokenize_comment(text: &str) -> Result<(TokenKind, usize), LexingError> { let comment = comment.trim_start(); let comment = comment.trim_end(); - return Ok((TokenKind::Comment(comment.to_owned()), chars_read + 2)); + Ok((TokenKind::Comment(comment.to_owned()), chars_read + 2)) } } diff --git a/trixy/trixy-lang_parser/src/lib.rs b/trixy/trixy-lang_parser/src/lib.rs index 406d554..65f69b3 100644 --- a/trixy/trixy-lang_parser/src/lib.rs +++ b/trixy/trixy-lang_parser/src/lib.rs @@ -9,7 +9,10 @@ pub mod error; pub mod lexing; pub mod parsing; -pub fn parse_trixy_lang(input: &str) -> Result { - let input_tokens = TokenStream::lex(input)?.parse()?; +pub fn parse_trixy_lang(input: &str) -> Result> { + let input_tokens = TokenStream::lex(input) + .map_err(|err| Box::new(err.into()))? + .parse() + .map_err(Into::::into)?; Ok(input_tokens) } diff --git a/trixy/trixy-lang_parser/src/parsing/checked/error.rs b/trixy/trixy-lang_parser/src/parsing/checked/error.rs index 51a5434..e088199 100644 --- a/trixy/trixy-lang_parser/src/parsing/checked/error.rs +++ b/trixy/trixy-lang_parser/src/parsing/checked/error.rs @@ -37,7 +37,7 @@ impl AdditionalHelp for ParsingError { #[derive(Debug)] pub struct SpannedParsingError { - pub source: ParsingError, + pub source: Box, pub context: ErrorContext, } diff --git a/trixy/trixy-lang_parser/src/parsing/checked/mod.rs b/trixy/trixy-lang_parser/src/parsing/checked/mod.rs index 841ba30..669fd1b 100644 --- a/trixy/trixy-lang_parser/src/parsing/checked/mod.rs +++ b/trixy/trixy-lang_parser/src/parsing/checked/mod.rs @@ -37,7 +37,7 @@ impl TokenStream { let unchecked = self.parse_unchecked().map_err(|err| { let span = *err.source.span(); SpannedParsingError { - source: ParsingError::from(err), + source: Box::new(ParsingError::from(err)), context: ErrorContext::from_span(span, &original_file), } })?; @@ -79,7 +79,7 @@ impl Parser { let namespace = self.process_namespace(namespace).map_err(|err| { let span = *err.span(); SpannedParsingError { - source: err, + source: Box::new(err), context: ErrorContext::from_span(span, &self.original_file), } })?; diff --git a/trixy/trixy-lang_parser/src/parsing/checked/test.rs b/trixy/trixy-lang_parser/src/parsing/checked/test.rs index 53e27a9..adf7a85 100644 --- a/trixy/trixy-lang_parser/src/parsing/checked/test.rs +++ b/trixy/trixy-lang_parser/src/parsing/checked/test.rs @@ -132,7 +132,7 @@ fn test_failing() { fn execute_callback(callback: Name); "; let output = TokenStream::lex(&input).unwrap().parse(); - match output.unwrap_err().source { + match *(output.unwrap_err().source) { super::error::ParsingError::TypeNotDeclared { r#type, .. } => { assert_eq!( r#type, diff --git a/trixy/trixy-lang_parser/src/parsing/unchecked/error.rs b/trixy/trixy-lang_parser/src/parsing/unchecked/error.rs index 5d5270c..f15c5d5 100644 --- a/trixy/trixy-lang_parser/src/parsing/unchecked/error.rs +++ b/trixy/trixy-lang_parser/src/parsing/unchecked/error.rs @@ -68,7 +68,7 @@ impl AdditionalHelp for ParsingError { #[derive(Debug, Clone)] pub struct SpannedParsingError { - pub source: ParsingError, + pub source: Box, pub context: ErrorContext, } diff --git a/trixy/trixy-lang_parser/src/parsing/unchecked/mod.rs b/trixy/trixy-lang_parser/src/parsing/unchecked/mod.rs index c6db5a2..b1175e0 100644 --- a/trixy/trixy-lang_parser/src/parsing/unchecked/mod.rs +++ b/trixy/trixy-lang_parser/src/parsing/unchecked/mod.rs @@ -45,7 +45,7 @@ impl Parser { let next = self.parse_next().map_err(|err| { let span = err.get_span(); SpannedParsingError { - source: err, + source: Box::new(err), context: ErrorContext::from_span(span, &self.token_stream.original_file), } })?; @@ -112,7 +112,7 @@ impl Parser { actual: self.peek_raw().kind().clone(), }; - return Err(err); + Err(err) } } } @@ -162,9 +162,11 @@ impl Parser { let attributes = self.parse_doc_comments()?; self.expect(token![nasp])?; - let mut namespace = Namespace::default(); - namespace.name = self.expect(token![Ident])?; - namespace.attributes = attributes; + let mut namespace = Namespace { + name: self.expect(token![Ident])?, + attributes, + ..Default::default() + }; self.expect(token![BraceOpen])?; @@ -345,11 +347,7 @@ impl Parser { Some(ok) => ok, None => return false, }; - if actual_token.kind().same_kind(&token) { - true - } else { - false - } + actual_token.kind().same_kind(&token) } /// Looks at the next token without removing it diff --git a/trixy/trixy-lang_parser/src/parsing/unchecked/test.rs b/trixy/trixy-lang_parser/src/parsing/unchecked/test.rs index ccf9b69..b5568fb 100644 --- a/trixy/trixy-lang_parser/src/parsing/unchecked/test.rs +++ b/trixy/trixy-lang_parser/src/parsing/unchecked/test.rs @@ -19,7 +19,7 @@ nasp trinitrix { {} "; let parsed = TokenStream::lex(input).unwrap().parse_unchecked(); let err = parsed.unwrap_err().source; - match err { + match *err { ParsingError::ExpectedKeyword { .. } => {} _ => panic!("Wrong error"), }