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 driver::session::Session;
13 use middle::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::{is_keyword,keywords,is_ident,Token};
24 pub struct SpanUtils<'a> {
25 pub sess: &'a Session,
26 pub err_count: Cell<int>,
29 impl<'a> SpanUtils<'a> {
30 // Standard string for extents/location.
31 pub fn extent_str(&self, span: Span) -> String {
32 let lo_loc = self.sess.codemap().lookup_char_pos(span.lo);
33 let hi_loc = self.sess.codemap().lookup_char_pos(span.hi);
34 let lo_pos = self.sess.codemap().lookup_byte_offset(span.lo).pos;
35 let hi_pos = self.sess.codemap().lookup_byte_offset(span.hi).pos;
37 format!("file_name,{},file_line,{},file_col,{},extent_start,{},\
38 file_line_end,{},file_col_end,{},extent_end,{}",
39 lo_loc.file.name, lo_loc.line, lo_loc.col.to_uint(), lo_pos.to_uint(),
40 hi_loc.line, hi_loc.col.to_uint(), hi_pos.to_uint())
43 // sub_span starts at span.lo, so we need to adjust the positions etc.
44 // If sub_span is None, we don't need to adjust.
45 pub fn make_sub_span(&self, span: Span, sub_span: Option<Span>) -> Option<Span> {
46 let loc = self.sess.codemap().lookup_char_pos(span.lo);
47 assert!(!generated_code(span),
48 "generated code; we should not be processing this `{}` in {}, line {}",
49 self.snippet(span), loc.file.name, loc.line);
54 let FileMapAndBytePos {fm, pos} =
55 self.sess.codemap().lookup_byte_offset(span.lo);
56 let base = pos + fm.start_pos;
58 lo: base + self.sess.codemap().lookup_byte_offset(sub.lo).pos,
59 hi: base + self.sess.codemap().lookup_byte_offset(sub.hi).pos,
66 pub fn snippet(&self, span: Span) -> String {
67 match self.sess.codemap().span_to_snippet(span) {
69 None => String::new(),
73 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
74 // sadness - we don't have spans for sub-expressions nor access to the tokens
75 // so in order to get extents for the function name itself (which dxr expects)
76 // we need to re-tokenise the fn definition
78 // Note: this is a bit awful - it adds the contents of span to the end of
79 // the codemap as a new filemap. This is mostly OK, but means we should
80 // not iterate over the codemap. Also, any spans over the new filemap
81 // are incompatible with spans over other filemaps.
82 let filemap = self.sess.codemap().new_filemap(String::from_str("<anon-dxr>"),
85 lexer::StringReader::new(s.diagnostic(), filemap)
88 // Re-parses a path and returns the span for the last identifier in the path
89 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
90 let mut result = None;
92 let mut toks = self.retokenise_span(span);
93 let mut bracket_count = 0u;
95 let ts = toks.next_token();
96 if ts.tok == token::EOF {
97 return self.make_sub_span(span, result)
99 if bracket_count == 0 &&
100 (is_ident(&ts.tok) || is_keyword(keywords::Self, &ts.tok)) {
101 result = Some(ts.sp);
104 bracket_count += match ts.tok {
107 token::BINOP(token::SHR) => -2,
113 // Return the span for the first identifier in the path.
114 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
115 let mut toks = self.retokenise_span(span);
116 let mut bracket_count = 0u;
118 let ts = toks.next_token();
119 if ts.tok == token::EOF {
122 if bracket_count == 0 &&
123 (is_ident(&ts.tok) || is_keyword(keywords::Self, &ts.tok)) {
124 return self.make_sub_span(span, Some(ts.sp));
127 bracket_count += match ts.tok {
130 token::BINOP(token::SHR) => -2,
136 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
137 // any brackets, or the last span.
138 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
139 let mut toks = self.retokenise_span(span);
140 let mut prev = toks.next_token();
141 let mut result = None;
142 let mut bracket_count = 0u;
143 let mut last_span = None;
144 while prev.tok != token::EOF {
146 let mut next = toks.next_token();
148 if (next.tok == token::LPAREN ||
149 next.tok == token::LT) &&
150 bracket_count == 0 &&
151 is_ident(&prev.tok) {
152 result = Some(prev.sp);
155 if bracket_count == 0 &&
156 next.tok == token::MOD_SEP {
159 next = toks.next_token();
160 if next.tok == token::LT &&
162 result = Some(old.sp);
166 bracket_count += match prev.tok {
167 token::LPAREN | token::LT => 1,
168 token::RPAREN | token::GT => -1,
169 token::BINOP(token::SHR) => -2,
173 if is_ident(&prev.tok) && bracket_count == 0 {
174 last_span = Some(prev.sp);
178 if result.is_none() && last_span.is_some() {
179 return self.make_sub_span(span, last_span);
181 return self.make_sub_span(span, result);
184 // Return the span for the last ident before a `<` and outside any
185 // brackets, or the last span.
186 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
187 let mut toks = self.retokenise_span(span);
188 let mut prev = toks.next_token();
189 let mut result = None;
190 let mut bracket_count = 0u;
192 let next = toks.next_token();
194 if (next.tok == token::LT ||
195 next.tok == token::COLON) &&
196 bracket_count == 0 &&
197 is_ident(&prev.tok) {
198 result = Some(prev.sp);
201 bracket_count += match prev.tok {
204 token::BINOP(token::SHR) => -2,
208 if next.tok == token::EOF {
213 if bracket_count != 0 {
214 let loc = self.sess.codemap().lookup_char_pos(span.lo);
215 self.sess.span_bug(span,
216 format!("Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
217 self.snippet(span), loc.file.name, loc.line).as_slice());
219 if result.is_none() && is_ident(&prev.tok) && bracket_count == 0 {
220 return self.make_sub_span(span, Some(prev.sp));
222 self.make_sub_span(span, result)
225 // Reparse span and return an owned vector of sub spans of the first limit
226 // identifier tokens in the given nesting level.
227 // example with Foo<Bar<T,V>, Bar<T,V>>
228 // Nesting = 0: all idents outside of brackets: ~[Foo]
229 // Nesting = 1: idents within one level of brackets: ~[Bar, Bar]
230 pub fn spans_with_brackets(&self, span: Span, nesting: int, limit: int) -> Vec<Span> {
231 let mut result: Vec<Span> = vec!();
233 let mut toks = self.retokenise_span(span);
234 // We keep track of how many brackets we're nested in
235 let mut bracket_count = 0i;
237 let ts = toks.next_token();
238 if ts.tok == token::EOF {
239 if bracket_count != 0 {
240 let loc = self.sess.codemap().lookup_char_pos(span.lo);
241 self.sess.span_bug(span, format!(
242 "Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}",
243 self.snippet(span), loc.file.name, loc.line).as_slice());
247 if (result.len() as int) == limit {
250 bracket_count += match ts.tok {
253 token::BINOP(token::SHL) => 2,
254 token::BINOP(token::SHR) => -2,
257 if is_ident(&ts.tok) &&
258 bracket_count == nesting {
259 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
264 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
265 let mut toks = self.retokenise_span(span);
266 let mut prev = toks.next_token();
268 if prev.tok == token::EOF {
271 let next = toks.next_token();
273 return self.make_sub_span(span, Some(prev.sp));
279 // Return an owned vector of the subspans of the tokens that come before tok2
280 // which is before tok1. If there is no instance of tok2 before tok1, then that
281 // place in the result is None.
282 // Everything returned must be inside a set of (non-angle) brackets, but no
283 // more deeply nested than that.
284 pub fn sub_spans_before_tokens(&self,
287 tok2: Token) -> Vec<Option<Span>> {
288 let mut sub_spans : Vec<Option<Span>> = vec!();
289 let mut toks = self.retokenise_span(span);
290 let mut prev = toks.next_token();
291 let mut next = toks.next_token();
292 let mut stored_val = false;
293 let mut found_val = false;
294 let mut bracket_count = 0u;
295 while next.tok != token::EOF {
296 if bracket_count == 1 {
297 if next.tok == tok2 {
298 sub_spans.push(self.make_sub_span(span, Some(prev.sp)));
302 if next.tok == tok1 {
304 sub_spans.push(None);
311 is_ident(&next.tok) {
316 bracket_count += match next.tok {
317 token::LPAREN | token::LBRACE => 1,
318 token::RPAREN | token::RBRACE => -1,
323 next = toks.next_token();
326 sub_spans.push(None);
331 pub fn sub_span_after_keyword(&self,
333 keyword: keywords::Keyword) -> Option<Span> {
334 let mut toks = self.retokenise_span(span);
336 let ts = toks.next_token();
337 if ts.tok == token::EOF {
340 if is_keyword(keyword, &ts.tok) {
341 let ts = toks.next_token();
342 if ts.tok == token::EOF {
345 return self.make_sub_span(span, Some(ts.sp));
351 // Returns a list of the spans of idents in a patch.
352 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
353 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
354 if generated_code(path.span) {
358 self.spans_with_brackets(path.span, 0, -1)
361 // Return an owned vector of the subspans of the param identifier
362 // tokens found in span.
363 pub fn spans_for_ty_params(&self, span: Span, number: int) -> Vec<Span> {
364 if generated_code(span) {
367 // Type params are nested within one level of brackets:
368 // i.e. we want ~[A, B] from Foo<A, B<T,U>>
369 self.spans_with_brackets(span, 1, number)
372 pub fn report_span_err(&self, kind: &str, span: Span) {
373 let loc = self.sess.codemap().lookup_char_pos(span.lo);
374 info!("({}) Could not find sub_span in `{}` in {}, line {}",
375 kind, self.snippet(span), loc.file.name, loc.line);
376 self.err_count.set(self.err_count.get()+1);
377 if self.err_count.get() > 1000 {
378 self.sess.bug("span errors reached 1000, giving up");