Skip to content

Commit 2ac6d1a

Browse files
committed
Add SourceLocation info
1 parent 2138791 commit 2ac6d1a

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

rust_src/source_files.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
33
use crate::error::Error;
44
type LineMap = Vec<[usize; 2]>;
5+
6+
#[derive(Clone, PartialEq, Debug)]
7+
/// Source locations
8+
pub struct SourceLocation {
9+
pub start: usize,
10+
pub end: usize,
11+
pub path: String,
12+
}
13+
514
/// The notcc representation of a SourceFile
615
pub struct SourceFile {
716
code: String,
@@ -15,6 +24,15 @@ impl SourceFile {
1524
&self.code
1625
}
1726

27+
/// Create a source location for this file with the given start and end
28+
pub fn source_location(&self, start: usize, end: usize) -> SourceLocation {
29+
SourceLocation {
30+
start,
31+
end,
32+
path: self.path.clone(),
33+
}
34+
}
35+
1836
/// Construct a new SourceFile given its code and a path
1937
pub fn new(code: String, path: String) -> SourceFile {
2038
let mut line_map: LineMap = Vec::new();
@@ -51,14 +69,14 @@ impl SourceFile {
5169
self.err_in_range(idx, idx + 1, msg)
5270
}
5371

54-
fn containing_line_and_loc(&self, idx: usize) -> (&str, Location) {
55-
let loc = self.char_to_location(idx);
72+
fn containing_line_and_loc(&self, idx: usize) -> (&str, LineColLocation) {
73+
let loc = self.char_to_line_col(idx);
5674
let [start, end] = self.line_map[loc.line];
5775
(&self.code[start..end], loc)
5876
}
5977

6078
/// Convert a character offset to a Location with line, column values
61-
pub fn char_to_location(&self, idx: usize) -> Location {
79+
fn char_to_line_col(&self, idx: usize) -> LineColLocation {
6280
let mut row: usize = 0;
6381
let mut col: usize = 0;
6482
for (row_idx, bounds) in self.line_map.iter().enumerate() {
@@ -67,15 +85,15 @@ impl SourceFile {
6785
col = idx - bounds[0];
6886
}
6987
}
70-
Location {
88+
LineColLocation {
7189
line: row,
7290
column: col,
7391
file_path: self.path.clone(),
7492
}
7593
}
7694
}
7795

78-
pub struct Location {
96+
pub struct LineColLocation {
7997
pub line: usize,
8098
pub column: usize,
8199
pub file_path: String,
@@ -94,23 +112,23 @@ mod tests {
94112
let path = "/foo/bar/baz.c";
95113
let source = SourceFile::new(code.to_string(), path.to_string());
96114
assert_eq!(source.code(), code);
97-
let mut loc = source.char_to_location(0);
115+
let mut loc = source.char_to_line_col(0);
98116
assert_eq!(loc.column, 0);
99117
assert_eq!(loc.line, 0);
100118
assert_eq!(loc.file_path, path);
101-
loc = source.char_to_location(6);
119+
loc = source.char_to_line_col(6);
102120
assert_eq!(loc.column, 6);
103121
assert_eq!(loc.line, 0);
104122
assert_eq!(loc.file_path, path);
105-
loc = source.char_to_location(17);
123+
loc = source.char_to_line_col(17);
106124
assert_eq!(loc.column, 0);
107125
assert_eq!(loc.line, 1);
108126
assert_eq!(loc.file_path, path);
109-
loc = source.char_to_location(28);
127+
loc = source.char_to_line_col(28);
110128
assert_eq!(loc.column, 11);
111129
assert_eq!(loc.line, 1);
112130
assert_eq!(loc.file_path, path);
113-
loc = source.char_to_location(29);
131+
loc = source.char_to_line_col(29);
114132
assert_eq!((loc.line, loc.column), (2, 0));
115133
assert_eq!(loc.file_path, path);
116134
}

0 commit comments

Comments
 (0)