From a63a4b7ececd588d541fe1dcb27255dad06f12a9 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Thu, 1 Feb 2024 11:59:21 -0600 Subject: [PATCH] util: Only contains `Span`, so rename module to `span.rs` --- src/assembler.rs | 4 ++-- src/lexer.rs | 2 +- src/lexer/token.rs | 2 +- src/lib.rs | 45 +-------------------------------------------- src/parser.rs | 4 ++-- src/parser/ast.rs | 2 +- src/preprocessor.rs | 2 +- src/span.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 src/span.rs diff --git a/src/assembler.rs b/src/assembler.rs index 22aef2f..28e3170 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -3,7 +3,7 @@ use error::{AResult, ErrorKind::*}; use std::collections::HashMap; -use crate::{assembler::canonical::Canonicalize, lexer::token, parser::ast::*, util::Span}; +use crate::{assembler::canonical::Canonicalize, lexer::token, parser::ast::*, span::Span}; use self::error::{Error, ErrorKind}; @@ -366,7 +366,7 @@ impl<'t> Assembler<'t> { pub mod error { use std::fmt::Display; - use crate::util::Span; + use crate::span::Span; pub type AResult = Result; diff --git a/src/lexer.rs b/src/lexer.rs index c2a8edc..a4c1290 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -5,7 +5,7 @@ pub mod token; use self::token::{Special, TokenKind, *}; -use crate::util::Span; +use crate::span::Span; use std::{ iter::Peekable, str::{CharIndices, FromStr}, diff --git a/src/lexer/token.rs b/src/lexer/token.rs index a467f68..119afb6 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -2,7 +2,7 @@ //! A [Token] is a [semantically-tagged](TokenKind) [sequence of characters](str) and a [Span] //! //! [Tokens](Token) are a borrowed, and cannot outlive their source slice (lifetime `'t`) -use crate::util::Span; +use crate::span::Span; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Token<'t> { pub lexeme: &'t str, diff --git a/src/lib.rs b/src/lib.rs index 63a4e5a..615d18f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,50 +54,7 @@ //! └─ EndOfFile //! ``` -pub mod util { - use std::{ - fmt::{Debug, Display}, - ops::{Index, Range}, - }; - /// A [Clone] + [Copy] + [!Iterator](Iterator) <\code> version of a [Range] - #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct Span { - pub start: Idx, - pub end: Idx, - } - impl From> for Range { - fn from(value: Span) -> Self { - value.start..value.end - } - } - impl From> for Span { - fn from(value: Range) -> Self { - Self { start: value.start, end: value.end } - } - } - impl Index> for [T] { - type Output = [T]; - fn index(&self, index: Span) -> &Self::Output { - self.index(Range::from(index)) - } - } - impl Index> for str { - type Output = str; - fn index(&self, index: Span) -> &Self::Output { - self.index(Range::from(index)) - } - } - impl Debug for Span { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}..{:?}", self.start, self.end) - } - } - impl Display for Span { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}..{}", self.start, self.end) - } - } -} +pub mod span; pub mod lexer; diff --git a/src/parser.rs b/src/parser.rs index e9c5643..451487e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -13,7 +13,7 @@ use crate::{ Lexer, }, preprocessor::Preprocessor, - util::Span, + span::Span, }; use ast::*; @@ -497,7 +497,7 @@ impl<'t> Parser<'t> { pub mod error { use super::Kind; - use crate::util::Span; + use crate::span::Span; use std::{fmt::Display, num::TryFromIntError}; pub type PResult = Result; diff --git a/src/parser/ast.rs b/src/parser/ast.rs index a798dd1..8337bb5 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -2,7 +2,7 @@ /// Represents MSP430 instructions, use crate::{ lexer::token::{self, Reg, Token}, - util::Span, + span::Span, }; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] diff --git a/src/preprocessor.rs b/src/preprocessor.rs index fd624b9..9df3bfe 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -5,7 +5,7 @@ use crate::{ token::{Token, TokenKind as Kind}, Lexer, }, - util::Span, + span::Span, }; use std::collections::{HashMap, VecDeque}; diff --git a/src/span.rs b/src/span.rs new file mode 100644 index 0000000..4ca993b --- /dev/null +++ b/src/span.rs @@ -0,0 +1,43 @@ +//! A [Span] is a [Range] that does not implement [Iterator]. It is a [Copy] type. +use std::{ + fmt::{Debug, Display}, + ops::{Index, Range}, +}; +/// A [Clone] + [Copy] + [!Iterator](Iterator) version of a [Range] +#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Span { + pub start: Idx, + pub end: Idx, +} +impl From> for Range { + fn from(value: Span) -> Self { + value.start..value.end + } +} +impl From> for Span { + fn from(value: Range) -> Self { + Self { start: value.start, end: value.end } + } +} +impl Index> for [T] { + type Output = [T]; + fn index(&self, index: Span) -> &Self::Output { + self.index(Range::from(index)) + } +} +impl Index> for str { + type Output = str; + fn index(&self, index: Span) -> &Self::Output { + self.index(Range::from(index)) + } +} +impl Debug for Span { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}..{:?}", self.start, self.end) + } +} +impl Display for Span { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}..{}", self.start, self.end) + } +}