From 223554d49d32d5164a3d01be15b468ed38f914ec Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Tue, 24 Dec 2024 12:54:01 +0100 Subject: [PATCH] Clean up some clippy lints --- onihime/src/lexer/mod.rs | 6 ------ onihime/src/parser/ast.rs | 20 +++----------------- onihime/src/parser/mod.rs | 9 +++------ onihime/src/span.rs | 18 ++++++++++++++---- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/onihime/src/lexer/mod.rs b/onihime/src/lexer/mod.rs index fccfd94..32fcad9 100644 --- a/onihime/src/lexer/mod.rs +++ b/onihime/src/lexer/mod.rs @@ -110,7 +110,6 @@ impl<'lexer> Lexer<'lexer> { } /// Read the next token from the input. - #[must_use] pub(crate) fn read(&mut self) -> Result, LexerError> { // Eat whitespace until we encounter a meaningful character, or simply return if // we have reached the end of input and no additional characters can be read: @@ -226,7 +225,6 @@ impl<'lexer> Lexer<'lexer> { TokenKind::BlockComment(comment.trim().into()) } - #[must_use] fn float_literal(&self, word: String, span: Span) -> Result { let float = word.parse().map_err(|_| { LexerError::new(LexerErrorKind::InvalidNumber(word), span.join(&self.span())) @@ -235,7 +233,6 @@ impl<'lexer> Lexer<'lexer> { Ok(TokenKind::Float(float)) } - #[must_use] fn integer_literal( &self, word: String, @@ -256,7 +253,6 @@ impl<'lexer> Lexer<'lexer> { Ok(TokenKind::Integer(integer)) } - #[must_use] fn numeric_literal(&mut self, span: Span) -> Result { let word = self.read_word(); @@ -269,7 +265,6 @@ impl<'lexer> Lexer<'lexer> { Ok(kind) } - #[must_use] fn char_literal(&mut self, span: Span) -> Result { self.advance(); // '\'' @@ -314,7 +309,6 @@ impl<'lexer> Lexer<'lexer> { Ok(TokenKind::Char(c)) } - #[must_use] fn string_literal(&mut self, span: Span) -> Result { self.advance(); // '"' diff --git a/onihime/src/parser/ast.rs b/onihime/src/parser/ast.rs index a1ff399..82a326d 100644 --- a/onihime/src/parser/ast.rs +++ b/onihime/src/parser/ast.rs @@ -28,7 +28,7 @@ impl From> for Ast { impl std::fmt::Display for Ast { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for node in &self.root { - write!(f, "{node}\n")?; + writeln!(f, "{node}")?; } Ok(()) @@ -63,14 +63,7 @@ impl Node { Expr::List(vec) | Expr::Map(vec) | Expr::Set(vec) | Expr::Vector(vec) => { vec.push(child); } - _ => { - // FIXME - // return Err(ParserError::new( - // ParserErrorKind::UnexpectedState, - // child.span, - // )) - todo!() - } + _ => unimplemented!(), } Ok(()) @@ -203,14 +196,7 @@ impl TryFrom for Expr { TokenKind::String(s) => Atom::String(s), TokenKind::Symbol(s) => Atom::Symbol(s), TokenKind::Nil => Atom::Nil, - _ => { - // FIXME - // return Err(ParserError::new( - // ParserErrorKind::UnexpectedState, - // token.span, - // )) - todo!() - } + _ => unimplemented!(), }; Ok(kind.into()) diff --git a/onihime/src/parser/mod.rs b/onihime/src/parser/mod.rs index 5defeb0..4fc5ad2 100644 --- a/onihime/src/parser/mod.rs +++ b/onihime/src/parser/mod.rs @@ -1,6 +1,7 @@ +pub(crate) use self::{ast::Ast, error::ParserError}; use self::{ - ast::{Ast, Expr, Node}, - error::{ParserError, ParserErrorKind}, + ast::{Expr, Node}, + error::ParserErrorKind, }; use crate::{ lexer::{Lexer, TokenKind}, @@ -36,7 +37,6 @@ impl<'parser> Parser<'parser> { } /// Produce an Abstract Syntax Tree (AST) from the source input. - #[must_use] pub(crate) fn parse(mut self) -> Result { // This parser is actually quite simple!Recursively parse expressions until we // run out of tokens, or an error occurs: @@ -65,7 +65,6 @@ impl<'parser> Parser<'parser> { } } - #[must_use] fn expr(&mut self) -> Result, ParserError> { if let Some(token) = self.lexer.read()? { match token.kind { @@ -103,7 +102,6 @@ impl<'parser> Parser<'parser> { } } - #[must_use] fn begin_sequence( &mut self, init: impl FnOnce(Vec) -> Expr, @@ -117,7 +115,6 @@ impl<'parser> Parser<'parser> { Ok(None) } - #[must_use] fn end_sequence(&mut self, kind: TokenKind, span: Span) -> Result, ParserError> { // We will ultimately return the current expression, so clone it and update its // span first: diff --git a/onihime/src/span.rs b/onihime/src/span.rs index 501f0a4..c2b7cf5 100644 --- a/onihime/src/span.rs +++ b/onihime/src/span.rs @@ -2,7 +2,7 @@ use std::{cmp::Ordering, iter, ops::Range, sync::Arc}; /// A location within some source text. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct Location { +pub struct Location { line: usize, column: usize, } @@ -26,7 +26,7 @@ impl PartialOrd for Location { /// Some (optionally named) source text. #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub(crate) struct Source { +pub struct Source { name: Option, contents: String, lines: Vec, @@ -87,8 +87,8 @@ impl Source { } /// A contiguous sequence of bytes within some source. -#[derive(Debug, Default, Clone, Eq, Hash)] -pub(crate) struct Span { +#[derive(Debug, Default, Clone, Eq)] +pub struct Span { bytes: Range, source: Arc, } @@ -144,6 +144,16 @@ impl PartialEq for Span { } } +impl std::hash::Hash for Span { + fn hash(&self, state: &mut H) + where + H: std::hash::Hasher, + { + self.bytes.hash(state); + self.source.hash(state); + } +} + #[cfg(test)] mod tests { use super::*;