1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::borrow::Cow;

use swc_atoms::JsWord;
use swc_common::{
    errors::{DiagnosticBuilder, Handler},
    Span,
};

/// Size is same as a size of a pointer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    inner: Box<(Span, ErrorKind)>,
}

impl Error {
    pub fn kind(&self) -> &ErrorKind {
        &self.inner.1
    }

    pub fn into_inner(self) -> Box<(Span, ErrorKind)> {
        self.inner
    }

    pub fn new(span: Span, kind: ErrorKind) -> Self {
        Error {
            inner: Box::new((span, kind)),
        }
    }

    pub fn message(&self) -> Cow<'static, str> {
        match &self.inner.1 {
            ErrorKind::Eof => "Unexpected end of file".into(),

            // Lexer errors
            ErrorKind::AbruptClosingOfEmptyComment => "Abrupt closing of empty comment".into(),
            ErrorKind::AbruptDoctypePublicIdentifier => "Abrupt doctype public identifier".into(),
            ErrorKind::AbruptDoctypeSystemIdentifier => "Abrupt doctype system identifier".into(),
            ErrorKind::AbsenceOfDigitsInNumericCharacterReference => {
                "Absence of digits in numeric character reference".into()
            }
            ErrorKind::CdataInHtmlContent => "Cdata in html content".into(),
            ErrorKind::CharacterReferenceOutsideUnicodeRange => {
                "Character reference outside unicode range".into()
            }
            ErrorKind::ControlCharacterInInputStream => "Control character in input stream".into(),
            ErrorKind::ControlCharacterReference => "Control character reference".into(),
            ErrorKind::EndTagWithAttributes => "End tag with attributes".into(),
            ErrorKind::DuplicateAttribute => "Duplicate attribute".into(),
            ErrorKind::EndTagWithTrailingSolidus => "End tag with trailing solidus".into(),
            ErrorKind::EofBeforeTagName => "Eof before tag name".into(),
            ErrorKind::EofInCdata => "Eof in cdata".into(),
            ErrorKind::EofInComment => "Eof in comment".into(),
            ErrorKind::EofInDoctype => "Eof in doctype".into(),
            ErrorKind::EofInScriptHtmlCommentLikeText => {
                "Eof in script html comment like text".into()
            }
            ErrorKind::EofInTag => "Eof in tag".into(),
            ErrorKind::IncorrectlyClosedComment => "Incorrectly closed comment".into(),
            ErrorKind::IncorrectlyOpenedComment => "Incorrectly opened comment".into(),
            ErrorKind::InvalidCharacterSequenceAfterDoctypeName => {
                "Invalid character sequence after doctype name".into()
            }
            ErrorKind::InvalidFirstCharacterOfTagName => {
                "Invalid first character of tag name".into()
            }
            ErrorKind::MissingAttributeValue => "Missing attribute value".into(),
            ErrorKind::MissingDoctypeName => "Missing doctype name".into(),
            ErrorKind::MissingDoctypePublicIdentifier => "Missing doctype public identifier".into(),
            ErrorKind::MissingDoctypeSystemIdentifier => "Missing doctype system identifier".into(),
            ErrorKind::MissingEndTagName => "Missing end tag name".into(),
            ErrorKind::MissingQuoteBeforeDoctypePublicIdentifier => {
                "Missing quote before doctype public identifier".into()
            }
            ErrorKind::MissingQuoteBeforeDoctypeSystemIdentifier => {
                "Missing quote before doctype system identifier".into()
            }
            ErrorKind::MissingSemicolonAfterCharacterReference => {
                "Missing semicolon after character reference".into()
            }
            ErrorKind::MissingWhitespaceAfterDoctypePublicKeyword => {
                "Missing whitespace after doctype public keyword".into()
            }
            ErrorKind::MissingWhitespaceAfterDoctypeSystemKeyword => {
                "Missing whitespace after doctype system keyword".into()
            }
            ErrorKind::MissingWhitespaceBeforeDoctypeName => {
                "Missing whitespace before doctype name".into()
            }
            ErrorKind::MissingWhitespaceBetweenAttributes => {
                "Missing whitespace between attributes".into()
            }
            ErrorKind::MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers => {
                "Missing whitespace between doctype public and system identifiers".into()
            }
            ErrorKind::NestedComment => "Nested comment".into(),
            ErrorKind::NoncharacterCharacterReference => "Noncharacter character reference".into(),
            ErrorKind::NoncharacterInInputStream => "Noncharacter in input stream".into(),
            ErrorKind::NonVoidHtmlElementStartTagWithTrailingSolidus => {
                "Non void html element start tag with trailing solidus".into()
            }
            ErrorKind::NullCharacterReference => "Null character reference".into(),
            ErrorKind::SurrogateCharacterReference => "Surrogate character reference".into(),
            ErrorKind::SurrogateInInputStream => "Surrogate in input stream".into(),
            ErrorKind::UnexpectedCharacterAfterDoctypeSystemIdentifier => {
                "Unexpected character after doctype system identifier".into()
            }
            ErrorKind::UnexpectedCharacterInAttributeName => {
                "Unexpected character in attribute name".into()
            }
            ErrorKind::UnexpectedCharacterInUnquotedAttributeValue => {
                "Unexpected character in unquoted attribute value".into()
            }
            ErrorKind::UnexpectedEqualsSignBeforeAttributeName => {
                "Unexpected equals sign before attribute name".into()
            }
            ErrorKind::UnexpectedNullCharacter => "Unexpected null character".into(),
            ErrorKind::UnexpectedQuestionMarkInsteadOfTagName => {
                "Unexpected question mark instead of tag name".into()
            }
            ErrorKind::UnexpectedSolidusInTag => "Unexpected solidus in tag".into(),
            ErrorKind::UnknownNamedCharacterReference => "Unknown named character reference".into(),

            // Parser errors
            ErrorKind::StrayStartTag(tag_name) => {
                format!("Stray start tag \"{}\"", tag_name).into()
            }
            ErrorKind::StrayEndTag(tag_name) => format!("Stray end tag \"{}\"", tag_name).into(),
            ErrorKind::UnclosedElements(tag_name) => format!(
                "End tag \"{}\" seen, but there were open elements",
                tag_name
            )
            .into(),
            ErrorKind::UnclosedElementsImplied(tag_name) => format!(
                "End tag \"{}\" implied, but there were open elements",
                tag_name
            )
            .into(),
            ErrorKind::UnclosedElementsCell => {
                "A table cell was implicitly closed, but there were open elements".into()
            }
            ErrorKind::StrayDoctype => "Stray doctype".into(),
            ErrorKind::NonConformingDoctype => "Non conforming doctype".into(),
            ErrorKind::NonSpaceCharacterInTrailer => "Non-space character in page trailer".into(),
            ErrorKind::NonSpaceCharacterAfterFrameset => {
                "Non-space character after \"frameset\"".into()
            }
            ErrorKind::NonSpaceCharacterInFrameset => "Non-space character in \"frameset\"".into(),
            ErrorKind::NonSpaceCharacterAfterBody => "Non-space character after body".into(),
            ErrorKind::NonSpaceCharacterInColumnGroup => {
                "Non-space character in \"colgroup\" element".into()
            }
            ErrorKind::NonSpaceCharacterInNoscriptInHead => {
                "Non-space character inside \"noscript\" inside \"head\"".into()
            }
            ErrorKind::SomethingBetweenHeadAndBody(tag_name) => {
                format!("\"{}\" element between \"head\" and \"body\"", tag_name).into()
            }
            ErrorKind::StartTagWithoutDoctype => {
                "Start tag seen without seeing a doctype first, expected \"<!DOCTYPE html>\"".into()
            }
            ErrorKind::StartSelectWhereEndSelectExpected => {
                "\"select\" start tag where end tag expected".into()
            }
            ErrorKind::StartTagWithSelectOpen(tag_name) => {
                format!("\"{}\" start tag with \"select\" open", tag_name).into()
            }
            ErrorKind::BadStartTagInNoscriptInHead(tag_name) => format!(
                "Bad start tag in \"{}\" in \"noscript\" in \"head\"",
                tag_name
            )
            .into(),
            ErrorKind::UnexpectedImageStartTag => {
                "Saw a start tag \"image\", \"img\" element is outdated".into()
            }
            ErrorKind::SomethingSeenWhenSomethingOpen(tag_name) => format!(
                "Start tag \"{}\" seen but an element of the same type was already open",
                tag_name
            )
            .into(),
            ErrorKind::HeadingWhenHeadingOpen => {
                "Heading cannot be a child of another heading".into()
            }
            ErrorKind::NoCellToClose => "No cell to close".into(),
            ErrorKind::StartTagInTable(tag_name) => {
                format!("Start tag \"{}\" seen in \"table\"", tag_name).into()
            }
            ErrorKind::FormWhenFormOpen => "Saw a \"form\" start tag, but there was already an \
                                            active \"form\" element, nested forms are not allowed"
                .into(),
            ErrorKind::TableSeenWhileTableOpen => {
                "Start tag for \"table\" seen but the previous \"table\" is still open".into()
            }
            ErrorKind::StartTagInTableBody(tag_name) => {
                format!("Start tag \"{}\" seen in \"table\" body", tag_name).into()
            }
            ErrorKind::EndTagSeenWithoutDoctype => {
                "End tag seen without seeing a doctype first, expected \"<!DOCTYPE html>\"".into()
            }
            ErrorKind::EndTagAfterBody => "Saw an end tag after \"body\" had been closed".into(),
            ErrorKind::EndTagSeenWithSelectOpen(tag_name) => {
                format!("\"{}\" end tag with \"select\" open", tag_name).into()
            }
            ErrorKind::GarbageInColumnGroup => "Garbage in \"colgroup\" element".into(),
            ErrorKind::EndTagBr => "End tag \"br\"".into(),
            ErrorKind::NoElementToCloseButEndTagSeen(tag_name) => format!(
                "No \"{}\" element in scope but a \"{}\" end tag seen",
                tag_name, tag_name
            )
            .into(),
            ErrorKind::HtmlStartTagInForeignContext(tag_name) => format!(
                "HTML start tag \"{}\" in a foreign namespace context",
                tag_name
            )
            .into(),
            ErrorKind::NoTableRowToClose => "No table row to close".into(),
            ErrorKind::NonSpaceCharacterInTable => {
                "Misplaced non-space characters inside a table".into()
            }
            ErrorKind::UnclosedChildrenInRuby => "Unclosed children in \"ruby\"".into(),
            ErrorKind::StartTagSeenWithoutRuby(tag_name) => format!(
                "Start tag \"{}\" seen without a \"ruby\" element being open",
                tag_name
            )
            .into(),
            ErrorKind::UnclosedElementsOnStack => "Unclosed elements on stack".into(),
            ErrorKind::EndTagDidNotMatchCurrentOpenElement(
                end_tag_name,
                current_element_tag_name,
            ) => format!(
                "End tag \"{}\" did not match the name of the current open element (\"{}\")",
                end_tag_name, current_element_tag_name
            )
            .into(),
            ErrorKind::EndTagViolatesNestingRules(tag_name) => {
                format!("End tag \"{}\" violates nesting rules", tag_name).into()
            }
            ErrorKind::EofWithUnclosedElements => {
                "End of file seen and there were open elements".into()
            }
            ErrorKind::EndTagWithUnclosedElements(tag_name) => format!(
                "Unexpected end tag for \"{}\", but there were unclosed elements",
                tag_name
            )
            .into(),
            ErrorKind::NonSpaceCharacterWithoutDoctype => "Non-space characters found without \
                                                           seeing a doctype first, expected \
                                                           \"<!DOCTYPE html>\""
                .into(),
            ErrorKind::EofWithoutDoctype => "End of file seen without seeing a doctype first, \
                                             expected \"<!DOCTYPE html>\""
                .into(),
            ErrorKind::EofInText => "End of file seen when expecting text or an end tag".into(),
        }
    }

    pub fn to_diagnostics<'a>(&self, handler: &'a Handler) -> DiagnosticBuilder<'a> {
        handler.struct_span_err(self.inner.0, &self.message())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
    Eof,

    // Lexer errors
    AbruptClosingOfEmptyComment,
    AbruptDoctypePublicIdentifier,
    AbruptDoctypeSystemIdentifier,
    AbsenceOfDigitsInNumericCharacterReference,
    CdataInHtmlContent,
    CharacterReferenceOutsideUnicodeRange,
    ControlCharacterInInputStream,
    ControlCharacterReference,
    EndTagWithAttributes,
    DuplicateAttribute,
    EndTagWithTrailingSolidus,
    EofBeforeTagName,
    EofInCdata,
    EofInComment,
    EofInDoctype,
    EofInScriptHtmlCommentLikeText,
    EofInTag,
    IncorrectlyClosedComment,
    IncorrectlyOpenedComment,
    InvalidCharacterSequenceAfterDoctypeName,
    InvalidFirstCharacterOfTagName,
    MissingAttributeValue,
    MissingDoctypeName,
    MissingDoctypePublicIdentifier,
    MissingDoctypeSystemIdentifier,
    MissingEndTagName,
    MissingQuoteBeforeDoctypePublicIdentifier,
    MissingQuoteBeforeDoctypeSystemIdentifier,
    MissingSemicolonAfterCharacterReference,
    MissingWhitespaceAfterDoctypePublicKeyword,
    MissingWhitespaceAfterDoctypeSystemKeyword,
    MissingWhitespaceBeforeDoctypeName,
    MissingWhitespaceBetweenAttributes,
    MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers,
    NestedComment,
    NoncharacterCharacterReference,
    NoncharacterInInputStream,
    NonVoidHtmlElementStartTagWithTrailingSolidus,
    NullCharacterReference,
    SurrogateCharacterReference,
    SurrogateInInputStream,
    UnexpectedCharacterAfterDoctypeSystemIdentifier,
    UnexpectedCharacterInAttributeName,
    UnexpectedCharacterInUnquotedAttributeValue,
    UnexpectedEqualsSignBeforeAttributeName,
    UnexpectedNullCharacter,
    UnexpectedQuestionMarkInsteadOfTagName,
    UnexpectedSolidusInTag,
    UnknownNamedCharacterReference,

    // Parser errors
    StrayStartTag(JsWord),
    StrayEndTag(JsWord),
    UnclosedElements(JsWord),
    UnclosedElementsImplied(JsWord),
    UnclosedElementsCell,
    StrayDoctype,
    NonConformingDoctype,
    NonSpaceCharacterInTrailer,
    NonSpaceCharacterAfterFrameset,
    NonSpaceCharacterInFrameset,
    NonSpaceCharacterAfterBody,
    NonSpaceCharacterInColumnGroup,
    NonSpaceCharacterInNoscriptInHead,
    SomethingBetweenHeadAndBody(JsWord),
    StartTagWithoutDoctype,
    StartSelectWhereEndSelectExpected,
    StartTagWithSelectOpen(JsWord),
    BadStartTagInNoscriptInHead(JsWord),
    UnexpectedImageStartTag,
    SomethingSeenWhenSomethingOpen(JsWord),
    HeadingWhenHeadingOpen,
    NoCellToClose,
    StartTagInTable(JsWord),
    FormWhenFormOpen,
    TableSeenWhileTableOpen,
    StartTagInTableBody(JsWord),
    EndTagSeenWithoutDoctype,
    EndTagAfterBody,
    EndTagSeenWithSelectOpen(JsWord),
    GarbageInColumnGroup,
    EndTagBr,
    NoElementToCloseButEndTagSeen(JsWord),
    HtmlStartTagInForeignContext(JsWord),
    NoTableRowToClose,
    NonSpaceCharacterInTable,
    UnclosedChildrenInRuby,
    StartTagSeenWithoutRuby(JsWord),
    UnclosedElementsOnStack,
    EndTagDidNotMatchCurrentOpenElement(JsWord, JsWord),
    EndTagViolatesNestingRules(JsWord),
    EofWithUnclosedElements,
    EndTagWithUnclosedElements(JsWord),
    NonSpaceCharacterWithoutDoctype,
    EofWithoutDoctype,
    EofInText,
}