1use std::{fmt, ops::Range};
2
3pub mod error;
4pub mod evaluate;
5pub mod expression;
6pub mod operator;
7pub mod parser;
8pub mod token;
9
10pub use error::{ParseError, ParseErrorKind, ParseErrors};
11
12#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
13pub struct Span<T> {
14 pub start: T,
15 pub end: T,
16}
17
18impl<T> Span<T> {
19 pub fn new(range: Range<T>) -> Self {
20 Self {
21 start: range.start,
22 end: range.end,
23 }
24 }
25
26 pub fn join(self, other: Self) -> Self
28 where
29 T: Ord,
30 {
31 Span {
32 start: self.start.min(other.start),
33 end: self.end.max(other.end),
34 }
35 }
36
37 pub fn into_range(self) -> Range<T> {
38 self.into()
39 }
40
41 pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Span<U> {
42 Span {
43 start: f(self.start),
44 end: f(self.end),
45 }
46 }
47}
48
49impl<T> From<Span<T>> for Range<T> {
50 fn from(span: Span<T>) -> Self {
51 span.start..span.end
52 }
53}
54
55impl<T> From<Range<T>> for Span<T> {
56 fn from(range: Range<T>) -> Self {
57 Self::new(range)
58 }
59}
60
61impl<T: fmt::Display> fmt::Display for Span<T> {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 write!(f, "{}..{}", self.start, self.end)
64 }
65}