php_rs_parser/
source_map.rs1use php_ast::Span;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23pub struct LineCol {
24 pub line: u32,
26 pub col: u32,
28}
29
30impl LineCol {
31 pub fn to_one_based(self) -> (u32, u32) {
33 (self.line + 1, self.col + 1)
34 }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39pub struct LineColSpan {
40 pub start: LineCol,
41 pub end: LineCol,
42}
43
44pub struct SourceMap {
49 line_starts: Vec<u32>,
51}
52
53impl SourceMap {
54 pub fn new(source: &str) -> Self {
56 let mut line_starts = vec![0u32];
57 for pos in memchr::memchr_iter(b'\n', source.as_bytes()) {
58 line_starts.push((pos + 1) as u32);
59 }
60 Self { line_starts }
61 }
62
63 pub fn line_count(&self) -> usize {
65 self.line_starts.len()
66 }
67
68 pub fn line_start(&self, line: u32) -> Option<u32> {
71 self.line_starts.get(line as usize).copied()
72 }
73
74 pub fn offset_to_line_col(&self, offset: u32) -> LineCol {
79 let line = match self.line_starts.binary_search(&offset) {
80 Ok(exact) => exact,
81 Err(after) => after - 1,
82 };
83 let col = offset - self.line_starts[line];
84 LineCol {
85 line: line as u32,
86 col,
87 }
88 }
89
90 pub fn span_to_line_col(&self, span: Span) -> LineColSpan {
92 LineColSpan {
93 start: self.offset_to_line_col(span.start),
94 end: self.offset_to_line_col(span.end),
95 }
96 }
97
98 pub fn line_col_to_offset(&self, lc: LineCol) -> Option<u32> {
101 self.line_starts
102 .get(lc.line as usize)
103 .map(|start| start + lc.col)
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn empty_source() {
113 let map = SourceMap::new("");
114 assert_eq!(map.line_count(), 1);
115 assert_eq!(map.offset_to_line_col(0), LineCol { line: 0, col: 0 });
116 }
117
118 #[test]
119 fn single_line_no_newline() {
120 let map = SourceMap::new("<?php echo 1;");
121 assert_eq!(map.line_count(), 1);
122 assert_eq!(map.offset_to_line_col(0), LineCol { line: 0, col: 0 });
123 assert_eq!(map.offset_to_line_col(6), LineCol { line: 0, col: 6 });
124 }
125
126 #[test]
127 fn multiple_lines() {
128 let src = "<?php\necho 'hi';\nreturn;\n";
129 let map = SourceMap::new(src);
130 assert_eq!(map.line_count(), 4); assert_eq!(map.offset_to_line_col(0), LineCol { line: 0, col: 0 });
134 assert_eq!(map.offset_to_line_col(6), LineCol { line: 1, col: 0 });
136 assert_eq!(map.offset_to_line_col(6), LineCol { line: 1, col: 0 });
138 assert_eq!(map.offset_to_line_col(17), LineCol { line: 2, col: 0 });
140 }
141
142 #[test]
143 fn span_conversion() {
144 let src = "<?php\necho 'hi';\n";
145 let map = SourceMap::new(src);
146 let span = Span::new(6, 10); let lc = map.span_to_line_col(span);
148 assert_eq!(lc.start, LineCol { line: 1, col: 0 });
149 assert_eq!(lc.end, LineCol { line: 1, col: 4 });
150 }
151
152 #[test]
153 fn round_trip() {
154 let src = "<?php\necho 'hi';\nreturn;\n";
155 let map = SourceMap::new(src);
156 let lc = LineCol { line: 1, col: 5 };
157 let offset = map.line_col_to_offset(lc).unwrap();
158 assert_eq!(map.offset_to_line_col(offset), lc);
159 }
160
161 #[test]
162 fn one_based() {
163 let lc = LineCol { line: 0, col: 0 };
164 assert_eq!(lc.to_one_based(), (1, 1));
165 let lc = LineCol { line: 2, col: 5 };
166 assert_eq!(lc.to_one_based(), (3, 6));
167 }
168
169 #[test]
170 fn line_start_lookup() {
171 let src = "aaa\nbbb\nccc";
172 let map = SourceMap::new(src);
173 assert_eq!(map.line_start(0), Some(0));
174 assert_eq!(map.line_start(1), Some(4));
175 assert_eq!(map.line_start(2), Some(8));
176 assert_eq!(map.line_start(3), None);
177 }
178
179 #[test]
180 fn crlf_treated_as_two_bytes() {
181 let src = "a\r\nb";
183 let map = SourceMap::new(src);
184 assert_eq!(map.line_count(), 2);
185 assert_eq!(map.offset_to_line_col(3), LineCol { line: 1, col: 0 });
187 }
188}