bluejay_parser/ast/
arguments.rs

1use crate::ast::{Argument, DepthLimiter, FromTokens, IsMatch, ParseError, Tokens};
2use crate::lexical_token::PunctuatorType;
3use crate::{HasSpan, Span};
4use bluejay_core::AsIter;
5
6#[derive(Debug)]
7pub struct Arguments<'a, const CONST: bool> {
8    arguments: Vec<Argument<'a, CONST>>,
9    span: Span,
10}
11
12pub type VariableArguments<'a> = Arguments<'a, false>;
13
14impl<'a, const CONST: bool> FromTokens<'a> for Arguments<'a, CONST> {
15    #[inline]
16    fn from_tokens(
17        tokens: &mut impl Tokens<'a>,
18        depth_limiter: DepthLimiter,
19    ) -> Result<Self, ParseError> {
20        let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
21        let mut arguments: Vec<Argument<CONST>> = Vec::new();
22        let close_span = loop {
23            arguments.push(Argument::from_tokens(tokens, depth_limiter.bump()?)?);
24            if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseRoundBracket) {
25                break close_span;
26            }
27        };
28        let span = open_span.merge(&close_span);
29        Ok(Self { arguments, span })
30    }
31}
32
33impl<'a, const CONST: bool> IsMatch<'a> for Arguments<'a, CONST> {
34    #[inline]
35    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
36        tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
37    }
38}
39
40impl<const CONST: bool> HasSpan for Arguments<'_, CONST> {
41    fn span(&self) -> &Span {
42        &self.span
43    }
44}
45
46impl<'a, const CONST: bool> bluejay_core::Arguments<CONST> for Arguments<'a, CONST> {
47    type Argument = Argument<'a, CONST>;
48}
49
50impl<'a, const CONST: bool> AsIter for Arguments<'a, CONST> {
51    type Item = Argument<'a, CONST>;
52    type Iterator<'b>
53        = std::slice::Iter<'b, Self::Item>
54    where
55        'a: 'b;
56
57    fn iter(&self) -> Self::Iterator<'_> {
58        self.arguments.iter()
59    }
60}