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    /// The `local` keyword.
13    pub local_keyword: Token,
14
15    /// The `function` keyword.
16    pub function_keyword: Token,
17
18    /// The name of the function.
19    pub function_name: Token,
20
21    /// The generics of the function.
22    pub generics: Option<Pointer<GenericDeclaration>>,
23
24    /// The parameters that this function accepts.
25    pub parameters: BracketedList<Parameter>,
26
27    /// The `:` character between closing parenthesis and returns.
28    pub colon: Option<Pointer<Token>>,
29
30    /// The return type of the function
31    pub return_type: Option<Pointer<TypeValue>>,
32
33    /// The body of the function.
34    pub body: Block,
35
36    /// The `end` keyword.
37    pub end_keyword: Token,
38}
39
40/// Parameter that a function can have. The difference between this and
41/// [`Name`](crate::types::Name) is the fact that [`Parameter.name`](Parameter::name)
42/// can match variadic values (`...`) while `Name` can't.
43#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
44#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
45pub struct Parameter {
46    /// The actual name.
47    pub name: Token,
48
49    /// `:` character.
50    pub colon: Option<Token>,
51
52    /// The type that was with this name, defined with the `: type` syntax.
53    #[range_or = "name"]
54    pub r#type: Option<Pointer<TypeValue>>,
55}
56
57/// An enum representing possible ways in which a global function's name can be.
58#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Print)]
59#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
60pub enum GlobalFunctionName {
61    /// Just a simple name, this is usually in local functions but some people don't do so.
62    SimpleName(Token),
63
64    /// A table.
65    ///
66    /// ```lua
67    /// function foo.bar()
68    /// end
69    /// ```
70    Table {
71        /// The table that's being accessed
72        ///
73        /// ```lua
74        /// local foo = {}
75        ///
76        /// function foo.bar()
77        /// end
78        /// ```
79        ///
80        /// Here, the table is `foo`.
81        table: Token,
82
83        /// Fields accessed from the table.
84        ///
85        /// ```lua
86        /// local foo = {}
87        ///
88        /// function foo.bar.qux:Test()
89        /// end
90        /// ```
91        ///
92        /// Here, the keys are `bar` and `qux`.
93        ///
94        /// # Note
95        ///
96        /// All [`ListItem`]s here will be will be [`NonTrailing`]. And `key.0` will always
97        /// be the dot character.
98        ///
99        /// [`ListItem`]: crate::types::ListItem
100        /// [`NonTrailing`]: crate::types::ListItem::NonTrailing
101        keys: Vec<TableAccessKey>,
102
103        /// The final name of the function, if it exists.
104        ///
105        /// ```lua
106        /// local foo = {}
107        ///
108        /// function foo.bar.qux:Test()
109        /// end
110        /// ```
111        ///
112        /// Here, the method is `Some((Colon, Test))`. While here:
113        ///
114        /// ```lua
115        /// local foo = {}
116        ///
117        /// function foo.bar.qux()
118        /// end
119        /// ```
120        ///
121        /// The method is `None` as there's no `:`.
122        method: Option<Pointer<(Token, Token)>>,
123    },
124}
125
126/// A struct representing a local function.
127#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
128#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
129pub struct GlobalFunction {
130    /// The `function` keyword.
131    pub function_keyword: Token,
132
133    /// The name of the function.
134    pub function_name: GlobalFunctionName,
135
136    /// The generics of the function.
137    pub generics: Option<Pointer<GenericDeclaration>>,
138
139    /// The parameters that this function accepts.
140    pub parameters: BracketedList<Parameter>,
141
142    /// The `:` character between closing parenthesis and returns.
143    pub colon: Option<Pointer<Token>>,
144
145    /// The return type of the function
146    pub return_type: Option<Pointer<TypeValue>>,
147
148    /// The body of the function.
149    pub body: Block,
150
151    /// The `end` keyword.
152    pub end_keyword: Token,
153}