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
;
18 use syntax
::codemap
::*;
19 use syntax
::parse
::lexer
;
20 use syntax
::parse
::lexer
::{Reader,StringReader}
;
21 use syntax
::parse
::token
;
22 use syntax
::parse
::token
::{keywords, Token}
;
25 pub struct SpanUtils
<'a
> {
26 pub sess
: &'a Session
,
27 pub err_count
: Cell
<isize>,
30 impl<'a
> SpanUtils
<'a
> {
31 // Standard string for extents/location.
32 pub fn extent_str(&self, span
: Span
) -> String
{
33 let lo_loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
34 let hi_loc
= self.sess
.codemap().lookup_char_pos(span
.hi
);
35 let lo_pos
= self.sess
.codemap().bytepos_to_file_charpos(span
.lo
);
36 let hi_pos
= self.sess
.codemap().bytepos_to_file_charpos(span
.hi
);
37 let lo_pos_byte
= self.sess
.codemap().lookup_byte_offset(span
.lo
).pos
;
38 let hi_pos_byte
= self.sess
.codemap().lookup_byte_offset(span
.hi
).pos
;
40 format
!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
41 file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
43 lo_loc
.line
, lo_loc
.col
.to_usize(), lo_pos
.to_usize(), lo_pos_byte
.to_usize(),
44 hi_loc
.line
, hi_loc
.col
.to_usize(), hi_pos
.to_usize(), hi_pos_byte
.to_usize())
47 // sub_span starts at span.lo, so we need to adjust the positions etc.
48 // If sub_span is None, we don't need to adjust.
49 pub fn make_sub_span(&self, span
: Span
, sub_span
: Option
<Span
>) -> Option
<Span
> {
50 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
51 assert
!(!generated_code(span
),
52 "generated code; we should not be processing this `{}` in {}, line {}",
53 self.snippet(span
), loc
.file
.name
, loc
.line
);
58 let FileMapAndBytePos {fm, pos}
=
59 self.sess
.codemap().lookup_byte_offset(span
.lo
);
60 let base
= pos
+ fm
.start_pos
;
62 lo
: base
+ self.sess
.codemap().lookup_byte_offset(sub
.lo
).pos
,
63 hi
: base
+ self.sess
.codemap().lookup_byte_offset(sub
.hi
).pos
,
64 expn_id
: NO_EXPANSION
,
70 pub fn snippet(&self, span
: Span
) -> String
{
71 match self.sess
.codemap().span_to_snippet(span
) {
73 Err(_
) => String
::new(),
77 pub fn retokenise_span(&self, span
: Span
) -> StringReader
<'a
> {
78 // sadness - we don't have spans for sub-expressions nor access to the tokens
79 // so in order to get extents for the function name itself (which dxr expects)
80 // we need to re-tokenise the fn definition
82 // Note: this is a bit awful - it adds the contents of span to the end of
83 // the codemap as a new filemap. This is mostly OK, but means we should
84 // not iterate over the codemap. Also, any spans over the new filemap
85 // are incompatible with spans over other filemaps.
86 let filemap
= self.sess
.codemap().new_filemap(String
::from("<anon-dxr>"),
89 lexer
::StringReader
::new(s
.diagnostic(), filemap
)
92 // Re-parses a path and returns the span for the last identifier in the path
93 pub fn span_for_last_ident(&self, span
: Span
) -> Option
<Span
> {
94 let mut result
= None
;
96 let mut toks
= self.retokenise_span(span
);
97 let mut bracket_count
= 0;
99 let ts
= toks
.real_token();
100 if ts
.tok
== token
::Eof
{
101 return self.make_sub_span(span
, result
)
103 if bracket_count
== 0 &&
104 (ts
.tok
.is_ident() || ts
.tok
.is_keyword(keywords
::SelfValue
)) {
105 result
= Some(ts
.sp
);
108 bracket_count
+= match ts
.tok
{
111 token
::BinOp(token
::Shr
) => -2,
117 // Return the span for the first identifier in the path.
118 pub fn span_for_first_ident(&self, span
: Span
) -> Option
<Span
> {
119 let mut toks
= self.retokenise_span(span
);
120 let mut bracket_count
= 0;
122 let ts
= toks
.real_token();
123 if ts
.tok
== token
::Eof
{
126 if bracket_count
== 0 &&
127 (ts
.tok
.is_ident() || ts
.tok
.is_keyword(keywords
::SelfValue
)) {
128 return self.make_sub_span(span
, Some(ts
.sp
));
131 bracket_count
+= match ts
.tok
{
134 token
::BinOp(token
::Shr
) => -2,
140 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
141 // any brackets, or the last span.
142 pub fn sub_span_for_meth_name(&self, span
: Span
) -> Option
<Span
> {
143 let mut toks
= self.retokenise_span(span
);
144 let mut prev
= toks
.real_token();
145 let mut result
= None
;
146 let mut bracket_count
= 0;
147 let mut last_span
= None
;
148 while prev
.tok
!= token
::Eof
{
150 let mut next
= toks
.real_token();
152 if (next
.tok
== token
::OpenDelim(token
::Paren
) ||
153 next
.tok
== token
::Lt
) &&
154 bracket_count
== 0 &&
155 prev
.tok
.is_ident() {
156 result
= Some(prev
.sp
);
159 if bracket_count
== 0 &&
160 next
.tok
== token
::ModSep
{
163 next
= toks
.real_token();
164 if next
.tok
== token
::Lt
&&
166 result
= Some(old
.sp
);
170 bracket_count
+= match prev
.tok
{
171 token
::OpenDelim(token
::Paren
) | token
::Lt
=> 1,
172 token
::CloseDelim(token
::Paren
) | token
::Gt
=> -1,
173 token
::BinOp(token
::Shr
) => -2,
177 if prev
.tok
.is_ident() && bracket_count
== 0 {
178 last_span
= Some(prev
.sp
);
182 if result
.is_none() && last_span
.is_some() {
183 return self.make_sub_span(span
, last_span
);
185 return self.make_sub_span(span
, result
);
188 // Return the span for the last ident before a `<` and outside any
189 // brackets, or the last span.
190 pub fn sub_span_for_type_name(&self, span
: Span
) -> Option
<Span
> {
191 let mut toks
= self.retokenise_span(span
);
192 let mut prev
= toks
.real_token();
193 let mut result
= None
;
194 let mut bracket_count
= 0;
196 let next
= toks
.real_token();
198 if (next
.tok
== token
::Lt
||
199 next
.tok
== token
::Colon
) &&
200 bracket_count
== 0 &&
201 prev
.tok
.is_ident() {
202 result
= Some(prev
.sp
);
205 bracket_count
+= match prev
.tok
{
208 token
::BinOp(token
::Shl
) => 2,
209 token
::BinOp(token
::Shr
) => -2,
213 if next
.tok
== token
::Eof
{
218 if bracket_count
!= 0 {
219 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
220 self.sess
.span_bug(span
,
221 &format
!("Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
222 self.snippet(span
), loc
.file
.name
, loc
.line
));
224 if result
.is_none() && prev
.tok
.is_ident() && bracket_count
== 0 {
225 return self.make_sub_span(span
, Some(prev
.sp
));
227 self.make_sub_span(span
, result
)
230 // Reparse span and return an owned vector of sub spans of the first limit
231 // identifier tokens in the given nesting level.
232 // example with Foo<Bar<T,V>, Bar<T,V>>
233 // Nesting = 0: all idents outside of brackets: [Foo]
234 // Nesting = 1: idents within one level of brackets: [Bar, Bar]
235 pub fn spans_with_brackets(&self, span
: Span
, nesting
: isize, limit
: isize) -> Vec
<Span
> {
236 let mut result
: Vec
<Span
> = vec
!();
238 let mut toks
= self.retokenise_span(span
);
239 // We keep track of how many brackets we're nested in
240 let mut bracket_count
: isize = 0;
241 let mut found_ufcs_sep
= false;
243 let ts
= toks
.real_token();
244 if ts
.tok
== token
::Eof
{
245 if bracket_count
!= 0 {
246 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
247 self.sess
.span_bug(span
, &format
!(
248 "Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
249 self.snippet(span
), loc
.file
.name
, loc
.line
));
253 if (result
.len() as isize) == limit
{
256 bracket_count
+= match ts
.tok
{
259 token
::BinOp(token
::Shl
) => 2,
260 token
::BinOp(token
::Shr
) => -2,
264 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
266 // The root cause of this hack is that the AST representation of
267 // qpaths is horrible. It treats <A as B>::C as a path with two
268 // segments, B and C and notes that there is also a self type A at
269 // position 0. Because we don't have spans for individual idents,
270 // only the whole path, we have to iterate over the tokens in the
271 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
272 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
273 // the start of the first ident to the end of the path.
274 if !found_ufcs_sep
&& bracket_count
== -1 {
275 found_ufcs_sep
= true;
278 if ts
.tok
.is_ident() && bracket_count
== nesting
{
279 result
.push(self.make_sub_span(span
, Some(ts
.sp
)).unwrap());
284 pub fn sub_span_before_token(&self, span
: Span
, tok
: Token
) -> Option
<Span
> {
285 let mut toks
= self.retokenise_span(span
);
286 let mut prev
= toks
.real_token();
288 if prev
.tok
== token
::Eof
{
291 let next
= toks
.real_token();
293 return self.make_sub_span(span
, Some(prev
.sp
));
299 pub fn sub_span_of_token(&self, span
: Span
, tok
: Token
) -> Option
<Span
> {
300 let mut toks
= self.retokenise_span(span
);
302 let next
= toks
.real_token();
303 if next
.tok
== token
::Eof
{
307 return self.make_sub_span(span
, Some(next
.sp
));
312 pub fn sub_span_after_keyword(&self,
314 keyword
: keywords
::Keyword
) -> Option
<Span
> {
315 self.sub_span_after(span
, |t
| t
.is_keyword(keyword
))
318 pub fn sub_span_after_token(&self,
320 tok
: Token
) -> Option
<Span
> {
321 self.sub_span_after(span
, |t
| t
== tok
)
324 fn sub_span_after
<F
: Fn(Token
) -> bool
>(&self,
326 f
: F
) -> Option
<Span
> {
327 let mut toks
= self.retokenise_span(span
);
329 let ts
= toks
.real_token();
330 if ts
.tok
== token
::Eof
{
334 let ts
= toks
.real_token();
335 if ts
.tok
== token
::Eof
{
338 return self.make_sub_span(span
, Some(ts
.sp
));
345 // Returns a list of the spans of idents in a path.
346 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
347 pub fn spans_for_path_segments(&self, path
: &ast
::Path
) -> Vec
<Span
> {
348 if generated_code(path
.span
) {
352 self.spans_with_brackets(path
.span
, 0, -1)
355 // Return an owned vector of the subspans of the param identifier
356 // tokens found in span.
357 pub fn spans_for_ty_params(&self, span
: Span
, number
: isize) -> Vec
<Span
> {
358 if generated_code(span
) {
361 // Type params are nested within one level of brackets:
362 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
363 self.spans_with_brackets(span
, 1, number
)
366 pub fn report_span_err(&self, kind
: &str, span
: Span
) {
367 let loc
= self.sess
.codemap().lookup_char_pos(span
.lo
);
368 info
!("({}) Could not find sub_span in `{}` in {}, line {}",
369 kind
, self.snippet(span
), loc
.file
.name
, loc
.line
);
370 self.err_count
.set(self.err_count
.get()+1);
371 if self.err_count
.get() > 1000 {
372 self.sess
.bug("span errors reached 1000, giving up");