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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! This module provides means of tracking location in a source code.

use crate::{source::Source, span::Span};
use std::{
    borrow::Borrow,
    cmp::Ordering,
    fmt,
    hash::{Hash, Hasher},
    ops::Deref,
};

/// The location in a source code.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Location {
    /// The source code object.
    source: Source,
    /// The string segment position.
    position: usize,
}

impl Location {
    /// Creates a new location given a source code object and a string segment
    /// position in the object.
    pub(super) fn new_unchecked(source: Source, position: usize) -> Self {
        Self { source, position }
    }

    /// Creates a new location given the source code object and position index.
    ///
    /// # Panics
    /// Panics if `position` is past beyond source length in number of segments.
    pub fn new(source: Source, position: usize) -> Self {
        if source.len() < position {
            panic!(
                "Location position is too big; availabe: {}, given: {}",
                source.len(),
                position,
            );
        }
        Self::new_unchecked(source, position)
    }

    /// This location's position in the source code in terms of grapheme
    /// clusters/segments.
    pub fn position(&self) -> usize {
        self.position
    }

    /// Returns a mutable reference to the location's position.
    pub(super) fn position_mut(&mut self) -> &mut usize {
        &mut self.position
    }

    /// The source code object this location refers to.
    pub fn source(&self) -> &Source {
        &self.source
    }

    /// Finds the line and column (respectively) of this location in the source
    /// code. Line and column count grapheme clusters/segments, not bytes nor
    /// characters.
    pub fn line_column(&self) -> (usize, usize) {
        let line = self.source.line(self.position);
        let line_start = self.source.line_start(line);
        (line, self.position - line_start)
    }

    /// Finds the line of this location in the source code. Line counts grapheme
    /// clusters/segments, not bytes nor characters.
    pub fn line(&self) -> usize {
        self.source.line(self.position)
    }

    /// Finds the column of this location in the source code. Column counts
    /// grapheme clusters/segments, not bytes nor characters.
    pub fn column(&self) -> usize {
        let (_, column) = self.line_column();
        column
    }

    /// Returns the underlying grapheme cluster segment content at this
    /// location.
    pub fn as_str(&self) -> &str {
        &self.source[self.position]
    }

    /// Returns the single segmented pointed by this location.
    pub fn segment(&self) -> LocatedSegment {
        LocatedSegment { location: self.clone() }
    }

    /// Creates a [`Span`] containing the whole line this location is in.
    pub fn line_span(&self) -> Span {
        let line = self.line();
        let init = self.source().line_start(line);
        let end = self
            .source()
            .try_line_start(line + 1)
            .unwrap_or(self.source().len());
        Span::new(Self::new(self.source.clone(), init), end - init)
    }
}

impl fmt::Debug for Location {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        let (line, column) = self.line_column();
        fmtr.debug_struct("Location")
            .field("source", &self.source)
            .field("position", &self.position)
            .field("line", &line)
            .field("column", &column)
            .finish()
    }
}

impl fmt::Display for Location {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        let (line, column) = self.line_column();
        write!(fmtr, "in {} ({}, {})", self.source, line + 1, column + 1)
    }
}

impl AsRef<Self> for Location {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl AsRef<str> for Location {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// A grapheme cluster segment with its location in the source code.
#[derive(Clone, Debug)]
pub struct LocatedSegment {
    /// Inner location.
    location: Location,
}

impl LocatedSegment {
    /// Returns the location of this segment.
    pub fn location(&self) -> &Location {
        &self.location
    }

    /// Returns the segment (a single grapheme cluster) as a string.
    pub fn as_str(&self) -> &str {
        self.location.as_str()
    }

    /// Tests whether this segment is a single character.
    pub fn is_single_char(&self) -> bool {
        self.len() == 1
    }

    /// Tests whether this segment is alphabetic. UTF-8 alphabetic characters
    /// with diacritics are also considered alphabetic.
    pub fn is_alphabetic(&self) -> bool {
        self.chars().next().map_or(false, |ch| ch.is_alphabetic())
    }

    /// Tests whether this segment is ASCII alphabetic.
    pub fn is_ascii_alphabetic(&self) -> bool {
        self.is_single_char()
            && self.chars().next().map_or(false, |ch| ch.is_ascii_alphabetic())
    }

    /// Tests whether this segment is numeric. UTF-8 numeric characters
    /// with diacritics are also considered numeric.
    pub fn is_numeric(&self) -> bool {
        self.chars().next().map_or(false, |ch| ch.is_numeric())
    }

    /// Tests whether this segment is ASCII numeric.
    pub fn is_ascii_numeric(&self) -> bool {
        self.is_single_char()
            && self.chars().next().map_or(false, |ch| ch.is_ascii_digit())
    }

    /// Tests whether this segment is alphanumeric. UTF-8 alphanumeric
    /// characters with diacritics are also considered alphanumeric.
    pub fn is_alphanumeric(&self) -> bool {
        self.chars().next().map_or(false, |ch| ch.is_alphanumeric())
    }

    /// Tests whether this segment is ASCII alphanumeric.
    pub fn is_ascii_alphanumeric(&self) -> bool {
        self.is_single_char()
            && self
                .chars()
                .next()
                .map_or(false, |ch| ch.is_ascii_alphanumeric())
    }

    /// Tests whether this segment is an ASCII digit. Digits characters with
    /// diacritics are NOT considered digits. Digit characters are `0-9`,
    /// `a-z`, `A-Z`, depending on the base.
    pub fn is_digit(&self, base: u32) -> bool {
        self.is_single_char()
            && self.chars().next().map_or(false, |ch| ch.is_digit(base))
    }

    /// Converts this grapheme cluster to a digit of given base. Digits with
    /// diacritics are not considered digits. Digit characters are `0-9`, `a-z`,
    /// `A-Z`, depending on the base.
    pub fn to_digit(&self, base: u32) -> Option<u32> {
        self.chars()
            .next()
            .and_then(|ch| ch.to_digit(base))
            .filter(|_| self.is_single_char())
    }

    /// Tests if this segment is only a linefeed character.
    pub fn is_newline(&self) -> bool {
        self == "\n"
    }

    /// Tests whether this segment is a single space.
    pub fn is_space(&self) -> bool {
        self == " "
    }

    /// Tests whether this segment is composed only by UTF-8 whitespace
    /// characters.
    pub fn is_whitespace(&self) -> bool {
        self.chars().all(char::is_whitespace)
    }
}

impl Deref for LocatedSegment {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for LocatedSegment {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(fmtr, "{}", &**self)
    }
}

impl PartialEq for LocatedSegment {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl PartialEq<str> for LocatedSegment {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl<'seg> PartialEq<&'seg str> for LocatedSegment {
    fn eq(&self, other: &&'seg str) -> bool {
        self.as_str() == *other
    }
}

impl Eq for LocatedSegment {}

impl PartialOrd for LocatedSegment {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialOrd<str> for LocatedSegment {
    fn partial_cmp(&self, other: &str) -> Option<Ordering> {
        self.as_str().partial_cmp(other)
    }
}

impl<'seg> PartialOrd<&'seg str> for LocatedSegment {
    fn partial_cmp(&self, other: &&'seg str) -> Option<Ordering> {
        self.as_str().partial_cmp(*other)
    }
}

impl Ord for LocatedSegment {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl Hash for LocatedSegment {
    fn hash<H>(&self, hasher: &mut H)
    where
        H: Hasher,
    {
        (**self).hash(hasher)
    }
}

impl AsRef<Self> for LocatedSegment {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl AsRef<str> for LocatedSegment {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for LocatedSegment {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}