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
;
13 use save
::generated_code
;
20 use syntax
::codemap
::*;
21 use syntax
::parse
::lexer
;
22 use syntax
::parse
::lexer
::{Reader, StringReader}
;
23 use syntax
::parse
::token
;
24 use syntax
::parse
::token
::{keywords, Token}
;
27 pub struct SpanUtils
<'a
> {
28 pub sess
: &'a Session
,
29 pub err_count
: Cell
<isize>,
32 impl<'a
> SpanUtils
<'a
> {
33 pub fn new(sess
: &'a Session
) -> SpanUtils
<'a
> {
36 err_count
: Cell
::new(0),
40 pub fn make_path_string(file_name
: &str) -> String
{
41 let path
= Path
::new(file_name
);
42 if path
.is_absolute() {
43 path
.clone().display().to_string()
45 env
::current_dir().unwrap().join(&path
).display().to_string()
49 // Standard string for extents/location.
51 pub fn extent_str(&self, span
: Span
) -> String
{
52 let lo_loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
53 let hi_loc
= self.sess
.codemap().lookup_char_pos(span
.hi
);
54 let lo_pos
= self.sess
.codemap().bytepos_to_file_charpos(span
.lo
);
55 let hi_pos
= self.sess
.codemap().bytepos_to_file_charpos(span
.hi
);
56 let lo_pos_byte
= self.sess
.codemap().lookup_byte_offset(span
.lo
).pos
;
57 let hi_pos_byte
= self.sess
.codemap().lookup_byte_offset(span
.hi
).pos
;
59 format
!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
60 file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
61 SpanUtils
::make_path_string(&lo_loc
.file
.name
),
62 lo_loc
.line
, lo_loc
.col
.to_usize(), lo_pos
.to_usize(), lo_pos_byte
.to_usize(),
63 hi_loc
.line
, hi_loc
.col
.to_usize(), hi_pos
.to_usize(), hi_pos_byte
.to_usize())
66 // sub_span starts at span.lo, so we need to adjust the positions etc.
67 // If sub_span is None, we don't need to adjust.
68 pub fn make_sub_span(&self, span
: Span
, sub_span
: Option
<Span
>) -> Option
<Span
> {
69 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
70 assert
!(!generated_code(span
),
71 "generated code; we should not be processing this `{}` in {}, line {}",
79 let FileMapAndBytePos {fm, pos}
= self.sess
.codemap().lookup_byte_offset(span
.lo
);
80 let base
= pos
+ fm
.start_pos
;
82 lo
: base
+ self.sess
.codemap().lookup_byte_offset(sub
.lo
).pos
,
83 hi
: base
+ self.sess
.codemap().lookup_byte_offset(sub
.hi
).pos
,
84 expn_id
: NO_EXPANSION
,
90 pub fn snippet(&self, span
: Span
) -> String
{
91 match self.sess
.codemap().span_to_snippet(span
) {
93 Err(_
) => String
::new(),
97 pub fn retokenise_span(&self, span
: Span
) -> StringReader
<'a
> {
98 // sadness - we don't have spans for sub-expressions nor access to the tokens
99 // so in order to get extents for the function name itself (which dxr expects)
100 // we need to re-tokenise the fn definition
102 // Note: this is a bit awful - it adds the contents of span to the end of
103 // the codemap as a new filemap. This is mostly OK, but means we should
104 // not iterate over the codemap. Also, any spans over the new filemap
105 // are incompatible with spans over other filemaps.
106 let filemap
= self.sess
108 .new_filemap(String
::from("<anon-dxr>"), self.snippet(span
));
110 lexer
::StringReader
::new(s
.diagnostic(), filemap
)
113 // Re-parses a path and returns the span for the last identifier in the path
114 pub fn span_for_last_ident(&self, span
: Span
) -> Option
<Span
> {
115 let mut result
= None
;
117 let mut toks
= self.retokenise_span(span
);
118 let mut bracket_count
= 0;
120 let ts
= toks
.real_token();
121 if ts
.tok
== token
::Eof
{
122 return self.make_sub_span(span
, result
)
124 if bracket_count
== 0 && (ts
.tok
.is_ident() || ts
.tok
.is_keyword(keywords
::SelfValue
)) {
125 result
= Some(ts
.sp
);
128 bracket_count
+= match ts
.tok
{
131 token
::BinOp(token
::Shr
) => -2,
137 // Return the span for the first identifier in the path.
138 pub fn span_for_first_ident(&self, span
: Span
) -> Option
<Span
> {
139 let mut toks
= self.retokenise_span(span
);
140 let mut bracket_count
= 0;
142 let ts
= toks
.real_token();
143 if ts
.tok
== token
::Eof
{
146 if bracket_count
== 0 && (ts
.tok
.is_ident() || ts
.tok
.is_keyword(keywords
::SelfValue
)) {
147 return self.make_sub_span(span
, Some(ts
.sp
));
150 bracket_count
+= match ts
.tok
{
153 token
::BinOp(token
::Shr
) => -2,
159 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
160 // any brackets, or the last span.
161 pub fn sub_span_for_meth_name(&self, span
: Span
) -> Option
<Span
> {
162 let mut toks
= self.retokenise_span(span
);
163 let mut prev
= toks
.real_token();
164 let mut result
= None
;
165 let mut bracket_count
= 0;
166 let mut last_span
= None
;
167 while prev
.tok
!= token
::Eof
{
169 let mut next
= toks
.real_token();
171 if (next
.tok
== token
::OpenDelim(token
::Paren
) || next
.tok
== token
::Lt
) &&
172 bracket_count
== 0 && prev
.tok
.is_ident() {
173 result
= Some(prev
.sp
);
176 if bracket_count
== 0 && next
.tok
== token
::ModSep
{
179 next
= toks
.real_token();
180 if next
.tok
== token
::Lt
&& old
.tok
.is_ident() {
181 result
= Some(old
.sp
);
185 bracket_count
+= match prev
.tok
{
186 token
::OpenDelim(token
::Paren
) | token
::Lt
=> 1,
187 token
::CloseDelim(token
::Paren
) | token
::Gt
=> -1,
188 token
::BinOp(token
::Shr
) => -2,
192 if prev
.tok
.is_ident() && bracket_count
== 0 {
193 last_span
= Some(prev
.sp
);
197 if result
.is_none() && last_span
.is_some() {
198 return self.make_sub_span(span
, last_span
);
200 return self.make_sub_span(span
, result
);
203 // Return the span for the last ident before a `<` and outside any
204 // brackets, or the last span.
205 pub fn sub_span_for_type_name(&self, span
: Span
) -> Option
<Span
> {
206 let mut toks
= self.retokenise_span(span
);
207 let mut prev
= toks
.real_token();
208 let mut result
= None
;
209 let mut bracket_count
= 0;
211 let next
= toks
.real_token();
213 if (next
.tok
== token
::Lt
|| next
.tok
== token
::Colon
) && bracket_count
== 0 &&
214 prev
.tok
.is_ident() {
215 result
= Some(prev
.sp
);
218 bracket_count
+= match prev
.tok
{
221 token
::BinOp(token
::Shl
) => 2,
222 token
::BinOp(token
::Shr
) => -2,
226 if next
.tok
== token
::Eof
{
231 if bracket_count
!= 0 {
232 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
233 self.sess
.span_bug(span
,
234 &format
!("Mis-counted brackets when breaking path? Parsing '{}' \
240 if result
.is_none() && prev
.tok
.is_ident() && bracket_count
== 0 {
241 return self.make_sub_span(span
, Some(prev
.sp
));
243 self.make_sub_span(span
, result
)
246 // Reparse span and return an owned vector of sub spans of the first limit
247 // identifier tokens in the given nesting level.
248 // example with Foo<Bar<T,V>, Bar<T,V>>
249 // Nesting = 0: all idents outside of brackets: [Foo]
250 // Nesting = 1: idents within one level of brackets: [Bar, Bar]
251 pub fn spans_with_brackets(&self, span
: Span
, nesting
: isize, limit
: isize) -> Vec
<Span
> {
252 let mut result
: Vec
<Span
> = vec
!();
254 let mut toks
= self.retokenise_span(span
);
255 // We keep track of how many brackets we're nested in
256 let mut bracket_count
: isize = 0;
257 let mut found_ufcs_sep
= false;
259 let ts
= toks
.real_token();
260 if ts
.tok
== token
::Eof
{
261 if bracket_count
!= 0 {
262 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
263 self.sess
.span_bug(span
,
264 &format
!("Mis-counted brackets when breaking path? \
265 Parsing '{}' in {}, line {}",
272 if (result
.len() as isize) == limit
{
275 bracket_count
+= match ts
.tok
{
278 token
::BinOp(token
::Shl
) => 2,
279 token
::BinOp(token
::Shr
) => -2,
283 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
285 // The root cause of this hack is that the AST representation of
286 // qpaths is horrible. It treats <A as B>::C as a path with two
287 // segments, B and C and notes that there is also a self type A at
288 // position 0. Because we don't have spans for individual idents,
289 // only the whole path, we have to iterate over the tokens in the
290 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
291 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
292 // the start of the first ident to the end of the path.
293 if !found_ufcs_sep
&& bracket_count
== -1 {
294 found_ufcs_sep
= true;
297 if ts
.tok
.is_ident() && bracket_count
== nesting
{
298 result
.push(self.make_sub_span(span
, Some(ts
.sp
)).unwrap());
303 pub fn sub_span_before_token(&self, span
: Span
, tok
: Token
) -> Option
<Span
> {
304 let mut toks
= self.retokenise_span(span
);
305 let mut prev
= toks
.real_token();
307 if prev
.tok
== token
::Eof
{
310 let next
= toks
.real_token();
312 return self.make_sub_span(span
, Some(prev
.sp
));
318 pub fn sub_span_of_token(&self, span
: Span
, tok
: Token
) -> Option
<Span
> {
319 let mut toks
= self.retokenise_span(span
);
321 let next
= toks
.real_token();
322 if next
.tok
== token
::Eof
{
326 return self.make_sub_span(span
, Some(next
.sp
));
331 pub fn sub_span_after_keyword(&self, span
: Span
, keyword
: keywords
::Keyword
) -> Option
<Span
> {
332 self.sub_span_after(span
, |t
| t
.is_keyword(keyword
))
335 pub fn sub_span_after_token(&self, span
: Span
, tok
: Token
) -> Option
<Span
> {
336 self.sub_span_after(span
, |t
| t
== tok
)
339 fn sub_span_after
<F
: Fn(Token
) -> bool
>(&self, span
: Span
, f
: F
) -> Option
<Span
> {
340 let mut toks
= self.retokenise_span(span
);
342 let ts
= toks
.real_token();
343 if ts
.tok
== token
::Eof
{
347 let ts
= toks
.real_token();
348 if ts
.tok
== token
::Eof
{
351 return self.make_sub_span(span
, Some(ts
.sp
));
358 // Returns a list of the spans of idents in a path.
359 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
360 pub fn spans_for_path_segments(&self, path
: &ast
::Path
) -> Vec
<Span
> {
361 if generated_code(path
.span
) {
365 self.spans_with_brackets(path
.span
, 0, -1)
368 // Return an owned vector of the subspans of the param identifier
369 // tokens found in span.
370 pub fn spans_for_ty_params(&self, span
: Span
, number
: isize) -> Vec
<Span
> {
371 if generated_code(span
) {
374 // Type params are nested within one level of brackets:
375 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
376 self.spans_with_brackets(span
, 1, number
)
379 pub fn report_span_err(&self, kind
: &str, span
: Span
) {
380 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
381 info
!("({}) Could not find sub_span in `{}` in {}, line {}",
386 self.err_count
.set(self.err_count
.get() + 1);
387 if self.err_count
.get() > 1000 {
388 self.sess
.bug("span errors reached 1000, giving up");