luau_parser/types/expression/
function.rs

1use luau_lexer::prelude::Token;
2use luau_parser_derive::{Print, Range};
3
4use crate::types::{
5    Block, BracketedList, Expression, GenericDeclaration, Parameter, Pointer, PrefixExp, Table,
6    TypeValue,
7};
8
9/// Different ways a function can be called.
10#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
11#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
12pub enum FunctionCallInvoked {
13    /// A standalone function call or one in a table.
14    /// ```lua
15    /// local _ = foo()
16    /// local _ = t.bar()
17    /// ```
18    Function(Pointer<PrefixExp>),
19
20    /// A **method** in a function, a method is a function that's called with `:` instead
21    /// of `.`.
22    ///
23    /// ```lua
24    /// local _ = t:foo()
25    /// ```
26    TableMethod {
27        /// The table this function is from.
28        table: Pointer<PrefixExp>,
29
30        /// The colon between the table and the method name.
31        colon: Pointer<Token>,
32
33        /// The actual name of the method being called.
34        method: Pointer<Token>,
35    },
36}
37
38/// A struct representing a function call.
39///
40/// ```lua
41/// local _ = foo(1, 2, 3)
42/// ```
43#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
44#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
45pub struct FunctionCall {
46    /// The function being called.
47    pub invoked: FunctionCallInvoked,
48
49    /// The arguments passed to the function.
50    pub arguments: FunctionArguments,
51}
52
53/// All possible arguments that can be passed to a function.
54#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
55#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
56pub enum FunctionArguments {
57    /// A standalone string.
58    ///
59    /// ```lua
60    /// local _ = foo"Hello, World!"
61    /// ```
62    String(Token),
63
64    /// A standalone table.
65    ///
66    /// ```lua
67    /// local _ = foo { bar = "Hello, World!" }
68    /// ```
69    Table(Table),
70
71    /// A list of arguments.
72    ///
73    /// ```lua
74    /// local _ = foo(1, 2, 3)
75    /// ```
76    List(BracketedList<Pointer<FunctionArgument>>),
77}
78
79/// Arguments that can be passed in a [`FunctionCall`].
80#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
81#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
82pub enum FunctionArgument {
83    /// A normal [`expression`](Expression).
84    Expression(Expression),
85
86    /// A variadic value
87    ///
88    /// ```lua
89    /// fn(...)
90    /// ```
91    VariadicValues(Token),
92}
93
94/// All possible arguments that can be passed to a function.
95#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
96#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
97pub struct Closure {
98    /// The `function` keyword at the start
99    pub function_keyword: Token,
100
101    /// The generics of this function.
102    pub generics: Option<Pointer<GenericDeclaration>>,
103
104    /// All [`parameters`](Name) of the function.
105    pub parameters: BracketedList<Parameter>,
106
107    /// The `:` character between closing parenthesis and returns.
108    pub colon: Option<Pointer<Token>>,
109
110    /// The return type of the function
111    pub return_type: Option<Pointer<TypeValue>>,
112
113    /// The body of the function.
114    pub body: Block,
115
116    /// The `end` keyword.
117    pub end_keyword: Token,
118}