|
8 | 8 | //! Note that these tokens, unlike the tokens we feed into the parser, do |
9 | 9 | //! include info about comments and whitespace. |
10 | 10 |
|
11 | | -use rustc_dependencies::lexer as rustc_lexer; |
| 11 | +use rustc_dependencies::lexer::{ |
| 12 | + self as rustc_lexer, |
| 13 | + unescape::{unescape_c_string, unescape_literal}, |
| 14 | +}; |
12 | 15 |
|
13 | 16 | use std::ops; |
14 | 17 |
|
@@ -284,18 +287,45 @@ impl<'a> Converter<'a> { |
284 | 287 | rustc_lexer::LiteralKind::Str { terminated } => { |
285 | 288 | if !terminated { |
286 | 289 | err = "Missing trailing `\"` symbol to terminate the string literal"; |
| 290 | + } else { |
| 291 | + let text = &self.res.text[self.offset + 1..][..len - 1]; |
| 292 | + let i = text.rfind('"').unwrap(); |
| 293 | + let text = &text[..i]; |
| 294 | + rustc_lexer::unescape::unescape_literal(text, Mode::Str, &mut |_, res| { |
| 295 | + if let Err(e) = res { |
| 296 | + err = error_to_diagnostic_message(e, Mode::Str); |
| 297 | + } |
| 298 | + }); |
287 | 299 | } |
288 | 300 | STRING |
289 | 301 | } |
290 | 302 | rustc_lexer::LiteralKind::ByteStr { terminated } => { |
291 | 303 | if !terminated { |
292 | 304 | err = "Missing trailing `\"` symbol to terminate the byte string literal"; |
| 305 | + } else { |
| 306 | + let text = &self.res.text[self.offset + 2..][..len - 2]; |
| 307 | + let i = text.rfind('"').unwrap(); |
| 308 | + let text = &text[..i]; |
| 309 | + rustc_lexer::unescape::unescape_literal(text, Mode::ByteStr, &mut |_, res| { |
| 310 | + if let Err(e) = res { |
| 311 | + err = error_to_diagnostic_message(e, Mode::ByteStr); |
| 312 | + } |
| 313 | + }) |
293 | 314 | } |
294 | 315 | BYTE_STRING |
295 | 316 | } |
296 | 317 | rustc_lexer::LiteralKind::CStr { terminated } => { |
297 | 318 | if !terminated { |
298 | 319 | err = "Missing trailing `\"` symbol to terminate the string literal"; |
| 320 | + } else { |
| 321 | + let text = &self.res.text[self.offset + 2..][..len - 2]; |
| 322 | + let i = text.rfind('"').unwrap(); |
| 323 | + let text = &text[..i]; |
| 324 | + rustc_lexer::unescape::unescape_c_string(text, Mode::CStr, &mut |_, res| { |
| 325 | + if let Err(e) = res { |
| 326 | + err = error_to_diagnostic_message(e, Mode::CStr); |
| 327 | + } |
| 328 | + }) |
299 | 329 | } |
300 | 330 | C_STRING |
301 | 331 | } |
@@ -360,3 +390,13 @@ fn error_to_diagnostic_message(error: EscapeError, mode: Mode) -> &'static str { |
360 | 390 | EscapeError::MultipleSkippedLinesWarning => "", |
361 | 391 | } |
362 | 392 | } |
| 393 | + |
| 394 | +fn fill_unescape_string_error(text: &str, mode: Mode, mut error_message: &str) { |
| 395 | + |
| 396 | + rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| { |
| 397 | + if let Err(e) = res { |
| 398 | + error_message = error_to_diagnostic_message(e, mode); |
| 399 | + } |
| 400 | + }); |
| 401 | +} |
| 402 | + |
0 commit comments