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.codemap().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.codemap().lookup_char_pos(span.lo());
157 "Mis-counted brackets when breaking path? Parsing '{}' \
165 if result.is_none() && prev.tok.is_ident() {
166 return Some(prev.sp);
171 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
172 let mut toks = self.retokenise_span(span);
173 let mut prev = toks.real_token();
175 if prev.tok == token::Eof {
178 let next = toks.real_token();
180 return Some(prev.sp);
186 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
187 let mut toks = self.retokenise_span(span);
189 let next = toks.real_token();
190 if next.tok == token::Eof {
194 return Some(next.sp);
199 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
200 self.sub_span_after(span, |t| t.is_keyword(keyword))
203 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
204 let mut toks = self.retokenise_span(span);
206 let ts = toks.real_token();
207 if ts.tok == token::Eof {
211 let ts = toks.real_token();
212 if ts.tok == token::Eof {
221 // // Return the name for a macro definition (identifier after first `!`)
222 // pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
223 // let mut toks = self.retokenise_span(span);
225 // let ts = toks.real_token();
226 // if ts.tok == token::Eof {
229 // if ts.tok == token::Not {
230 // let ts = toks.real_token();
231 // if ts.tok.is_ident() {
232 // return Some(ts.sp);
240 // // Return the name for a macro use (identifier before first `!`).
241 // pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
242 // let mut toks = self.retokenise_span(span);
243 // let mut prev = toks.real_token();
245 // if prev.tok == token::Eof {
248 // let ts = toks.real_token();
249 // if ts.tok == token::Not {
250 // if prev.tok.is_ident() {
251 // return Some(prev.sp);
260 /// Return true if the span is generated code, and
261 /// it is not a subspan of the root callsite.
263 /// Used to filter out spans of minimal value,
264 /// such as references to macro internal variables.
265 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
266 if !generated_code(parent) {
267 if sub_span.is_none() {
268 // Edge case - this occurs on generated code with incorrect expansion info.
273 // If sub_span is none, filter out generated code.
274 let sub_span = match sub_span {
279 //If the span comes from a fake filemap, filter it.
282 .lookup_char_pos(parent.lo())
289 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
290 // callsite. This filters out macro internal variables and most malformed spans.
291 !parent.source_callsite().contains(sub_span)
295 macro_rules! filter {
296 ($util: expr, $span: expr, $parent: expr, None) => {
297 if $util.filter_generated($span, $parent) {
301 ($util: expr, $span: ident, $parent: expr) => {
302 if $util.filter_generated($span, $parent) {