@@ -24,7 +24,16 @@ use std::str;
24
24
use std:: string;
25
25
use std:: iter;
26
26
27
- use syntax_pos:: Symbol ;
27
+ use syntax_pos:: { InnerSpan , Symbol } ;
28
+
29
+ #[ derive( Copy , Clone ) ]
30
+ struct InnerOffset ( usize ) ;
31
+
32
+ impl InnerOffset {
33
+ fn to ( self , end : InnerOffset ) -> InnerSpan {
34
+ InnerSpan :: new ( self . 0 , end. 0 )
35
+ }
36
+ }
28
37
29
38
/// A piece is a portion of the format string which represents the next part
30
39
/// to emit. These are emitted as a stream by the `Parser` class.
@@ -136,9 +145,8 @@ pub struct ParseError {
136
145
pub description : string:: String ,
137
146
pub note : Option < string:: String > ,
138
147
pub label : string:: String ,
139
- pub start : SpanIndex ,
140
- pub end : SpanIndex ,
141
- pub secondary_label : Option < ( string:: String , SpanIndex , SpanIndex ) > ,
148
+ pub span : InnerSpan ,
149
+ pub secondary_label : Option < ( string:: String , InnerSpan ) > ,
142
150
}
143
151
144
152
/// The parser structure for interpreting the input format string. This is
@@ -157,44 +165,36 @@ pub struct Parser<'a> {
157
165
/// `Some(raw count)` when the string is "raw", used to position spans correctly
158
166
style : Option < usize > ,
159
167
/// Start and end byte offset of every successfully parsed argument
160
- pub arg_places : Vec < ( SpanIndex , SpanIndex ) > ,
168
+ pub arg_places : Vec < InnerSpan > ,
161
169
/// Characters that need to be shifted
162
170
skips : Vec < usize > ,
163
- /// Span offset of the last opening brace seen, used for error reporting
164
- last_opening_brace_pos : Option < SpanIndex > ,
171
+ /// Span of the last opening brace seen, used for error reporting
172
+ last_opening_brace : Option < InnerSpan > ,
165
173
/// Wether the source string is comes from `println!` as opposed to `format!` or `print!`
166
174
append_newline : bool ,
167
175
}
168
176
169
- #[ derive( Clone , Copy , Debug ) ]
170
- pub struct SpanIndex ( pub usize ) ;
171
-
172
- impl SpanIndex {
173
- pub fn unwrap ( self ) -> usize {
174
- self . 0
175
- }
176
- }
177
-
178
177
impl < ' a > Iterator for Parser < ' a > {
179
178
type Item = Piece < ' a > ;
180
179
181
180
fn next ( & mut self ) -> Option < Piece < ' a > > {
182
181
if let Some ( & ( pos, c) ) = self . cur . peek ( ) {
183
182
match c {
184
183
'{' => {
185
- let curr_last_brace = self . last_opening_brace_pos ;
186
- self . last_opening_brace_pos = Some ( self . to_span_index ( pos) ) ;
184
+ let curr_last_brace = self . last_opening_brace ;
185
+ let byte_pos = self . to_span_index ( pos) ;
186
+ self . last_opening_brace = Some ( byte_pos. to ( byte_pos) ) ;
187
187
self . cur . next ( ) ;
188
188
if self . consume ( '{' ) {
189
- self . last_opening_brace_pos = curr_last_brace;
189
+ self . last_opening_brace = curr_last_brace;
190
190
191
191
Some ( String ( self . string ( pos + 1 ) ) )
192
192
} else {
193
193
let arg = self . argument ( ) ;
194
- if let Some ( arg_pos ) = self . must_consume ( '}' ) . map ( |end| {
195
- ( self . to_span_index ( pos) , self . to_span_index ( end + 1 ) )
196
- } ) {
197
- self . arg_places . push ( arg_pos ) ;
194
+ if let Some ( end ) = self . must_consume ( '}' ) {
195
+ let start = self . to_span_index ( pos) ;
196
+ let end = self . to_span_index ( end + 1 ) ;
197
+ self . arg_places . push ( start . to ( end ) ) ;
198
198
}
199
199
Some ( NextArgument ( arg) )
200
200
}
@@ -209,8 +209,7 @@ impl<'a> Iterator for Parser<'a> {
209
209
"unmatched `}` found" ,
210
210
"unmatched `}`" ,
211
211
"if you intended to print `}`, you can escape it using `}}`" ,
212
- err_pos,
213
- err_pos,
212
+ err_pos. to ( err_pos) ,
214
213
) ;
215
214
None
216
215
}
@@ -242,7 +241,7 @@ impl<'a> Parser<'a> {
242
241
style,
243
242
arg_places : vec ! [ ] ,
244
243
skips,
245
- last_opening_brace_pos : None ,
244
+ last_opening_brace : None ,
246
245
append_newline,
247
246
}
248
247
}
@@ -254,15 +253,13 @@ impl<'a> Parser<'a> {
254
253
& mut self ,
255
254
description : S1 ,
256
255
label : S2 ,
257
- start : SpanIndex ,
258
- end : SpanIndex ,
256
+ span : InnerSpan ,
259
257
) {
260
258
self . errors . push ( ParseError {
261
259
description : description. into ( ) ,
262
260
note : None ,
263
261
label : label. into ( ) ,
264
- start,
265
- end,
262
+ span,
266
263
secondary_label : None ,
267
264
} ) ;
268
265
}
@@ -275,15 +272,13 @@ impl<'a> Parser<'a> {
275
272
description : S1 ,
276
273
label : S2 ,
277
274
note : S3 ,
278
- start : SpanIndex ,
279
- end : SpanIndex ,
275
+ span : InnerSpan ,
280
276
) {
281
277
self . errors . push ( ParseError {
282
278
description : description. into ( ) ,
283
279
note : Some ( note. into ( ) ) ,
284
280
label : label. into ( ) ,
285
- start,
286
- end,
281
+ span,
287
282
secondary_label : None ,
288
283
} ) ;
289
284
}
@@ -304,7 +299,7 @@ impl<'a> Parser<'a> {
304
299
}
305
300
}
306
301
307
- fn to_span_index ( & self , pos : usize ) -> SpanIndex {
302
+ fn to_span_index ( & self , pos : usize ) -> InnerOffset {
308
303
let mut pos = pos;
309
304
let raw = self . style . map ( |raw| raw + 1 ) . unwrap_or ( 0 ) ;
310
305
for skip in & self . skips {
@@ -316,7 +311,7 @@ impl<'a> Parser<'a> {
316
311
break ;
317
312
}
318
313
}
319
- SpanIndex ( raw + pos + 1 )
314
+ InnerOffset ( raw + pos + 1 )
320
315
}
321
316
322
317
/// Forces consumption of the specified character. If the character is not
@@ -334,8 +329,8 @@ impl<'a> Parser<'a> {
334
329
let label = "expected `}`" . to_owned ( ) ;
335
330
let ( note, secondary_label) = if c == '}' {
336
331
( Some ( "if you intended to print `{`, you can escape it using `{{`" . to_owned ( ) ) ,
337
- self . last_opening_brace_pos . map ( |pos | {
338
- ( "because of this opening brace" . to_owned ( ) , pos , pos )
332
+ self . last_opening_brace . map ( |sp | {
333
+ ( "because of this opening brace" . to_owned ( ) , sp )
339
334
} ) )
340
335
} else {
341
336
( None , None )
@@ -344,8 +339,7 @@ impl<'a> Parser<'a> {
344
339
description,
345
340
note,
346
341
label,
347
- start : pos,
348
- end : pos,
342
+ span : pos. to ( pos) ,
349
343
secondary_label,
350
344
} ) ;
351
345
None
@@ -359,8 +353,8 @@ impl<'a> Parser<'a> {
359
353
let label = format ! ( "expected `{:?}`" , c) ;
360
354
let ( note, secondary_label) = if c == '}' {
361
355
( Some ( "if you intended to print `{`, you can escape it using `{{`" . to_owned ( ) ) ,
362
- self . last_opening_brace_pos . map ( |pos | {
363
- ( "because of this opening brace" . to_owned ( ) , pos , pos )
356
+ self . last_opening_brace . map ( |sp | {
357
+ ( "because of this opening brace" . to_owned ( ) , sp )
364
358
} ) )
365
359
} else {
366
360
( None , None )
@@ -369,12 +363,11 @@ impl<'a> Parser<'a> {
369
363
description,
370
364
note,
371
365
label,
372
- start : pos,
373
- end : pos,
366
+ span : pos. to ( pos) ,
374
367
secondary_label,
375
368
} ) ;
376
369
} else {
377
- self . err ( description, format ! ( "expected `{:?}`" , c) , pos, pos) ;
370
+ self . err ( description, format ! ( "expected `{:?}`" , c) , pos. to ( pos) ) ;
378
371
}
379
372
None
380
373
}
@@ -446,8 +439,10 @@ impl<'a> Parser<'a> {
446
439
self . err_with_note ( format ! ( "invalid argument name `{}`" , invalid_name) ,
447
440
"invalid argument name" ,
448
441
"argument names cannot start with an underscore" ,
449
- self . to_span_index ( pos) ,
450
- self . to_span_index ( pos + invalid_name. len ( ) ) ) ;
442
+ self . to_span_index ( pos) . to (
443
+ self . to_span_index ( pos + invalid_name. len ( ) )
444
+ ) ,
445
+ ) ;
451
446
Some ( ArgumentNamed ( Symbol :: intern ( invalid_name) ) )
452
447
} ,
453
448
0 commit comments