]> git.lizzy.rs Git - rust.git/blob - src/source_map.rs
Configurations: fix typos and mismatches
[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::visitor::SnippetProvider;
9
10 pub trait SpanUtils {
11     fn span_after(&self, original: Span, needle: &str) -> BytePos;
12     fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
13     fn span_before(&self, original: Span, needle: &str) -> BytePos;
14     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
15     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
16 }
17
18 pub trait LineRangeUtils {
19     /// Returns the `LineRange` that corresponds to `span` in `self`.
20     ///
21     /// # Panics
22     ///
23     /// Panics if `span` crosses a file boundary, which shouldn't happen.
24     fn lookup_line_range(&self, span: Span) -> LineRange;
25 }
26
27 impl<'a> SpanUtils for SnippetProvider<'a> {
28     fn span_after(&self, original: Span, needle: &str) -> BytePos {
29         self.opt_span_after(original, needle).expect("bad span")
30     }
31
32     fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
33         let snippet = self.span_to_snippet(original).unwrap();
34         let mut offset = 0;
35
36         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
37             offset += additional_offset + needle.len();
38         }
39
40         original.lo() + BytePos(offset as u32)
41     }
42
43     fn span_before(&self, original: Span, needle: &str) -> BytePos {
44         self.opt_span_before(original, needle).unwrap_or_else(|| {
45             panic!(
46                 "bad span: {}: {}",
47                 needle,
48                 self.span_to_snippet(original).unwrap()
49             )
50         })
51     }
52
53     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
54         self.opt_span_before(original, needle)
55             .map(|bytepos| bytepos + BytePos(needle.len() as u32))
56     }
57
58     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos> {
59         let snippet = self.span_to_snippet(original)?;
60         let offset = snippet.find_uncommented(needle)?;
61
62         Some(original.lo() + BytePos(offset as u32))
63     }
64 }
65
66 impl LineRangeUtils for SourceMap {
67     fn lookup_line_range(&self, span: Span) -> LineRange {
68         let lo = self.lookup_line(span.lo()).unwrap();
69         let hi = self.lookup_line(span.hi()).unwrap();
70
71         debug_assert_eq!(
72             lo.sf.name, hi.sf.name,
73             "span crossed file boundary: lo: {:?}, hi: {:?}",
74             lo, hi
75         );
76
77         // Line numbers start at 1
78         LineRange {
79             file: lo.sf.clone(),
80             lo: lo.line + 1,
81             hi: hi.line + 1,
82         }
83     }
84 }