@@ -24,7 +24,7 @@ use str::char_at;
24
24
use std:: io:: Read ;
25
25
use std:: usize;
26
26
27
- #[ derive( Clone , Copy , PartialEq ) ]
27
+ #[ derive( Clone , Copy , PartialEq , Debug ) ]
28
28
pub enum CommentStyle {
29
29
/// No code on either side of each line of the comment
30
30
Isolated ,
@@ -155,14 +155,13 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) {
155
155
156
156
fn consume_whitespace_counting_blank_lines ( rdr : & mut StringReader , comments : & mut Vec < Comment > ) {
157
157
while is_pattern_whitespace ( rdr. ch ) && !rdr. is_eof ( ) {
158
- if rdr. col == CharPos ( 0 ) && rdr . ch_is ( '\n' ) {
158
+ if rdr. ch_is ( '\n' ) {
159
159
push_blank_line_comment ( rdr, & mut * comments) ;
160
160
}
161
161
rdr. bump ( ) ;
162
162
}
163
163
}
164
164
165
-
166
165
fn read_shebang_comment ( rdr : & mut StringReader ,
167
166
code_to_the_left : bool ,
168
167
comments : & mut Vec < Comment > ) {
@@ -317,14 +316,22 @@ fn read_block_comment(rdr: &mut StringReader,
317
316
}
318
317
319
318
320
- fn consume_comment ( rdr : & mut StringReader , code_to_the_left : bool , comments : & mut Vec < Comment > ) {
319
+ fn consume_comment ( rdr : & mut StringReader ,
320
+ comments : & mut Vec < Comment > ,
321
+ code_to_the_left : & mut bool ,
322
+ anything_to_the_left : & mut bool ) {
321
323
debug ! ( ">>> consume comment" ) ;
322
324
if rdr. ch_is ( '/' ) && rdr. nextch_is ( '/' ) {
323
- read_line_comments ( rdr, code_to_the_left, comments) ;
325
+ read_line_comments ( rdr, * code_to_the_left, comments) ;
326
+ * code_to_the_left = false ;
327
+ * anything_to_the_left = false ;
324
328
} else if rdr. ch_is ( '/' ) && rdr. nextch_is ( '*' ) {
325
- read_block_comment ( rdr, code_to_the_left, comments) ;
329
+ read_block_comment ( rdr, * code_to_the_left, comments) ;
330
+ * anything_to_the_left = true ;
326
331
} else if rdr. ch_is ( '#' ) && rdr. nextch_is ( '!' ) {
327
- read_shebang_comment ( rdr, code_to_the_left, comments) ;
332
+ read_shebang_comment ( rdr, * code_to_the_left, comments) ;
333
+ * code_to_the_left = false ;
334
+ * anything_to_the_left = false ;
328
335
} else {
329
336
panic ! ( ) ;
330
337
}
@@ -352,23 +359,29 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
352
359
353
360
let mut comments: Vec < Comment > = Vec :: new ( ) ;
354
361
let mut literals: Vec < Literal > = Vec :: new ( ) ;
355
- let mut first_read: bool = true ;
362
+ let mut code_to_the_left = false ; // Only code
363
+ let mut anything_to_the_left = false ; // Code or comments
356
364
while !rdr. is_eof ( ) {
357
365
loop {
358
- let mut code_to_the_left = !first_read ;
366
+ // Eat all the whitespace and count blank lines.
359
367
rdr. consume_non_eol_whitespace ( ) ;
360
368
if rdr. ch_is ( '\n' ) {
361
- code_to_the_left = false ;
369
+ if anything_to_the_left {
370
+ rdr. bump ( ) ; // The line is not blank, do not count.
371
+ }
362
372
consume_whitespace_counting_blank_lines ( & mut rdr, & mut comments) ;
373
+ code_to_the_left = false ;
374
+ anything_to_the_left = false ;
363
375
}
364
- while rdr. peeking_at_comment ( ) {
365
- consume_comment ( & mut rdr, code_to_the_left, & mut comments) ;
366
- consume_whitespace_counting_blank_lines ( & mut rdr, & mut comments) ;
376
+ // Eat one comment group
377
+ if rdr. peeking_at_comment ( ) {
378
+ consume_comment ( & mut rdr, & mut comments,
379
+ & mut code_to_the_left, & mut anything_to_the_left) ;
380
+ } else {
381
+ break
367
382
}
368
- break ;
369
383
}
370
384
371
-
372
385
let bstart = rdr. pos ;
373
386
rdr. next_token ( ) ;
374
387
// discard, and look ahead; we're working with internal state
@@ -384,7 +397,8 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
384
397
} else {
385
398
debug ! ( "tok: {}" , pprust:: token_to_string( & tok) ) ;
386
399
}
387
- first_read = false ;
400
+ code_to_the_left = true ;
401
+ anything_to_the_left = true ;
388
402
}
389
403
390
404
( comments, literals)
0 commit comments