darklua_core/nodes/expressions/
index.rs

1use crate::nodes::{Expression, Prefix, Token};
2
3/// Contains token information for an index expression.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct IndexExpressionTokens {
6    /// The opening bracket token
7    pub opening_bracket: Token,
8    /// The closing bracket token
9    pub closing_bracket: Token,
10}
11
12impl IndexExpressionTokens {
13    super::impl_token_fns!(target = [opening_bracket, closing_bracket]);
14}
15
16/// Represents a table index access expression.
17///
18/// An index expression accesses a value in a table using square bracket notation,
19/// such as `table[key]`. It consists of a prefix (the table being accessed)
20/// and an index expression that evaluates to the key.
21#[derive(Clone, Debug, PartialEq, Eq)]
22pub struct IndexExpression {
23    prefix: Prefix,
24    index: Expression,
25    tokens: Option<IndexExpressionTokens>,
26}
27
28impl IndexExpression {
29    /// Creates a new index expression with the given prefix and index expression.
30    pub fn new<P: Into<Prefix>, E: Into<Expression>>(prefix: P, expression: E) -> Self {
31        Self {
32            prefix: prefix.into(),
33            index: expression.into(),
34            tokens: None,
35        }
36    }
37
38    /// Attaches tokens to this index expression.
39    pub fn with_tokens(mut self, tokens: IndexExpressionTokens) -> Self {
40        self.tokens = Some(tokens);
41        self
42    }
43
44    /// Attaches tokens to this index expression.
45    #[inline]
46    pub fn set_tokens(&mut self, tokens: IndexExpressionTokens) {
47        self.tokens = Some(tokens);
48    }
49
50    /// Returns a reference to the tokens attached to this index expression, if any.
51    #[inline]
52    pub fn get_tokens(&self) -> Option<&IndexExpressionTokens> {
53        self.tokens.as_ref()
54    }
55
56    /// Returns a reference to the prefix of this index expression.
57    #[inline]
58    pub fn get_prefix(&self) -> &Prefix {
59        &self.prefix
60    }
61
62    /// Returns a reference to the index expression of this index expression.
63    #[inline]
64    pub fn get_index(&self) -> &Expression {
65        &self.index
66    }
67
68    /// Returns a mutable reference to the prefix of this index expression.
69    #[inline]
70    pub fn mutate_prefix(&mut self) -> &mut Prefix {
71        &mut self.prefix
72    }
73
74    /// Returns a mutable reference to the index expression of this index expression.
75    #[inline]
76    pub fn mutate_index(&mut self) -> &mut Expression {
77        &mut self.index
78    }
79
80    /// Returns a mutable reference to the first token of this index expression,
81    /// creating it if missing.
82    pub fn mutate_first_token(&mut self) -> &mut crate::nodes::Token {
83        self.prefix.mutate_first_token()
84    }
85
86    /// Returns a mutable reference to the last token of this index expression,
87    /// creating it if missing.
88    pub fn mutate_last_token(&mut self) -> &mut Token {
89        if self.tokens.is_none() {
90            self.tokens = Some(IndexExpressionTokens {
91                opening_bracket: Token::from_content("["),
92                closing_bracket: Token::from_content("]"),
93            });
94        }
95        &mut self.tokens.as_mut().unwrap().closing_bracket
96    }
97
98    super::impl_token_fns!(iter = [tokens]);
99}