css_ast 0.0.31

CSS Abstract Syntax Trees with visitable nodes and style value types.
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
use super::prelude::*;
use crate::{
	AngleOrNumber, AttrFunction, EnvFunction, FirstValidFunction, IfFunction, NoneOr, Number, TreeCountingFunction,
	Unresolved, VarFunction,
};
use css_parse::SemanticEq;

/// ```text,ignore
/// <calc()>  = calc( <calc-sum> )
/// <min()>   = min( <calc-sum># )
/// <max()>   = max( <calc-sum># )
/// <clamp()> = clamp( [ <calc-sum> | none ], <calc-sum>, [ <calc-sum> | none ] )
/// <round()> = round( <rounding-strategy>?, <calc-sum>, <calc-sum>? )
/// <mod()>   = mod( <calc-sum>, <calc-sum> )
/// <rem()>   = rem( <calc-sum>, <calc-sum> )
/// <sin()>   = sin( <calc-sum> )
/// <cos()>   = cos( <calc-sum> )
/// <tan()>   = tan( <calc-sum> )
/// <asin()>  = asin( <calc-sum> )
/// <acos()>  = acos( <calc-sum> )
/// <atan()>  = atan( <calc-sum> )
/// <atan2()> = atan2( <calc-sum>, <calc-sum> )
/// <pow()>   = pow( <calc-sum>, <calc-sum> )
/// <sqrt()>  = sqrt( <calc-sum> )
/// <hypot()> = hypot( <calc-sum># )
/// <log()>   = log( <calc-sum>, <calc-sum>? )
/// <exp()>   = exp( <calc-sum> )
/// <abs()>   = abs( <calc-sum> )
/// <sign()>  = sign( <calc-sum> )
/// <calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*
/// <calc-product> = <calc-value> [ [ '*' | / ] <calc-value> ]*
/// <calc-value> = <number> | <dimension> | <percentage> |
///                <calc-keyword> | ( <calc-sum> )
/// <calc-keyword> = e | pi | infinity | -infinity | NaN
/// <rounding-strategy> = nearest | up | down | to-zero | line-width
/// ```
#[node]
#[derive(Parse, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub enum MathFunction<'a, T> {
	CalcFunction(CalcFunction<'a, T>),
	MinFunction(MinFunction<'a, T>),
	MaxFunction(MaxFunction<'a, T>),
	ClampFunction(ClampFunction<'a, T>),
	RoundFunction(RoundFunction<'a, T>),
	ModFunction(ModFunction<'a, T>),
	RemFunction(RemFunction<'a, T>),
	SinFunction(SinFunction<'a>),
	CosFunction(CosFunction<'a>),
	TanFunction(TanFunction<'a>),
	AsinFunction(AsinFunction<'a>),
	AcosFunction(AcosFunction<'a>),
	AtanFunction(AtanFunction<'a>),
	Atan2Function(Atan2Function<'a, T>),
	PowFunction(PowFunction<'a>),
	SqrtFunction(SqrtFunction<'a>),
	HypotFunction(HypotFunction<'a, T>),
	LogFunction(LogFunction<'a>),
	ExpFunction(ExpFunction<'a>),
	AbsFunction(AbsFunction<'a, T>),
	SignFunction(SignFunction<'a, T>),
}

/// Returns true if the given atom is a CSS math function name.
pub fn is_math_function(atom: CssAtomSet) -> bool {
	matches!(
		atom,
		CssAtomSet::Calc
			| CssAtomSet::Min
			| CssAtomSet::Max
			| CssAtomSet::Clamp
			| CssAtomSet::Round
			| CssAtomSet::Mod
			| CssAtomSet::Rem
			| CssAtomSet::Sin
			| CssAtomSet::Cos
			| CssAtomSet::Tan
			| CssAtomSet::Asin
			| CssAtomSet::Acos
			| CssAtomSet::Atan
			| CssAtomSet::Atan2
			| CssAtomSet::Pow
			| CssAtomSet::Sqrt
			| CssAtomSet::Hypot
			| CssAtomSet::Log
			| CssAtomSet::Exp
			| CssAtomSet::Abs
			| CssAtomSet::Sign
	)
}

impl<'a, T> Peek<'a> for MathFunction<'a, T> {
	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
	where
		I: Iterator<Item = Cursor> + Clone,
	{
		<T![Function]>::peek(p, c) && is_math_function(p.to_atom::<CssAtomSet>(c))
	}
}

#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct CalcFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Calc)]
	pub name: Function,
	pub params: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct MinFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Min)]
	pub name: Function,
	pub params: CommaSeparated<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct MaxFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Max)]
	pub name: Function,
	pub params: CommaSeparated<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-clamp>
///
/// ```text,ignore
/// clamp( [ <calc-sum> | none ], <calc-sum>, [ <calc-sum> | none ] )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct ClampFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Clamp)]
	pub name: Function,
	pub min: NoneOr<CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma_1: Comma,
	pub value: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma_2: Comma,
	pub max: NoneOr<CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-round>
///
/// ```text,ignore
/// round( <rounding-strategy>?, <calc-sum>, <calc-sum>? )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct RoundFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Round)]
	pub name: Function,
	pub strategy: Option<RoundingStrategy>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma_1: Option<Comma>,
	pub value: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma_2: Option<Comma>,
	pub step: Option<Box<'a, CalcSum<'a, T>>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-mod>
///
/// ```text,ignore
/// mod( <calc-sum>, <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct ModFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Mod)]
	pub name: Function,
	pub dividend: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma: Comma,
	pub divisor: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-rem>
///
/// ```text,ignore
/// rem( <calc-sum>, <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct RemFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Rem)]
	pub name: Function,
	pub dividend: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma: Comma,
	pub divisor: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-sin>
///
/// ```text,ignore
/// sin( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct SinFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Sin)]
	pub name: Function,
	pub params: CalcSum<'a, AngleOrNumber>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-cos>
///
/// ```text,ignore
/// cos( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct CosFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Cos)]
	pub name: Function,
	pub params: CalcSum<'a, AngleOrNumber>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-tan>
///
/// ```text,ignore
/// tan( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct TanFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Tan)]
	pub name: Function,
	pub params: CalcSum<'a, AngleOrNumber>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-asin>
///
/// ```text,ignore
/// asin( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct AsinFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Asin)]
	pub name: Function,
	pub params: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-acos>
///
/// ```text,ignore
/// acos( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct AcosFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Acos)]
	pub name: Function,
	pub params: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-atan>
///
/// ```text,ignore
/// atan( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct AtanFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Atan)]
	pub name: Function,
	pub params: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-atan2>
///
/// ```text,ignore
/// atan2( <calc-sum>, <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct Atan2Function<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Atan2)]
	pub name: Function,
	pub y: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma: Comma,
	pub x: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-pow>
///
/// ```text,ignore
/// pow( <calc-sum>, <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct PowFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Pow)]
	pub name: Function,
	pub base: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma: Comma,
	pub exponent: Box<'a, CalcSum<'a, Number>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-sqrt>
///
/// ```text,ignore
/// sqrt( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct SqrtFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Sqrt)]
	pub name: Function,
	pub params: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-hypot>
///
/// ```text,ignore
/// hypot( <calc-sum># )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct HypotFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Hypot)]
	pub name: Function,
	pub params: CommaSeparated<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-log>
///
/// ```text,ignore
/// log( <calc-sum>, <calc-sum>? )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct LogFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Log)]
	pub name: Function,
	pub value: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub comma: Option<Comma>,
	pub base: Option<Box<'a, CalcSum<'a, Number>>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-exp>
///
/// ```text,ignore
/// exp( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct ExpFunction<'a> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Exp)]
	pub name: Function,
	pub params: CalcSum<'a, Number>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-abs>
///
/// ```text,ignore
/// abs( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct AbsFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Abs)]
	pub name: Function,
	pub params: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#funcdef-sign>
///
/// ```text,ignore
/// sign( <calc-sum> )
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub struct SignFunction<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[atom(CssAtomSet::Sign)]
	pub name: Function,
	pub params: CalcSum<'a, T>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#typedef-calc-sum>
///
/// ```text,ignore
/// <calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct CalcSum<'a, T> {
	pub first: CalcProduct<'a, T>,
	pub rest: Vec<'a, (CalcSumOperator, CalcProduct<'a, T>)>,
}

/// The `+` or `-` operator inside a `<calc-sum>`.
///
/// Per <https://drafts.csswg.org/css-values-4/#calc-syntax>, whitespace is required on both sides
/// of these operators (unlike `*` and `/`), since `1px -2px` (no space before `-`) must lex as two
/// adjacent values rather than a subtraction. This is why `Parse`/`Peek` are hand-written here.
#[node]
#[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum CalcSumOperator {
	Add(T![+]),
	Subtract(T![-]),
}

impl<'a> Peek<'a> for CalcSumOperator {
	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
	where
		I: Iterator<Item = Cursor> + Clone,
	{
		c == Kind::Delim && (c == '+' || c == '-') && p.peek_n_including_whitespace(1) == Kind::Whitespace
	}
}

impl<'a> Parse<'a> for CalcSumOperator {
	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
	where
		I: Iterator<Item = Cursor> + Clone,
	{
		let c = p.peek_n(1);
		// `+`/`-` in a `<calc-sum>` require whitespace on both sides (spec) and that whitespace is
		// significant: `calc(1px+1px)` is invalid and stripping it would change tokenisation.
		let rules = AssociatedWhitespaceRules::EnforceBefore | AssociatedWhitespaceRules::EnforceAfter;
		let op = if c == '+' {
			Self::Add(p.parse::<T![+]>()?.with_associated_whitespace(rules))
		} else {
			Self::Subtract(p.parse::<T![-]>()?.with_associated_whitespace(rules))
		};
		if p.peek_n_including_whitespace(1) != Kind::Whitespace {
			Err(Diagnostic::new(p.peek_n(1), Diagnostic::unexpected_delim))?;
		}
		Ok(op)
	}
}

/// A single operand of a `<calc-product>`: either a literal `<calc-value>` (number, dimension,
/// percentage, calc-keyword, or parenthesized `<calc-sum>`), or an arbitrary substitution
/// function (`var()`/`env()`/`attr()`/`if()`/`first-valid()`) per
/// <https://drafts.csswg.org/css-values-5/#arbitrary-substitution-function>.
#[node]
#[derive(Peek, ToCursors, ToSpan, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(children))]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub enum CalcOperand<'a, T> {
	Literal(CalcValue<'a, T>),
	Substituted(Box<'a, CalcOperandSubstitutionFunction<'a, T>>),
	#[peek(skip)]
	Unresolved(Box<'a, Unresolved<'a>>),
}

crate::values::impl_value_slot_parse!(CalcOperand, CalcOperandSubstitutionFunction, CalcValue<T>);

impl<'a, T: SemanticEq> SemanticEq for CalcOperand<'a, T> {
	fn semantic_eq(&self, other: &Self) -> bool {
		calc_operand_eq(self, other)
	}
}

fn calc_operand_eq<'a, T: SemanticEq>(a: &CalcOperand<'a, T>, b: &CalcOperand<'a, T>) -> bool {
	match (a, b) {
		(CalcOperand::Literal(a), CalcOperand::Literal(b)) => calc_value_eq(a, b),
		(CalcOperand::Substituted(a), CalcOperand::Substituted(b)) => calc_operand_substitution_eq(a, b),
		(CalcOperand::Unresolved(a), CalcOperand::Unresolved(b)) => a.semantic_eq(b),
		_ => false,
	}
}

fn calc_value_eq<'a, T: SemanticEq>(a: &CalcValue<'a, T>, b: &CalcValue<'a, T>) -> bool {
	match (a, b) {
		(CalcValue::Number(a), CalcValue::Number(b)) => a.semantic_eq(b),
		(CalcValue::Typed(a), CalcValue::Typed(b)) => a.semantic_eq(b),
		(CalcValue::Keyword(a), CalcValue::Keyword(b)) => a.semantic_eq(b),
		(CalcValue::TreeCounting(a), CalcValue::TreeCounting(b)) => a.semantic_eq(b),
		(CalcValue::Parenthesized(a), CalcValue::Parenthesized(b)) => calc_in_parens_eq(a, b),
		_ => false,
	}
}

fn calc_in_parens_eq<'a, T: SemanticEq>(a: &CalcInParens<'a, T>, b: &CalcInParens<'a, T>) -> bool {
	a.open.semantic_eq(&b.open) && calc_sum_eq(&a.sum, &b.sum)
}

fn calc_sum_eq<'a, T: SemanticEq>(a: &CalcSum<'a, T>, b: &CalcSum<'a, T>) -> bool {
	if a.rest.len() != b.rest.len() {
		return false;
	}
	calc_product_eq(&a.first, &b.first)
		&& a.rest.iter().zip(b.rest.iter()).all(|((op_a, p_a), (op_b, p_b))| op_a == op_b && calc_product_eq(p_a, p_b))
}

fn calc_product_eq<'a, T: SemanticEq>(a: &CalcProduct<'a, T>, b: &CalcProduct<'a, T>) -> bool {
	if a.rest.len() != b.rest.len() {
		return false;
	}
	calc_operand_eq(&a.first, &b.first)
		&& a.rest.iter().zip(b.rest.iter()).all(|((op_a, p_a), (op_b, p_b))| op_a == op_b && calc_operand_eq(p_a, p_b))
}

fn calc_operand_substitution_eq<'a, T: SemanticEq>(
	a: &CalcOperandSubstitutionFunction<'a, T>,
	b: &CalcOperandSubstitutionFunction<'a, T>,
) -> bool {
	match (a, b) {
		(CalcOperandSubstitutionFunction::Math(a), CalcOperandSubstitutionFunction::Math(b)) => math_function_eq(a, b),
		(CalcOperandSubstitutionFunction::Var(a), CalcOperandSubstitutionFunction::Var(b)) => a.semantic_eq(b),
		(CalcOperandSubstitutionFunction::Env(a), CalcOperandSubstitutionFunction::Env(b)) => a.semantic_eq(b),
		(CalcOperandSubstitutionFunction::Attr(a), CalcOperandSubstitutionFunction::Attr(b)) => a.semantic_eq(b),
		(CalcOperandSubstitutionFunction::If(a), CalcOperandSubstitutionFunction::If(b)) => a.semantic_eq(b),
		(CalcOperandSubstitutionFunction::FirstValid(a), CalcOperandSubstitutionFunction::FirstValid(b)) => {
			a.semantic_eq(b)
		}
		_ => false,
	}
}

fn math_function_eq<'a, T: SemanticEq>(a: &MathFunction<'a, T>, b: &MathFunction<'a, T>) -> bool {
	use MathFunction::*;
	match (a, b) {
		(CalcFunction(a), CalcFunction(b)) => calc_sum_eq(&a.params, &b.params),
		(MinFunction(a), MinFunction(b)) => a.params.semantic_eq(&b.params),
		(MaxFunction(a), MaxFunction(b)) => a.params.semantic_eq(&b.params),
		(ClampFunction(a), ClampFunction(b)) => {
			calc_sum_or_none_eq(&a.min, &b.min)
				&& calc_sum_eq(&a.value, &b.value)
				&& calc_sum_or_none_eq(&a.max, &b.max)
		}
		(RoundFunction(a), RoundFunction(b)) => {
			a.strategy == b.strategy && calc_sum_eq(&a.value, &b.value) && calc_sum_opt_eq(&a.step, &b.step)
		}
		(ModFunction(a), ModFunction(b)) => {
			calc_sum_eq(&a.dividend, &b.dividend) && calc_sum_eq(&a.divisor, &b.divisor)
		}
		(RemFunction(a), RemFunction(b)) => {
			calc_sum_eq(&a.dividend, &b.dividend) && calc_sum_eq(&a.divisor, &b.divisor)
		}
		(SinFunction(a), SinFunction(b)) => a.semantic_eq(b),
		(CosFunction(a), CosFunction(b)) => a.semantic_eq(b),
		(TanFunction(a), TanFunction(b)) => a.semantic_eq(b),
		(AsinFunction(a), AsinFunction(b)) => a.semantic_eq(b),
		(AcosFunction(a), AcosFunction(b)) => a.semantic_eq(b),
		(AtanFunction(a), AtanFunction(b)) => a.semantic_eq(b),
		(Atan2Function(a), Atan2Function(b)) => a.semantic_eq(b),
		(PowFunction(a), PowFunction(b)) => a.semantic_eq(b),
		(SqrtFunction(a), SqrtFunction(b)) => a.semantic_eq(b),
		(HypotFunction(a), HypotFunction(b)) => a.semantic_eq(b),
		(LogFunction(a), LogFunction(b)) => a.semantic_eq(b),
		(ExpFunction(a), ExpFunction(b)) => a.semantic_eq(b),
		(AbsFunction(a), AbsFunction(b)) => a.semantic_eq(b),
		(SignFunction(a), SignFunction(b)) => calc_sum_eq(&a.params, &b.params),
		_ => false,
	}
}

fn calc_sum_or_none_eq<'a, T: SemanticEq>(a: &NoneOr<CalcSum<'a, T>>, b: &NoneOr<CalcSum<'a, T>>) -> bool {
	match (a, b) {
		(NoneOr::Some(a), NoneOr::Some(b)) => calc_sum_eq(a, b),
		(NoneOr::None(_), NoneOr::None(_)) => true,
		_ => false,
	}
}

fn calc_sum_opt_eq<'a, T: SemanticEq>(
	a: &Option<Box<'a, CalcSum<'a, T>>>,
	b: &Option<Box<'a, CalcSum<'a, T>>>,
) -> bool {
	match (a, b) {
		(None, None) => true,
		(Some(a), Some(b)) => calc_sum_eq(a, b),
		_ => false,
	}
}

/// A substitution or nested math function appearing in a [`CalcOperand`] slot.
#[node]
#[derive(Peek, Parse, ToCursors, ToSpan, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(children))]
#[derive(csskit_derives::NodeWithMetadata)]
#[metadata(declaration_kinds = Computed)]
pub enum CalcOperandSubstitutionFunction<'a, T> {
	Math(MathFunction<'a, T>),
	Var(VarFunction<'a, CalcOperand<'a, T>>),
	Env(EnvFunction<'a, CalcOperand<'a, T>>),
	Attr(AttrFunction<'a>),
	If(IfFunction<'a, CalcOperand<'a, T>>),
	FirstValid(FirstValidFunction<'a, CalcOperand<'a, T>>),
}

/// <https://drafts.csswg.org/css-values-4/#typedef-calc-product>
///
/// ```text,ignore
/// <calc-product> = <calc-value> [ [ '*' | / ] <calc-value> ]*
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct CalcProduct<'a, T> {
	pub first: CalcOperand<'a, T>,
	pub rest: Vec<'a, (CalcProductOperator, CalcOperand<'a, T>)>,
}

impl<'a, T: SemanticEq> SemanticEq for CalcProduct<'a, T> {
	fn semantic_eq(&self, other: &Self) -> bool {
		calc_product_eq(self, other)
	}
}

/// The `*` or `/` operator inside a `<calc-product>`. Unlike `+`/`-`, no surrounding whitespace is
/// required.
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum CalcProductOperator {
	Multiply(T![*]),
	Divide(T![/]),
}

/// <https://drafts.csswg.org/css-values-4/#typedef-calc-value>
///
/// ```text,ignore
/// <calc-value> = <number> | <dimension> | <percentage> | <calc-keyword> | ( <calc-sum> )
///              | <sibling-count()> | <sibling-index()>
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(children))]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum CalcValue<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	Number(Number),
	Typed(T),
	Keyword(CalcKeyword),
	TreeCounting(TreeCountingFunction),
	Parenthesized(CalcInParens<'a, T>),
}

/// A parenthesized `<calc-sum>`, e.g. the `( 1px + 2px )` in `calc((1px + 2px) * 2)`.
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct CalcInParens<'a, T> {
	#[cfg_attr(feature = "visitable", visit(skip))]
	pub open: LeftParen,
	pub sum: Box<'a, CalcSum<'a, T>>,
	#[cfg_attr(feature = "visitable", visit(skip))]
	#[semantic_eq(skip)]
	pub close: RightParen,
}

/// <https://drafts.csswg.org/css-values-4/#typedef-calc-keyword>
///
/// ```text,ignore
/// <calc-keyword> = e | pi | infinity | -infinity | NaN
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum CalcKeyword {
	#[atom(CssAtomSet::E)]
	E(Ident),
	#[atom(CssAtomSet::Pi)]
	Pi(Ident),
	#[atom(CssAtomSet::Infinity)]
	Infinity(Ident),
	#[atom(CssAtomSet::_NegInfinity)]
	NegativeInfinity(Ident),
	#[atom(CssAtomSet::NaN)]
	NaN(Ident),
}

/// <https://drafts.csswg.org/css-values-4/#typedef-rounding-strategy>
///
/// ```text,ignore
/// <rounding-strategy> = nearest | up | down | to-zero | line-width
/// ```
#[node]
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum RoundingStrategy {
	#[atom(CssAtomSet::Nearest)]
	Nearest(Ident),
	#[atom(CssAtomSet::Up)]
	Up(Ident),
	#[atom(CssAtomSet::Down)]
	Down(Ident),
	#[atom(CssAtomSet::ToZero)]
	ToZero(Ident),
	#[atom(CssAtomSet::LineWidth)]
	LineWidth(Ident),
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{CssAtomSet, Length};
	use css_parse::{assert_parse, assert_parse_error};

	type LengthMathFunction<'a> = MathFunction<'a, Length>;

	#[test]
	fn test_calc_functions() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(2 + 3)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "min(1, 2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "max(3, 4)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "clamp(0, 5, 10)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(5.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "abs(-5)");
	}

	#[test]
	fn test_clamp() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "clamp(none, 5px, 10px)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "clamp(0px, 5px, none)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "clamp(1px, 2px, 3px)");
	}

	#[test]
	fn test_round() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(5.5, 2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(up, 5.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(nearest, 5.5, 2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(to-zero, 5.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "round(line-width, 5.5)");
	}

	#[test]
	fn test_mod_rem() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "mod(18, 5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "rem(18, 5)");
	}

	#[test]
	fn test_trig_and_exponential() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "sin(45deg)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "cos(45deg)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "tan(45deg)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "asin(0.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "acos(0.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "atan(0.5)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "atan2(1, 1)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "pow(2, 3)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "sqrt(2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "hypot(1, 2, 3)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "log(8, 2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "log(8)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "exp(1)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "sign(-5)");
	}

	#[test]
	fn test_calc_keywords() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(e)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(pi)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(infinity)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(-infinity)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(NaN)");
	}

	#[test]
	fn test_calc_nesting_and_precedence() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px + 2px * 3)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc((1px + 2px) * 3)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px * 2 / 3)");
	}

	#[test]
	fn test_calc_substitution() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(var(--foo) * 2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px + var(--foo))");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(var(--foo, 1px) + var(--bar, 2px))");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "min(var(--foo), 10px)");
	}

	#[test]
	fn test_calc_nested_math_function() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(min(1px, 2px) + 3px)");
	}

	#[test]
	fn test_calc_tree_counting_functions() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(sibling-index() * 10px)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(10px * sibling-count())");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "min(sibling-index(), 4)");
	}

	#[test]
	fn test_calc_operator_whitespace_sensitivity() {
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px + -2px)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px*2)");
		assert_parse!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px/2)");
	}

	#[test]
	fn test_calc_errors() {
		assert_parse_error!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px -2px)");
		assert_parse_error!(CssAtomSet::ATOMS, LengthMathFunction, "calc()");
		assert_parse_error!(CssAtomSet::ATOMS, LengthMathFunction, "clamp(1px, 2px)");
		assert_parse_error!(CssAtomSet::ATOMS, LengthMathFunction, "calc(1px + 10%)");
	}
}