Implement custom, Copy span
This commit is contained in:
parent
2ad7be8bc6
commit
b24f218e0b
2 changed files with 67 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ use clap::Parser;
|
|||
mod ast;
|
||||
mod builtin;
|
||||
mod parser;
|
||||
mod span;
|
||||
mod table;
|
||||
mod value;
|
||||
|
||||
|
|
|
|||
66
src/span.rs
Normal file
66
src/span.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use std::fmt;
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Span {
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub fn new(start: usize, end: usize) -> Self {
|
||||
assert!(start <= end, "start must be less than or equal to end");
|
||||
Self { start, end }
|
||||
}
|
||||
|
||||
pub fn range(self) -> Range<usize> {
|
||||
self.start..self.end
|
||||
}
|
||||
|
||||
pub fn join(self, other: Self) -> Self {
|
||||
let start = self.start.min(other.start);
|
||||
let end = self.end.max(other.end);
|
||||
Self::new(start, end)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Span {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.range().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Span> for Range<usize> {
|
||||
fn from(span: Span) -> Self {
|
||||
span.range()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasSpan {
|
||||
fn span(&self) -> Span;
|
||||
}
|
||||
|
||||
impl<T> HasSpan for (T, Span) {
|
||||
fn span(&self) -> Span {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
|
||||
impl chumsky::Span for Span {
|
||||
type Context = ();
|
||||
type Offset = usize;
|
||||
|
||||
fn new(_: Self::Context, range: Range<Self::Offset>) -> Self {
|
||||
Self::new(range.start, range.end)
|
||||
}
|
||||
|
||||
fn context(&self) -> Self::Context {}
|
||||
|
||||
fn start(&self) -> Self::Offset {
|
||||
self.start
|
||||
}
|
||||
|
||||
fn end(&self) -> Self::Offset {
|
||||
self.end
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue