1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::{
fmt,
ops::{Add, AddAssign, Sub, SubAssign},
};
macro_rules! impl_pos {
(
$(
$(#[$attr:meta])*
$vis:vis struct $ident:ident($inner_vis:vis $inner_ty:ty);
)*
) => {
$(
$(#[$attr])*
$vis struct $ident($inner_vis $inner_ty);
impl fmt::Debug for $ident {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({})", stringify!($ident), self.0)
}
}
impl $ident {
#[inline(always)]
pub fn from_u32(n: u32) -> Self {
Self(n as $inner_ty)
}
#[inline(always)]
pub fn from_usize(n: usize) -> Self {
Self(n as $inner_ty)
}
#[inline(always)]
pub fn to_u32(self) -> u32 {
self.0 as u32
}
#[inline(always)]
pub fn to_usize(self) -> usize {
self.0 as usize
}
}
impl Add for $ident {
type Output = Self;
#[inline(always)]
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl Add<$inner_ty> for $ident {
type Output = Self;
#[inline(always)]
fn add(self, rhs: $inner_ty) -> Self {
Self(self.0 + rhs)
}
}
impl AddAssign for $ident {
#[inline(always)]
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0);
}
}
impl AddAssign<$inner_ty> for $ident {
#[inline(always)]
fn add_assign(&mut self, rhs: $inner_ty) {
self.0 += rhs;
}
}
impl Sub for $ident {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl Sub<$inner_ty> for $ident {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: $inner_ty) -> Self {
Self(self.0 - rhs)
}
}
impl SubAssign for $ident {
#[inline(always)]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl SubAssign<$inner_ty> for $ident {
#[inline(always)]
fn sub_assign(&mut self, rhs: $inner_ty) {
self.0 -= rhs;
}
}
)*
};
}
impl_pos! {
/// A byte offset relative to the global source map.
///
/// See [`Span`](crate::Span) for more information.
// Keep this small (currently 32-bits), as AST contains a lot of them.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BytePos(pub u32);
/// A byte offset relative to file beginning.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RelativeBytePos(pub u32);
/// A character offset.
///
/// Because of multibyte UTF-8 characters, a byte offset
/// is not equivalent to a character offset. The [`SourceMap`](crate::SourceMap) will convert
/// [`BytePos`] values to [`CharPos`] values as necessary.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CharPos(pub usize);
}