darklua_core/nodes/expressions/
field.rs

1use crate::nodes::{Identifier, Prefix, Token};
2
3/// Represents a field access expression.
4///
5/// A field access expression accesses a member of a table using dot notation,
6/// such as `table.field`. It consists of a prefix (the table being accessed)
7/// and a field identifier.
8#[derive(Clone, Debug, PartialEq, Eq)]
9pub struct FieldExpression {
10    prefix: Prefix,
11    field: Identifier,
12    token: Option<Token>,
13}
14
15impl FieldExpression {
16    /// Creates a new field access expression.
17    pub fn new<IntoPrefix: Into<Prefix>, IntoIdentifier: Into<Identifier>>(
18        prefix: IntoPrefix,
19        field: IntoIdentifier,
20    ) -> Self {
21        Self {
22            prefix: prefix.into(),
23            field: field.into(),
24            token: None,
25        }
26    }
27
28    /// Attaches a token to this field expression and returns the updated expression.
29    pub fn with_token(mut self, token: Token) -> Self {
30        self.token = Some(token);
31        self
32    }
33
34    /// Attaches a token to this field expression.
35    #[inline]
36    pub fn set_token(&mut self, token: Token) {
37        self.token = Some(token);
38    }
39
40    /// Returns a reference to the token attached to this field expression, if any.
41    #[inline]
42    pub fn get_token(&self) -> Option<&Token> {
43        self.token.as_ref()
44    }
45
46    /// Returns a reference to the prefix of this field expression.
47    #[inline]
48    pub fn get_prefix(&self) -> &Prefix {
49        &self.prefix
50    }
51
52    /// Returns a reference to the field identifier of this field expression.
53    #[inline]
54    pub fn get_field(&self) -> &Identifier {
55        &self.field
56    }
57
58    /// Returns a mutable reference to the prefix of this field expression.
59    pub fn mutate_prefix(&mut self) -> &mut Prefix {
60        &mut self.prefix
61    }
62
63    /// Returns a mutable reference to the first token of this field expression,
64    /// creating it if missing.
65    pub fn mutate_first_token(&mut self) -> &mut Token {
66        self.prefix.mutate_first_token()
67    }
68
69    /// Returns a mutable reference to the last token of this field expression,
70    /// creating it if missing.
71    pub fn mutate_last_token(&mut self) -> &mut Token {
72        self.field.mutate_or_insert_token()
73    }
74
75    super::impl_token_fns!(
76        target = [field]
77        iter = [token]
78    );
79}