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;
20 use syntax::parse::lexer::{self, StringReader};
21 use syntax::parse::token::{self, Token};
22 use syntax::symbol::keywords;
26 pub struct SpanUtils<'a> {
27 pub sess: &'a Session,
28 // FIXME given that we clone SpanUtils all over the place, this err_count is
29 // probably useless and any logic relying on it is bogus.
30 pub err_count: Cell<isize>,
33 impl<'a> SpanUtils<'a> {
34 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
37 err_count: Cell::new(0),
41 pub fn make_path_string(file_name: &str) -> String {
42 let path = Path::new(file_name);
43 if path.is_absolute() {
44 path.clone().display().to_string()
46 env::current_dir().unwrap().join(&path).display().to_string()
50 pub fn snippet(&self, span: Span) -> String {
51 match self.sess.codemap().span_to_snippet(span) {
53 Err(_) => String::new(),
57 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
58 lexer::StringReader::retokenize(&self.sess.parse_sess, span)
61 // Re-parses a path and returns the span for the last identifier in the path
62 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
63 let mut result = None;
65 let mut toks = self.retokenise_span(span);
66 let mut bracket_count = 0;
68 let ts = toks.real_token();
69 if ts.tok == token::Eof {
72 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
76 bracket_count += match ts.tok {
79 token::BinOp(token::Shr) => -2,
85 // Return the span for the first identifier in the path.
86 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
87 let mut toks = self.retokenise_span(span);
88 let mut bracket_count = 0;
90 let ts = toks.real_token();
91 if ts.tok == token::Eof {
94 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
98 bracket_count += match ts.tok {
101 token::BinOp(token::Shr) => -2,
107 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
108 // any brackets, or the last span.
109 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
110 let mut toks = self.retokenise_span(span);
111 let mut prev = toks.real_token();
112 let mut result = None;
113 let mut bracket_count = 0;
114 let mut prev_span = None;
115 while prev.tok != token::Eof {
117 let mut next = toks.real_token();
119 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
120 bracket_count == 0 && prev.tok.is_ident() {
121 result = Some(prev.sp);
124 if bracket_count == 0 && next.tok == token::ModSep {
127 next = toks.real_token();
128 if next.tok == token::Lt && old.tok.is_ident() {
129 result = Some(old.sp);
133 bracket_count += match prev.tok {
134 token::OpenDelim(token::Paren) | token::Lt => 1,
135 token::CloseDelim(token::Paren) | token::Gt => -1,
136 token::BinOp(token::Shr) => -2,
140 if prev.tok.is_ident() && bracket_count == 0 {
141 prev_span = Some(prev.sp);
148 // Return the span for the last ident before a `<` and outside any
149 // angle brackets, or the last span.
150 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
151 let mut toks = self.retokenise_span(span);
152 let mut prev = toks.real_token();
153 let mut result = None;
155 // We keep track of the following two counts - the depth of nesting of
156 // angle brackets, and the depth of nesting of square brackets. For the
157 // angle bracket count, we only count tokens which occur outside of any
158 // square brackets (i.e. bracket_count == 0). The intutition here is
159 // that we want to count angle brackets in the type, but not any which
160 // could be in expression context (because these could mean 'less than',
162 let mut angle_count = 0;
163 let mut bracket_count = 0;
165 let next = toks.real_token();
167 if (next.tok == token::Lt || next.tok == token::Colon) &&
169 bracket_count == 0 &&
170 prev.tok.is_ident() {
171 result = Some(prev.sp);
174 if bracket_count == 0 {
175 angle_count += match prev.tok {
178 token::BinOp(token::Shl) => 2,
179 token::BinOp(token::Shr) => -2,
184 bracket_count += match prev.tok {
185 token::OpenDelim(token::Bracket) => 1,
186 token::CloseDelim(token::Bracket) => -1,
190 if next.tok == token::Eof {
195 if angle_count != 0 || bracket_count != 0 {
196 let loc = self.sess.codemap().lookup_char_pos(span.lo);
198 "Mis-counted brackets when breaking path? Parsing '{}' \
204 if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
205 return Some(prev.sp);
210 // Reparse span and return an owned vector of sub spans of the first limit
211 // identifier tokens in the given nesting level.
212 // example with Foo<Bar<T,V>, Bar<T,V>>
213 // Nesting = 0: all idents outside of angle brackets: [Foo]
214 // Nesting = 1: idents within one level of angle brackets: [Bar, Bar]
215 pub fn spans_with_brackets(&self, span: Span, nesting: isize, limit: isize) -> Vec<Span> {
216 let mut result: Vec<Span> = vec![];
218 let mut toks = self.retokenise_span(span);
219 // We keep track of how many brackets we're nested in
220 let mut angle_count: isize = 0;
221 let mut bracket_count: isize = 0;
222 let mut found_ufcs_sep = false;
224 let ts = toks.real_token();
225 if ts.tok == token::Eof {
226 if angle_count != 0 || bracket_count != 0 {
227 if generated_code(span) {
230 let loc = self.sess.codemap().lookup_char_pos(span.lo);
232 "Mis-counted brackets when breaking path? \
233 Parsing '{}' in {}, line {}",
240 if (result.len() as isize) == limit {
243 bracket_count += match ts.tok {
244 token::OpenDelim(token::Bracket) => 1,
245 token::CloseDelim(token::Bracket) => -1,
248 if bracket_count > 0 {
251 angle_count += match ts.tok {
254 token::BinOp(token::Shl) => 2,
255 token::BinOp(token::Shr) => -2,
259 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
261 // The root cause of this hack is that the AST representation of
262 // qpaths is horrible. It treats <A as B>::C as a path with two
263 // segments, B and C and notes that there is also a self type A at
264 // position 0. Because we don't have spans for individual idents,
265 // only the whole path, we have to iterate over the tokens in the
266 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
267 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
268 // the start of the first ident to the end of the path.
269 if !found_ufcs_sep && angle_count == -1 {
270 found_ufcs_sep = true;
273 if ts.tok.is_ident() && angle_count == nesting {
279 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
280 let mut toks = self.retokenise_span(span);
281 let mut prev = toks.real_token();
283 if prev.tok == token::Eof {
286 let next = toks.real_token();
288 return Some(prev.sp);
294 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
295 let mut toks = self.retokenise_span(span);
297 let next = toks.real_token();
298 if next.tok == token::Eof {
302 return Some(next.sp);
307 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
308 self.sub_span_after(span, |t| t.is_keyword(keyword))
311 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
312 self.sub_span_after(span, |t| t == tok)
315 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
316 let mut toks = self.retokenise_span(span);
318 let ts = toks.real_token();
319 if ts.tok == token::Eof {
323 let ts = toks.real_token();
324 if ts.tok == token::Eof {
334 // Returns a list of the spans of idents in a path.
335 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
336 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
337 self.spans_with_brackets(path.span, 0, -1)
340 // Return an owned vector of the subspans of the param identifier
341 // tokens found in span.
342 pub fn spans_for_ty_params(&self, span: Span, number: isize) -> Vec<Span> {
343 // Type params are nested within one level of brackets:
344 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
345 self.spans_with_brackets(span, 1, number)
348 // // Return the name for a macro definition (identifier after first `!`)
349 // pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
350 // let mut toks = self.retokenise_span(span);
352 // let ts = toks.real_token();
353 // if ts.tok == token::Eof {
356 // if ts.tok == token::Not {
357 // let ts = toks.real_token();
358 // if ts.tok.is_ident() {
359 // return Some(ts.sp);
367 // // Return the name for a macro use (identifier before first `!`).
368 // pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
369 // let mut toks = self.retokenise_span(span);
370 // let mut prev = toks.real_token();
372 // if prev.tok == token::Eof {
375 // let ts = toks.real_token();
376 // if ts.tok == token::Not {
377 // if prev.tok.is_ident() {
378 // return Some(prev.sp);
387 /// Return true if the span is generated code, and
388 /// it is not a subspan of the root callsite.
390 /// Used to filter out spans of minimal value,
391 /// such as references to macro internal variables.
392 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
393 if !generated_code(parent) {
394 if sub_span.is_none() {
395 // Edge case - this occurs on generated code with incorrect expansion info.
400 // If sub_span is none, filter out generated code.
401 if sub_span.is_none() {
405 //If the span comes from a fake filemap, filter it.
406 if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
410 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
411 // callsite. This filters out macro internal variables and most malformed spans.
412 !parent.source_callsite().contains(parent)
416 macro_rules! filter {
417 ($util: expr, $span: ident, $parent: expr, None) => {
418 if $util.filter_generated($span, $parent) {
422 ($util: expr, $span: ident, $parent: expr) => {
423 if $util.filter_generated($span, $parent) {