bluejay_parser/ast/
directives.rs1use crate::{
2 ast::{DepthLimiter, Directive, FromTokens, IsMatch, ParseError, Tokens, TryFromTokens},
3 HasSpan, Span,
4};
5use bluejay_core::AsIter;
6
7#[derive(Debug)]
8pub struct Directives<'a, const CONST: bool> {
9 directives: Vec<Directive<'a, CONST>>,
10 span: Option<Span>,
11}
12
13pub type ConstDirectives<'a> = Directives<'a, true>;
14pub type VariableDirectives<'a> = Directives<'a, false>;
15
16impl<'a, const CONST: bool> FromTokens<'a> for Directives<'a, CONST> {
17 #[inline]
18 fn from_tokens(
19 tokens: &mut impl Tokens<'a>,
20 depth_limiter: DepthLimiter,
21 ) -> Result<Self, ParseError> {
22 let mut directives: Vec<Directive<'a, CONST>> = Vec::new();
23 while let Some(directive) = Directive::try_from_tokens(tokens, depth_limiter.bump()?) {
24 directives.push(directive?);
25 }
26 let span = match directives.as_slice() {
27 [] => None,
28 [first] => Some(first.span().clone()),
29 [first, .., last] => Some(first.span().merge(last.span())),
30 };
31 Ok(Self { directives, span })
32 }
33}
34
35impl<'a, const CONST: bool> IsMatch<'a> for Directives<'a, CONST> {
36 #[inline]
37 fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
38 Directive::<'a, CONST>::is_match(tokens)
39 }
40}
41
42impl<'a, const CONST: bool> bluejay_core::Directives<CONST> for Directives<'a, CONST> {
43 type Directive = Directive<'a, CONST>;
44}
45
46impl<'a, const CONST: bool> AsIter for Directives<'a, CONST> {
47 type Item = Directive<'a, CONST>;
48 type Iterator<'b>
49 = std::slice::Iter<'b, Self::Item>
50 where
51 'a: 'b;
52
53 fn iter(&self) -> Self::Iterator<'_> {
54 self.directives.iter()
55 }
56}
57
58impl<const CONST: bool> Directives<'_, CONST> {
59 pub(crate) fn span(&self) -> Option<&Span> {
60 self.span.as_ref()
61 }
62}
63
64impl<'a, const CONST: bool> IntoIterator for Directives<'a, CONST> {
65 type Item = Directive<'a, CONST>;
66 type IntoIter = std::vec::IntoIter<Self::Item>;
67
68 fn into_iter(self) -> Self::IntoIter {
69 self.directives.into_iter()
70 }
71}