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