@@ -17,7 +17,6 @@ use std::env;
17
17
use std:: path:: Path ;
18
18
19
19
use syntax:: ast;
20
- use syntax:: parse:: filemap_to_tts;
21
20
use syntax:: parse:: lexer:: { self , StringReader } ;
22
21
use syntax:: parse:: token:: { self , Token } ;
23
22
use syntax:: symbol:: keywords;
@@ -49,23 +48,6 @@ impl<'a> SpanUtils<'a> {
49
48
}
50
49
}
51
50
52
- // sub_span starts at span.lo, so we need to adjust the positions etc.
53
- // If sub_span is None, we don't need to adjust.
54
- pub fn make_sub_span ( & self , span : Span , sub_span : Option < Span > ) -> Option < Span > {
55
- match sub_span {
56
- None => None ,
57
- Some ( sub) => {
58
- let FileMapAndBytePos { fm, pos} = self . sess . codemap ( ) . lookup_byte_offset ( span. lo ) ;
59
- let base = pos + fm. start_pos ;
60
- Some ( Span {
61
- lo : base + self . sess . codemap ( ) . lookup_byte_offset ( sub. lo ) . pos ,
62
- hi : base + self . sess . codemap ( ) . lookup_byte_offset ( sub. hi ) . pos ,
63
- expn_id : span. expn_id ,
64
- } )
65
- }
66
- }
67
- }
68
-
69
51
pub fn snippet ( & self , span : Span ) -> String {
70
52
match self . sess . codemap ( ) . span_to_snippet ( span) {
71
53
Ok ( s) => s,
@@ -74,24 +56,7 @@ impl<'a> SpanUtils<'a> {
74
56
}
75
57
76
58
pub fn retokenise_span ( & self , span : Span ) -> StringReader < ' a > {
77
- // sadness - we don't have spans for sub-expressions nor access to the tokens
78
- // so in order to get extents for the function name itself (which dxr expects)
79
- // we need to re-tokenise the fn definition
80
-
81
- // Note: this is a bit awful - it adds the contents of span to the end of
82
- // the codemap as a new filemap. This is mostly OK, but means we should
83
- // not iterate over the codemap. Also, any spans over the new filemap
84
- // are incompatible with spans over other filemaps.
85
- let filemap = self . sess
86
- . codemap ( )
87
- . new_filemap ( String :: from ( "<anon-dxr>" ) , None , self . snippet ( span) ) ;
88
- lexer:: StringReader :: new ( & self . sess . parse_sess , filemap)
89
- }
90
-
91
- fn span_to_tts ( & self , span : Span ) -> Vec < TokenTree > {
92
- let filename = String :: from ( "<anon-dxr>" ) ;
93
- let filemap = self . sess . codemap ( ) . new_filemap ( filename, None , self . snippet ( span) ) ;
94
- filemap_to_tts ( & self . sess . parse_sess , filemap)
59
+ lexer:: StringReader :: retokenize ( & self . sess . parse_sess , span)
95
60
}
96
61
97
62
// Re-parses a path and returns the span for the last identifier in the path
@@ -103,7 +68,7 @@ impl<'a> SpanUtils<'a> {
103
68
loop {
104
69
let ts = toks. real_token ( ) ;
105
70
if ts. tok == token:: Eof {
106
- return self . make_sub_span ( span , result)
71
+ return result
107
72
}
108
73
if bracket_count == 0 && ( ts. tok . is_ident ( ) || ts. tok . is_keyword ( keywords:: SelfValue ) ) {
109
74
result = Some ( ts. sp ) ;
@@ -128,7 +93,7 @@ impl<'a> SpanUtils<'a> {
128
93
return None ;
129
94
}
130
95
if bracket_count == 0 && ( ts. tok . is_ident ( ) || ts. tok . is_keyword ( keywords:: SelfValue ) ) {
131
- return self . make_sub_span ( span , Some ( ts. sp ) ) ;
96
+ return Some ( ts. sp ) ;
132
97
}
133
98
134
99
bracket_count += match ts. tok {
@@ -178,10 +143,7 @@ impl<'a> SpanUtils<'a> {
178
143
}
179
144
prev = next;
180
145
}
181
- if result. is_none ( ) && prev_span. is_some ( ) {
182
- return self . make_sub_span ( span, prev_span) ;
183
- }
184
- return self . make_sub_span ( span, result) ;
146
+ result. or ( prev_span)
185
147
}
186
148
187
149
// Return the span for the last ident before a `<` and outside any
@@ -241,9 +203,9 @@ impl<'a> SpanUtils<'a> {
241
203
loc. line) ;
242
204
}
243
205
if result. is_none ( ) && prev. tok . is_ident ( ) && angle_count == 0 {
244
- return self . make_sub_span ( span , Some ( prev. sp ) ) ;
206
+ return Some ( prev. sp ) ;
245
207
}
246
- self . make_sub_span ( span , result)
208
+ result
247
209
}
248
210
249
211
// Reparse span and return an owned vector of sub spans of the first limit
@@ -310,7 +272,7 @@ impl<'a> SpanUtils<'a> {
310
272
angle_count += 1 ;
311
273
}
312
274
if ts. tok . is_ident ( ) && angle_count == nesting {
313
- result. push ( self . make_sub_span ( span , Some ( ts. sp ) ) . unwrap ( ) ) ;
275
+ result. push ( ts. sp ) ;
314
276
}
315
277
}
316
278
}
@@ -320,8 +282,11 @@ impl<'a> SpanUtils<'a> {
320
282
/// end of the 'signature' part, that is up to, but not including an opening
321
283
/// brace or semicolon.
322
284
pub fn signature_string_for_span ( & self , span : Span ) -> String {
323
- let mut toks = self . span_to_tts ( span) . into_iter ( ) ;
285
+ let mut toks = self . retokenise_span ( span) ;
286
+ toks. real_token ( ) ;
287
+ let mut toks = toks. parse_all_token_trees ( ) . unwrap ( ) . into_iter ( ) ;
324
288
let mut prev = toks. next ( ) . unwrap ( ) ;
289
+
325
290
let first_span = prev. get_span ( ) ;
326
291
let mut angle_count = 0 ;
327
292
for tok in toks {
@@ -360,7 +325,7 @@ impl<'a> SpanUtils<'a> {
360
325
}
361
326
let next = toks. real_token ( ) ;
362
327
if next. tok == tok {
363
- return self . make_sub_span ( span , Some ( prev. sp ) ) ;
328
+ return Some ( prev. sp ) ;
364
329
}
365
330
prev = next;
366
331
}
@@ -374,7 +339,7 @@ impl<'a> SpanUtils<'a> {
374
339
return None ;
375
340
}
376
341
if next. tok == tok {
377
- return self . make_sub_span ( span , Some ( next. sp ) ) ;
342
+ return Some ( next. sp ) ;
378
343
}
379
344
}
380
345
}
@@ -399,7 +364,7 @@ impl<'a> SpanUtils<'a> {
399
364
if ts. tok == token:: Eof {
400
365
return None
401
366
} else {
402
- return self . make_sub_span ( span , Some ( ts. sp ) ) ;
367
+ return Some ( ts. sp ) ;
403
368
}
404
369
}
405
370
}
@@ -444,7 +409,7 @@ impl<'a> SpanUtils<'a> {
444
409
if ts. tok == token:: Not {
445
410
let ts = toks. real_token ( ) ;
446
411
if ts. tok . is_ident ( ) {
447
- return self . make_sub_span ( span , Some ( ts. sp ) ) ;
412
+ return Some ( ts. sp ) ;
448
413
} else {
449
414
return None ;
450
415
}
@@ -463,7 +428,7 @@ impl<'a> SpanUtils<'a> {
463
428
let ts = toks. real_token ( ) ;
464
429
if ts. tok == token:: Not {
465
430
if prev. tok . is_ident ( ) {
466
- return self . make_sub_span ( span , Some ( prev. sp ) ) ;
431
+ return Some ( prev. sp ) ;
467
432
} else {
468
433
return None ;
469
434
}
0 commit comments