1 // Copyright 2012-2014 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.
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.
11 use rustc::session::Session;
17 use syntax::parse::lexer::{self, StringReader};
18 use syntax::parse::token::{self, Token};
19 use syntax::symbol::keywords;
23 pub struct SpanUtils<'a> {
24 pub sess: &'a Session,
25 // FIXME given that we clone SpanUtils all over the place, this err_count is
26 // probably useless and any logic relying on it is bogus.
27 pub err_count: Cell<isize>,
30 impl<'a> SpanUtils<'a> {
31 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
34 err_count: Cell::new(0),
38 pub fn make_path_string(&self, path: &FileName) -> String {
40 FileName::Real(ref path) if !path.is_absolute() =>
41 self.sess.working_dir.0
45 _ => path.to_string(),
49 pub fn snippet(&self, span: Span) -> String {
50 match self.sess.source_map().span_to_snippet(span) {
52 Err(_) => String::new(),
56 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
57 lexer::StringReader::retokenize(&self.sess.parse_sess, span)
60 // Re-parses a path and returns the span for the last identifier in the path
61 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
62 let mut result = None;
64 let mut toks = self.retokenise_span(span);
65 let mut bracket_count = 0;
67 let ts = toks.real_token();
68 if ts.tok == token::Eof {
71 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
75 bracket_count += match ts.tok {
78 token::BinOp(token::Shr) => -2,
84 // Return the span for the first identifier in the path.
85 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
86 let mut toks = self.retokenise_span(span);
87 let mut bracket_count = 0;
89 let ts = toks.real_token();
90 if ts.tok == token::Eof {
93 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
97 bracket_count += match ts.tok {
100 token::BinOp(token::Shr) => -2,
106 // Return the span for the last ident before a `<` and outside any
107 // angle brackets, or the last span.
108 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
109 let mut toks = self.retokenise_span(span);
110 let mut prev = toks.real_token();
111 let mut result = None;
113 // We keep track of the following two counts - the depth of nesting of
114 // angle brackets, and the depth of nesting of square brackets. For the
115 // angle bracket count, we only count tokens which occur outside of any
116 // square brackets (i.e. bracket_count == 0). The intuition here is
117 // that we want to count angle brackets in the type, but not any which
118 // could be in expression context (because these could mean 'less than',
120 let mut angle_count = 0;
121 let mut bracket_count = 0;
123 let next = toks.real_token();
125 if (next.tok == token::Lt || next.tok == token::Colon) && angle_count == 0
126 && bracket_count == 0 && prev.tok.is_ident()
128 result = Some(prev.sp);
131 if bracket_count == 0 {
132 angle_count += match prev.tok {
135 token::BinOp(token::Shl) => 2,
136 token::BinOp(token::Shr) => -2,
141 bracket_count += match prev.tok {
142 token::OpenDelim(token::Bracket) => 1,
143 token::CloseDelim(token::Bracket) => -1,
147 if next.tok == token::Eof {
152 #[cfg(debug_assertions)] {
153 if angle_count != 0 || bracket_count != 0 {
154 let loc = self.sess.source_map().lookup_char_pos(span.lo());
157 "Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
164 if result.is_none() && prev.tok.is_ident() {
165 return Some(prev.sp);
170 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
171 let mut toks = self.retokenise_span(span);
172 let mut prev = toks.real_token();
174 if prev.tok == token::Eof {
177 let next = toks.real_token();
179 return Some(prev.sp);
185 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
186 let mut toks = self.retokenise_span(span);
188 let next = toks.real_token();
189 if next.tok == token::Eof {
193 return Some(next.sp);
198 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
199 self.sub_span_after(span, |t| t.is_keyword(keyword))
202 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
203 let mut toks = self.retokenise_span(span);
205 let ts = toks.real_token();
206 if ts.tok == token::Eof {
210 let ts = toks.real_token();
211 if ts.tok == token::Eof {
220 // // Return the name for a macro definition (identifier after first `!`)
221 // pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
222 // let mut toks = self.retokenise_span(span);
224 // let ts = toks.real_token();
225 // if ts.tok == token::Eof {
228 // if ts.tok == token::Not {
229 // let ts = toks.real_token();
230 // if ts.tok.is_ident() {
231 // return Some(ts.sp);
239 // // Return the name for a macro use (identifier before first `!`).
240 // pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
241 // let mut toks = self.retokenise_span(span);
242 // let mut prev = toks.real_token();
244 // if prev.tok == token::Eof {
247 // let ts = toks.real_token();
248 // if ts.tok == token::Not {
249 // if prev.tok.is_ident() {
250 // return Some(prev.sp);
259 /// Return true if the span is generated code, and
260 /// it is not a subspan of the root callsite.
262 /// Used to filter out spans of minimal value,
263 /// such as references to macro internal variables.
264 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
265 if !generated_code(parent) {
266 if sub_span.is_none() {
267 // Edge case - this occurs on generated code with incorrect expansion info.
272 // If sub_span is none, filter out generated code.
273 let sub_span = match sub_span {
278 //If the span comes from a fake source_file, filter it.
281 .lookup_char_pos(parent.lo())
288 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
289 // callsite. This filters out macro internal variables and most malformed spans.
290 !parent.source_callsite().contains(sub_span)
294 macro_rules! filter {
295 ($util: expr, $span: expr, $parent: expr, None) => {
296 if $util.filter_generated($span, $parent) {
300 ($util: expr, $span: ident, $parent: expr) => {
301 if $util.filter_generated($span, $parent) {