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