omena-transform-passes 0.1.14

Transform pass registry and DAG planner for Omena CSS
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use omena_parser::StyleDialect;
use omena_syntax::SyntaxKind;

use crate::helpers::source_rewrite::rewrite_lexer_tokens;
use crate::helpers::values::{
    parse_whole_function_value_arguments, parse_whole_function_value_inner,
};

pub(crate) fn compress_css_numbers_with_lexer(
    source: &str,
    dialect: StyleDialect,
) -> (String, usize) {
    rewrite_lexer_tokens(source, dialect, |kind, text| {
        if matches!(
            kind,
            SyntaxKind::Number | SyntaxKind::Percentage | SyntaxKind::Dimension
        ) {
            return compress_numeric_token_text(text);
        }
        None
    })
}

fn compress_numeric_token_text(text: &str) -> Option<String> {
    let split = numeric_prefix_end(text)?;
    let (number, suffix) = text.split_at(split);
    let compressed = compress_number_prefix(number);
    let rewritten = format!("{compressed}{suffix}");
    (rewritten != text).then_some(rewritten)
}

pub(crate) fn parse_reducible_calc_value(value: &str) -> Option<String> {
    let inner = parse_whole_function_value_inner(value, "calc")?;
    let reduced = parse_reducible_numeric_expression(inner)?;
    Some(format_numeric_value_with_unit(reduced))
}

/// Reduces a standalone static numeric CSS expression into its shortest value text.
pub fn reduce_static_numeric_expression(value: &str) -> Option<String> {
    let reduced = parse_reducible_numeric_expression(value)?;
    Some(format_numeric_value_with_unit(reduced))
}

pub(crate) fn parse_reducible_abs_value(value: &str) -> Option<String> {
    let inner = parse_whole_function_value_inner(value, "abs")?;
    let parsed = parse_reducible_numeric_expression(inner)?;
    Some(format_numeric_value_with_unit(NumericValueWithUnit {
        value: parsed.value.abs(),
        unit: parsed.unit,
    }))
}

pub(crate) fn parse_reducible_sign_value(value: &str) -> Option<String> {
    let inner = parse_whole_function_value_inner(value, "sign")?;
    let parsed = parse_reducible_numeric_expression(inner)?;
    let value = if parsed.value > 0.0 {
        1.0
    } else if parsed.value < 0.0 {
        -1.0
    } else {
        0.0
    };
    Some(format_css_number(value))
}

pub(crate) fn parse_reducible_round_value(value: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, "round")?;
    let (strategy, value, interval) = match arguments.as_slice() {
        [value, interval] => (
            StaticRoundStrategy::Nearest,
            value.as_str(),
            interval.as_str(),
        ),
        [strategy, value, interval] => (
            StaticRoundStrategy::parse(strategy.trim())?,
            value.as_str(),
            interval.as_str(),
        ),
        _ => return None,
    };
    let value = parse_reducible_numeric_expression(value.trim())?;
    let interval = parse_reducible_numeric_expression(interval.trim())?;
    if value.unit != interval.unit || interval.value <= 0.0 {
        return None;
    }
    let quotient = value.value / interval.value;
    let rounded = strategy.apply(quotient)?;
    Some(format_numeric_value_with_unit(NumericValueWithUnit {
        value: rounded * interval.value,
        unit: value.unit,
    }))
}

pub(crate) fn parse_reducible_mod_value(value: &str) -> Option<String> {
    parse_reducible_positive_remainder_value(value, "mod")
}

pub(crate) fn parse_reducible_rem_value(value: &str) -> Option<String> {
    parse_reducible_positive_remainder_value(value, "rem")
}

pub(crate) fn parse_reducible_hypot_value(value: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, "hypot")?;
    let first_argument = arguments.first()?;
    let first = parse_reducible_numeric_expression(first_argument.trim())?;
    let mut sum_of_squares = first.value * first.value;

    for argument in arguments.iter().skip(1) {
        let parsed = parse_reducible_numeric_expression(argument.trim())?;
        if parsed.unit != first.unit {
            return None;
        }
        sum_of_squares += parsed.value * parsed.value;
    }

    Some(format_numeric_value_with_unit(NumericValueWithUnit {
        value: sum_of_squares.sqrt(),
        unit: first.unit,
    }))
}

pub(crate) fn parse_reducible_sqrt_value(value: &str) -> Option<String> {
    let inner = parse_whole_function_value_inner(value, "sqrt")?;
    let parsed = parse_reducible_numeric_expression(inner)?;
    if !parsed.unit.is_empty() || parsed.value < 0.0 {
        return None;
    }
    Some(format_css_number(parsed.value.sqrt()))
}

pub(crate) fn parse_reducible_pow_value(value: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, "pow")?;
    let [base, exponent] = arguments.as_slice() else {
        return None;
    };
    let base = parse_reducible_numeric_expression(base.trim())?;
    let exponent = parse_reducible_numeric_expression(exponent.trim())?;
    if !base.unit.is_empty() || !exponent.unit.is_empty() {
        return None;
    }
    let value = base.value.powf(exponent.value);
    value.is_finite().then(|| format_css_number(value))
}

pub(crate) fn parse_reducible_exp_value(value: &str) -> Option<String> {
    let inner = parse_whole_function_value_inner(value, "exp")?;
    let parsed = parse_reducible_numeric_expression(inner)?;
    if !parsed.unit.is_empty() {
        return None;
    }
    let value = parsed.value.exp();
    value.is_finite().then(|| format_css_number(value))
}

pub(crate) fn parse_reducible_log_value(value: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, "log")?;
    let value = match arguments.as_slice() {
        [value] | [value, _] => value,
        _ => return None,
    };
    let value = parse_reducible_numeric_expression(value.trim())?;
    if !value.unit.is_empty() || value.value <= 0.0 {
        return None;
    };
    let base = match arguments.as_slice() {
        [_] => std::f64::consts::E,
        [_, base] => {
            let base = parse_reducible_numeric_expression(base.trim())?;
            if !base.unit.is_empty() || base.value <= 0.0 || base.value == 1.0 {
                return None;
            }
            base.value
        }
        _ => return None,
    };
    let result = value.value.log(base);
    result.is_finite().then(|| format_css_number(result))
}

pub(crate) fn parse_reducible_min_value(value: &str) -> Option<String> {
    parse_reducible_extreme_value(value, "min", f64::min)
}

pub(crate) fn parse_reducible_max_value(value: &str) -> Option<String> {
    parse_reducible_extreme_value(value, "max", f64::max)
}

pub(crate) fn parse_reducible_clamp_value(value: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, "clamp")?;
    let [minimum, preferred, maximum] = arguments.as_slice() else {
        return None;
    };
    let minimum = parse_numeric_value_with_unit(minimum.trim())?;
    let preferred = parse_numeric_value_with_unit(preferred.trim())?;
    let maximum = parse_numeric_value_with_unit(maximum.trim())?;
    if preferred.unit != minimum.unit || maximum.unit != minimum.unit {
        return None;
    }
    let selected = preferred.value.min(maximum.value).max(minimum.value);
    Some(format!("{}{}", format_css_number(selected), minimum.unit))
}

fn parse_reducible_extreme_value(
    value: &str,
    function_name: &str,
    reduce: fn(f64, f64) -> f64,
) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, function_name)?;
    let first = arguments.first()?;
    let first = parse_numeric_value_with_unit(first.trim())?;
    let mut selected = first.value;
    let unit = first.unit;

    for argument in arguments.iter().skip(1) {
        let candidate = parse_numeric_value_with_unit(argument.trim())?;
        if candidate.unit != unit {
            return None;
        }
        selected = reduce(selected, candidate.value);
    }

    Some(format!("{}{}", format_css_number(selected), unit))
}

fn parse_reducible_positive_remainder_value(value: &str, function_name: &str) -> Option<String> {
    let arguments = parse_whole_function_value_arguments(value, function_name)?;
    let [dividend, divisor] = arguments.as_slice() else {
        return None;
    };
    let dividend = parse_reducible_numeric_expression(dividend.trim())?;
    let divisor = parse_reducible_numeric_expression(divisor.trim())?;
    if dividend.unit != divisor.unit || dividend.value < 0.0 || divisor.value <= 0.0 {
        return None;
    }
    Some(format_numeric_value_with_unit(NumericValueWithUnit {
        value: dividend.value % divisor.value,
        unit: dividend.unit,
    }))
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct NumericValueWithUnit<'a> {
    pub(crate) value: f64,
    pub(crate) unit: &'a str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StaticRoundStrategy {
    Nearest,
    Up,
    Down,
    ToZero,
}

impl StaticRoundStrategy {
    fn parse(text: &str) -> Option<Self> {
        match text.to_ascii_lowercase().as_str() {
            "nearest" => Some(Self::Nearest),
            "up" => Some(Self::Up),
            "down" => Some(Self::Down),
            "to-zero" => Some(Self::ToZero),
            _ => None,
        }
    }

    fn apply(self, value: f64) -> Option<f64> {
        match self {
            Self::Nearest if quotient_is_halfway_between_integers(value) => None,
            Self::Nearest => Some(value.round()),
            Self::Up => Some(value.ceil()),
            Self::Down => Some(value.floor()),
            Self::ToZero => Some(value.trunc()),
        }
    }
}

fn quotient_is_halfway_between_integers(value: f64) -> bool {
    (value.abs().fract() - 0.5).abs() < f64::EPSILON
}

pub(crate) fn parse_numeric_value_with_unit(text: &str) -> Option<NumericValueWithUnit<'_>> {
    let text = text.trim();
    let mut parser = NumericExpressionParser::new(text);
    let parsed = parser.parse_number()?;
    parser.skip_whitespace();
    (parser.is_eof()).then_some(parsed)
}

fn parse_reducible_numeric_expression(inner: &str) -> Option<NumericValueWithUnit<'_>> {
    let mut parser = NumericExpressionParser::new(inner);
    let parsed = parser.parse_expression()?;
    parser.skip_whitespace();
    parser.is_eof().then_some(parsed)
}

struct NumericExpressionParser<'a> {
    text: &'a str,
    index: usize,
}

impl<'a> NumericExpressionParser<'a> {
    fn new(text: &'a str) -> Self {
        Self { text, index: 0 }
    }

    fn parse_expression(&mut self) -> Option<NumericValueWithUnit<'a>> {
        let mut left = self.parse_term()?;
        loop {
            self.skip_whitespace();
            let Some(operator) = self.peek_char().filter(|ch| matches!(ch, '+' | '-')) else {
                break;
            };
            self.index += operator.len_utf8();
            let right = self.parse_term()?;
            left = combine_numeric_additive(left, right, operator)?;
        }
        Some(left)
    }

    fn parse_term(&mut self) -> Option<NumericValueWithUnit<'a>> {
        let mut left = self.parse_factor()?;
        loop {
            self.skip_whitespace();
            let Some(operator) = self.peek_char().filter(|ch| matches!(ch, '*' | '/')) else {
                break;
            };
            self.index += operator.len_utf8();
            let right = self.parse_factor()?;
            left = combine_numeric_multiplicative(left, right, operator)?;
        }
        Some(left)
    }

    fn parse_factor(&mut self) -> Option<NumericValueWithUnit<'a>> {
        self.skip_whitespace();
        if self.consume_char('(') {
            let parsed = self.parse_expression()?;
            self.skip_whitespace();
            self.consume_char(')').then_some(parsed)
        } else {
            self.parse_number()
        }
    }

    fn parse_number(&mut self) -> Option<NumericValueWithUnit<'a>> {
        self.skip_whitespace();
        let start = self.index;
        let split = numeric_prefix_end(&self.text[start..])?;
        let number_end = start + split;
        let unit_start = number_end;
        self.index = number_end;
        if self.peek_char() == Some('%') {
            self.index += '%'.len_utf8();
        } else {
            while self.peek_char().is_some_and(is_css_numeric_unit_continue) {
                let ch = self.peek_char()?;
                self.index += ch.len_utf8();
            }
        }
        let number = &self.text[start..number_end];
        let unit = &self.text[unit_start..self.index];
        let value = number.parse::<f64>().ok()?;
        value
            .is_finite()
            .then_some(NumericValueWithUnit { value, unit })
    }

    fn skip_whitespace(&mut self) {
        while let Some(ch) = self.peek_char() {
            if !ch.is_whitespace() {
                break;
            }
            self.index += ch.len_utf8();
        }
    }

    fn consume_char(&mut self, expected: char) -> bool {
        if self.peek_char() == Some(expected) {
            self.index += expected.len_utf8();
            true
        } else {
            false
        }
    }

    fn peek_char(&self) -> Option<char> {
        self.text[self.index..].chars().next()
    }

    fn is_eof(&self) -> bool {
        self.index == self.text.len()
    }
}

fn combine_numeric_additive<'a>(
    left: NumericValueWithUnit<'a>,
    right: NumericValueWithUnit<'a>,
    operator: char,
) -> Option<NumericValueWithUnit<'a>> {
    if left.unit != right.unit {
        return None;
    }
    let value = if operator == '+' {
        left.value + right.value
    } else {
        left.value - right.value
    };
    Some(NumericValueWithUnit {
        value,
        unit: left.unit,
    })
}

fn combine_numeric_multiplicative<'a>(
    left: NumericValueWithUnit<'a>,
    right: NumericValueWithUnit<'a>,
    operator: char,
) -> Option<NumericValueWithUnit<'a>> {
    match operator {
        '*' if left.unit.is_empty() && right.unit.is_empty() => Some(NumericValueWithUnit {
            value: left.value * right.value,
            unit: "",
        }),
        '*' if left.unit.is_empty() => Some(NumericValueWithUnit {
            value: left.value * right.value,
            unit: right.unit,
        }),
        '*' if right.unit.is_empty() => Some(NumericValueWithUnit {
            value: left.value * right.value,
            unit: left.unit,
        }),
        '/' if right.unit.is_empty() && right.value != 0.0 => Some(NumericValueWithUnit {
            value: left.value / right.value,
            unit: left.unit,
        }),
        _ => None,
    }
}

fn format_numeric_value_with_unit(value: NumericValueWithUnit<'_>) -> String {
    format!("{}{}", format_css_number(value.value), value.unit)
}

fn is_css_numeric_unit_continue(ch: char) -> bool {
    ch.is_ascii_alphabetic()
}

pub(crate) fn format_css_number(value: f64) -> String {
    if value.fract() == 0.0 {
        return format!("{value:.0}");
    }
    let formatted = format!("{value:.6}");
    formatted
        .trim_end_matches('0')
        .trim_end_matches('.')
        .to_string()
}

pub(crate) fn numeric_prefix_end(text: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut index = 0;

    if matches!(bytes.get(index), Some(b'+') | Some(b'-')) {
        index += 1;
    }

    let integer_start = index;
    while matches!(bytes.get(index), Some(b'0'..=b'9')) {
        index += 1;
    }
    let saw_integer_digit = index > integer_start;

    if bytes.get(index) == Some(&b'.') {
        index += 1;
        let fraction_start = index;
        while matches!(bytes.get(index), Some(b'0'..=b'9')) {
            index += 1;
        }
        if !saw_integer_digit && index == fraction_start {
            return None;
        }
    } else if !saw_integer_digit {
        return None;
    }

    if matches!(bytes.get(index), Some(b'e') | Some(b'E')) {
        let exponent_marker = index;
        let mut exponent_index = index + 1;
        if matches!(bytes.get(exponent_index), Some(b'+') | Some(b'-')) {
            exponent_index += 1;
        }
        let exponent_digit_start = exponent_index;
        while matches!(bytes.get(exponent_index), Some(b'0'..=b'9')) {
            exponent_index += 1;
        }
        if exponent_index > exponent_digit_start {
            index = exponent_index;
        } else {
            index = exponent_marker;
        }
    }

    Some(index)
}

pub(crate) fn compress_number_prefix(number: &str) -> String {
    let (sign, unsigned) = match number.as_bytes().first() {
        Some(b'+') | Some(b'-') => (&number[..1], &number[1..]),
        _ => ("", number),
    };
    let sign = if sign == "+" || is_zero_number_prefix(unsigned) {
        ""
    } else {
        sign
    };
    let (mantissa, exponent) = split_number_exponent(unsigned);
    let compressed_mantissa = compress_decimal_mantissa(mantissa);
    let mut compressed = format!("{sign}{compressed_mantissa}");

    if let Some(exponent) = exponent {
        let normalized_exponent = normalize_exponent_suffix(exponent);
        if normalized_exponent != "0" && !is_zero_number_prefix(&compressed) {
            compressed.push('e');
            compressed.push_str(&normalized_exponent);
        }
    }

    compressed
}

fn split_number_exponent(number: &str) -> (&str, Option<&str>) {
    if let Some(index) = number.find(['e', 'E']) {
        (&number[..index], Some(&number[index + 1..]))
    } else {
        (number, None)
    }
}

fn compress_decimal_mantissa(mantissa: &str) -> String {
    let Some((before_dot, after_dot)) = mantissa.split_once('.') else {
        return compress_integer_digits(mantissa);
    };

    let trimmed_fraction = after_dot.trim_end_matches('0');
    let compressed_integer = compress_integer_digits(before_dot);
    let mut compressed_unsigned = if trimmed_fraction.is_empty() {
        compressed_integer
    } else {
        format!("{compressed_integer}.{trimmed_fraction}")
    };

    if let Some(rest) = compressed_unsigned.strip_prefix("0.") {
        compressed_unsigned = format!(".{rest}");
    }

    if compressed_unsigned.is_empty() {
        compressed_unsigned.push('0');
    }

    compressed_unsigned
}

fn compress_integer_digits(digits: &str) -> String {
    let trimmed = digits.trim_start_matches('0');
    if trimmed.is_empty() {
        "0".to_string()
    } else {
        trimmed.to_string()
    }
}

fn normalize_exponent_suffix(exponent: &str) -> String {
    let (sign, digits) = match exponent.as_bytes().first() {
        Some(b'+') => ("", &exponent[1..]),
        Some(b'-') => ("-", &exponent[1..]),
        _ => ("", exponent),
    };
    let digits = digits.trim_start_matches('0');
    let digits = if digits.is_empty() { "0" } else { digits };
    if digits == "0" {
        digits.to_string()
    } else {
        format!("{sign}{digits}")
    }
}

fn is_zero_number_prefix(number: &str) -> bool {
    number.chars().all(|ch| matches!(ch, '0' | '.' | '+' | '-'))
}