Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/proc-macro2/src/rustc_literal_escaper.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / proc-macro2 / src / rustc_literal_escaper.rs` 701 lines · 22380 bytes Raw Blame History 1 // Vendored from rustc-literal-escaper v0.0.5 2 // https://github.com/rust-lang/literal-escaper/tree/v0.0.5 3 4 //! Utilities for validating (raw) string, char, and byte literals and 5 //! turning escape sequences into the values they represent. 6 7 use crate::num::NonZeroChar; 8 use core::ffi::CStr; 9 use core::num::NonZeroU8; 10 use core::ops::Range; 11 use core::str::Chars; 12 13 /// Errors and warnings that can occur during string, char, and byte unescaping. 14 /// 15 /// Mostly relating to malformed escape sequences, but also a few other problems. 16 #[derive(Debug, PartialEq, Eq)] 17 pub enum EscapeError { 18 /// Expected 1 char, but 0 were found. 19 ZeroChars, 20 /// Expected 1 char, but more than 1 were found. 21 MoreThanOneChar, 22 23 /// Escaped '\' character without continuation. 24 LoneSlash, 25 /// Invalid escape character (e.g. '\z'). 26 InvalidEscape, 27 /// Raw '\r' encountered. 28 BareCarriageReturn, 29 /// Raw '\r' encountered in raw string. 30 BareCarriageReturnInRawString, 31 /// Unescaped character that was expected to be escaped (e.g. raw '\t'). 32 EscapeOnlyChar, 33 34 /// Numeric character escape is too short (e.g. '\x1'). 35 TooShortHexEscape, 36 /// Invalid character in numeric escape (e.g. '\xz') 37 InvalidCharInHexEscape, 38 /// Character code in numeric escape is non-ascii (e.g. '\xFF'). 39 OutOfRangeHexEscape, 40 41 /// '\u' not followed by '{'. 42 NoBraceInUnicodeEscape, 43 /// Non-hexadecimal value in '\u{..}'. 44 InvalidCharInUnicodeEscape, 45 /// '\u{}' 46 EmptyUnicodeEscape, 47 /// No closing brace in '\u{..}', e.g. '\u{12'. 48 UnclosedUnicodeEscape, 49 /// '\u{_12}' 50 LeadingUnderscoreUnicodeEscape, 51 /// More than 6 characters in '\u{..}', e.g. '\u{10FFFF_FF}' 52 OverlongUnicodeEscape, 53 /// Invalid in-bound unicode character code, e.g. '\u{DFFF}'. 54 LoneSurrogateUnicodeEscape, 55 /// Out of bounds unicode character code, e.g. '\u{FFFFFF}'. 56 OutOfRangeUnicodeEscape, 57 58 /// Unicode escape code in byte literal. 59 UnicodeEscapeInByte, 60 /// Non-ascii character in byte literal, byte string literal, or raw byte string literal. 61 NonAsciiCharInByte, 62 63 /// `\0` in a C string literal. 64 NulInCStr, 65 66 /// After a line ending with '\', the next line contains whitespace 67 /// characters that are not skipped. 68 UnskippedWhitespaceWarning, 69 70 /// After a line ending with '\', multiple lines are skipped. 71 MultipleSkippedLinesWarning, 72 } 73 74 impl EscapeError { 75 /// Returns true for actual errors, as opposed to warnings. 76 pub fn is_fatal(&self) -> bool { 77 !matches!( 78 self, 79 EscapeError::UnskippedWhitespaceWarning | EscapeError::MultipleSkippedLinesWarning 80 ) 81 } 82 } 83 84 /// Check a raw string literal for validity 85 /// 86 /// Takes the contents of a raw string literal (without quotes) 87 /// and produces a sequence of characters or errors, 88 /// which are returned by invoking `callback`. 89 /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r'). 90 pub fn check_raw_str(src: &str, callback: impl FnMut(Range, Result)) { 91 str::check_raw(src, callback); 92 } 93 94 /// Check a raw byte string literal for validity 95 /// 96 /// Takes the contents of a raw byte string literal (without quotes) 97 /// and produces a sequence of bytes or errors, 98 /// which are returned by invoking `callback`. 99 /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r'). 100 pub fn check_raw_byte_str(src: &str, callback: impl FnMut(Range, Result)) { 101 <[u8]>::check_raw(src, callback); 102 } 103 104 /// Check a raw C string literal for validity 105 /// 106 /// Takes the contents of a raw C string literal (without quotes) 107 /// and produces a sequence of characters or errors, 108 /// which are returned by invoking `callback`. 109 /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r'). 110 pub fn check_raw_c_str( 111 src: &str, 112 callback: impl FnMut(Range, Result), 113 ) { 114 CStr::check_raw(src, callback); 115 } 116 117 /// Trait for checking raw string literals for validity 118 trait CheckRaw { 119 /// Unit type of the implementing string type (`char` for string, `u8` for byte string) 120 type RawUnit; 121 122 /// Converts chars to the unit type of the literal type 123 fn char2raw_unit(c: char) -> Result; 124 125 /// Takes the contents of a raw literal (without quotes) 126 /// and produces a sequence of `Result` 127 /// which are returned via `callback`. 128 /// 129 /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r'). 130 fn check_raw( 131 src: &str, 132 mut callback: impl FnMut(Range, Result), 133 ) { 134 let mut chars = src.chars(); 135 while let Some(c) = chars.next() { 136 let start = src.len() - chars.as_str().len() - c.len_utf8(); 137 let res = match c { 138 '\r' => Err(EscapeError::BareCarriageReturnInRawString), 139 _ => Self::char2raw_unit(c), 140 }; 141 let end = src.len() - chars.as_str().len(); 142 callback(start..end, res); 143 } 144 145 // Unfortunately, it is a bit unclear whether the following equivalent code is slower or faster: bug 141855 146 // src.char_indices().for_each(|(pos, c)| { 147 // callback( 148 // pos..pos + c.len_utf8(), 149 // if c == '\r' { 150 // Err(EscapeError::BareCarriageReturnInRawString) 151 // } else { 152 // Self::char2raw_unit(c) 153 // }, 154 // ); 155 // }); 156 } 157 } 158 159 impl CheckRaw for str { 160 type RawUnit = char; 161 162 #[inline] 163 fn char2raw_unit(c: char) -> Result { 164 Ok(c) 165 } 166 } 167 168 impl CheckRaw for [u8] { 169 type RawUnit = u8; 170 171 #[inline] 172 fn char2raw_unit(c: char) -> Result { 173 char2byte(c) 174 } 175 } 176 177 /// Turn an ascii char into a byte 178 #[inline] 179 fn char2byte(c: char) -> Result { 180 // do NOT do: c.try_into().ok_or(EscapeError::NonAsciiCharInByte) 181 if c.is_ascii() { 182 Ok(c as u8) 183 } else { 184 Err(EscapeError::NonAsciiCharInByte) 185 } 186 } 187 188 impl CheckRaw for CStr { 189 type RawUnit = NonZeroChar; 190 191 #[inline] 192 fn char2raw_unit(c: char) -> Result { 193 NonZeroChar::new(c).ok_or(EscapeError::NulInCStr) 194 } 195 } 196 197 /// Unescape a char literal 198 /// 199 /// Takes the contents of a char literal (without quotes), 200 /// and returns an unescaped char or an error. 201 #[inline] 202 pub fn unescape_char(src: &str) -> Result { 203 str::unescape_single(&mut src.chars()) 204 } 205 206 /// Unescape a byte literal 207 /// 208 /// Takes the contents of a byte literal (without quotes), 209 /// and returns an unescaped byte or an error. 210 #[inline] 211 pub fn unescape_byte(src: &str) -> Result { 212 <[u8]>::unescape_single(&mut src.chars()) 213 } 214 215 /// Unescape a string literal 216 /// 217 /// Takes the contents of a string literal (without quotes) 218 /// and produces a sequence of escaped characters or errors, 219 /// which are returned by invoking `callback`. 220 pub fn unescape_str(src: &str, callback: impl FnMut(Range, Result)) { 221 str::unescape(src, callback) 222 } 223 224 /// Unescape a byte string literal 225 /// 226 /// Takes the contents of a byte string literal (without quotes) 227 /// and produces a sequence of escaped bytes or errors, 228 /// which are returned by invoking `callback`. 229 pub fn unescape_byte_str(src: &str, callback: impl FnMut(Range, Result)) { 230 <[u8]>::unescape(src, callback) 231 } 232 233 /// Unescape a C string literal 234 /// 235 /// Takes the contents of a C string literal (without quotes) 236 /// and produces a sequence of escaped MixedUnits or errors, 237 /// which are returned by invoking `callback`. 238 pub fn unescape_c_str( 239 src: &str, 240 callback: impl FnMut(Range, Result), 241 ) { 242 CStr::unescape(src, callback) 243 } 244 245 /// Enum representing either a char or a byte 246 /// 247 /// Used for mixed utf8 string literals, i.e. those that allow both unicode 248 /// chars and high bytes. 249 #[derive(Copy, Clone, Debug, PartialEq, Eq)] 250 pub enum MixedUnit { 251 /// Used for ASCII chars (written directly or via `\x00`..`\x7f` escapes) 252 /// and Unicode chars (written directly or via `\u` escapes). 253 /// 254 /// For example, if '¥' appears in a string it is represented here as 255 /// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte 256 /// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]` 257 Char(NonZeroChar), 258 259 /// Used for high bytes (`\x80`..`\xff`). 260 /// 261 /// For example, if `\xa5` appears in a string it is represented here as 262 /// `MixedUnit::HighByte(0xa5)`, and it will be appended to the relevant 263 /// byte string as the single byte `0xa5`. 264 HighByte(NonZeroU8), 265 } 266 267 impl From for MixedUnit { 268 #[inline] 269 fn from(c: NonZeroChar) -> Self { 270 MixedUnit::Char(c) 271 } 272 } 273 274 impl From for MixedUnit { 275 #[inline] 276 fn from(byte: NonZeroU8) -> Self { 277 if byte.get().is_ascii() { 278 MixedUnit::Char(NonZeroChar::new(byte.get() as char).unwrap()) 279 } else { 280 MixedUnit::HighByte(byte) 281 } 282 } 283 } 284 285 impl TryFrom for MixedUnit { 286 type Error = EscapeError; 287 288 #[inline] 289 fn try_from(c: char) -> Result { 290 NonZeroChar::new(c) 291 .map(MixedUnit::Char) 292 .ok_or(EscapeError::NulInCStr) 293 } 294 } 295 296 impl TryFrom for MixedUnit { 297 type Error = EscapeError; 298 299 #[inline] 300 fn try_from(byte: u8) -> Result { 301 NonZeroU8::new(byte) 302 .map(From::from) 303 .ok_or(EscapeError::NulInCStr) 304 } 305 } 306 307 /// Trait for unescaping escape sequences in strings 308 trait Unescape { 309 /// Unit type of the implementing string type (`char` for string, `u8` for byte string) 310 type Unit; 311 312 /// Result of unescaping the zero char ('\0') 313 const ZERO_RESULT: Result; 314 315 /// Converts non-zero bytes to the unit type 316 fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit; 317 318 /// Converts chars to the unit type 319 fn char2unit(c: char) -> Result; 320 321 /// Converts the byte of a hex escape to the unit type 322 fn hex2unit(b: u8) -> Result; 323 324 /// Converts the result of a unicode escape to the unit type 325 fn unicode2unit(r: Result) -> Result; 326 327 /// Unescape a single unit (single quote syntax) 328 fn unescape_single(chars: &mut Chars<'_>) -> Result { 329 let res = match chars.next().ok_or(EscapeError::ZeroChars)? { 330 '\\' => Self::unescape_1(chars), 331 '\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar), 332 '\r' => Err(EscapeError::BareCarriageReturn), 333 c => Self::char2unit(c), 334 }?; 335 if chars.next().is_some() { 336 return Err(EscapeError::MoreThanOneChar); 337 } 338 Ok(res) 339 } 340 341 /// Unescape the first unit of a string (double quoted syntax) 342 fn unescape_1(chars: &mut Chars<'_>) -> Result { 343 // Previous character was '\\', unescape what follows. 344 let c = chars.next().ok_or(EscapeError::LoneSlash)?; 345 if c == '0' { 346 Self::ZERO_RESULT 347 } else { 348 simple_escape(c) 349 .map(|b| Self::nonzero_byte2unit(b)) 350 .or_else(|c| match c { 351 'x' => Self::hex2unit(hex_escape(chars)?), 352 'u' => Self::unicode2unit({ 353 let value = unicode_escape(chars)?; 354 if value > char::MAX as u32 { 355 Err(EscapeError::OutOfRangeUnicodeEscape) 356 } else { 357 char::from_u32(value).ok_or(EscapeError::LoneSurrogateUnicodeEscape) 358 } 359 }), 360 _ => Err(EscapeError::InvalidEscape), 361 }) 362 } 363 } 364 365 /// Unescape a string literal 366 /// 367 /// Takes the contents of a raw string literal (without quotes) 368 /// and produces a sequence of `Result` 369 /// which are returned via `callback`. 370 fn unescape( 371 src: &str, 372 mut callback: impl FnMut(Range, Result), 373 ) { 374 let mut chars = src.chars(); 375 while let Some(c) = chars.next() { 376 let start = src.len() - chars.as_str().len() - c.len_utf8(); 377 let res = match c { 378 '\\' => { 379 if let Some(b'\n') = chars.as_str().as_bytes().first() { 380 let _ = chars.next(); 381 // skip whitespace for backslash newline, see [Rust language reference] 382 // (https://doc.rust-lang.org/reference/tokens.html#string-literals). 383 let callback_err = |range, err| callback(range, Err(err)); 384 skip_ascii_whitespace(&mut chars, start, callback_err); 385 continue; 386 } else { 387 Self::unescape_1(&mut chars) 388 } 389 } 390 '"' => Err(EscapeError::EscapeOnlyChar), 391 '\r' => Err(EscapeError::BareCarriageReturn), 392 c => Self::char2unit(c), 393 }; 394 let end = src.len() - chars.as_str().len(); 395 callback(start..end, res); 396 } 397 } 398 } 399 400 /// Interpret a non-nul ASCII escape 401 /// 402 /// Parses the character of an ASCII escape (except nul) without the leading backslash. 403 #[inline] // single use in Unescape::unescape_1 404 fn simple_escape(c: char) -> Result { 405 // Previous character was '\\', unescape what follows. 406 Ok(NonZeroU8::new(match c { 407 '"' => b'"', 408 'n' => b'\n', 409 'r' => b'\r', 410 't' => b'\t', 411 '\\' => b'\\', 412 '\'' => b'\'', 413 _ => Err(c)?, 414 }) 415 .unwrap()) 416 } 417 418 /// Interpret a hexadecimal escape 419 /// 420 /// Parses the two hexadecimal characters of a hexadecimal escape without the leading r"\x". 421 #[inline] // single use in Unescape::unescape_1 422 fn hex_escape(chars: &mut impl Iterator) -> Result { 423 let hi = chars.next().ok_or(EscapeError::TooShortHexEscape)?; 424 let hi = hi.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?; 425 426 let lo = chars.next().ok_or(EscapeError::TooShortHexEscape)?; 427 let lo = lo.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?; 428 429 Ok((hi * 16 + lo) as u8) 430 } 431 432 /// Interpret a unicode escape 433 /// 434 /// Parse the braces with hexadecimal characters (and underscores) part of a unicode escape. 435 /// This r"{...}" normally comes after r"\u" and cannot start with an underscore. 436 #[inline] // single use in Unescape::unescape_1 437 fn unicode_escape(chars: &mut impl Iterator) -> Result { 438 if chars.next() != Some('{') { 439 return Err(EscapeError::NoBraceInUnicodeEscape); 440 } 441 442 // First character must be a hexadecimal digit. 443 let mut value: u32 = match chars.next().ok_or(EscapeError::UnclosedUnicodeEscape)? { 444 '_' => return Err(EscapeError::LeadingUnderscoreUnicodeEscape), 445 '}' => return Err(EscapeError::EmptyUnicodeEscape), 446 c => c 447 .to_digit(16) 448 .ok_or(EscapeError::InvalidCharInUnicodeEscape)?, 449 }; 450 451 // First character is valid, now parse the rest of the number 452 // and closing brace. 453 let mut n_digits = 1; 454 loop { 455 match chars.next() { 456 None => return Err(EscapeError::UnclosedUnicodeEscape), 457 Some('_') => continue, 458 Some('}') => { 459 // Incorrect syntax has higher priority for error reporting 460 // than unallowed value for a literal. 461 return if n_digits > 6 { 462 Err(EscapeError::OverlongUnicodeEscape) 463 } else { 464 Ok(value) 465 }; 466 } 467 Some(c) => { 468 let digit: u32 = c 469 .to_digit(16) 470 .ok_or(EscapeError::InvalidCharInUnicodeEscape)?; 471 n_digits += 1; 472 if n_digits > 6 { 473 // Stop updating value since we're sure that it's incorrect already. 474 continue; 475 } 476 value = value * 16 + digit; 477 } 478 }; 479 } 480 } 481 482 /// Interpret a string continuation escape (https://doc.rust-lang.org/reference/expressions/literal-expr.html#string-continuation-escapes) 483 /// 484 /// Skip ASCII whitespace, except for the formfeed character 485 /// (see [this issue](https://github.com/rust-lang/rust/issues/136600)). 486 /// Warns on unescaped newline and following non-ASCII whitespace. 487 #[inline] // single use in Unescape::unescape 488 fn skip_ascii_whitespace( 489 chars: &mut Chars<'_>, 490 start: usize, 491 mut callback: impl FnMut(Range, EscapeError), 492 ) { 493 let rest = chars.as_str(); 494 let first_non_space = rest 495 .bytes() 496 .position(|b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r') 497 .unwrap_or(rest.len()); 498 let (space, rest) = rest.split_at(first_non_space); 499 // backslash newline adds 2 bytes 500 let end = start + 2 + first_non_space; 501 if space.contains('\n') { 502 callback(start..end, EscapeError::MultipleSkippedLinesWarning); 503 } 504 *chars = rest.chars(); 505 if let Some(c) = chars.clone().next() { 506 if c.is_whitespace() { 507 // for error reporting, include the character that was not skipped in the span 508 callback( 509 start..end + c.len_utf8(), 510 EscapeError::UnskippedWhitespaceWarning, 511 ); 512 } 513 } 514 } 515 516 impl Unescape for str { 517 type Unit = char; 518 519 const ZERO_RESULT: Result = Ok('\0'); 520 521 #[inline] 522 fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit { 523 b.get().into() 524 } 525 526 #[inline] 527 fn char2unit(c: char) -> Result { 528 Ok(c) 529 } 530 531 #[inline] 532 fn hex2unit(b: u8) -> Result { 533 if b.is_ascii() { 534 Ok(b as char) 535 } else { 536 Err(EscapeError::OutOfRangeHexEscape) 537 } 538 } 539 540 #[inline] 541 fn unicode2unit(r: Result) -> Result { 542 r 543 } 544 } 545 546 impl Unescape for [u8] { 547 type Unit = u8; 548 549 const ZERO_RESULT: Result = Ok(b'\0'); 550 551 #[inline] 552 fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit { 553 b.get() 554 } 555 556 #[inline] 557 fn char2unit(c: char) -> Result { 558 char2byte(c) 559 } 560 561 #[inline] 562 fn hex2unit(b: u8) -> Result { 563 Ok(b) 564 } 565 566 #[inline] 567 fn unicode2unit(_r: Result) -> Result { 568 Err(EscapeError::UnicodeEscapeInByte) 569 } 570 } 571 572 impl Unescape for CStr { 573 type Unit = MixedUnit; 574 575 const ZERO_RESULT: Result = Err(EscapeError::NulInCStr); 576 577 #[inline] 578 fn nonzero_byte2unit(b: NonZeroU8) -> Self::Unit { 579 b.into() 580 } 581 582 #[inline] 583 fn char2unit(c: char) -> Result { 584 c.try_into() 585 } 586 587 #[inline] 588 fn hex2unit(byte: u8) -> Result { 589 byte.try_into() 590 } 591 592 #[inline] 593 fn unicode2unit(r: Result) -> Result { 594 Self::char2unit(r?) 595 } 596 } 597 598 /// Enum of the different kinds of literal 599 #[derive(Debug, Clone, Copy, PartialEq)] 600 pub enum Mode { 601 /// `'a'` 602 Char, 603 604 /// `b'a'` 605 Byte, 606 607 /// `"hello"` 608 Str, 609 /// `r"hello"` 610 RawStr, 611 612 /// `b"hello"` 613 ByteStr, 614 /// `br"hello"` 615 RawByteStr, 616 617 /// `c"hello"` 618 CStr, 619 /// `cr"hello"` 620 RawCStr, 621 } 622 623 impl Mode { 624 pub fn in_double_quotes(self) -> bool { 625 match self { 626 Mode::Str 627 | Mode::RawStr 628 | Mode::ByteStr 629 | Mode::RawByteStr 630 | Mode::CStr 631 | Mode::RawCStr => true, 632 Mode::Char | Mode::Byte => false, 633 } 634 } 635 636 pub fn prefix_noraw(self) -> &'static str { 637 match self { 638 Mode::Char | Mode::Str | Mode::RawStr => "", 639 Mode::Byte | Mode::ByteStr | Mode::RawByteStr => "b", 640 Mode::CStr | Mode::RawCStr => "c", 641 } 642 } 643 } 644 645 /// Check a literal only for errors 646 /// 647 /// Takes the contents of a literal (without quotes) 648 /// and produces a sequence of only errors, 649 /// which are returned by invoking `error_callback`. 650 /// 651 /// NB Does not produce any output other than errors 652 pub fn check_for_errors( 653 src: &str, 654 mode: Mode, 655 mut error_callback: impl FnMut(Range, EscapeError), 656 ) { 657 match mode { 658 Mode::Char => { 659 let mut chars = src.chars(); 660 if let Err(e) = str::unescape_single(&mut chars) { 661 error_callback(0..(src.len() - chars.as_str().len()), e); 662 } 663 } 664 Mode::Byte => { 665 let mut chars = src.chars(); 666 if let Err(e) = <[u8]>::unescape_single(&mut chars) { 667 error_callback(0..(src.len() - chars.as_str().len()), e); 668 } 669 } 670 Mode::Str => unescape_str(src, |range, res| { 671 if let Err(e) = res { 672 error_callback(range, e); 673 } 674 }), 675 Mode::ByteStr => unescape_byte_str(src, |range, res| { 676 if let Err(e) = res { 677 error_callback(range, e); 678 } 679 }), 680 Mode::CStr => unescape_c_str(src, |range, res| { 681 if let Err(e) = res { 682 error_callback(range, e); 683 } 684 }), 685 Mode::RawStr => check_raw_str(src, |range, res| { 686 if let Err(e) = res { 687 error_callback(range, e); 688 } 689 }), 690 Mode::RawByteStr => check_raw_byte_str(src, |range, res| { 691 if let Err(e) = res { 692 error_callback(range, e); 693 } 694 }), 695 Mode::RawCStr => check_raw_c_str(src, |range, res| { 696 if let Err(e) = res { 697 error_callback(range, e); 698 } 699 }), 700 } 701 }