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