bluejay_parser/ast/definition/
arguments_definition.rs

1use crate::ast::definition::{Context, InputValueDefinition};
2use crate::ast::{FromTokens, IsMatch, ParseError, Tokens};
3use crate::lexical_token::PunctuatorType;
4use crate::Span;
5use bluejay_core::definition::ArgumentsDefinition as CoreArgumentsDefinition;
6use bluejay_core::AsIter;
7
8#[derive(Debug)]
9pub struct ArgumentsDefinition<'a, C: Context> {
10    argument_definitions: Vec<InputValueDefinition<'a, C>>,
11    _span: Span,
12}
13
14impl<'a, C: Context> AsIter for ArgumentsDefinition<'a, C> {
15    type Item = InputValueDefinition<'a, C>;
16    type Iterator<'b> = std::slice::Iter<'b, Self::Item> where 'a: 'b;
17
18    fn iter(&self) -> Self::Iterator<'_> {
19        self.argument_definitions.iter()
20    }
21}
22
23impl<'a, C: Context> CoreArgumentsDefinition for ArgumentsDefinition<'a, C> {
24    type ArgumentDefinition = InputValueDefinition<'a, C>;
25}
26
27impl<'a, C: Context> FromTokens<'a> for ArgumentsDefinition<'a, C> {
28    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
29        let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
30        let mut argument_definitions: Vec<InputValueDefinition<C>> = Vec::new();
31        let close_span = loop {
32            argument_definitions.push(InputValueDefinition::from_tokens(tokens)?);
33            if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseRoundBracket) {
34                break close_span;
35            }
36        };
37        let span = open_span.merge(&close_span);
38        Ok(Self {
39            argument_definitions,
40            _span: span,
41        })
42    }
43}
44
45impl<'a, C: Context> IsMatch<'a> for ArgumentsDefinition<'a, C> {
46    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
47        tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
48    }
49}