Skip to main content

css_parse/syntax/
function_block.rs

1use crate::{
2	ComponentValues, CursorSink, Kind, KindSet, Parse, Parser, Peek, Result as ParserResult, SemanticEq, Span, T,
3	ToCursors, ToSpan,
4};
5use csskit_proc_macro::node;
6
7#[node]
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
10pub struct FunctionBlock<'a> {
11	pub name: T![Function],
12	pub params: ComponentValues<'a>,
13	pub close: T![')'],
14}
15
16impl<'a> Peek<'a> for FunctionBlock<'a> {
17	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Function]);
18}
19
20// https://drafts.csswg.org/css-syntax-3/#consume-function
21impl<'a> Parse<'a> for FunctionBlock<'a> {
22	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> ParserResult<Self>
23	where
24		Iter: Iterator<Item = crate::Cursor> + Clone,
25	{
26		let name = p.parse::<T![Function]>()?;
27		let params = p.parse::<ComponentValues>()?;
28		let close = p.parse::<T![')']>()?;
29		Ok(Self { name, params, close })
30	}
31}
32
33impl<'a> ToCursors for FunctionBlock<'a> {
34	fn to_cursors(&self, s: &mut impl CursorSink) {
35		ToCursors::to_cursors(&self.name, s);
36		ToCursors::to_cursors(&self.params, s);
37		ToCursors::to_cursors(&self.close, s);
38	}
39}
40
41impl<'a> ToSpan for FunctionBlock<'a> {
42	fn to_span(&self) -> Span {
43		self.name.to_span() + self.close.to_span()
44	}
45}
46
47impl<'a> SemanticEq for FunctionBlock<'a> {
48	fn semantic_eq(&self, other: &Self) -> bool {
49		self.name.semantic_eq(&other.name)
50			&& self.params.semantic_eq(&other.params)
51			&& self.close.semantic_eq(&other.close)
52	}
53}
54
55#[cfg(test)]
56mod tests {
57	use super::*;
58	use crate::EmptyAtomSet;
59	use crate::test_helpers::*;
60
61	#[test]
62	fn test_writes() {
63		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar)");
64		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar{})");
65	}
66}