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
use is_macro::Is;
use swc_atoms::Atom;
use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span};

use crate::{
    AlphaValue, AnglePercentage, AtRule, CalcSum, CmykComponent, Color, ComplexSelector,
    DashedIdent, Delimiter, Dimension, FrequencyPercentage, Hue, IdSelector, Ident, Integer,
    KeyframeBlock, LayerName, LengthPercentage, Number, Percentage, Ratio, RelativeSelectorList,
    SelectorList, Str, SupportsCondition, TimePercentage, TokenAndSpan, UnicodeRange, Url,
};

#[ast_node("Stylesheet")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Stylesheet {
    pub span: Span,
    pub rules: Vec<Rule>,
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum Rule {
    #[tag("QualifiedRule")]
    QualifiedRule(Box<QualifiedRule>),

    #[tag("AtRule")]
    AtRule(Box<AtRule>),

    #[tag("ListOfComponentValues")]
    ListOfComponentValues(Box<ListOfComponentValues>),
}

impl Take for Rule {
    fn dummy() -> Self {
        Self::QualifiedRule(Take::dummy())
    }
}

#[ast_node("QualifiedRule")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct QualifiedRule {
    pub span: Span,
    pub prelude: QualifiedRulePrelude,
    pub block: SimpleBlock,
}

impl Take for QualifiedRule {
    fn dummy() -> Self {
        Self {
            span: Take::dummy(),
            prelude: Take::dummy(),
            block: Take::dummy(),
        }
    }
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum QualifiedRulePrelude {
    #[tag("SelectorList")]
    SelectorList(SelectorList),
    #[tag("RelativeSelectorList")]
    RelativeSelectorList(RelativeSelectorList),
    #[tag("ListOfComponentValues")]
    ListOfComponentValues(ListOfComponentValues),
}

impl Take for QualifiedRulePrelude {
    fn dummy() -> Self {
        Self::SelectorList(Take::dummy())
    }
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum StyleBlock {
    #[tag("AtRule")]
    AtRule(Box<AtRule>),
    #[tag("Declaration")]
    Declaration(Box<Declaration>),
    #[tag("QualifiedRule")]
    QualifiedRule(Box<QualifiedRule>),
    #[tag("ListOfComponentValues")]
    ListOfComponentValues(Box<ListOfComponentValues>),
}

#[ast_node("SimpleBlock")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct SimpleBlock {
    pub span: Span,
    pub name: TokenAndSpan,
    pub value: Vec<ComponentValue>,
}

impl Take for SimpleBlock {
    fn dummy() -> Self {
        Self {
            span: Take::dummy(),
            name: Take::dummy(),
            value: Take::dummy(),
        }
    }
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum FunctionName {
    #[tag("Ident")]
    Ident(Ident),
    #[tag("DashedIdent")]
    DashedIdent(DashedIdent),
}

impl PartialEq<str> for FunctionName {
    fn eq(&self, other: &str) -> bool {
        match self {
            FunctionName::DashedIdent(v) => *v == *other,
            FunctionName::Ident(v) => *v == *other,
        }
    }
}

impl PartialEq<&'_ str> for FunctionName {
    fn eq(&self, other: &&str) -> bool {
        match self {
            FunctionName::DashedIdent(v) => *v == **other,
            FunctionName::Ident(v) => *v == **other,
        }
    }
}

impl PartialEq<Atom> for FunctionName {
    fn eq(&self, other: &Atom) -> bool {
        match self {
            FunctionName::DashedIdent(v) => v.value == *other,
            FunctionName::Ident(v) => v.value == *other,
        }
    }
}

impl FunctionName {
    pub fn as_str(&self) -> &str {
        match self {
            FunctionName::DashedIdent(v) => &v.value,
            FunctionName::Ident(v) => &v.value,
        }
    }
}

#[ast_node("Function")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Function {
    /// Span starting from the `lo` of identifier and to the end of `)`.
    pub span: Span,
    pub name: FunctionName,
    pub value: Vec<ComponentValue>,
}

#[ast_node("ListOfComponentValues")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct ListOfComponentValues {
    pub span: Span,
    pub children: Vec<ComponentValue>,
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ComponentValue {
    // No grammar
    #[tag("TokenAndSpan")]
    PreservedToken(Box<TokenAndSpan>),
    #[tag("Function")]
    Function(Box<Function>),
    #[tag("SimpleBlock")]
    SimpleBlock(Box<SimpleBlock>),

    #[tag("AtRule")]
    AtRule(Box<AtRule>),

    #[tag("QualifiedRule")]
    QualifiedRule(Box<QualifiedRule>),

    #[tag("ListOfComponentValues")]
    ListOfComponentValues(Box<ListOfComponentValues>),

    #[tag("KeyframeBlock")]
    KeyframeBlock(Box<KeyframeBlock>),

    // Arbitrary Contents grammar
    #[tag("Ident")]
    Ident(Box<Ident>),
    #[tag("DashedIdent")]
    DashedIdent(Box<DashedIdent>),
    #[tag("String")]
    Str(Box<Str>),
    #[tag("Url")]
    Url(Box<Url>),
    #[tag("Integer")]
    Integer(Box<Integer>),
    #[tag("Number")]
    Number(Box<Number>),
    #[tag("Percentage")]
    Percentage(Box<Percentage>),
    #[tag("Dimension")]
    Dimension(Box<Dimension>),
    #[tag("LengthPercentage")]
    LengthPercentage(Box<LengthPercentage>),
    #[tag("FrequencyPercentage")]
    FrequencyPercentage(Box<FrequencyPercentage>),
    #[tag("AnglePercentage")]
    AnglePercentage(Box<AnglePercentage>),
    #[tag("TimePercentage")]
    TimePercentage(Box<TimePercentage>),
    #[tag("Ratio")]
    Ratio(Box<Ratio>),
    #[tag("UnicodeRange")]
    UnicodeRange(Box<UnicodeRange>),
    #[tag("Color")]
    Color(Box<Color>),
    #[tag("AlphaValue")]
    AlphaValue(Box<AlphaValue>),
    #[tag("Hue")]
    Hue(Box<Hue>),
    #[tag("CmykComponent")]
    CmykComponent(Box<CmykComponent>),
    #[tag("Delimiter")]
    Delimiter(Box<Delimiter>),

    // Special function Contents grammar
    #[tag("CalcSum")]
    CalcSum(Box<CalcSum>),
    #[tag("ComplexSelector")]
    ComplexSelector(Box<ComplexSelector>),
    #[tag("LayerName")]
    LayerName(Box<LayerName>),
    #[tag("SupportsCondition")]
    SupportsCondition(Box<SupportsCondition>),
    #[tag("Declaration")]
    Declaration(Box<Declaration>),
    #[tag("IdSelector")]
    IdSelector(Box<IdSelector>),
}

impl From<StyleBlock> for ComponentValue {
    #[inline]
    fn from(block: StyleBlock) -> Self {
        match block {
            StyleBlock::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
            StyleBlock::Declaration(declaration) => ComponentValue::Declaration(declaration),
            StyleBlock::QualifiedRule(qualified_rule) => {
                ComponentValue::QualifiedRule(qualified_rule)
            }
            StyleBlock::ListOfComponentValues(list_of_component_values) => {
                ComponentValue::ListOfComponentValues(list_of_component_values)
            }
        }
    }
}

impl From<DeclarationOrAtRule> for ComponentValue {
    #[inline]
    fn from(rule: DeclarationOrAtRule) -> Self {
        match rule {
            DeclarationOrAtRule::Declaration(declaration) => {
                ComponentValue::Declaration(declaration)
            }
            DeclarationOrAtRule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
            DeclarationOrAtRule::ListOfComponentValues(list_of_component_values) => {
                ComponentValue::ListOfComponentValues(list_of_component_values)
            }
        }
    }
}

impl From<Rule> for ComponentValue {
    #[inline]
    fn from(rule: Rule) -> Self {
        match rule {
            Rule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
            Rule::QualifiedRule(qualified_rule) => ComponentValue::QualifiedRule(qualified_rule),
            Rule::ListOfComponentValues(list_of_component_values) => {
                ComponentValue::ListOfComponentValues(list_of_component_values)
            }
        }
    }
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DeclarationOrAtRule {
    #[tag("Declaration")]
    Declaration(Box<Declaration>),
    #[tag("AtRule")]
    AtRule(Box<AtRule>),
    // For recovery mode
    #[tag("ListOfComponentValues")]
    ListOfComponentValues(Box<ListOfComponentValues>),
}

#[ast_node("Declaration")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Declaration {
    pub span: Span,
    pub name: DeclarationName,
    pub value: Vec<ComponentValue>,
    /// The span includes `!`
    pub important: Option<ImportantFlag>,
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DeclarationName {
    #[tag("Ident")]
    Ident(Ident),
    #[tag("DashedIdent")]
    DashedIdent(DashedIdent),
}

impl PartialEq<str> for DeclarationName {
    fn eq(&self, other: &str) -> bool {
        match self {
            DeclarationName::DashedIdent(v) => *v == *other,
            DeclarationName::Ident(v) => *v == *other,
        }
    }
}

impl PartialEq<Atom> for DeclarationName {
    fn eq(&self, other: &Atom) -> bool {
        match self {
            DeclarationName::DashedIdent(v) => v.value == *other,
            DeclarationName::Ident(v) => v.value == *other,
        }
    }
}

#[ast_node("ImportantFlag")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct ImportantFlag {
    pub span: Span,
    pub value: Ident,
}