util: Only contains Span, so rename module to span.rs

This commit is contained in:
John 2024-02-01 11:59:21 -06:00
parent 11bae9b348
commit a63a4b7ece
8 changed files with 52 additions and 52 deletions

View File

@ -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<T> = Result<T, Error>;

View File

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

View File

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

View File

@ -54,50 +54,7 @@
//! └─ EndOfFile
//! ```
pub mod util {
use std::{
fmt::{Debug, Display},
ops::{Index, Range},
};
/// A <code> [Clone] + [Copy] + [!Iterator](Iterator) <\code> version of a [Range]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span<Idx> {
pub start: Idx,
pub end: Idx,
}
impl<Idx> From<Span<Idx>> for Range<Idx> {
fn from(value: Span<Idx>) -> Self {
value.start..value.end
}
}
impl<Idx> From<Range<Idx>> for Span<Idx> {
fn from(value: Range<Idx>) -> Self {
Self { start: value.start, end: value.end }
}
}
impl<T> Index<Span<usize>> for [T] {
type Output = [T];
fn index(&self, index: Span<usize>) -> &Self::Output {
self.index(Range::from(index))
}
}
impl Index<Span<usize>> for str {
type Output = str;
fn index(&self, index: Span<usize>) -> &Self::Output {
self.index(Range::from(index))
}
}
impl<Idx: Debug> Debug for Span<Idx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}..{:?}", self.start, self.end)
}
}
impl<Idx: Display> Display for Span<Idx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}
}
pub mod span;
pub mod lexer;

View File

@ -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<T> = Result<T, Error>;

View File

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

View File

@ -5,7 +5,7 @@ use crate::{
token::{Token, TokenKind as Kind},
Lexer,
},
util::Span,
span::Span,
};
use std::collections::{HashMap, VecDeque};

43
src/span.rs Normal file
View File

@ -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 <code> [Clone] + [Copy] + [!Iterator](Iterator) </code> version of a [Range]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span<Idx> {
pub start: Idx,
pub end: Idx,
}
impl<Idx> From<Span<Idx>> for Range<Idx> {
fn from(value: Span<Idx>) -> Self {
value.start..value.end
}
}
impl<Idx> From<Range<Idx>> for Span<Idx> {
fn from(value: Range<Idx>) -> Self {
Self { start: value.start, end: value.end }
}
}
impl<T> Index<Span<usize>> for [T] {
type Output = [T];
fn index(&self, index: Span<usize>) -> &Self::Output {
self.index(Range::from(index))
}
}
impl Index<Span<usize>> for str {
type Output = str;
fn index(&self, index: Span<usize>) -> &Self::Output {
self.index(Range::from(index))
}
}
impl<Idx: Debug> Debug for Span<Idx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}..{:?}", self.start, self.end)
}
}
impl<Idx: Display> Display for Span<Idx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}