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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
// Copyright (C) 2024 Ethan Uppal. All rights reserved.
use std::{cmp::Ordering, fmt::Display, rc::Rc};
/// Different sources of text data.
#[derive(Clone, Debug, Eq)]
pub enum Source {
/// `Source::File { name, contents }` is a text file with name `name` and
/// contents `contents`.
///
/// Invariant: if two [`Source::File`]s have the same `name`, they must
/// represent the same file. For this reason, it is recommended to use
/// fully-qualified paths for `name`.
File {
name: String,
contents: String
},
Unknown
}
impl Source {
pub fn file(name: String, contents: String) -> Rc<Source> {
Rc::new(Source::File { name, contents })
}
/// `contents(source)` is the string contents of `source`.
pub fn contents(&self) -> &str {
match self {
Self::File { name: _, contents } => contents,
Self::Unknown => ""
}
}
/// @see [`Loc::lines`]
fn lines(
&self, pos: usize, before: usize, after: usize
) -> (Vec<String>, usize) {
match self {
Self::File { name: _, contents } => {
assert!(pos < contents.len());
let bytes = contents.as_bytes();
// Find the bounds of the current line
let mut start_pos = pos;
while start_pos > 0 && bytes[start_pos - 1] != b'\n' {
start_pos -= 1;
}
let mut end_pos = start_pos;
while end_pos < contents.len() && bytes[end_pos] != b'\n' {
end_pos += 1;
}
end_pos += 1;
// Slice the contents to get the current line
let line = contents
.get(start_pos..end_pos - 1)
.unwrap_or_default()
.to_string();
// Make iterators for the before/after lines
let before_lines: Vec<_> = {
let (before_contents, _) = contents.split_at(start_pos);
let mut result: Vec<_> = before_contents
.lines()
.rev()
.take(before)
.map(String::from)
.collect();
result.reverse();
result
};
let after_lines: Vec<_> = if end_pos < contents.len() {
let (_, after_contents) = contents.split_at(end_pos);
after_contents
.lines()
.take(after)
.map(String::from)
.collect()
} else {
std::iter::empty().collect()
};
// Construct the final result
let mut result = vec![];
result.extend(before_lines);
let pos_current_line = result.len();
result.push(line);
result.extend(after_lines);
(result, pos_current_line)
}
Self::Unknown => (vec![], 0)
}
}
}
impl Display for Source {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Source::File { name, contents: _ } => write!(f, "{}", name),
Source::Unknown => write!(f, "<unknown>")
}
}
}
impl Default for Source {
fn default() -> Self {
Source::File {
name: String::new(),
contents: String::new()
}
}
}
impl PartialEq for Source {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unknown, Self::Unknown) => true,
(
Self::File { name, contents: _ },
Self::File {
name: other_name,
contents: _
}
) => name == other_name,
_ => false
}
}
}
/// `Loc(line, col, pos, source)` is a location referring to line `line` and
/// column `col` of `source`, where the combination of `line` and `col` produces
/// a direct offset `pos`. It is formatted as `"{source}:{line}:{col}"` where
/// `{source}` is the formatted substitution of `source` and likewise for
/// `line`/`col`. It is required that no numeric field is negative, that is,
/// `line`, `col`, and `pos` should be treated as if they were of type `usize`.
#[derive(Debug, Clone, Eq)]
pub struct Loc {
pub line: isize,
pub col: isize,
pub pos: isize,
pub source: Rc<Source>
}
impl Loc {
/// `loc.lines(before, after)` is a pair of a vector containing the
/// line in `loc.source` at position `loc.pos`, preceded by the up to
/// `before` previous lines and up to `after` subsequent lines, as well
/// as an index into the vector for the line containing `loc.pos`.
///
/// Requires: `loc.pos` is a valid position in `loc.source`.
pub fn lines(&self, before: usize, after: usize) -> (Vec<String>, usize) {
self.source.lines(self.pos as usize, before, after)
}
pub fn make_invalid() -> Self {
Loc {
line: 0,
col: 0,
pos: 0,
source: Rc::new(Source::Unknown)
}
}
pub fn is_invalid(&self) -> bool {
let invalid = Loc::make_invalid();
self.line == invalid.line
&& self.col == invalid.col
&& self.pos == invalid.pos
}
}
impl Display for Loc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.source, self.line, self.col)
}
}
impl Default for Loc {
fn default() -> Self {
Loc {
line: 1,
col: 1,
pos: 0,
source: Rc::new(Source::Unknown)
}
}
}
impl PartialEq for Loc {
fn eq(&self, other: &Self) -> bool {
self.line == other.line
&& self.col == other.col
&& self.pos == other.pos
&& self.source.as_ref() == other.source.as_ref()
}
}
impl PartialOrd for Loc {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.source != other.source {
None
} else {
self.pos.partial_cmp(&other.pos)
}
}
}
/// The location enclosed by a region begins at `start` and ends exclusively
/// at `end`. It is required that both locations come from the same source and
/// that `end` monotonically proceeds `start` (so `start` and `end` can compare
/// equal). This invariant is enforced when constructing through
/// [`Region::new`].
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Region {
/// An inclusive lower bound (see [`Loc::partial_cmp`]) on the region
/// enclosed.
pub start: Loc,
/// An exclusive upper bound (see [`Loc::partial_cmp`]) on the region
/// enclosed.
pub end: Loc
}
/// The line section with `start` and `end` represents the characters at
/// positions from lower bound `start` to exclusive upper bound `end` on a line.
/// The core invariant that `end >= start` is enforced by [`LineSection::new`].
pub struct LineSection {
/// The initial position on the line.
pub start: isize,
/// One after the final valid position contained by this line section on
/// the line, that is, an exclusive upper bound on the indices of the range
/// of characters contained by this line section.
pub end: isize
}
impl LineSection {
pub fn new(start: isize, end: isize) -> Self {
assert!(start <= end);
Self { start, end }
}
pub fn length(&self) -> usize {
(self.end - self.start) as usize
}
}
impl Region {
/// A region from `start` up to (but not including) `end`.
pub fn new(start: Loc, end: Loc) -> Region {
assert!(start <= end, "`Region::from`: `start` and `end` must from the same source and `end` must be at least after `start`.");
Region { start, end }
}
/// A region at `start` of length 1.
///
/// Requires: the `start.line` contains at least one more character after
/// `start.col`.
pub fn unit(start: Loc) -> Region {
let mut end = start.clone();
end.pos += 1;
end.col += 1;
Region::new(start, end)
}
/// The source where this region occurs.
pub fn source(&self) -> Rc<Source> {
self.start.source.clone()
}
pub fn start_line(&self) -> isize {
self.start.line
}
pub fn end_line(&self) -> isize {
self.end.line
}
/// Given a set of *complete* `lines` from the same source as `source()`
/// and the line number of the first line in `lines`, `start_line`, this
/// function computes the intersection of this region and the given
/// lines. If the output vector is non-empty, the first entry in the output
/// vector corresponds to the first line of this region, which is not
/// necessarily the first line in `lines`. See [`LineSection`].
pub fn find_intersection(
&self, lines: &Vec<String>, start_line: isize
) -> Vec<LineSection> {
let mut result = vec![];
for (i, line) in lines.iter().enumerate() {
let actual_line = start_line + (i as isize);
if actual_line >= self.start_line()
&& actual_line <= self.end_line()
{
let mut start_pos = 0;
let mut end_pos = line.len() as isize;
if actual_line == self.start_line() {
start_pos = self.start.col - 1;
}
if actual_line == self.end_line() {
end_pos = self.end.col - 1;
}
result.push(LineSection::new(start_pos, end_pos));
}
}
result
}
}
pub trait RegionProvider {
/// The starting location of this region.
fn start(&self) -> Loc;
/// Must be in the same source and monotonically after
/// [`RegionProvider::start`]. See [`Region`] for details.
fn end(&self) -> Loc;
/// The region of this object.
fn region(&self) -> Region {
Region::new(self.start(), self.end())
}
}
impl RegionProvider for Region {
fn start(&self) -> Loc {
self.start.clone()
}
fn end(&self) -> Loc {
self.end.clone()
}
}