bluejay_parser/ast/
argument.rs

1use crate::ast::{DepthLimiter, FromTokens, ParseError, Tokens, Value};
2use crate::lexical_token::{Name, PunctuatorType};
3use crate::{HasSpan, Span};
4
5#[derive(Debug)]
6pub struct Argument<'a, const CONST: bool> {
7    name: Name<'a>,
8    value: Value<'a, CONST>,
9    span: Span,
10}
11
12impl<'a, const CONST: bool> Argument<'a, CONST> {
13    pub fn name(&self) -> &Name<'a> {
14        &self.name
15    }
16
17    pub fn value(&self) -> &Value<'a, CONST> {
18        &self.value
19    }
20}
21
22pub type ConstArgument<'a> = Argument<'a, true>;
23pub type VariableArgument<'a> = Argument<'a, false>;
24
25impl<'a, const CONST: bool> FromTokens<'a> for Argument<'a, CONST> {
26    #[inline]
27    fn from_tokens(
28        tokens: &mut impl Tokens<'a>,
29        depth_limiter: DepthLimiter,
30    ) -> Result<Self, ParseError> {
31        let name = tokens.expect_name()?;
32        tokens.expect_punctuator(PunctuatorType::Colon)?;
33        let value = Value::from_tokens(tokens, depth_limiter.bump()?)?;
34        let span = name.span().merge(value.span());
35        Ok(Self { name, value, span })
36    }
37}
38
39impl<'a, const CONST: bool> bluejay_core::Argument<CONST> for Argument<'a, CONST> {
40    type Value = Value<'a, CONST>;
41
42    fn name(&self) -> &str {
43        self.name.as_ref()
44    }
45
46    fn value(&self) -> &Value<'a, CONST> {
47        &self.value
48    }
49}
50
51impl<const CONST: bool> HasSpan for Argument<'_, CONST> {
52    fn span(&self) -> &Span {
53        &self.span
54    }
55}