1use super::prelude::*;
2use crate::{AssociatedWhitespaceRules, FunctionBlock, Result, SimpleBlock};
3
4#[node]
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(untagged))]
11pub enum ComponentValue<'a> {
12 SimpleBlock(SimpleBlock<'a>),
13 Function(FunctionBlock<'a>),
14 Whitespace(T![Whitespace]),
15 Number(T![Number]),
16 Dimension(T![Dimension]),
17 Ident(T![Ident]),
18 AtKeyword(T![AtKeyword]),
19 Hash(T![Hash]),
20 String(T![String]),
21 Url(T![Url]),
22 Delim(T![Delim]),
23 Colon(T![:]),
24 Semicolon(T![;]),
25 Comma(T![,]),
26}
27
28impl<'a, M: NodeMetadata> NodeWithMetadata<M> for ComponentValue<'a> {
29 fn metadata(&self) -> M {
30 M::default()
31 }
32}
33
34impl<'a> Peek<'a> for ComponentValue<'a> {
35 const PEEK_KINDSET: KindSet = KindSet::new(&[
36 Kind::Whitespace,
37 Kind::Number,
38 Kind::Dimension,
39 Kind::Ident,
40 Kind::AtKeyword,
41 Kind::Hash,
42 Kind::String,
43 Kind::Url,
44 Kind::Delim,
45 Kind::Colon,
46 Kind::Semicolon,
47 Kind::Comma,
48 Kind::Function,
49 Kind::LeftCurly,
50 Kind::LeftParen,
51 Kind::LeftSquare,
52 ]);
53 #[inline(always)]
54 fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
55 where
56 Iter: Iterator<Item = Cursor> + Clone,
57 {
58 c == Self::PEEK_KINDSET || <T![' ']>::peek(p, c)
59 }
60}
61
62impl<'a> Parse<'a> for ComponentValue<'a> {
64 fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
65 where
66 Iter: Iterator<Item = Cursor> + Clone,
67 {
68 let c = p.peek_n(1);
69 Ok(if <T![' ']>::peek(p, c) {
70 Self::Whitespace(p.parse::<T![' ']>()?)
71 } else if <T![PairWiseStart]>::peek(p, c) {
72 let old_state = p.set_state(State::Nested);
73 let block = p.parse::<SimpleBlock>();
74 p.set_state(old_state);
75 Self::SimpleBlock(block?)
76 } else if <T![Function]>::peek(p, c) {
77 Self::Function(p.parse::<FunctionBlock>()?)
78 } else if <T![Number]>::peek(p, c) {
79 Self::Number(p.parse::<T![Number]>()?)
80 } else if <T![Dimension]>::peek(p, c) {
81 Self::Dimension(p.parse::<T![Dimension]>()?)
82 } else if <T![Ident]>::peek(p, c) {
83 Self::Ident(p.parse::<T![Ident]>()?)
84 } else if <T![AtKeyword]>::peek(p, c) {
85 Self::AtKeyword(p.parse::<T![AtKeyword]>()?)
86 } else if <T![Hash]>::peek(p, c) {
87 Self::Hash(p.parse::<T![Hash]>()?)
88 } else if <T![String]>::peek(p, c) {
89 Self::String(p.parse::<T![String]>()?)
90 } else if <T![Url]>::peek(p, c) {
91 Self::Url(p.parse::<T![Url]>()?)
92 } else if <T![Delim]>::peek(p, c) {
93 p.parse::<T![Delim]>().map(|delim| {
94 let mut rules = AssociatedWhitespaceRules::none();
96 if p.peek_n_with_skip(1, KindSet::COMMENTS) == Kind::Whitespace {
97 rules |= AssociatedWhitespaceRules::EnforceAfter;
98 } else {
99 rules |= AssociatedWhitespaceRules::BanAfter;
100 }
101 Self::Delim(delim.with_associated_whitespace(rules))
102 })?
103 } else if <T![:]>::peek(p, c) {
104 Self::Colon(p.parse::<T![:]>()?)
105 } else if <T![;]>::peek(p, c) {
106 Self::Semicolon(p.parse::<T![;]>()?)
107 } else if <T![,]>::peek(p, c) {
108 Self::Comma(p.parse::<T![,]>()?)
109 } else {
110 Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
111 })
112 }
113}
114
115impl<'a> ToCursors for ComponentValue<'a> {
116 fn to_cursors(&self, s: &mut impl CursorSink) {
117 match self {
118 Self::SimpleBlock(t) => ToCursors::to_cursors(t, s),
119 Self::Function(t) => ToCursors::to_cursors(t, s),
120 Self::Ident(t) => ToCursors::to_cursors(t, s),
121 Self::AtKeyword(t) => ToCursors::to_cursors(t, s),
122 Self::Hash(t) => ToCursors::to_cursors(t, s),
123 Self::String(t) => ToCursors::to_cursors(t, s),
124 Self::Url(t) => ToCursors::to_cursors(t, s),
125 Self::Delim(t) => ToCursors::to_cursors(t, s),
126 Self::Number(t) => ToCursors::to_cursors(t, s),
127 Self::Dimension(t) => ToCursors::to_cursors(t, s),
128 Self::Whitespace(t) => ToCursors::to_cursors(t, s),
129 Self::Colon(t) => ToCursors::to_cursors(t, s),
130 Self::Semicolon(t) => ToCursors::to_cursors(t, s),
131 Self::Comma(t) => ToCursors::to_cursors(t, s),
132 }
133 }
134}
135
136impl<'a> ToSpan for ComponentValue<'a> {
137 fn to_span(&self) -> Span {
138 match self {
139 Self::SimpleBlock(t) => t.to_span(),
140 Self::Function(t) => t.to_span(),
141 Self::Ident(t) => t.to_span(),
142 Self::AtKeyword(t) => t.to_span(),
143 Self::Hash(t) => t.to_span(),
144 Self::String(t) => t.to_span(),
145 Self::Url(t) => t.to_span(),
146 Self::Delim(t) => t.to_span(),
147 Self::Number(t) => t.to_span(),
148 Self::Dimension(t) => t.to_span(),
149 Self::Whitespace(t) => t.to_span(),
150 Self::Colon(t) => t.to_span(),
151 Self::Semicolon(t) => t.to_span(),
152 Self::Comma(t) => t.to_span(),
153 }
154 }
155}
156
157impl<'a> SemanticEq for ComponentValue<'a> {
158 fn semantic_eq(&self, other: &Self) -> bool {
159 match (self, other) {
160 (Self::SimpleBlock(a), Self::SimpleBlock(b)) => a.semantic_eq(b),
161 (Self::Function(a), Self::Function(b)) => a.semantic_eq(b),
162 (Self::Number(a), Self::Number(b)) => a.semantic_eq(b),
163 (Self::Dimension(a), Self::Dimension(b)) => a.semantic_eq(b),
164 (Self::Ident(a), Self::Ident(b)) => a.semantic_eq(b),
165 (Self::AtKeyword(a), Self::AtKeyword(b)) => a.semantic_eq(b),
166 (Self::Hash(a), Self::Hash(b)) => a.semantic_eq(b),
167 (Self::String(a), Self::String(b)) => a.semantic_eq(b),
168 (Self::Url(a), Self::Url(b)) => a.semantic_eq(b),
169 (Self::Delim(a), Self::Delim(b)) => a.semantic_eq(b),
170 (Self::Colon(a), Self::Colon(b)) => a.semantic_eq(b),
171 (Self::Semicolon(a), Self::Semicolon(b)) => a.semantic_eq(b),
172 (Self::Comma(a), Self::Comma(b)) => a.semantic_eq(b),
173 (Self::Whitespace(_), Self::Whitespace(_)) => true,
175 _ => false, }
177 }
178}
179
180#[cfg(test)]
181mod tests {
182 use super::*;
183 use crate::{EmptyAtomSet, test_helpers::*};
184
185 #[test]
186 fn test_writes() {
187 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, "foo");
188 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, " ");
189 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, "{block}");
190 }
191}