darklua_core/nodes/expressions/
parenthese.rs

1use crate::nodes::{Expression, Token};
2
3/// Contains token information for a parenthesized expression.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct ParentheseTokens {
6    /// The left (opening) parenthesis token
7    pub left_parenthese: Token,
8    /// The right (closing) parenthesis token
9    pub right_parenthese: Token,
10}
11
12impl ParentheseTokens {
13    super::impl_token_fns!(target = [left_parenthese, right_parenthese]);
14}
15
16/// Represents a parenthesized expression.
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct ParentheseExpression {
19    expression: Expression,
20    tokens: Option<ParentheseTokens>,
21}
22
23impl ParentheseExpression {
24    /// Creates a new parenthesized expression containing the given expression.
25    pub fn new<E: Into<Expression>>(expression: E) -> Self {
26        Self {
27            expression: expression.into(),
28            tokens: None,
29        }
30    }
31
32    /// Returns a reference to the inner expression contained in the parentheses.
33    #[inline]
34    pub fn inner_expression(&self) -> &Expression {
35        &self.expression
36    }
37
38    /// Consumes this parenthesized expression and returns the inner expression.
39    #[inline]
40    pub fn into_inner_expression(self) -> Expression {
41        self.expression
42    }
43
44    /// Returns a mutable reference to the inner expression contained in the parentheses.
45    #[inline]
46    pub fn mutate_inner_expression(&mut self) -> &mut Expression {
47        &mut self.expression
48    }
49
50    /// Attaches tokens to this parenthesized expression.
51    pub fn with_tokens(mut self, tokens: ParentheseTokens) -> Self {
52        self.tokens = Some(tokens);
53        self
54    }
55
56    /// Attaches tokens to this parenthesized expression.
57    #[inline]
58    pub fn set_tokens(&mut self, tokens: ParentheseTokens) {
59        self.tokens = Some(tokens);
60    }
61
62    /// Returns a reference to the tokens attached to this parenthesized expression, if any.
63    #[inline]
64    pub fn get_tokens(&self) -> Option<&ParentheseTokens> {
65        self.tokens.as_ref()
66    }
67
68    /// Returns a mutable reference to the tokens attached to this parenthesized expression, if any.
69    #[inline]
70    pub fn mutate_tokens(&mut self) -> Option<&mut ParentheseTokens> {
71        self.tokens.as_mut()
72    }
73
74    /// Returns a mutable reference to the first token of this parenthesized expression.
75    ///
76    /// Ensures the left parenthesis token exists and returns it.
77    pub fn mutate_first_token(&mut self) -> &mut Token {
78        self.set_default_tokens();
79        &mut self.mutate_tokens().unwrap().left_parenthese
80    }
81
82    /// Returns a mutable reference to the last token of this parenthesized expression,
83    /// creating it if missing.
84    pub fn mutate_last_token(&mut self) -> &mut Token {
85        self.set_default_tokens();
86        &mut self.mutate_tokens().unwrap().right_parenthese
87    }
88
89    fn set_default_tokens(&mut self) {
90        if self.tokens.is_none() {
91            self.tokens = Some(ParentheseTokens {
92                left_parenthese: Token::from_content("("),
93                right_parenthese: Token::from_content(")"),
94            });
95        }
96    }
97
98    super::impl_token_fns!(iter = [tokens]);
99}