1use core::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct CompileError {
9 pub kind: CompileErrorKind,
11 pub line_number: usize,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum CompileErrorKind {
19 InvalidLengthGuard(String),
21 UnterminatedLengthGuard,
23 UnterminatedGroupSet,
25 EmptyGroupSet,
27 UnknownGroupName(String),
29 TokenAfterTrailingBoundary,
31 ExpectedDigitAfterBackslash,
33 IncreasingPriority {
35 base: u8,
37 min: u8,
39 },
40 BackslashWithoutDigit,
42 CaretNotFollowed,
44 EmptyGroupName,
46 NoLetters,
48 StrayCharacter(char),
50 ConflictingWeights,
52 WeightOutsideRun,
55}
56
57impl fmt::Display for CompileErrorKind {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 CompileErrorKind::InvalidLengthGuard(body) => {
61 write!(f, "Invalid length guard “[{body}]”")
62 }
63 CompileErrorKind::UnterminatedLengthGuard => write!(f, "Unterminated length guard"),
64 CompileErrorKind::UnterminatedGroupSet => write!(f, "Unterminated “{{” group set"),
65 CompileErrorKind::EmptyGroupSet => write!(f, "Empty “{{}}” group set"),
66 CompileErrorKind::UnknownGroupName(name) => {
67 write!(f, "Unknown Unicode Joining_Group name “{name}”")
68 }
69 CompileErrorKind::TokenAfterTrailingBoundary => {
70 write!(f, "Token after a trailing “.” boundary")
71 }
72 CompileErrorKind::ExpectedDigitAfterBackslash => {
73 write!(f, "Expected a digit after “\\”")
74 }
75 CompileErrorKind::IncreasingPriority { base, min } => {
76 write!(f, "Priority must not increase ({base}\\{min})")
77 }
78 CompileErrorKind::BackslashWithoutDigit => {
79 write!(f, "“\\” must follow a priority digit")
80 }
81 CompileErrorKind::CaretNotFollowed => {
82 write!(f, "“^” must be followed by “{{”, “@”, or “=”")
83 }
84 CompileErrorKind::EmptyGroupName => write!(f, "Empty group name"),
85 CompileErrorKind::NoLetters => write!(f, "Pattern has no letters"),
86 CompileErrorKind::StrayCharacter(ch) => write!(f, "Stray character {ch:?}"),
87 CompileErrorKind::ConflictingWeights => {
88 write!(f, "Conflicting weights at one junction")
89 }
90 CompileErrorKind::WeightOutsideRun => {
91 write!(f, "Weight outside the run at a “.” boundary")
92 }
93 }
94 }
95}
96
97impl fmt::Display for CompileError {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 write!(f, "line {}: {}", self.line_number, self.kind)
100 }
101}
102
103impl std::error::Error for CompileError {}