Skip to main content

solar_parse/lexer/unescape/
errors.rs

1use solar_interface::{BytePos, Span, diagnostics::DiagCtxt};
2use std::ops::Range;
3
4/// Errors and warnings that can occur during string unescaping.
5#[derive(Debug, PartialEq, Eq)]
6pub enum EscapeError {
7    /// Escaped '\' character without continuation.
8    LoneSlash,
9    /// Invalid escape character (e.g. '\z').
10    InvalidEscape,
11    /// Raw '\r' encountered.
12    BareCarriageReturn,
13
14    /// Numeric character escape is too short (e.g. '\x1').
15    HexEscapeTooShort,
16    /// Invalid character in numeric escape (e.g. '\xz1').
17    InvalidHexEscape,
18
19    /// Unicode character escape is too short (e.g. '\u1').
20    UnicodeEscapeTooShort,
21    /// Invalid character in unicode character escape (e.g. '\uz111').
22    InvalidUnicodeEscape,
23
24    /// Newline in string literal. These must be escaped.
25    StrNewline,
26    /// Non-ASCII character in non-unicode literal.
27    StrNonAsciiChar,
28
29    /// Non hex-digit character in hex literal.
30    HexNotHexDigit,
31    /// Underscore in hex literal.
32    HexBadUnderscore,
33    /// Odd number of hex digits in hex literal.
34    HexOddDigits,
35    /// Hex literal with the `0x` prefix.
36    HexPrefix,
37}
38
39impl EscapeError {
40    fn msg(&self) -> &'static str {
41        match self {
42            Self::LoneSlash => "invalid trailing slash in literal",
43            Self::InvalidEscape => "unknown character escape",
44            Self::BareCarriageReturn => "bare CR not allowed in string, use `\\r` instead",
45            Self::HexEscapeTooShort => "hex escape must be followed by 2 hex digits",
46            Self::InvalidHexEscape => "invalid character in hex escape",
47            Self::UnicodeEscapeTooShort => "unicode escape must be followed by 4 hex digits",
48            Self::InvalidUnicodeEscape => "invalid character in unicode escape",
49            Self::StrNewline => "unescaped newline",
50            Self::StrNonAsciiChar => {
51                "unicode characters are not allowed in string literals; use a `unicode\"...\"` literal instead"
52            }
53            Self::HexNotHexDigit => "invalid hex digit",
54            Self::HexBadUnderscore => "invalid underscore in hex literal",
55            Self::HexOddDigits => "odd number of hex nibbles",
56            Self::HexPrefix => "hex prefix is not allowed",
57        }
58    }
59}
60
61pub(crate) fn emit_unescape_error(
62    dcx: &DiagCtxt,
63    // interior part of the literal, between quotes
64    lit: &str,
65    // span of the error part of the literal
66    err_span: Span,
67    // range of the error inside `lit`
68    range: Range<usize>,
69    error: EscapeError,
70) {
71    let last_char = || {
72        let c = lit[range.clone()].chars().next_back().unwrap();
73        let span = err_span.with_lo(err_span.hi() - BytePos(c.len_utf8() as u32));
74        (c, span)
75    };
76    let mut diag = dcx.err(error.msg()).span(err_span);
77    if matches!(
78        error,
79        EscapeError::InvalidEscape
80            | EscapeError::InvalidHexEscape
81            | EscapeError::InvalidUnicodeEscape
82    ) {
83        diag = diag.span(last_char().1);
84    }
85    diag.emit();
86}