]> git.lizzy.rs Git - rust.git/blob - src/source_map.rs
fix: support raw prefix identifiers in statics
[rust.git] / src / source_map.rs
1 //! This module contains utilities that work with the `SourceMap` from `libsyntax`/`syntex_syntax`.
2 //! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
3
4 use syntax::source_map::{BytePos, SourceMap, Span};
5
6 use crate::comment::FindUncommented;
7 use crate::config::file_lines::LineRange;
8 use crate::utils::starts_with_newline;
9 use crate::visitor::SnippetProvider;
10
11 pub(crate) trait SpanUtils {
12     fn span_after(&self, original: Span, needle: &str) -> BytePos;
13     fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
14     fn span_before(&self, original: Span, needle: &str) -> BytePos;
15     fn span_before_last(&self, original: Span, needle: &str) -> BytePos;
16     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
17     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
18 }
19
20 pub(crate) trait LineRangeUtils {
21     /// Returns the `LineRange` that corresponds to `span` in `self`.
22     ///
23     /// # Panics
24     ///
25     /// Panics if `span` crosses a file boundary, which shouldn't happen.
26     fn lookup_line_range(&self, span: Span) -> LineRange;
27 }
28
29 impl<'a> SpanUtils for SnippetProvider<'a> {
30     fn span_after(&self, original: Span, needle: &str) -> BytePos {
31         self.opt_span_after(original, needle).unwrap_or_else(|| {
32             panic!(
33                 "bad span: `{}`: `{}`",
34                 needle,
35                 self.span_to_snippet(original).unwrap()
36             )
37         })
38     }
39
40     fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
41         let snippet = self.span_to_snippet(original).unwrap();
42         let mut offset = 0;
43
44         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
45             offset += additional_offset + needle.len();
46         }
47
48         original.lo() + BytePos(offset as u32)
49     }
50
51     fn span_before(&self, original: Span, needle: &str) -> BytePos {
52         self.opt_span_before(original, needle).unwrap_or_else(|| {
53             panic!(
54                 "bad span: `{}`: `{}`",
55                 needle,
56                 self.span_to_snippet(original).unwrap()
57             )
58         })
59     }
60
61     fn span_before_last(&self, original: Span, needle: &str) -> BytePos {
62         let snippet = self.span_to_snippet(original).unwrap();
63         let mut offset = 0;
64
65         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
66             offset += additional_offset + needle.len();
67         }
68
69         original.lo() + BytePos(offset as u32 - 1)
70     }
71
72     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
73         self.opt_span_before(original, needle)
74             .map(|bytepos| bytepos + BytePos(needle.len() as u32))
75     }
76
77     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos> {
78         let snippet = self.span_to_snippet(original)?;
79         let offset = snippet.find_uncommented(needle)?;
80
81         Some(original.lo() + BytePos(offset as u32))
82     }
83 }
84
85 impl LineRangeUtils for SourceMap {
86     fn lookup_line_range(&self, span: Span) -> LineRange {
87         let snippet = self.span_to_snippet(span).unwrap_or_default();
88         let lo = self.lookup_line(span.lo()).unwrap();
89         let hi = self.lookup_line(span.hi()).unwrap();
90
91         debug_assert_eq!(
92             lo.sf.name, hi.sf.name,
93             "span crossed file boundary: lo: {:?}, hi: {:?}",
94             lo, hi
95         );
96
97         // in case the span starts with a newline, the line range is off by 1 without the
98         // adjustment below
99         let offset = 1 + if starts_with_newline(&snippet) { 1 } else { 0 };
100         // Line numbers start at 1
101         LineRange {
102             file: lo.sf.clone(),
103             lo: lo.line + offset,
104             hi: hi.line + offset,
105         }
106     }
107 }