css_ast/visit/
mod.rs

1include!(concat!(env!("OUT_DIR"), "/css_node_kind.rs"));
2include!(concat!(env!("OUT_DIR"), "/css_apply_visit_methods.rs"));
3pub use apply_visit_methods;
4
5use bumpalo::collections::Vec;
6use css_parse::{
7	AtRule, Block, CommaSeparated, ComponentValues, Declaration, DeclarationList, DeclarationValue, Function,
8	NoBlockAllowed, NoPreludeAllowed, QualifiedRule, RuleList, syntax::BadDeclaration, token_macros,
9};
10
11use crate::*;
12
13macro_rules! visit_mut_trait {
14	( $(
15		$name: ident$(<$life:lifetime>)?($obj: ty),
16	)+ ) => {
17		pub trait VisitMut: Sized {
18			fn visit_declaration<'a, T: DeclarationValue<'a>>(&mut self, _rule: &mut Declaration<'a, T>) {}
19			fn visit_bad_declaration<'a>(&mut self, _rule: &mut BadDeclaration<'a>) {}
20			fn visit_string(&mut self, _str: &mut token_macros::String) {}
21			// fn visit_function<'a, T>(&mut self, _function: &mut Function<'a, FT, T>) {}
22			$(
23				fn $name$(<$life>)?(&mut self, _rule: &mut $obj) {}
24			)+
25		}
26	}
27}
28apply_visit_methods!(visit_mut_trait);
29
30macro_rules! visit_trait {
31	( $(
32		$name: ident$(<$life:lifetime>)?($obj: ty),
33	)+ ) => {
34		pub trait Visit: Sized {
35			fn visit_declaration<'a, T: DeclarationValue<'a>>(&mut self, _rule: &Declaration<'a, T>) {}
36			fn visit_bad_declaration<'a>(&mut self, _rule: &BadDeclaration<'a>) {}
37			fn visit_string(&mut self, _str: &token_macros::String) {}
38			// fn visit_function<'a, T>(&mut self, _function: &mut Function<'a, FT, T>) {}
39			$(
40				fn $name$(<$life>)?(&mut self, _rule: &$obj) {}
41			)+
42		}
43	}
44}
45apply_visit_methods!(visit_trait);
46
47pub trait VisitableMut {
48	fn accept_mut<V: VisitMut>(&mut self, v: &mut V);
49}
50
51pub trait Visitable {
52	fn accept<V: Visit>(&self, v: &mut V);
53}
54
55impl<T> VisitableMut for Option<T>
56where
57	T: VisitableMut,
58{
59	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
60		if let Some(node) = self {
61			node.accept_mut(v)
62		}
63	}
64}
65
66impl Visitable for token_macros::String {
67	fn accept<V: Visit>(&self, v: &mut V) {
68		v.visit_string(self);
69	}
70}
71
72impl VisitableMut for token_macros::String {
73	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
74		v.visit_string(self);
75	}
76}
77
78impl<T> Visitable for Option<T>
79where
80	T: Visitable,
81{
82	fn accept<V: Visit>(&self, v: &mut V) {
83		if let Some(node) = self {
84			node.accept(v)
85		}
86	}
87}
88
89impl<'a, T, const MIN: usize> VisitableMut for CommaSeparated<'a, T, MIN>
90where
91	T: VisitableMut + Peek<'a> + Parse<'a> + ToCursors + ToSpan,
92{
93	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
94		for (node, _) in self {
95			node.accept_mut(v)
96		}
97	}
98}
99
100impl<'a, T, const MIN: usize> Visitable for CommaSeparated<'a, T, MIN>
101where
102	T: Visitable + Peek<'a> + Parse<'a> + ToCursors + ToSpan,
103{
104	fn accept<V: Visit>(&self, v: &mut V) {
105		for (node, _) in self {
106			node.accept(v)
107		}
108	}
109}
110
111impl<FT, T> VisitableMut for Function<FT, T>
112where
113	FT: Into<token_macros::Function>,
114	T: VisitableMut,
115{
116	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
117		self.parameters.accept_mut(v);
118	}
119}
120
121impl<FT, T> Visitable for Function<FT, T>
122where
123	FT: Into<token_macros::Function>,
124	T: Visitable,
125{
126	fn accept<V: Visit>(&self, v: &mut V) {
127		self.parameters.accept(v);
128	}
129}
130
131impl<'a, T> VisitableMut for Declaration<'a, T>
132where
133	T: VisitableMut + DeclarationValue<'a>,
134{
135	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
136		v.visit_declaration::<T>(self);
137		self.value.accept_mut(v)
138	}
139}
140
141impl<'a, T> Visitable for Declaration<'a, T>
142where
143	T: Visitable + DeclarationValue<'a>,
144{
145	fn accept<V: Visit>(&self, v: &mut V) {
146		v.visit_declaration::<T>(self);
147		self.value.accept(v)
148	}
149}
150
151impl<'a, T> VisitableMut for DeclarationList<'a, T>
152where
153	T: VisitableMut + DeclarationValue<'a>,
154{
155	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
156		for declaration in &mut self.declarations {
157			declaration.accept_mut(v);
158		}
159	}
160}
161
162impl<'a, T> Visitable for DeclarationList<'a, T>
163where
164	T: Visitable + DeclarationValue<'a>,
165{
166	fn accept<V: Visit>(&self, v: &mut V) {
167		for declaration in &self.declarations {
168			declaration.accept(v);
169		}
170	}
171}
172
173impl<AT, P, B> VisitableMut for AtRule<AT, P, B>
174where
175	AT: Into<token_macros::AtKeyword>,
176	P: VisitableMut,
177	B: VisitableMut,
178{
179	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
180		self.prelude.accept_mut(v);
181		self.block.accept_mut(v);
182	}
183}
184
185impl<AT, P, B> Visitable for AtRule<AT, P, B>
186where
187	AT: Into<token_macros::AtKeyword>,
188	P: Visitable,
189	B: Visitable,
190{
191	fn accept<V: Visit>(&self, v: &mut V) {
192		self.prelude.accept(v);
193		self.block.accept(v);
194	}
195}
196
197impl<'a, T> VisitableMut for RuleList<'a, T>
198where
199	T: VisitableMut + Parse<'a> + ToCursors + ToSpan,
200{
201	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
202		self.rules.accept_mut(v);
203	}
204}
205
206impl<'a, T> Visitable for RuleList<'a, T>
207where
208	T: Visitable + Parse<'a> + ToCursors + ToSpan,
209{
210	fn accept<V: Visit>(&self, v: &mut V) {
211		self.rules.accept(v);
212	}
213}
214
215impl<'a, P, D, R> VisitableMut for QualifiedRule<'a, P, D, R>
216where
217	P: VisitableMut + Peek<'a> + Parse<'a> + ToCursors + ToSpan,
218	D: VisitableMut + DeclarationValue<'a>,
219	R: VisitableMut + Parse<'a> + ToCursors + ToSpan,
220	Block<'a, D, R>: Parse<'a> + ToCursors + ToSpan,
221{
222	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
223		self.prelude.accept_mut(v);
224		self.block.accept_mut(v);
225	}
226}
227
228impl<'a, P, D, R> Visitable for QualifiedRule<'a, P, D, R>
229where
230	P: Visitable + Peek<'a> + Parse<'a> + ToCursors + ToSpan,
231	D: Visitable + DeclarationValue<'a>,
232	R: Visitable + Parse<'a> + ToCursors + ToSpan,
233	Block<'a, D, R>: Parse<'a> + ToCursors + ToSpan,
234{
235	fn accept<V: Visit>(&self, v: &mut V) {
236		self.prelude.accept(v);
237		self.block.accept(v);
238	}
239}
240
241impl<'a, D, R> VisitableMut for Block<'a, D, R>
242where
243	D: VisitableMut + DeclarationValue<'a>,
244	R: VisitableMut + Parse<'a> + ToCursors + ToSpan,
245{
246	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
247		for declaration in &mut self.declarations {
248			declaration.accept_mut(v);
249		}
250		for rule in &mut self.rules {
251			rule.accept_mut(v);
252		}
253	}
254}
255
256impl<'a, D, R> Visitable for Block<'a, D, R>
257where
258	D: Visitable + DeclarationValue<'a>,
259	R: Visitable + Parse<'a> + ToCursors + ToSpan,
260{
261	fn accept<V: Visit>(&self, v: &mut V) {
262		for declaration in &self.declarations {
263			declaration.accept(v);
264		}
265		for rule in &self.rules {
266			rule.accept(v);
267		}
268	}
269}
270
271impl<'a, T> VisitableMut for Vec<'a, T>
272where
273	T: VisitableMut,
274{
275	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
276		for node in self {
277			node.accept_mut(v);
278		}
279	}
280}
281
282impl<'a, T> Visitable for Vec<'a, T>
283where
284	T: Visitable,
285{
286	fn accept<V: Visit>(&self, v: &mut V) {
287		for node in self {
288			node.accept(v)
289		}
290	}
291}
292
293impl<'a> VisitableMut for BadDeclaration<'a> {
294	fn accept_mut<V: VisitMut>(&mut self, v: &mut V) {
295		v.visit_bad_declaration(self);
296	}
297}
298
299impl<'a> Visitable for BadDeclaration<'a> {
300	fn accept<V: Visit>(&self, v: &mut V) {
301		v.visit_bad_declaration(self);
302	}
303}
304
305impl<'a> VisitableMut for ComponentValues<'a> {
306	fn accept_mut<V: VisitMut>(&mut self, _: &mut V) {}
307}
308
309impl<'a> Visitable for ComponentValues<'a> {
310	fn accept<V: Visit>(&self, _: &mut V) {}
311}
312
313impl VisitableMut for NoPreludeAllowed {
314	fn accept_mut<V: VisitMut>(&mut self, _: &mut V) {}
315}
316
317impl Visitable for NoPreludeAllowed {
318	fn accept<V: Visit>(&self, _: &mut V) {}
319}
320
321impl VisitableMut for NoBlockAllowed {
322	fn accept_mut<V: VisitMut>(&mut self, _: &mut V) {}
323}
324
325impl Visitable for NoBlockAllowed {
326	fn accept<V: Visit>(&self, _: &mut V) {}
327}
328
329macro_rules! impl_tuple_mut {
330    ($($T:ident),*) => {
331				impl<$($T),*> VisitableMut for ($($T),*)
332        where
333            $($T: VisitableMut,)*
334        {
335            #[allow(non_snake_case)]
336            #[allow(unused)]
337						fn accept_mut<VI: VisitMut>(&mut self, v: &mut VI) {
338                let ($($T),*) = self;
339                $($T.accept_mut(v);)*
340            }
341        }
342    };
343}
344
345impl_tuple_mut!(T, U);
346impl_tuple_mut!(T, U, V);
347impl_tuple_mut!(T, U, V, W);
348
349macro_rules! impl_tuple {
350    ($($T:ident),*) => {
351				impl<$($T),*> Visitable for ($($T),*)
352        where
353            $($T: Visitable,)*
354        {
355            #[allow(non_snake_case)]
356            #[allow(unused)]
357						fn accept<VI: Visit>(&self, v: &mut VI) {
358                let ($($T),*) = self;
359                $($T.accept(v);)*
360            }
361        }
362    };
363}
364impl_tuple!(T, U);
365impl_tuple!(T, U, V);
366impl_tuple!(T, U, V, W);