luau_parser/types/block/
function.rs

1//! Local and global functions.
2
3use luau_lexer::prelude::Token;
4use luau_parser_derive::{Print, Range};
5
6use crate::types::{Block, BracketedList, GenericDeclaration, Pointer, TableAccessKey, TypeValue};
7
8/// A struct representing a local function.
9#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
10#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
11pub struct LocalFunction {
12    /// Attributes before the function.
13    pub attributes: Vec<Attribute>,
14
15    /// The `local` keyword.
16    pub local_keyword: Token,
17
18    /// The `function` keyword.
19    pub function_keyword: Token,
20
21    /// The name of the function.
22    pub function_name: Token,
23
24    /// The generics of the function.
25    pub generics: Option<Pointer<GenericDeclaration>>,
26
27    /// The parameters that this function accepts.
28    pub parameters: BracketedList<Parameter>,
29
30    /// The `:` character between closing parenthesis and returns.
31    pub colon: Option<Pointer<Token>>,
32
33    /// The return type of the function
34    pub return_type: Option<Pointer<TypeValue>>,
35
36    /// The body of the function.
37    pub body: Block,
38
39    /// The `end` keyword.
40    pub end_keyword: Token,
41}
42
43/// Parameter that a function can have. The difference between this and
44/// [`Name`](crate::types::Name) is the fact that [`Parameter.name`](Parameter::name)
45/// can match variadic values (`...`) while `Name` can't.
46#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
47#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
48pub struct Parameter {
49    /// The actual name.
50    pub name: Token,
51
52    /// `:` character.
53    pub colon: Option<Token>,
54
55    /// The type that was with this name, defined with the `: type` syntax.
56    #[range_or = "name"]
57    pub r#type: Option<Pointer<TypeValue>>,
58}
59
60/// An attribute that can be placed before a function.
61#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
62#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
63pub struct Attribute {
64    /// `@` character.
65    pub at: Token,
66
67    /// The actual attribute.
68    pub attribute: Token,
69}
70
71/// An enum representing possible ways in which a global function's name can be.
72#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Print)]
73#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
74pub enum GlobalFunctionName {
75    /// Just a simple name, this is usually in local functions but some people don't do so.
76    SimpleName(Token),
77
78    /// A table.
79    ///
80    /// ```lua
81    /// function foo.bar()
82    /// end
83    /// ```
84    Table {
85        /// The table that's being accessed
86        ///
87        /// ```lua
88        /// local foo = {}
89        ///
90        /// function foo.bar()
91        /// end
92        /// ```
93        ///
94        /// Here, the table is `foo`.
95        table: Token,
96
97        /// Fields accessed from the table.
98        ///
99        /// ```lua
100        /// local foo = {}
101        ///
102        /// function foo.bar.qux:Test()
103        /// end
104        /// ```
105        ///
106        /// Here, the keys are `bar` and `qux`.
107        ///
108        /// # Note
109        ///
110        /// All [`ListItem`]s here will be will be [`NonTrailing`]. And `key.0` will always
111        /// be the dot character.
112        ///
113        /// [`ListItem`]: crate::types::ListItem
114        /// [`NonTrailing`]: crate::types::ListItem::NonTrailing
115        keys: Vec<TableAccessKey>,
116
117        /// The final name of the function, if it exists.
118        ///
119        /// ```lua
120        /// local foo = {}
121        ///
122        /// function foo.bar.qux:Test()
123        /// end
124        /// ```
125        ///
126        /// Here, the method is `Some((Colon, Test))`. While here:
127        ///
128        /// ```lua
129        /// local foo = {}
130        ///
131        /// function foo.bar.qux()
132        /// end
133        /// ```
134        ///
135        /// The method is `None` as there's no `:`.
136        method: Option<Pointer<(Token, Token)>>,
137    },
138}
139
140/// A struct representing a local function.
141#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
142#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
143pub struct GlobalFunction {
144    /// Attributes before the function.
145    pub attributes: Vec<Attribute>,
146
147    /// The `function` keyword.
148    pub function_keyword: Token,
149
150    /// The name of the function.
151    pub function_name: GlobalFunctionName,
152
153    /// The generics of the function.
154    pub generics: Option<Pointer<GenericDeclaration>>,
155
156    /// The parameters that this function accepts.
157    pub parameters: BracketedList<Parameter>,
158
159    /// The `:` character between closing parenthesis and returns.
160    pub colon: Option<Pointer<Token>>,
161
162    /// The return type of the function
163    pub return_type: Option<Pointer<TypeValue>>,
164
165    /// The body of the function.
166    pub body: Block,
167
168    /// The `end` keyword.
169    pub end_keyword: Token,
170}
171
172/// Type functions (LuauSolver V2).
173#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
174#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
175pub struct TypeFunction {
176    /// The `export` keyword.
177    #[range_or = "type_keyword"]
178    pub export_keyword: Option<Token>,
179
180    /// The `type` keyword.
181    pub type_keyword: Token,
182
183    /// The `function` keyword.
184    pub function_keyword: Token,
185
186    /// The name of the function.
187    pub function_name: Token,
188
189    /// The generics of the function.
190    pub generics: Option<Pointer<GenericDeclaration>>,
191
192    /// The parameters that this function accepts.
193    pub parameters: BracketedList<Parameter>,
194
195    /// The `:` character between closing parenthesis and returns.
196    pub colon: Option<Pointer<Token>>,
197
198    /// The return type of the function
199    pub return_type: Option<Pointer<TypeValue>>,
200
201    /// The body of the function.
202    pub body: Block,
203
204    /// The `end` keyword.
205    pub end_keyword: Token,
206}