Parse a hex representation of any Unicode scalar value. This expects the parser to be positioned at the opening brace `{` and will advance the parser to the first character following the closing brace `}`.
(
&self,
kind: ast::HexLiteralKind,
)
| 1647 | /// the parser to be positioned at the opening brace `{` and will advance |
| 1648 | /// the parser to the first character following the closing brace `}`. |
| 1649 | fn parse_hex_brace( |
| 1650 | &self, |
| 1651 | kind: ast::HexLiteralKind, |
| 1652 | ) -> Result<ast::Literal> { |
| 1653 | use std::char; |
| 1654 | use std::u32; |
| 1655 | |
| 1656 | let mut scratch = self.parser().scratch.borrow_mut(); |
| 1657 | scratch.clear(); |
| 1658 | |
| 1659 | let brace_pos = self.pos(); |
| 1660 | let start = self.span_char().end; |
| 1661 | while self.bump_and_bump_space() && self.char() != '}' { |
| 1662 | if !is_hex(self.char()) { |
| 1663 | return Err(self.error( |
| 1664 | self.span_char(), |
| 1665 | ast::ErrorKind::EscapeHexInvalidDigit, |
| 1666 | )); |
| 1667 | } |
| 1668 | scratch.push(self.char()); |
| 1669 | } |
| 1670 | if self.is_eof() { |
| 1671 | return Err(self.error( |
| 1672 | Span::new(brace_pos, self.pos()), |
| 1673 | ast::ErrorKind::EscapeUnexpectedEof, |
| 1674 | )); |
| 1675 | } |
| 1676 | let end = self.pos(); |
| 1677 | let hex = scratch.as_str(); |
| 1678 | assert_eq!(self.char(), '}'); |
| 1679 | self.bump_and_bump_space(); |
| 1680 | |
| 1681 | if hex.is_empty() { |
| 1682 | return Err(self.error( |
| 1683 | Span::new(brace_pos, self.pos()), |
| 1684 | ast::ErrorKind::EscapeHexEmpty, |
| 1685 | )); |
| 1686 | } |
| 1687 | match u32::from_str_radix(hex, 16).ok().and_then(char::from_u32) { |
| 1688 | None => Err(self.error( |
| 1689 | Span::new(start, end), |
| 1690 | ast::ErrorKind::EscapeHexInvalid, |
| 1691 | )), |
| 1692 | Some(c) => Ok(ast::Literal { |
| 1693 | span: Span::new(start, self.pos()), |
| 1694 | kind: ast::LiteralKind::HexBrace(kind), |
| 1695 | c: c, |
| 1696 | }), |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /// Parse a decimal number into a u32 while trimming leading and trailing |
| 1701 | /// whitespace. |
no test coverage detected