1use crate::{
2 CursorSink, Declaration, DeclarationValue, Kind, KindSet, NodeMetadata, NodeWithMetadata, Parse, Parser, Peek,
3 Result, SemanticEq, Span, T, ToCursors, ToSpan, Vec, token_macros,
4};
5use csskit_proc_macro::node;
6
7#[node]
25#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27#[cfg_attr(feature = "serde", serde(bound(serialize = "V: serde::Serialize")))]
28pub struct DeclarationList<'a, V, M>
29where
30 V: DeclarationValue<'a, M>,
31 M: NodeMetadata,
32{
33 pub open_curly: token_macros::LeftCurly,
34 pub declarations: Vec<'a, Declaration<'a, V, M>>,
35 pub close_curly: Option<token_macros::RightCurly>,
36 #[cfg_attr(feature = "serde", serde(skip))]
37 meta: M,
38}
39
40impl<'a, V, M> NodeWithMetadata<M> for DeclarationList<'a, V, M>
41where
42 V: DeclarationValue<'a, M>,
43 M: NodeMetadata,
44{
45 fn metadata(&self) -> M {
46 self.meta
47 }
48}
49
50impl<'a, V, M> Peek<'a> for DeclarationList<'a, V, M>
51where
52 V: DeclarationValue<'a, M>,
53 M: NodeMetadata,
54{
55 const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::LeftCurly]);
56}
57
58impl<'a, V, M> Parse<'a> for DeclarationList<'a, V, M>
59where
60 V: DeclarationValue<'a, M>,
61 M: NodeMetadata,
62{
63 fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
64 where
65 Iter: Iterator<Item = crate::Cursor> + Clone,
66 {
67 let open_curly = p.parse::<T!['{']>()?;
68 let mut declarations = Vec::new_in(p.alloc());
69 let mut meta: M = Default::default();
70 loop {
71 if p.at_end() {
72 meta = meta.with_size(declarations.len().min(u16::MAX as usize) as u16);
73 return Ok(Self { open_curly, declarations, close_curly: None, meta });
74 }
75 let close_curly = p.parse_if_peek::<T!['}']>()?;
76 if close_curly.is_some() {
77 meta = meta.with_size(declarations.len().min(u16::MAX as usize) as u16);
78 return Ok(Self { open_curly, declarations, close_curly, meta });
79 }
80 let declaration = p.parse::<Declaration<'a, V, M>>()?;
81 meta = meta.merge(declaration.metadata());
82 declarations.push(declaration);
83 }
84 }
85}
86
87impl<'a, V, M> ToCursors for DeclarationList<'a, V, M>
88where
89 V: DeclarationValue<'a, M> + ToCursors,
90 M: NodeMetadata,
91{
92 fn to_cursors(&self, s: &mut impl CursorSink) {
93 ToCursors::to_cursors(&self.open_curly, s);
94 ToCursors::to_cursors(&self.declarations, s);
95 ToCursors::to_cursors(&self.close_curly, s);
96 }
97}
98
99impl<'a, V, M> ToSpan for DeclarationList<'a, V, M>
100where
101 V: DeclarationValue<'a, M> + ToSpan,
102 M: NodeMetadata,
103{
104 fn to_span(&self) -> Span {
105 self.open_curly.to_span()
106 + if let Some(close) = self.close_curly { close.to_span() } else { self.declarations.to_span() }
107 }
108}
109
110impl<'a, V, M> SemanticEq for DeclarationList<'a, V, M>
111where
112 V: DeclarationValue<'a, M>,
113 M: NodeMetadata,
114{
115 fn semantic_eq(&self, other: &Self) -> bool {
116 self.open_curly.semantic_eq(&other.open_curly)
117 && self.declarations.semantic_eq(&other.declarations)
118 && self.close_curly.semantic_eq(&other.close_curly)
119 }
120}