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<int>,
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_uint(), lo_pos.to_uint(), lo_pos_byte.to_uint(),
44 hi_loc.line, hi_loc.col.to_uint(), hi_pos.to_uint(), hi_pos_byte.to_uint())
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 None => 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_str("<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 = 0u;
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::Self)) {
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 = 0u;
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::Self)) {
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 = 0u;
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 = 0u;
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::Shr) => -2,
212 if next.tok == token::Eof {
217 if bracket_count != 0 {
218 let loc = self.sess.codemap().lookup_char_pos(span.lo);
219 self.sess.span_bug(span,
220 format!("Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
221 self.snippet(span), loc.file.name, loc.line)[]);
223 if result.is_none() && prev.tok.is_ident() && bracket_count == 0 {
224 return self.make_sub_span(span, Some(prev.sp));
226 self.make_sub_span(span, result)
229 // Reparse span and return an owned vector of sub spans of the first limit
230 // identifier tokens in the given nesting level.
231 // example with Foo<Bar<T,V>, Bar<T,V>>
232 // Nesting = 0: all idents outside of brackets: ~[Foo]
233 // Nesting = 1: idents within one level of brackets: ~[Bar, Bar]
234 pub fn spans_with_brackets(&self, span: Span, nesting: int, limit: int) -> Vec<Span> {
235 let mut result: Vec<Span> = vec!();
237 let mut toks = self.retokenise_span(span);
238 // We keep track of how many brackets we're nested in
239 let mut bracket_count = 0i;
241 let ts = toks.real_token();
242 if ts.tok == token::Eof {
243 if bracket_count != 0 {
244 let loc = self.sess.codemap().lookup_char_pos(span.lo);
245 self.sess.span_bug(span, format!(
246 "Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
247 self.snippet(span), loc.file.name, loc.line)[]);
251 if (result.len() as int) == limit {
254 bracket_count += match ts.tok {
257 token::BinOp(token::Shl) => 2,
258 token::BinOp(token::Shr) => -2,
261 if ts.tok.is_ident() &&
262 bracket_count == nesting {
263 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
268 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
269 let mut toks = self.retokenise_span(span);
270 let mut prev = toks.real_token();
272 if prev.tok == token::Eof {
275 let next = toks.real_token();
277 return self.make_sub_span(span, Some(prev.sp));
283 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
284 let mut toks = self.retokenise_span(span);
286 let next = toks.real_token();
287 if next.tok == token::Eof {
291 return self.make_sub_span(span, Some(next.sp));
296 pub fn sub_span_after_keyword(&self,
298 keyword: keywords::Keyword) -> Option<Span> {
299 let mut toks = self.retokenise_span(span);
301 let ts = toks.real_token();
302 if ts.tok == token::Eof {
305 if ts.tok.is_keyword(keyword) {
306 let ts = toks.real_token();
307 if ts.tok == token::Eof {
310 return self.make_sub_span(span, Some(ts.sp));
316 // Returns a list of the spans of idents in a patch.
317 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
318 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
319 if generated_code(path.span) {
323 self.spans_with_brackets(path.span, 0, -1)
326 // Return an owned vector of the subspans of the param identifier
327 // tokens found in span.
328 pub fn spans_for_ty_params(&self, span: Span, number: int) -> Vec<Span> {
329 if generated_code(span) {
332 // Type params are nested within one level of brackets:
333 // i.e. we want ~[A, B] from Foo<A, B<T,U>>
334 self.spans_with_brackets(span, 1, number)
337 pub fn report_span_err(&self, kind: &str, span: Span) {
338 let loc = self.sess.codemap().lookup_char_pos(span.lo);
339 info!("({}) Could not find sub_span in `{}` in {}, line {}",
340 kind, self.snippet(span), loc.file.name, loc.line);
341 self.err_count.set(self.err_count.get()+1);
342 if self.err_count.get() > 1000 {
343 self.sess.bug("span errors reached 1000, giving up");