css_parse/syntax/
no_block_allowed.rs

1use crate::{Cursor, CursorSink, Diagnostic, Parse, Parser, Peek, Result, SemanticEq, Span, T, ToCursors, ToSpan};
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#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
11pub struct NoBlockAllowed(Option<T![;]>);
12
13impl<'a> Parse<'a> for NoBlockAllowed {
14	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
15	where
16		Iter: Iterator<Item = crate::Cursor> + Clone,
17	{
18		if p.at_end() {
19			Ok(Self(None))
20		} else if let Some(semicolon) = p.parse_if_peek::<T![;]>()? {
21			Ok(Self(Some(semicolon)))
22		} else {
23			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
24		}
25	}
26}
27
28impl<'a> Peek<'a> for NoBlockAllowed {
29	fn peek<Iter>(_: &Parser<'a, Iter>, _: Cursor) -> bool
30	where
31		Iter: Iterator<Item = crate::Cursor> + Clone,
32	{
33		false
34	}
35}
36
37impl ToCursors for NoBlockAllowed {
38	fn to_cursors(&self, s: &mut impl CursorSink) {
39		if let Some(semicolon) = self.0 {
40			s.append(semicolon.into());
41		}
42	}
43}
44
45impl ToSpan for NoBlockAllowed {
46	fn to_span(&self) -> Span {
47		self.0.to_span()
48	}
49}
50
51impl SemanticEq for NoBlockAllowed {
52	fn semantic_eq(&self, other: &Self) -> bool {
53		self.0.semantic_eq(&other.0)
54	}
55}
56
57impl<M: crate::NodeMetadata> crate::NodeWithMetadata<M> for NoBlockAllowed {
58	fn metadata(&self) -> M {
59		M::default()
60	}
61}