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
use std::borrow::Cow;

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::ControlCharacterInInputStream => "Control character in input stream".into(),
            ErrorKind::EndTagWithAttributes => "End tag with attributes".into(),
            ErrorKind::ShortTagWithAttributes => "Short 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::EofInTag => "Eof in tag".into(),
            ErrorKind::EofInProcessingInstruction => "Eof in processing instruction".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::InvalidCharacterOfProcessingInstruction => {
                "Invalid character of processing instruction".into()
            }
            ErrorKind::InvalidCharacterInTag => "Invalid character in tag".into(),
            ErrorKind::InvalidEntityCharacter => "Invalid entity character".into(),
            ErrorKind::MissingDoctypeName => "Missing doctype name".into(),
            ErrorKind::MissingDoctypePublicIdentifier => "Missing doctype public identifier".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::MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers => {
                "Missing whitespace between doctype public and system identifiers".into()
            }
            ErrorKind::MissingEndTagName => "Missing end tag name".into(),
            ErrorKind::MissingQuoteBeforeAttributeValue => {
                "Missing quote before attribute value".into()
            }
            ErrorKind::MissingEqualAfterAttributeName => {
                "Missing equal after attribute name".into()
            }
            ErrorKind::MissingSpaceBetweenAttributes => "Missing space between attributes".into(),
            ErrorKind::NestedComment => "Nested comment".into(),
            ErrorKind::DoubleHyphenWithInComment => "Double hyper within comment".into(),
            ErrorKind::NoncharacterInInputStream => "Noncharacter in input stream".into(),
            ErrorKind::SurrogateInInputStream => "Surrogate in input stream".into(),
            ErrorKind::SurrogateCharacterReference => "Surrogate character reference".into(),
            ErrorKind::UnexpectedCharacterAfterDoctypeSystemIdentifier => {
                "Unexpected character after doctype system identifier".into()
            }
            ErrorKind::UnexpectedColonBeforeAttributeName => {
                "Unexpected colon before attribute name".into()
            }
            ErrorKind::UnexpectedSolidusInTag => "Unexpected solidus in tag".into(),
            ErrorKind::NoTargetNameInProcessingInstruction => "No target name".into(),
            ErrorKind::MissingWhitespaceBeforeQuestionInProcessingInstruction => {
                "Missing whitespace before '?'".into()
            }
            ErrorKind::UnescapedCharacterInAttributeValue(c) => {
                format!("Unescaped \"{}\" not allowed in attribute values", c).into()
            }

            // Parser errors
            ErrorKind::UnexpectedTokenInStartPhase => "Unexpected token in start phase".into(),
            ErrorKind::UnexpectedTokenInMainPhase => "Unexpected token in main phase".into(),
            ErrorKind::UnexpectedTokenInEndPhase => "Unexpected token in end phase".into(),
            ErrorKind::UnexpectedEofInStartPhase => "Unexpected end of file in start phase".into(),
            ErrorKind::UnexpectedEofInMainPhase => "Unexpected end of file in main phase".into(),
            ErrorKind::OpeningAndEndingTagMismatch => "Opening and ending tag mismatch".into(),
            ErrorKind::UnexpectedCharacter => {
                "Unexpected character, only whitespace character allowed".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,
    ControlCharacterInInputStream,
    EndTagWithAttributes,
    ShortTagWithAttributes,
    DuplicateAttribute,
    EndTagWithTrailingSolidus,
    EofBeforeTagName,
    EofInCdata,
    EofInComment,
    EofInDoctype,
    EofInTag,
    EofInProcessingInstruction,
    IncorrectlyClosedComment,
    IncorrectlyOpenedComment,
    InvalidCharacterSequenceAfterDoctypeName,
    InvalidFirstCharacterOfTagName,
    InvalidCharacterOfProcessingInstruction,
    InvalidCharacterInTag,
    InvalidEntityCharacter,
    MissingDoctypeName,
    MissingDoctypePublicIdentifier,
    MissingQuoteBeforeDoctypePublicIdentifier,
    MissingQuoteBeforeDoctypeSystemIdentifier,
    MissingSemicolonAfterCharacterReference,
    MissingWhitespaceAfterDoctypePublicKeyword,
    MissingWhitespaceAfterDoctypeSystemKeyword,
    MissingWhitespaceBeforeDoctypeName,
    MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers,
    MissingEndTagName,
    MissingQuoteBeforeAttributeValue,
    MissingEqualAfterAttributeName,
    MissingSpaceBetweenAttributes,
    NestedComment,
    DoubleHyphenWithInComment,
    NoncharacterInInputStream,
    SurrogateInInputStream,
    SurrogateCharacterReference,
    UnexpectedCharacterAfterDoctypeSystemIdentifier,
    UnexpectedColonBeforeAttributeName,
    UnexpectedSolidusInTag,
    NoTargetNameInProcessingInstruction,
    MissingWhitespaceBeforeQuestionInProcessingInstruction,
    UnescapedCharacterInAttributeValue(char),

    // Parser errors
    UnexpectedTokenInStartPhase,
    UnexpectedTokenInMainPhase,
    UnexpectedTokenInEndPhase,
    UnexpectedEofInStartPhase,
    UnexpectedEofInMainPhase,
    OpeningAndEndingTagMismatch,
    UnexpectedCharacter,
}