1
+ use rustc_ast:: ast:: AttrStyle ;
1
2
use rustc_ast:: token:: { self , CommentKind , Token , TokenKind } ;
2
- use rustc_ast:: util:: comments;
3
3
use rustc_data_structures:: sync:: Lrc ;
4
4
use rustc_errors:: { error_code, Applicability , DiagnosticBuilder , FatalError } ;
5
5
use rustc_lexer:: Base ;
@@ -15,7 +15,7 @@ mod tokentrees;
15
15
mod unescape_error_reporting;
16
16
mod unicode_chars;
17
17
18
- use rustc_lexer:: unescape:: Mode ;
18
+ use rustc_lexer:: { unescape:: Mode , DocStyle } ;
19
19
use unescape_error_reporting:: { emit_unescape_error, push_escaped_char} ;
20
20
21
21
#[ derive( Clone , Debug ) ]
@@ -168,25 +168,23 @@ impl<'a> StringReader<'a> {
168
168
/// symbols and runs additional validation.
169
169
fn cook_lexer_token ( & self , token : rustc_lexer:: TokenKind , start : BytePos ) -> TokenKind {
170
170
match token {
171
- rustc_lexer:: TokenKind :: LineComment => {
172
- let string = self . str_from ( start) ;
173
- if let Some ( attr_style) = comments:: line_doc_comment_style ( string) {
174
- self . forbid_bare_cr ( start, string, "bare CR not allowed in doc-comment" ) ;
175
- // Opening delimiter of the length 3 is not included into the symbol.
176
- token:: DocComment ( CommentKind :: Line , attr_style, Symbol :: intern ( & string[ 3 ..] ) )
177
- } else {
178
- token:: Comment
171
+ rustc_lexer:: TokenKind :: LineComment { doc_style } => {
172
+ match doc_style {
173
+ Some ( doc_style) => {
174
+ // Opening delimiter of the length 3 is not included into the symbol.
175
+ let content_start = start + BytePos ( 3 ) ;
176
+ let content = self . str_from ( content_start) ;
177
+
178
+ self . cook_doc_comment ( content_start, content, CommentKind :: Line , doc_style)
179
+ }
180
+ None => token:: Comment ,
179
181
}
180
182
}
181
- rustc_lexer:: TokenKind :: BlockComment { terminated } => {
182
- let string = self . str_from ( start) ;
183
- let attr_style = comments:: block_doc_comment_style ( string, terminated) ;
184
-
183
+ rustc_lexer:: TokenKind :: BlockComment { doc_style, terminated } => {
185
184
if !terminated {
186
- let msg = if attr_style. is_some ( ) {
187
- "unterminated block doc-comment"
188
- } else {
189
- "unterminated block comment"
185
+ let msg = match doc_style {
186
+ Some ( _) => "unterminated block doc-comment" ,
187
+ None => "unterminated block comment" ,
190
188
} ;
191
189
let last_bpos = self . pos ;
192
190
self . sess
@@ -199,18 +197,17 @@ impl<'a> StringReader<'a> {
199
197
. emit ( ) ;
200
198
FatalError . raise ( ) ;
201
199
}
202
-
203
- if let Some ( attr_style) = attr_style {
204
- self . forbid_bare_cr ( start, string, "bare CR not allowed in block doc-comment" ) ;
205
- // Opening delimiter of the length 3 and closing delimiter of the length 2
206
- // are not included into the symbol.
207
- token:: DocComment (
208
- CommentKind :: Block ,
209
- attr_style,
210
- Symbol :: intern ( & string[ 3 ..string. len ( ) - if terminated { 2 } else { 0 } ] ) ,
211
- )
212
- } else {
213
- token:: Comment
200
+ match doc_style {
201
+ Some ( doc_style) => {
202
+ // Opening delimiter of the length 3 and closing delimiter of the length 2
203
+ // are not included into the symbol.
204
+ let content_start = start + BytePos ( 3 ) ;
205
+ let content_end = self . pos - BytePos ( if terminated { 2 } else { 0 } ) ;
206
+ let content = self . str_from_to ( content_start, content_end) ;
207
+
208
+ self . cook_doc_comment ( content_start, content, CommentKind :: Block , doc_style)
209
+ }
210
+ None => token:: Comment ,
214
211
}
215
212
}
216
213
rustc_lexer:: TokenKind :: Whitespace => token:: Whitespace ,
@@ -319,6 +316,34 @@ impl<'a> StringReader<'a> {
319
316
}
320
317
}
321
318
319
+ fn cook_doc_comment (
320
+ & self ,
321
+ content_start : BytePos ,
322
+ content : & str ,
323
+ comment_kind : CommentKind ,
324
+ doc_style : DocStyle ,
325
+ ) -> TokenKind {
326
+ if content. contains ( '\r' ) {
327
+ for ( idx, _) in content. char_indices ( ) . filter ( |& ( _, c) | c == '\r' ) {
328
+ self . err_span_ (
329
+ content_start + BytePos ( idx as u32 ) ,
330
+ content_start + BytePos ( idx as u32 + 1 ) ,
331
+ match comment_kind {
332
+ CommentKind :: Line => "bare CR not allowed in doc-comment" ,
333
+ CommentKind :: Block => "bare CR not allowed in block doc-comment" ,
334
+ } ,
335
+ ) ;
336
+ }
337
+ }
338
+
339
+ let attr_style = match doc_style {
340
+ DocStyle :: Outer => AttrStyle :: Outer ,
341
+ DocStyle :: Inner => AttrStyle :: Inner ,
342
+ } ;
343
+
344
+ token:: DocComment ( comment_kind, attr_style, Symbol :: intern ( content) )
345
+ }
346
+
322
347
fn cook_lexer_literal (
323
348
& self ,
324
349
start : BytePos ,
@@ -472,17 +497,6 @@ impl<'a> StringReader<'a> {
472
497
& self . src [ self . src_index ( start) ..self . src_index ( end) ]
473
498
}
474
499
475
- fn forbid_bare_cr ( & self , start : BytePos , s : & str , errmsg : & str ) {
476
- let mut idx = 0 ;
477
- loop {
478
- idx = match s[ idx..] . find ( '\r' ) {
479
- None => break ,
480
- Some ( it) => idx + it + 1 ,
481
- } ;
482
- self . err_span_ ( start + BytePos ( idx as u32 - 1 ) , start + BytePos ( idx as u32 ) , errmsg) ;
483
- }
484
- }
485
-
486
500
fn report_raw_str_error ( & self , start : BytePos , opt_err : Option < RawStrError > ) {
487
501
match opt_err {
488
502
Some ( RawStrError :: InvalidStarter { bad_char } ) => {
0 commit comments