pomsky_syntax/lexer/
error.rs

1//! This module contains errors that can occur during lexing.
2
3/// An error message for a token that is invalid in a pomsky expression.
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum LexErrorMsg {
7    GroupNonCapturing,
8    GroupLookahead,
9    GroupLookaheadNeg,
10    GroupLookbehind,
11    GroupLookbehindNeg,
12    GroupNamedCapture,
13    GroupPcreBackreference,
14    GroupComment,
15    GroupAtomic,
16    GroupConditional,
17    GroupBranchReset,
18    GroupSubroutineCall,
19    GroupOther,
20
21    Backslash,
22    BackslashU4,
23    BackslashX2,
24    BackslashUnicode,
25    BackslashProperty,
26    BackslashGK,
27
28    UnclosedString,
29    LeadingZero,
30    InvalidCodePoint,
31
32    FileTooBig,
33}
34
35impl LexErrorMsg {
36    /// Returns a help message for fixing this error, if available.
37    ///
38    /// The `slice` argument must be the same string that you tried to parse.
39    pub fn get_help(&self, slice: &str) -> Option<String> {
40        super::diagnostics::get_parse_error_msg_help(*self, slice)
41    }
42}
43
44impl std::error::Error for LexErrorMsg {}
45
46impl core::fmt::Display for LexErrorMsg {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        let error = match self {
49            LexErrorMsg::GroupNonCapturing
50            | LexErrorMsg::GroupLookahead
51            | LexErrorMsg::GroupLookaheadNeg
52            | LexErrorMsg::GroupLookbehind
53            | LexErrorMsg::GroupLookbehindNeg
54            | LexErrorMsg::GroupNamedCapture
55            | LexErrorMsg::GroupPcreBackreference
56            | LexErrorMsg::GroupOther => "This syntax is not supported",
57            LexErrorMsg::GroupComment => "Comments have a different syntax",
58            LexErrorMsg::GroupAtomic => "Atomic groups are not supported",
59            LexErrorMsg::GroupConditional => "Conditionals are not supported",
60            LexErrorMsg::GroupBranchReset => "Branch reset groups are not supported",
61            LexErrorMsg::GroupSubroutineCall => "Subroutines are not supported",
62
63            LexErrorMsg::Backslash
64            | LexErrorMsg::BackslashU4
65            | LexErrorMsg::BackslashX2
66            | LexErrorMsg::BackslashUnicode
67            | LexErrorMsg::BackslashProperty
68            | LexErrorMsg::BackslashGK => "Backslash escapes are not supported",
69
70            LexErrorMsg::UnclosedString => "This string literal doesn't have a closing quote",
71            LexErrorMsg::LeadingZero => "Numbers can't have leading zeroes",
72            LexErrorMsg::InvalidCodePoint => "Code point contains non-hexadecimal digit",
73            LexErrorMsg::FileTooBig => "File too big (> 4 GiB)",
74        };
75
76        f.write_str(error)
77    }
78}