luau_parser/types/expression/
function.rs

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