luau_parser/types/expression/
mod.rs

1//! Types representing all valid Luau expressions.
2
3use luau_lexer::prelude::Token;
4use luau_parser_derive::{Print, Range};
5
6use crate::types::{Bracketed, Pointer, Table, TypeValue};
7
8reexport!(table, var, function);
9
10/// A struct representing an expression wrapped in parenthesis.
11pub type ExpressionWrap = Bracketed<Pointer<Expression>>;
12
13/// Part of expressions that are usually at the start of actual expressions.
14///
15/// ```lua
16/// local _ = foo
17/// local _ = foo()
18/// local _ = (foo)
19/// ```
20#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
21#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
22pub enum PrefixExp {
23    /// A normal variable reference.
24    ///
25    /// ```lua
26    /// local _ = foo
27    /// ```
28    Var(Var),
29
30    /// A function call.
31    /// ```lua
32    /// local _ = foo()
33    /// ```
34    FunctionCall(FunctionCall),
35
36    /// An expression wrapped in parenthesis
37    ///
38    /// ```lua
39    /// local _ = (foo)
40    /// ```
41    ExpressionWrap(ExpressionWrap),
42}
43
44/// An enum representing all possible values for an expression.
45#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
46#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
47pub enum Expression {
48    /// This [`Expression`] had a syntax error.
49    #[default]
50    ERROR,
51
52    /// The `nil` value.
53    Nil(Token),
54
55    /// A `true` or `false` value.
56    Boolean(Token),
57
58    /// Any number, be it a float, an unsigned integer, an integer or a hex digit.
59    Number(Token),
60
61    /// A string, be it double quotes, single quotes, interpolated string, or multi-line.
62    String(Token),
63
64    /// An **anonymous** function.
65    ///
66    /// ```lua
67    /// local foo = function(arg1: number): boolean
68    /// end
69    /// ```
70    Closure(Pointer<Closure>),
71
72    /// A function call.
73    ///
74    /// ```lua
75    /// local foo = bar()
76    /// ```
77    FunctionCall(FunctionCall),
78
79    /// An expression wrapped in parenthesis.
80    ///
81    /// ```lua
82    /// local _ = (foo)
83    /// ```
84    ExpressionWrap(ExpressionWrap),
85
86    /// A reference to another variable.
87    ///
88    /// ```lua
89    /// local _ = foo
90    /// ```
91    Var(Var),
92
93    /// A Table.
94    ///
95    /// ```lua
96    /// local _ = { foo = "bar" }
97    /// ```
98    Table(Table),
99
100    /// A unary expression, these are expressions that have an operator before the actual
101    /// expression:
102    ///
103    /// ```lua
104    /// local foo = not 1
105    /// local bar = -1
106    /// local qux = #t
107    /// ```
108    UnaryExpression {
109        /// The operator.
110        operator: Token,
111
112        /// The actual expression this operator is affecting.
113        expression: Pointer<Expression>,
114    },
115
116    /// A binary expression, this represents any 2 expressions with an operator between
117    /// them.
118    ///
119    /// ```lua
120    /// local foo = 1 + 1
121    /// local bar = 1 == 1
122    /// local qux = bar // 2
123    /// ```
124    BinaryExpression {
125        /// The left expression.
126        left: Pointer<Expression>,
127
128        /// The operator between the expressions.
129        operator: Token,
130
131        /// The right expression.
132        right: Pointer<Expression>,
133    },
134
135    /// A typecast.
136    ///
137    /// ```lua
138    /// local foo = "hi" :: string
139    /// local bar = foo :: number
140    /// local qux = {} :: { number }
141    /// ```
142    TypeCast {
143        /// The actual expression.
144        expression: Pointer<Expression>,
145
146        /// The `::` operator.
147        operator: Token,
148
149        /// The type that's being casted to.
150        cast_to: Pointer<TypeValue>,
151    },
152
153    /// An if expression.
154    IfExpression(IfExpression),
155}
156
157/// A struct representing an elseif **expression**, only exists in variable declarations.
158#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
159#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
160pub struct IfExpression {
161    /// The `if` keyword.
162    pub if_keyword: Token,
163
164    /// The condition after the `if` keyword.
165    pub condition: Pointer<Expression>,
166
167    /// The `then` keyword after the condition.
168    pub then_keyword: Token,
169
170    /// The [`expression`](Expression) that this statement would resolve to if the
171    /// [`condition`](Expression::IfExpression::condition) evaluated to `true`.
172    pub if_expression: Pointer<Expression>,
173
174    /// All `elseif` expressions.
175    pub else_if_expressions: Pointer<Vec<ElseIfExpression>>,
176
177    /// The final part of the expression, the `else` keyword.
178    pub else_keyword: Token,
179
180    /// The final value if all other conditions were not met.
181    pub else_expression: Pointer<Expression>,
182}
183
184/// A struct representing an elseif **expression**, only exists in expressions.
185#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
186#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
187pub struct ElseIfExpression {
188    /// The `elseif` keyword.
189    pub else_if_keyword: Token,
190
191    /// The condition after the `elseif`.
192    pub condition: Pointer<Expression>,
193
194    /// The `then` keyword after the condition.
195    pub then_keyword: Token,
196
197    /// The [`expression`](Expression) that this statement would resolve to if the
198    /// [`condition`](ElseIfExpression::condition) evaluated to `true`.
199    pub expression: Pointer<Expression>,
200}