1use crate::{SourcePosition, SourceSpan};
2use rd_ast::{RdAstPath, RdAstPathSegment};
3use std::ops::Range;
4
5#[derive(Debug, Clone)]
16pub struct RdSourceMap {
17 root: SourceSpan,
18 top_level: Vec<RdSourceSpanNode>,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22struct RdSourceSpanNode {
23 span: SourceSpan,
24 option: Option<RdSourceSpanSequence>,
25 children: Vec<RdSourceSpanNode>,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29struct RdSourceSpanSequence {
30 span: SourceSpan,
31 nodes: Vec<RdSourceSpanNode>,
32}
33
34impl RdSourceMap {
35 pub(crate) fn from_extents(source: &SourceMap, extents: SourceExtents) -> Self {
36 Self {
37 root: source.span(extents.root),
38 top_level: extents
39 .top_level
40 .into_iter()
41 .map(|node| RdSourceSpanNode::from_extent(source, node))
42 .collect(),
43 }
44 }
45
46 pub fn span(&self, path: &RdAstPath) -> Option<&SourceSpan> {
48 if path.segments().is_empty() {
49 return Some(&self.root);
50 }
51 let [RdAstPathSegment::TopLevel(index), rest @ ..] = path.segments() else {
52 return None;
53 };
54 let mut node = self.top_level.get(*index)?;
55 let mut option_nodes: Option<&[RdSourceSpanNode]> = None;
56 for (position, segment) in rest.iter().enumerate() {
57 match segment {
58 RdAstPathSegment::Child(index) => {
59 node = match option_nodes.take() {
60 Some(nodes) => nodes.get(*index),
61 None => node.children.get(*index),
62 }?;
63 }
64 RdAstPathSegment::Option => {
65 if option_nodes.is_some() {
66 return None;
67 }
68 let option = node.option.as_ref()?;
69 if position + 1 == rest.len() {
70 return Some(&option.span);
71 }
72 option_nodes = Some(&option.nodes);
73 }
74 RdAstPathSegment::TopLevel(_) => return None,
75 _ => return None,
76 }
77 }
78 if option_nodes.is_some() {
79 return None;
80 }
81 Some(&node.span)
82 }
83}
84
85impl RdSourceSpanNode {
86 fn from_extent(source: &SourceMap, extent: SourceExtentNode) -> Self {
87 Self {
88 span: source.span(extent.bytes),
89 option: extent.option.map(|option| RdSourceSpanSequence {
90 span: source.span(option.bytes),
91 nodes: option
92 .nodes
93 .into_iter()
94 .map(|node| Self::from_extent(source, node))
95 .collect(),
96 }),
97 children: extent
98 .children
99 .into_iter()
100 .map(|node| Self::from_extent(source, node))
101 .collect(),
102 }
103 }
104}
105
106#[derive(Debug, Clone, PartialEq, Eq)]
112pub(crate) struct SourceExtentNode {
113 pub(crate) bytes: Range<usize>,
114 pub(crate) option: Option<SourceExtentSequence>,
115 pub(crate) children: Vec<SourceExtentNode>,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq)]
119pub(crate) struct SourceExtentSequence {
120 pub(crate) bytes: Range<usize>,
121 pub(crate) nodes: Vec<SourceExtentNode>,
122}
123
124impl SourceExtentNode {
125 pub(crate) fn leaf(bytes: Range<usize>) -> Self {
126 Self {
127 bytes,
128 option: None,
129 children: Vec::new(),
130 }
131 }
132
133 pub(crate) fn with_children(
134 bytes: Range<usize>,
135 option: Option<SourceExtentSequence>,
136 children: Vec<SourceExtentNode>,
137 ) -> Self {
138 Self {
139 bytes,
140 option,
141 children,
142 }
143 }
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
147pub(crate) struct SourceExtents {
148 pub(crate) root: Range<usize>,
149 pub(crate) top_level: Vec<SourceExtentNode>,
150}
151
152impl SourceExtents {
153 #[allow(dead_code)]
154 pub(crate) fn entry_count(&self) -> usize {
155 1 + self
156 .top_level
157 .iter()
158 .map(SourceExtentNode::entry_count)
159 .sum::<usize>()
160 }
161
162 #[allow(dead_code)]
163 pub(crate) fn at(&self, path: &RdAstPath) -> Option<&Range<usize>> {
164 if path.segments().is_empty() {
165 return Some(&self.root);
166 }
167 let [RdAstPathSegment::TopLevel(index), rest @ ..] = path.segments() else {
168 return None;
169 };
170 let mut node = self.top_level.get(*index)?;
171 let mut option_nodes: Option<&[SourceExtentNode]> = None;
172 for (position, segment) in rest.iter().enumerate() {
173 match segment {
174 RdAstPathSegment::Child(index) => {
175 node = match option_nodes.take() {
176 Some(nodes) => nodes.get(*index),
177 None => node.children.get(*index),
178 }?;
179 }
180 RdAstPathSegment::Option => {
181 if option_nodes.is_some() {
182 return None;
183 }
184 let option = node.option.as_ref()?;
185 if position + 1 == rest.len() {
186 return Some(&option.bytes);
187 }
188 option_nodes = Some(&option.nodes);
189 }
190 RdAstPathSegment::TopLevel(_) => return None,
191 _ => return None,
192 }
193 }
194 if option_nodes.is_some() {
195 return None;
196 }
197 Some(&node.bytes)
198 }
199}
200
201impl SourceExtentNode {
202 fn entry_count(&self) -> usize {
203 1 + self.option.as_ref().map_or(0, |option| {
204 1 + option
205 .nodes
206 .iter()
207 .map(SourceExtentNode::entry_count)
208 .sum::<usize>()
209 }) + self
210 .children
211 .iter()
212 .map(SourceExtentNode::entry_count)
213 .sum::<usize>()
214 }
215}
216
217pub(crate) struct SourceMap {
218 newlines: Vec<usize>,
219 input: String,
220}
221impl SourceMap {
222 pub(crate) fn new(input: &str) -> Self {
223 let mut newlines = Vec::new();
224 let bytes = input.as_bytes();
225 let mut i = 0;
226 while i < bytes.len() {
227 if bytes[i] == b'\n' || (bytes[i] == b'\r' && bytes.get(i + 1) != Some(&b'\n')) {
228 newlines.push(i);
229 }
230 i += 1;
231 }
232 Self {
233 newlines,
234 input: input.into(),
235 }
236 }
237 pub(crate) fn span(&self, range: std::ops::Range<usize>) -> SourceSpan {
238 SourceSpan::new(
239 range.clone(),
240 self.position(range.start),
241 self.position(range.end),
242 )
243 }
244 pub(crate) fn position(&self, offset: usize) -> SourcePosition {
245 let line = self.newlines.partition_point(|n| *n < offset);
246 let line_start = if line == 0 {
247 0
248 } else {
249 self.newlines[line - 1] + 1
250 };
251 let end = offset.min(self.input.len());
252 let column = self.input[line_start..end].chars().count() as u32 + 1;
253 SourcePosition::new(line as u32 + 1, column)
254 }
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260 #[test]
261 fn positions_count_scalars_and_crlf_as_one_line() {
262 let map = SourceMap::new("é\r\n日本語");
263 assert_eq!(map.position(0), SourcePosition::new(1, 1));
264 assert_eq!(map.position("é".len()), SourcePosition::new(1, 2));
265 assert_eq!(map.position("é\r\n".len()), SourcePosition::new(2, 1));
266 assert_eq!(map.position("é\r\n日本語".len()), SourcePosition::new(2, 4));
267 assert_eq!(map.span(0..map.input.len()).bytes(), 0..map.input.len());
268 }
269}