Skip to main content

css_parse/syntax/
no_block_allowed.rs

1use super::prelude::*;
2
3/// A struct to provide to rules to disallow blocks.
4///
5/// Sometimes a rule will not allow a block - for example `@charset`, `@import`. In those case, assigning this struct
6/// to the `Block` can be useful to ensure that the [QualifiedRule][crate::syntax::QualifiedRule] appropriately errors
7/// if it enters the Block parsing context. This captures the `;` token that may optionally end a "statement-style"
8/// at-rule.
9///
10/// The phantom data allows this type to be compatible with different declaration value and metadata types,
11/// even though it doesn't actually use them (since no block is allowed).
12#[node]
13#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
15pub struct NoBlockAllowed<D = (), M = ()> {
16	semicolon: Option<crate::token_macros::Semicolon>,
17	_phantom: std::marker::PhantomData<(D, M)>,
18}
19
20impl<'a, D, M> Parse<'a> for NoBlockAllowed<D, M> {
21	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
22	where
23		Iter: Iterator<Item = crate::Cursor> + Clone,
24	{
25		if p.at_end() {
26			Ok(Self { semicolon: None, _phantom: std::marker::PhantomData })
27		} else if let Some(semicolon) = p.parse_if_peek::<T![;]>()? {
28			Ok(Self { semicolon: Some(semicolon), _phantom: std::marker::PhantomData })
29		} else {
30			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
31		}
32	}
33}
34
35impl<'a, D, M> Peek<'a> for NoBlockAllowed<D, M> {
36	const PEEK_KINDSET: KindSet = KindSet::NONE;
37}
38
39impl<D, M> ToCursors for NoBlockAllowed<D, M> {
40	fn to_cursors(&self, s: &mut impl CursorSink) {
41		if let Some(semicolon) = self.semicolon {
42			s.append(semicolon.into());
43		}
44	}
45}
46
47impl<D, M> ToSpan for NoBlockAllowed<D, M> {
48	fn to_span(&self) -> Span {
49		self.semicolon.to_span()
50	}
51}
52
53impl<D, M> SemanticEq for NoBlockAllowed<D, M> {
54	fn semantic_eq(&self, other: &Self, source_text: &str) -> bool {
55		self.semicolon.semantic_eq(&other.semicolon, source_text)
56	}
57}
58
59impl<D, M: crate::NodeMetadata> crate::NodeWithMetadata<M> for NoBlockAllowed<D, M> {
60	fn metadata(&self) -> M {
61		M::default()
62	}
63}
64
65impl<'a, D, M> crate::RuleVariants<'a> for NoBlockAllowed<D, M>
66where
67	D: crate::DeclarationValue<'a, M>,
68	M: crate::NodeMetadata,
69{
70	type DeclarationValue = D;
71	type Metadata = M;
72}