@@ -24,7 +24,7 @@ use str::char_at;
2424use std:: io:: Read ;
2525use std:: usize;
2626
27- #[ derive( Clone , Copy , PartialEq ) ]
27+ #[ derive( Clone , Copy , PartialEq , Debug ) ]
2828pub enum CommentStyle {
2929 /// No code on either side of each line of the comment
3030 Isolated ,
@@ -155,14 +155,13 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) {
155155
156156fn consume_whitespace_counting_blank_lines ( rdr : & mut StringReader , comments : & mut Vec < Comment > ) {
157157 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' ) {
159159 push_blank_line_comment ( rdr, & mut * comments) ;
160160 }
161161 rdr. bump ( ) ;
162162 }
163163}
164164
165-
166165fn read_shebang_comment ( rdr : & mut StringReader ,
167166 code_to_the_left : bool ,
168167 comments : & mut Vec < Comment > ) {
@@ -317,14 +316,22 @@ fn read_block_comment(rdr: &mut StringReader,
317316}
318317
319318
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 ) {
321323 debug ! ( ">>> consume comment" ) ;
322324 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 ;
324328 } 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 ;
326331 } 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 ;
328335 } else {
329336 panic ! ( ) ;
330337 }
@@ -352,23 +359,29 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
352359
353360 let mut comments: Vec < Comment > = Vec :: new ( ) ;
354361 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
356364 while !rdr. is_eof ( ) {
357365 loop {
358- let mut code_to_the_left = !first_read ;
366+ // Eat all the whitespace and count blank lines.
359367 rdr. consume_non_eol_whitespace ( ) ;
360368 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+ }
362372 consume_whitespace_counting_blank_lines ( & mut rdr, & mut comments) ;
373+ code_to_the_left = false ;
374+ anything_to_the_left = false ;
363375 }
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
367382 }
368- break ;
369383 }
370384
371-
372385 let bstart = rdr. pos ;
373386 rdr. next_token ( ) ;
374387 // discard, and look ahead; we're working with internal state
@@ -384,7 +397,8 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
384397 } else {
385398 debug ! ( "tok: {}" , pprust:: token_to_string( & tok) ) ;
386399 }
387- first_read = false ;
400+ code_to_the_left = true ;
401+ anything_to_the_left = true ;
388402 }
389403
390404 ( comments, literals)
0 commit comments