1use std::borrow::Cow;
2
3use swc_common::{
4 errors::{DiagnosticBuilder, Handler},
5 Span,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Error {
11 inner: Box<(Span, ErrorKind)>,
12}
13
14impl Error {
15 pub fn kind(&self) -> &ErrorKind {
16 &self.inner.1
17 }
18
19 pub fn into_inner(self) -> Box<(Span, ErrorKind)> {
20 self.inner
21 }
22
23 pub fn new(span: Span, kind: ErrorKind) -> Self {
24 Error {
25 inner: Box::new((span, kind)),
26 }
27 }
28
29 pub fn message(&self) -> Cow<'static, str> {
30 match &self.inner.1 {
31 ErrorKind::Eof => "Unexpected end of file".into(),
32
33 ErrorKind::AbruptClosingOfEmptyComment => "Abrupt closing of empty comment".into(),
35 ErrorKind::AbruptDoctypePublicIdentifier => "Abrupt doctype public identifier".into(),
36 ErrorKind::AbruptDoctypeSystemIdentifier => "Abrupt doctype system identifier".into(),
37 ErrorKind::ControlCharacterInInputStream => "Control character in input stream".into(),
38 ErrorKind::EndTagWithAttributes => "End tag with attributes".into(),
39 ErrorKind::ShortTagWithAttributes => "Short tag with attributes".into(),
40 ErrorKind::DuplicateAttribute => "Duplicate attribute".into(),
41 ErrorKind::EndTagWithTrailingSolidus => "End tag with trailing solidus".into(),
42 ErrorKind::EofBeforeTagName => "Eof before tag name".into(),
43 ErrorKind::EofInCdata => "Eof in cdata".into(),
44 ErrorKind::EofInComment => "Eof in comment".into(),
45 ErrorKind::EofInDoctype => "Eof in doctype".into(),
46 ErrorKind::EofInTag => "Eof in tag".into(),
47 ErrorKind::EofInProcessingInstruction => "Eof in processing instruction".into(),
48 ErrorKind::IncorrectlyClosedComment => "Incorrectly closed comment".into(),
49 ErrorKind::IncorrectlyOpenedComment => "Incorrectly opened comment".into(),
50 ErrorKind::InvalidCharacterSequenceAfterDoctypeName => {
51 "Invalid character sequence after doctype name".into()
52 }
53 ErrorKind::InvalidFirstCharacterOfTagName => {
54 "Invalid first character of tag name".into()
55 }
56 ErrorKind::InvalidCharacterOfProcessingInstruction => {
57 "Invalid character of processing instruction".into()
58 }
59 ErrorKind::InvalidCharacterInTag => "Invalid character in tag".into(),
60 ErrorKind::InvalidEntityCharacter => "Invalid entity character".into(),
61 ErrorKind::MissingDoctypeName => "Missing doctype name".into(),
62 ErrorKind::MissingDoctypePublicIdentifier => "Missing doctype public identifier".into(),
63 ErrorKind::MissingQuoteBeforeDoctypePublicIdentifier => {
64 "Missing quote before doctype public identifier".into()
65 }
66 ErrorKind::MissingQuoteBeforeDoctypeSystemIdentifier => {
67 "Missing quote before doctype system identifier".into()
68 }
69 ErrorKind::MissingSemicolonAfterCharacterReference => {
70 "Missing semicolon after character reference".into()
71 }
72 ErrorKind::MissingWhitespaceAfterDoctypePublicKeyword => {
73 "Missing whitespace after doctype public keyword".into()
74 }
75 ErrorKind::MissingWhitespaceAfterDoctypeSystemKeyword => {
76 "Missing whitespace after doctype system keyword".into()
77 }
78 ErrorKind::MissingWhitespaceBeforeDoctypeName => {
79 "Missing whitespace before doctype name".into()
80 }
81 ErrorKind::MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers => {
82 "Missing whitespace between doctype public and system identifiers".into()
83 }
84 ErrorKind::MissingEndTagName => "Missing end tag name".into(),
85 ErrorKind::MissingQuoteBeforeAttributeValue => {
86 "Missing quote before attribute value".into()
87 }
88 ErrorKind::MissingEqualAfterAttributeName => {
89 "Missing equal after attribute name".into()
90 }
91 ErrorKind::MissingSpaceBetweenAttributes => "Missing space between attributes".into(),
92 ErrorKind::NestedComment => "Nested comment".into(),
93 ErrorKind::DoubleHyphenWithInComment => "Double hyper within comment".into(),
94 ErrorKind::NoncharacterInInputStream => "Noncharacter in input stream".into(),
95 ErrorKind::SurrogateInInputStream => "Surrogate in input stream".into(),
96 ErrorKind::SurrogateCharacterReference => "Surrogate character reference".into(),
97 ErrorKind::UnexpectedCharacterAfterDoctypeSystemIdentifier => {
98 "Unexpected character after doctype system identifier".into()
99 }
100 ErrorKind::UnexpectedColonBeforeAttributeName => {
101 "Unexpected colon before attribute name".into()
102 }
103 ErrorKind::UnexpectedSolidusInTag => "Unexpected solidus in tag".into(),
104 ErrorKind::NoTargetNameInProcessingInstruction => "No target name".into(),
105 ErrorKind::MissingWhitespaceBeforeQuestionInProcessingInstruction => {
106 "Missing whitespace before '?'".into()
107 }
108 ErrorKind::UnescapedCharacterInAttributeValue(c) => {
109 format!("Unescaped \"{c}\" not allowed in attribute values").into()
110 }
111
112 ErrorKind::UnexpectedTokenInStartPhase => "Unexpected token in start phase".into(),
114 ErrorKind::UnexpectedTokenInMainPhase => "Unexpected token in main phase".into(),
115 ErrorKind::UnexpectedTokenInEndPhase => "Unexpected token in end phase".into(),
116 ErrorKind::UnexpectedEofInStartPhase => "Unexpected end of file in start phase".into(),
117 ErrorKind::UnexpectedEofInMainPhase => "Unexpected end of file in main phase".into(),
118 ErrorKind::OpeningAndEndingTagMismatch => "Opening and ending tag mismatch".into(),
119 ErrorKind::UnexpectedCharacter => {
120 "Unexpected character, only whitespace character allowed".into()
121 }
122 }
123 }
124
125 pub fn to_diagnostics<'a>(&self, handler: &'a Handler) -> DiagnosticBuilder<'a> {
126 handler.struct_span_err(self.inner.0, &self.message())
127 }
128}
129
130#[derive(Debug, Clone, PartialEq, Eq)]
131#[non_exhaustive]
132pub enum ErrorKind {
133 Eof,
134
135 AbruptClosingOfEmptyComment,
137 AbruptDoctypePublicIdentifier,
138 AbruptDoctypeSystemIdentifier,
139 ControlCharacterInInputStream,
140 EndTagWithAttributes,
141 ShortTagWithAttributes,
142 DuplicateAttribute,
143 EndTagWithTrailingSolidus,
144 EofBeforeTagName,
145 EofInCdata,
146 EofInComment,
147 EofInDoctype,
148 EofInTag,
149 EofInProcessingInstruction,
150 IncorrectlyClosedComment,
151 IncorrectlyOpenedComment,
152 InvalidCharacterSequenceAfterDoctypeName,
153 InvalidFirstCharacterOfTagName,
154 InvalidCharacterOfProcessingInstruction,
155 InvalidCharacterInTag,
156 InvalidEntityCharacter,
157 MissingDoctypeName,
158 MissingDoctypePublicIdentifier,
159 MissingQuoteBeforeDoctypePublicIdentifier,
160 MissingQuoteBeforeDoctypeSystemIdentifier,
161 MissingSemicolonAfterCharacterReference,
162 MissingWhitespaceAfterDoctypePublicKeyword,
163 MissingWhitespaceAfterDoctypeSystemKeyword,
164 MissingWhitespaceBeforeDoctypeName,
165 MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers,
166 MissingEndTagName,
167 MissingQuoteBeforeAttributeValue,
168 MissingEqualAfterAttributeName,
169 MissingSpaceBetweenAttributes,
170 NestedComment,
171 DoubleHyphenWithInComment,
172 NoncharacterInInputStream,
173 SurrogateInInputStream,
174 SurrogateCharacterReference,
175 UnexpectedCharacterAfterDoctypeSystemIdentifier,
176 UnexpectedColonBeforeAttributeName,
177 UnexpectedSolidusInTag,
178 NoTargetNameInProcessingInstruction,
179 MissingWhitespaceBeforeQuestionInProcessingInstruction,
180 UnescapedCharacterInAttributeValue(char),
181
182 UnexpectedTokenInStartPhase,
184 UnexpectedTokenInMainPhase,
185 UnexpectedTokenInEndPhase,
186 UnexpectedEofInStartPhase,
187 UnexpectedEofInMainPhase,
188 OpeningAndEndingTagMismatch,
189 UnexpectedCharacter,
190}