luau_parser/types/expression/
table.rs

1use luau_lexer::prelude::Token;
2use luau_parser_derive::{Print, Range};
3
4use crate::types::{ExpressionWrap, FunctionCall, Pointer, TableKey};
5
6/// An enum representing different ways in which a table value can be returned from.
7#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
8#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
9pub enum TableAccessPrefix {
10    /// Just a simple access.
11    ///
12    /// ```lua
13    /// local _ = t.name
14    /// ```
15    Name(Token),
16
17    /// A function call
18    ///
19    /// ```lua
20    /// local t = fn()
21    /// ```
22    FunctionCall(Pointer<FunctionCall>),
23
24    /// Accessing a table from `(...)`.
25    ///
26    /// ```lua
27    /// local _ = ({ a = "Hello, World!" })
28    /// local _ = (t)
29    /// ```
30    ExpressionWrap(Pointer<ExpressionWrap>),
31}
32
33/// Represents an access to a table index.
34#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
35#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
36pub struct TableAccess {
37    /// The actual table being indexed
38    pub prefix: TableAccessPrefix,
39
40    /// All keys accessed by the expression.
41    ///
42    /// ```lua
43    /// local _ = t.a.b.c
44    /// ```
45    ///
46    /// Will be `a`, `b`, `c` in this case.
47    pub accessed_keys: Vec<TableAccessKey>,
48}
49
50/// Enum representing different ways in which a table's index can be accessed.
51#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
52#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
53pub enum TableAccessKey {
54    /// An expression, this'll only have the enum [`TableKey::Expression`].
55    Expression(Pointer<TableKey>),
56
57    /// A simple name.
58    Name {
59        /// The `.` **before** the key.
60        dot: Pointer<Token>,
61
62        /// The actual key being accessed.
63        name: Pointer<Token>,
64    },
65}