float-pigment-css 0.8.2

The CSS parser for the float-pigment project.
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use alloc::string::ToString;
use core::{f32::consts::PI, marker::PhantomData};

use super::*;

impl CalcExpr {
    /// Check if the expression is a number.
    pub fn is_number(&self) -> bool {
        if let CalcExpr::Number(_) = self {
            return true;
        }
        false
    }

    /// Get the number value if the expression is a number.
    pub fn get_number(&self) -> Option<&Number> {
        match self {
            CalcExpr::Number(num) => Some(num.as_ref()),
            _ => None,
        }
    }

    /// Check if the expression is a static number `0`.
    pub fn is_zero(&self) -> bool {
        match self {
            CalcExpr::Number(v) => match *v.as_ref() {
                Number::F32(f) => f == 0.,
                Number::I32(i) => i == 0,
                _ => false,
            },
            _ => unreachable!(),
        }
    }

    /// Check if the expression is a literal value, a.k.a. does not contain any operator.
    pub fn is_specified_value(&self) -> bool {
        match self {
            CalcExpr::Angle(angle) => !matches!(angle.as_ref(), Angle::Calc(_)),
            CalcExpr::Number(num) => !matches!(num.as_ref(), Number::Calc(_)),
            CalcExpr::Length(length) => {
                !matches!(length, Length::Expr(_) | Length::Auto | Length::Undefined)
            }
            _ => false,
        }
    }

    /// Multiply `rhs` if `mul` is true; divide `rhs` otherwise.
    pub fn mul_div(&self, rhs: f32, mul: bool) -> Self {
        match self {
            CalcExpr::Angle(angle) => {
                let v = if mul {
                    angle.to_f32() * rhs
                } else {
                    angle.to_f32() / rhs
                };
                let ret = match angle.as_ref() {
                    Angle::Deg(_) => Angle::Deg(v),
                    Angle::Grad(_) => Angle::Grad(v),
                    Angle::Rad(_) => Angle::Rad(v),
                    Angle::Turn(_) => Angle::Turn(v),
                    Angle::Calc(_) => unreachable!(),
                };
                CalcExpr::Angle(Box::new(ret))
            }
            CalcExpr::Length(length) => {
                let v = if mul {
                    length.to_f32() * rhs
                } else {
                    length.to_f32() / rhs
                };
                let ret = match length {
                    Length::Px(_) => Length::Px(v),
                    Length::Em(_) => Length::Em(v),
                    Length::Rpx(_) => Length::Rpx(v),
                    Length::Ratio(_) => Length::Ratio(v),
                    Length::Rem(_) => Length::Rem(v),
                    Length::Vh(_) => Length::Vh(v),
                    Length::Vw(_) => Length::Vw(v),
                    Length::Vmax(_) => Length::Vmax(v),
                    Length::Vmin(_) => Length::Vmin(v),
                    _ => unreachable!(),
                };
                CalcExpr::Length(ret)
            }
            CalcExpr::Number(num) => {
                let ret = if mul {
                    num.to_f32() * rhs
                } else {
                    num.to_f32() / rhs
                };
                CalcExpr::Number(Box::new(Number::F32(ret)))
            }
            _ => unreachable!(),
        }
    }
}

impl Angle {
    /// Convert to `rad` form.
    ///
    /// Panics if it is an expression.
    pub fn to_rad(&self) -> Angle {
        match self {
            Angle::Rad(rad) => Angle::Rad(*rad),
            Angle::Deg(deg) => Angle::Rad(deg * PI / 180.),
            Angle::Grad(grad) => Angle::Rad(grad * PI / 200.),
            Angle::Turn(turn) => Angle::Rad(turn * 2. * PI),
            _ => panic!("not a literal value"),
        }
    }

    /// Create a new value from a turn-based value.
    pub fn from_ratio(turn: f32) -> Angle {
        Angle::Rad(turn * 2. * PI)
    }

    /// Erase the unit and leave the `f32` value.
    ///
    /// Panics if it is an expression.
    pub fn to_f32(&self) -> f32 {
        match self {
            Angle::Calc(_) => panic!("not a literal value"),
            Angle::Rad(v) => *v,
            Angle::Deg(v) => *v,
            Angle::Grad(v) => *v,
            Angle::Turn(v) => *v,
        }
    }
}

impl Length {
    /// Erase the unit and leave the `f32` value.
    ///
    /// Panics if it is an expression or it does not contain a unit.
    pub fn to_f32(&self) -> f32 {
        match self {
            Length::Px(v) => *v,
            Length::Em(v) => *v,
            Length::Rpx(v) => *v,
            Length::Ratio(v) => *v,
            Length::Rem(v) => *v,
            Length::Vh(v) => *v,
            Length::Vw(v) => *v,
            Length::Vmax(v) => *v,
            Length::Vmin(v) => *v,
            Length::Expr(_) | Length::Auto | Length::Undefined => panic!("not a literal value"),
        }
    }
}

pub(crate) struct ComputeCalcExpr<T> {
    _mark: PhantomData<*const T>,
}

impl ComputeCalcExpr<Angle> {
    pub fn try_compute(expr: &CalcExpr) -> Option<Angle> {
        match expr {
            CalcExpr::Angle(angle) => Some(angle.as_ref().clone().to_rad()),
            CalcExpr::Plus(l, r) | CalcExpr::Sub(l, r) => {
                let l = Self::try_compute(l)?;

                let r = Self::try_compute(r)?;
                match expr {
                    CalcExpr::Plus(_, _) => Some(Angle::Rad(l.to_f32() + r.to_f32())),
                    CalcExpr::Sub(_, _) => Some(Angle::Rad(l.to_f32() - r.to_f32())),
                    _ => None,
                }
            }
            CalcExpr::Mul(l, r) | CalcExpr::Div(l, r) => {
                let l = Self::try_compute(l)?;
                let r = ComputeCalcExpr::<Number>::try_compute(r)?;
                match expr {
                    CalcExpr::Mul(_, _) => Some(Angle::Rad(l.to_f32() * r.to_f32())),
                    CalcExpr::Div(_, _) => Some(Angle::Rad(l.to_f32() / r.to_f32())),
                    _ => None,
                }
            }
            CalcExpr::Length(Length::Ratio(ratio)) => Some(Angle::from_ratio(*ratio)),
            _ => None,
        }
    }
}

impl ComputeCalcExpr<Number> {
    pub fn try_compute(expr: &CalcExpr) -> Option<Number> {
        match expr {
            CalcExpr::Number(num) => Some(*num.clone()),
            CalcExpr::Plus(l, r)
            | CalcExpr::Sub(l, r)
            | CalcExpr::Mul(l, r)
            | CalcExpr::Div(l, r) => {
                let l = Self::try_compute(l)?;
                let r = Self::try_compute(r)?;
                match expr {
                    CalcExpr::Plus(_, _) => Some(Number::F32(l.to_f32() + r.to_f32())),
                    CalcExpr::Sub(_, _) => Some(Number::F32(l.to_f32() - r.to_f32())),
                    CalcExpr::Mul(_, _) => Some(Number::F32(l.to_f32() * r.to_f32())),
                    CalcExpr::Div(_, _) => Some(Number::F32(l.to_f32() / r.to_f32())),
                    _ => None,
                }
            }
            _ => None,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub(crate) enum LengthUnit {
    Px,
    Vw,
    Vh,
    Rem,
    Rpx,
    Em,
    Ratio,
    Vmin,
    Vmax,

    Undefined,
    Expr,
    Auto,
}

impl LengthUnit {
    pub(crate) fn is_specified_unit(&self) -> bool {
        !matches!(self, Self::Undefined | Self::Expr | Self::Auto)
    }
    pub(crate) fn to_length(unit: LengthUnit, value: f32) -> Length {
        match unit {
            LengthUnit::Auto => Length::Auto,
            LengthUnit::Undefined => Length::Undefined,
            LengthUnit::Expr => todo!(),
            LengthUnit::Px => Length::Px(value),
            LengthUnit::Vw => Length::Vw(value),
            LengthUnit::Vh => Length::Vh(value),
            LengthUnit::Rem => Length::Rem(value),
            LengthUnit::Em => Length::Em(value),
            LengthUnit::Rpx => Length::Rpx(value),
            LengthUnit::Ratio => Length::Ratio(value),
            LengthUnit::Vmin => Length::Vmin(value),
            LengthUnit::Vmax => Length::Vmax(value),
        }
    }
}

impl ComputeCalcExpr<Length> {
    pub(crate) fn try_compute(expr: &CalcExpr) -> Option<Length> {
        match expr {
            CalcExpr::Length(l) => Some(l.clone()),
            CalcExpr::Angle(_) | CalcExpr::Number(_) => None,
            CalcExpr::Plus(l, r) | CalcExpr::Sub(l, r) => {
                let l = Self::try_compute(l)?;
                let r = Self::try_compute(r)?;
                let ((l_unit, l_v), (r_unit, r_v)) =
                    (Self::length_unit_value(&l), Self::length_unit_value(&r));
                // TODO
                // merge same unit
                if (l_unit == r_unit) && l_unit.is_specified_unit() {
                    match expr {
                        CalcExpr::Plus(_, _) => {
                            return Some(LengthUnit::to_length(l_unit, l_v + r_v));
                        }
                        CalcExpr::Sub(_, _) => {
                            return Some(LengthUnit::to_length(l_unit, l_v - r_v));
                        }
                        _ => unreachable!(),
                    }
                }
                //
                None
            }
            _ => None,
        }
    }
    pub(crate) fn length_unit_value(length: &Length) -> (LengthUnit, f32) {
        match length {
            Length::Px(v) => (LengthUnit::Px, *v),
            Length::Em(v) => (LengthUnit::Em, *v),
            Length::Ratio(v) => (LengthUnit::Ratio, *v),
            Length::Rem(v) => (LengthUnit::Rem, *v),
            Length::Rpx(v) => (LengthUnit::Rpx, *v),
            Length::Vh(v) => (LengthUnit::Vh, *v),
            Length::Vw(v) => (LengthUnit::Vw, *v),
            Length::Vmax(v) => (LengthUnit::Vmax, *v),
            Length::Vmin(v) => (LengthUnit::Vmin, *v),
            Length::Undefined => (LengthUnit::Undefined, f32::NAN),
            Length::Auto => (LengthUnit::Auto, f32::NAN),
            Length::Expr(_) => (LengthUnit::Expr, f32::NAN),
        }
    }
}

#[inline(never)]
fn next_operator<'a, 't: 'a, 'i: 't>(parser: &'a mut Parser<'i, 't>) -> Option<Operator> {
    let mut ret = None;
    let _ = parser.try_parse::<_, (), ParseError<'_, CustomError>>(|parser| {
        let token = parser.next()?.clone();
        ret = match token {
            Token::Delim(c) => match c {
                '+' => Some(Operator::Plus),
                '-' => Some(Operator::Sub),
                '*' => Some(Operator::Mul),
                '/' => Some(Operator::Div),
                _ => None,
            },
            _ => None,
        };
        Err(parser.new_custom_error(CustomError::Unsupported))
    });
    ret
}

#[derive(Debug, PartialEq, Eq)]
enum Operator {
    Plus,
    Sub,
    Mul,
    Div,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum ExpectValueType {
    Number,
    NumberAndLength,
    NumberAndAngle,
    AngleAndLength,
}

#[inline(never)]
fn combine_calc_expr(operator: Operator, lhs: CalcExpr, rhs: CalcExpr) -> CalcExpr {
    // Unwrap nested `Length::Expr(Calc(...))` to flatten the expression tree.
    let final_lhs = match lhs {
        CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
        other => Box::new(other),
    };
    let final_rhs = match rhs {
        CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
        other => Box::new(other),
    };
    match operator {
        Operator::Plus => CalcExpr::Plus(final_lhs, final_rhs),
        Operator::Sub => CalcExpr::Sub(final_lhs, final_rhs),
        Operator::Mul => CalcExpr::Mul(final_lhs, final_rhs),
        Operator::Div => CalcExpr::Div(final_lhs, final_rhs),
    }
}

#[inline(never)]
pub(crate) fn parse_calc_inner<'a, 't: 'a, 'i: 't>(
    parser: &'a mut Parser<'i, 't>,
    properties: &mut Vec<PropertyMeta>,
    st: &mut ParseState,
    expect_type: ExpectValueType,
) -> Result<CalcExpr, ParseError<'i, CustomError>> {
    parser.parse_nested_block(|parser| {
        let ret = parse_calc_sum_expr(parser, properties, st, expect_type)?;
        Ok(ret)
    })
}

#[inline(never)]
fn parse_calc_sum_expr<'a, 't: 'a, 'i: 't>(
    parser: &'a mut Parser<'i, 't>,
    properties: &mut Vec<PropertyMeta>,
    st: &mut ParseState,
    expect_type: ExpectValueType,
) -> Result<CalcExpr, ParseError<'i, CustomError>> {
    let mut expr = parse_calc_product_expr(parser, properties, st, expect_type)?;
    loop {
        let op = next_operator(parser);
        if !(op.is_some() && (op == Some(Operator::Plus) || op == Some(Operator::Sub))) {
            return Ok(expr);
        }
        /*
         * The + and - operators must be surrounded by whitespace.
         */
        parser.next_including_whitespace()?;
        parser.next()?;
        parser.next_including_whitespace()?;
        let rhs = parse_calc_product_expr(parser, properties, st, expect_type)?;
        if let Some(op) = op {
            match op {
                Operator::Plus | Operator::Sub => {
                    expr = combine_calc_expr(op, expr, rhs);
                }
                _ => unreachable!(),
            }
        } else {
            return Err(parser.new_custom_error(CustomError::Unsupported));
        }
    }
}

#[inline(never)]
fn parse_calc_product_expr<'a, 't: 'a, 'i: 't>(
    parser: &'a mut Parser<'i, 't>,
    properties: &mut Vec<PropertyMeta>,
    st: &mut ParseState,
    expect_type: ExpectValueType,
) -> Result<CalcExpr, ParseError<'i, CustomError>> {
    let mut expr = parse_calc_parenthesis_expr(parser, properties, st, expect_type)?;
    loop {
        let op = next_operator(parser);
        if !(op.is_some() && (op == Some(Operator::Mul) || op == Some(Operator::Div))) {
            return Ok(expr);
        }
        /*
         * The * and / operators do not require whitespace, but adding it for consistency is recommended.
         */
        parser.next()?;
        let rhs = parse_calc_parenthesis_expr(parser, properties, st, expect_type)?;
        match op.unwrap() {
            Operator::Mul => {
                if expr.is_number() && rhs.is_number() {
                    let l_v = expr.get_number().unwrap();
                    let r_v = rhs.get_number().unwrap();
                    expr = CalcExpr::Number(Box::new(Number::F32(l_v.to_f32() * r_v.to_f32())));
                } else if expr.is_number() || rhs.is_number() {
                    if expr.is_number() {
                        if rhs.is_specified_value() {
                            expr = rhs.mul_div(expr.get_number().unwrap().to_f32(), true);
                        } else {
                            expr = combine_calc_expr(Operator::Mul, rhs, expr);
                        }
                    } else if expr.is_specified_value() {
                        expr = expr.mul_div(rhs.get_number().unwrap().to_f32(), true);
                    } else {
                        expr = combine_calc_expr(Operator::Mul, expr, rhs);
                    }
                } else {
                    return Err(parser.new_custom_error(CustomError::Unsupported));
                }
            }
            Operator::Div => {
                // NAN & zero
                if !rhs.is_number() || rhs.is_zero() {
                    return Err(
                        parser.new_custom_error(CustomError::Reason("divided by zero".to_string()))
                    );
                }
                if expr.is_number() && rhs.is_number() {
                    let l_v = expr.get_number().unwrap();
                    let r_v = rhs.get_number().unwrap();
                    expr = CalcExpr::Number(Box::new(Number::F32(l_v.to_f32() / r_v.to_f32())));
                } else if expr.is_specified_value() && rhs.is_number() {
                    expr = expr.mul_div(rhs.get_number().unwrap().to_f32(), false)
                } else {
                    expr = combine_calc_expr(Operator::Div, expr, rhs);
                }
            }
            _ => unreachable!(),
        }
    }
}

#[inline(never)]
fn parse_calc_parenthesis_expr<'a, 't: 'a, 'i: 't>(
    parser: &'a mut Parser<'i, 't>,
    properties: &mut Vec<PropertyMeta>,
    st: &mut ParseState,
    expect_type: ExpectValueType,
) -> Result<CalcExpr, ParseError<'i, CustomError>> {
    let value = parse_calc_value(parser, properties, st, expect_type);
    if value.is_ok() {
        return value;
    }
    parser.try_parse::<_, CalcExpr, ParseError<'_, CustomError>>(|parser| {
        parser.expect_parenthesis_block()?;
        parser.parse_nested_block(|parser| parse_calc_sum_expr(parser, properties, st, expect_type))
    })
}

#[inline(never)]
fn parse_calc_value<'a, 't: 'a, 'i: 't>(
    parser: &'a mut Parser<'i, 't>,
    properties: &mut Vec<PropertyMeta>,
    st: &mut ParseState,
    expect_type: ExpectValueType,
) -> Result<CalcExpr, ParseError<'i, CustomError>> {
    // match number
    let num =
        parser.try_parse::<_, Number, ParseError<'_, _>>(|parser| number(parser, properties, st));
    if let Ok(num) = num {
        return Ok(CalcExpr::Number(Box::new(num)));
    }
    // match length
    let length = parser.try_parse::<_, Length, ParseError<'_, _>>(|parser| {
        length_percentage_auto(parser, properties, st)
    });
    if let Ok(length) = length {
        if expect_type == ExpectValueType::NumberAndLength
            || expect_type == ExpectValueType::AngleAndLength
        {
            return Ok(CalcExpr::Length(length));
        }
    }
    // match angle
    let angle =
        parser.try_parse::<_, Angle, ParseError<'_, _>>(|parser| angle(parser, properties, st));
    if let Ok(angle) = angle {
        if expect_type == ExpectValueType::NumberAndAngle
            || expect_type == ExpectValueType::AngleAndLength
        {
            return Ok(CalcExpr::Angle(Box::new(angle)));
        }
    }
    Err(parser.new_custom_error(CustomError::Unmatched))
}