1
0
Fork 0

chore(trixy-lang_parser): Apply clippy's suggestions

This commit is contained in:
Benedikt Peetz 2023-12-22 20:56:36 +01:00
parent 26e0bbb972
commit d95b26655f
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
11 changed files with 36 additions and 44 deletions

View File

@ -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,

View File

@ -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
}
}
}

View File

@ -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)

View File

@ -27,7 +27,7 @@ impl<'a> Tokenizer<'a> {
pub(super) fn next_token(&mut self) -> Result<Option<Token>, 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))
}
}

View File

@ -9,7 +9,10 @@ pub mod error;
pub mod lexing;
pub mod parsing;
pub fn parse_trixy_lang(input: &str) -> Result<CommandSpec, TrixyError> {
let input_tokens = TokenStream::lex(input)?.parse()?;
pub fn parse_trixy_lang(input: &str) -> Result<CommandSpec, Box<TrixyError>> {
let input_tokens = TokenStream::lex(input)
.map_err(|err| Box::new(err.into()))?
.parse()
.map_err(Into::<TrixyError>::into)?;
Ok(input_tokens)
}

View File

@ -37,7 +37,7 @@ impl AdditionalHelp for ParsingError {
#[derive(Debug)]
pub struct SpannedParsingError {
pub source: ParsingError,
pub source: Box<ParsingError>,
pub context: ErrorContext,
}

View File

@ -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),
}
})?;

View File

@ -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,

View File

@ -68,7 +68,7 @@ impl AdditionalHelp for ParsingError {
#[derive(Debug, Clone)]
pub struct SpannedParsingError {
pub source: ParsingError,
pub source: Box<ParsingError>,
pub context: ErrorContext,
}

View File

@ -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

View File

@ -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"),
}