Parse an N-digit hex representation of a Unicode codepoint. This expects the parser to be positioned at the first digit and will advance the parser to the first character immediately following the escape sequence. The number of digits given must be 2 (for `\xNN`), 4 (for `\uNNNN`) or 8 (for `\UNNNNNNNN`).
(
&self,
kind: ast::HexLiteralKind,
)
| 1600 | /// The number of digits given must be 2 (for `\xNN`), 4 (for `\uNNNN`) |
| 1601 | /// or 8 (for `\UNNNNNNNN`). |
| 1602 | fn parse_hex_digits( |
| 1603 | &self, |
| 1604 | kind: ast::HexLiteralKind, |
| 1605 | ) -> Result<ast::Literal> { |
| 1606 | use std::char; |
| 1607 | use std::u32; |
| 1608 | |
| 1609 | let mut scratch = self.parser().scratch.borrow_mut(); |
| 1610 | scratch.clear(); |
| 1611 | |
| 1612 | let start = self.pos(); |
| 1613 | for i in 0..kind.digits() { |
| 1614 | if i > 0 && !self.bump_and_bump_space() { |
| 1615 | return Err(self.error( |
| 1616 | self.span(), |
| 1617 | ast::ErrorKind::EscapeUnexpectedEof, |
| 1618 | )); |
| 1619 | } |
| 1620 | if !is_hex(self.char()) { |
| 1621 | return Err(self.error( |
| 1622 | self.span_char(), |
| 1623 | ast::ErrorKind::EscapeHexInvalidDigit, |
| 1624 | )); |
| 1625 | } |
| 1626 | scratch.push(self.char()); |
| 1627 | } |
| 1628 | // The final bump just moves the parser past the literal, which may |
| 1629 | // be EOF. |
| 1630 | self.bump_and_bump_space(); |
| 1631 | let end = self.pos(); |
| 1632 | let hex = scratch.as_str(); |
| 1633 | match u32::from_str_radix(hex, 16).ok().and_then(char::from_u32) { |
| 1634 | None => Err(self.error( |
| 1635 | Span::new(start, end), |
| 1636 | ast::ErrorKind::EscapeHexInvalid, |
| 1637 | )), |
| 1638 | Some(c) => Ok(ast::Literal { |
| 1639 | span: Span::new(start, end), |
| 1640 | kind: ast::LiteralKind::HexFixed(kind), |
| 1641 | c: c, |
| 1642 | }), |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | /// Parse a hex representation of any Unicode scalar value. This expects |
| 1647 | /// the parser to be positioned at the opening brace `{` and will advance |
no test coverage detected