]> git.lizzy.rs Git - rust.git/blob - src/codemap.rs
Add copyright notices to added files
[rust.git] / src / codemap.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::rc::Rc;
12
13 use syntax::codemap::{BytePos, CodeMap, FileMap, Span};
14
15 use comment::FindUncommented;
16
17 /// A range of lines in a file, inclusive of both ends.
18 pub struct LineRange {
19     pub file: Rc<FileMap>,
20     pub lo: usize,
21     pub hi: usize,
22 }
23
24 impl LineRange {
25     pub fn file_name(&self) -> &str {
26         self.file.as_ref().name.as_str()
27     }
28 }
29
30 pub trait SpanUtils {
31     fn span_after(&self, original: Span, needle: &str) -> BytePos;
32     fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
33     fn span_before(&self, original: Span, needle: &str) -> BytePos;
34 }
35
36 pub trait LineRangeUtils {
37     /// Returns the `LineRange` that corresponds to `span` in `self`.
38     ///
39     /// # Panics
40     ///
41     /// Panics if `span` crosses a file boundary, which shouldn't happen.
42     fn lookup_line_range(&self, span: Span) -> LineRange;
43 }
44
45 impl SpanUtils for CodeMap {
46     #[inline]
47     fn span_after(&self, original: Span, needle: &str) -> BytePos {
48         let snippet = self.span_to_snippet(original).unwrap();
49         let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
50
51         original.lo + BytePos(offset as u32)
52     }
53
54     #[inline]
55     fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
56         let snippet = self.span_to_snippet(original).unwrap();
57         let mut offset = 0;
58
59         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
60             offset += additional_offset + needle.len();
61         }
62
63         original.lo + BytePos(offset as u32)
64     }
65
66     #[inline]
67     fn span_before(&self, original: Span, needle: &str) -> BytePos {
68         let snippet = self.span_to_snippet(original).unwrap();
69         let offset = snippet.find_uncommented(needle).unwrap();
70
71         original.lo + BytePos(offset as u32)
72     }
73 }
74
75 impl LineRangeUtils for CodeMap {
76     fn lookup_line_range(&self, span: Span) -> LineRange {
77         let lo = self.lookup_char_pos(span.lo);
78         let hi = self.lookup_char_pos(span.hi);
79
80         assert!(lo.file.name == hi.file.name,
81                 "span crossed file boundary: lo: {:?}, hi: {:?}",
82                 lo,
83                 hi);
84
85         LineRange {
86             file: lo.file.clone(),
87             lo: lo.line,
88             hi: hi.line,
89         }
90     }
91 }