luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use super::*;

impl<'source, 'ast, 'name, 'names> Parser<'source, 'ast, 'name, 'names>
where
    'name: 'ast,
{
    pub(in crate::parser) fn parse_expression_node(
        self,
    ) -> std::result::Result<ParseNodeResult<'ast, Expression<'ast>>, ParseErrors> {
        self.parse_node(|parser| parser.parse_expression())
    }

    pub(in crate::parser) fn parse_expression(&mut self) -> Result<Expression<'ast>> {
        self.parse_expression_mut()
    }

    pub(in crate::parser) fn parse_expression_mut(&mut self) -> Result<Expression<'ast>> {
        self.parse_expression_with_limit(0)
    }

    pub(in crate::parser) fn parse_expression_with_limit(
        &mut self,
        limit: u8,
    ) -> Result<Expression<'ast>> {
        let old_recursion_counter = self.enter_recursion("expression")?;
        let result = self.parse_expression_with_limit_inner(limit);
        self.contexts.recursion_counter = old_recursion_counter;
        result
    }

    pub(in crate::parser) fn parse_expression_with_limit_inner(
        &mut self,
        limit: u8,
    ) -> Result<Expression<'ast>> {
        let lhs = match self.current {
            Token::Hash => Some(UnaryOp::Length),
            Token::Reserved(R::Not) => Some(UnaryOp::Not),
            Token::Minus => Some(UnaryOp::Negate),
            Token::Bang => {
                self.report_parse_error(self.located("Unexpected '!'; did you mean 'not'?"));
                Some(UnaryOp::Not)
            }
            _ => None,
        };

        let lhs = if let Some(op) = lhs {
            let op_position = self.current_position();
            self.advance();
            let rhs = self.parse_expression_with_limit(8)?;
            let location = Location::new(op_position, rhs.location().end);
            let cst = if self.cst.enabled() {
                Some(CstNode::ExprOp(CstExprOp { op: op_position }))
            } else {
                None
            };
            let expression = self.arena.alloc_expression_unary_direct(location, op, rhs);
            self.attach_expression_cst(expression, cst)
        } else {
            self.parse_assertion_expression()?
        };

        self.parse_expression_after_lhs(lhs, limit)
    }

    pub(in crate::parser) fn parse_expression_after_lhs(
        &mut self,
        mut lhs: Expression<'ast>,
        limit: u8,
    ) -> Result<Expression<'ast>> {
        loop {
            let Some(priority) = self
                .current
                .binary_priority()
                .or_else(|| self.check_binary_confusables(limit))
            else {
                return Ok(lhs);
            };

            if priority.left <= limit {
                return Ok(lhs);
            }

            let op_position = priority
                .op_position
                .unwrap_or_else(|| self.current_position());
            self.advance();
            let rhs = self.parse_expression_with_limit(priority.right)?;
            let lhs_ref: Expression<'ast> = lhs;
            let location = Location::new(lhs_ref.location().begin, rhs.location().end);
            let cst = if self.cst.enabled() {
                Some(CstNode::ExprOp(CstExprOp { op: op_position }))
            } else {
                None
            };
            let expression =
                self.arena
                    .alloc_expression_binary_direct(location, lhs_ref, priority.op, rhs);
            lhs = self.attach_expression_cst(expression, cst);
            self.enter_recursion("expression")?;
        }
    }

    pub(in crate::parser) fn parse_name_expression(
        &mut self,
        _context: &'static str,
    ) -> Result<Expression<'ast>> {
        let location = self.current_token_location();
        let name = if let Token::Ident(name) = self.current {
            name
        } else {
            let message_index = self.report_parse_error(self.identifier_error(Some(_context)));
            return Ok(self.arena.alloc_expression_error_direct(
                location,
                self.empty_expression_slice(),
                message_index,
            ));
        };
        self.advance();

        if let Some(local) = self.visible_local(name) {
            if self.local_is_type_function_capture(local) {
                let error_location = self.current_location();
                let message_index = self.report_parse_error(self.error_at(
                    error_location,
                    self.name_message(
                        b"Type function cannot reference outer local '",
                        local.name,
                        b"'",
                    ),
                ));
                return Ok(self.arena.alloc_expression_error_direct(
                    error_location,
                    self.empty_expression_slice(),
                    message_index,
                ));
            }

            return Ok(self.arena.alloc_expression_local_direct(
                location,
                local,
                local.function_depth != self.contexts.functions.len(),
            ));
        }

        Ok(self.arena.alloc_expression_global_direct(location, name))
    }

    pub(in crate::parser) fn parse_prefix_expression(&mut self) -> Result<Expression<'ast>> {
        if self.current.kind() == TokenKind::LeftParen {
            let location = self.current_location();
            self.advance();
            let expression = self.parse_expression()?;
            let (end, close_position) = if self.current.kind() == TokenKind::RightParen {
                self.advance();
                (
                    self.previous_token_end_position(),
                    self.previous_token_location().begin,
                )
            } else {
                self.report_parse_error(self.unmatched_group_error(location));
                (self.previous_token_end_position(), Position::missing())
            };
            let cst = if self.cst.enabled() {
                Some(CstNode::ExprGroup(CstExprGroup { close_position }))
            } else {
                None
            };
            let expression = self
                .arena
                .alloc_expression_grouped_direct(Location::new(location.begin, end), expression);
            return Ok(self.attach_expression_cst(expression, cst));
        }

        self.parse_name_expression("expression")
    }

    pub(in crate::parser) fn parse_primary_expression(
        &mut self,
        as_statement: bool,
    ) -> Result<Expression<'ast>> {
        let mut expression_line = self.current_line();
        let expression = self.parse_prefix_expression()?;
        if matches!(expression.kind(), ExpressionKind::Grouped(_)) {
            expression_line = self.previous_line();
        }
        self.parse_primary_expression_after_prefix(expression, expression_line, as_statement)
    }

    pub(in crate::parser) fn parse_simple_expression(&mut self) -> Result<Expression<'ast>> {
        match self.current {
            Token::Number(source) => self.parse_number_expression(source),
            Token::InterpStringSimple(value) => {
                let location = self.current_token_location();
                let cst = if self.cst.enabled() {
                    Some(CstNode::ExprConstantString(CstExprConstantString {
                        source_string: self.current_source_string(),
                        quote_style: CstStringQuoteStyle::QuotedInterp,
                        block_depth: 0,
                    }))
                } else {
                    None
                };
                let expression =
                    self.parse_quoted_string(value, location, StringQuoteStyle::QuotedSimple);
                self.advance();
                Ok(self.attach_expression_cst(expression, cst))
            }
            Token::InterpStringBegin(value) => self.parse_interpolated_string(value),
            Token::Reserved(R::False) => {
                let location = self.current_token_location();
                self.advance();
                Ok(self.arena.alloc_expression_boolean_direct(location, false))
            }
            Token::Reserved(R::Nil) => {
                let location = self.current_token_location();
                self.advance();
                Ok(self.arena.alloc_expression_nil_direct(location))
            }
            Token::Attribute(_) | Token::AttributeOpen => {
                let location = self.current_location();
                let attributes = self.parse_attributes()?;
                let Token::Reserved(R::Function) = self.current else {
                    let message_index =
                        self.report_parse_error(self.attribute_expression_error(location));
                    return Ok(self.arena.alloc_expression_error_direct(
                        location,
                        self.empty_expression_slice(),
                        message_index,
                    ));
                };
                self.parse_function_literal_with_attributes(attributes, location)
            }
            Token::Reserved(R::Function) => self.parse_function_literal(),
            Token::Reserved(R::If) => self.parse_if_expression(),
            Token::Reserved(R::True) => {
                let location = self.current_token_location();
                self.advance();
                Ok(self.arena.alloc_expression_boolean_direct(location, true))
            }
            Token::QuotedString { value, quote_style } => {
                let ast_quote_style = StringQuoteStyle::from(quote_style);
                let location = self.current_token_location();
                let cst = if self.cst.enabled() {
                    Some(CstNode::ExprConstantString(CstExprConstantString {
                        source_string: self.current_source_string(),
                        quote_style: CstStringQuoteStyle::from(quote_style),
                        block_depth: 0,
                    }))
                } else {
                    None
                };
                self.advance();
                let expression = self.parse_quoted_string(value, location, ast_quote_style);
                Ok(self.attach_expression_cst(expression, cst))
            }
            Token::RawString { value, block_depth } => {
                let location = self.current_token_location();
                let cst = if self.cst.enabled() {
                    Some(CstNode::ExprConstantString(CstExprConstantString {
                        source_string: self.current_source_string(),
                        quote_style: CstStringQuoteStyle::QuotedRaw,
                        block_depth: block_depth as u32,
                    }))
                } else {
                    None
                };
                self.advance();
                let expression = self.arena.alloc_expression_string_direct(
                    location,
                    self.ast_string(&multiline_string_bytes(value)),
                    StringQuoteStyle::QuotedRaw,
                );
                Ok(self.attach_expression_cst(expression, cst))
            }
            Token::LeftBrace => {
                let location = self.current_location();
                self.advance();
                self.parse_table_constructor(location)
            }
            Token::Ellipsis => {
                if !self.in_vararg_function() {
                    let error = self.varargs_error();
                    let location = error.location;
                    let message_index = self.report_parse_error(error);
                    self.advance();
                    return Ok(self.alloc_expression(ExpressionInit::new(
                        location,
                        ExpressionKind::Error {
                            expressions: self.empty_expression_slice(),
                            message_index,
                        },
                    )));
                }
                let location = self.current_token_location();
                self.advance();
                Ok(self.alloc_expression(ExpressionInit::new(location, ExpressionKind::Varargs)))
            }
            Token::BrokenString => {
                let error = self.malformed_string_error();
                let location = error.location;
                let message_index = self.report_parse_error(error);
                self.advance();
                Ok(self.alloc_expression(ExpressionInit::new(
                    location,
                    ExpressionKind::Error {
                        expressions: self.empty_expression_slice(),
                        message_index,
                    },
                )))
            }
            Token::BrokenInterpDoubleBrace(_) => {
                let error = self.interpolated_double_brace_error();
                let location = error.location;
                let message_index = self.report_parse_error(error);
                self.advance();
                Ok(self.alloc_expression(ExpressionInit::new(
                    location,
                    ExpressionKind::Error {
                        expressions: self.empty_expression_slice(),
                        message_index,
                    },
                )))
            }
            _ => self.parse_primary_expression(false),
        }
    }

    pub(in crate::parser) fn parse_assertion_expression(&mut self) -> Result<Expression<'ast>> {
        let expression = self.parse_simple_expression()?;

        if self.current.kind() != TokenKind::DoubleColon {
            return Ok(expression);
        }

        let op = self.current_position();
        self.advance();
        let annotation = self.parse_type_annotation()?;
        let begin = expression.location().begin;
        Ok(self.alloc_expression_with_cst(
            ExpressionInit::new(
                Location::new(begin, annotation.location.end),
                ExpressionKind::TypeAssertion {
                    expr: expression,
                    annotation,
                },
            ),
            if self.cst.enabled() {
                Some(CstNode::ExprTypeAssertion(CstExprTypeAssertion { op }))
            } else {
                None
            },
        ))
    }

    pub(in crate::parser) fn parse_if_expression(&mut self) -> Result<Expression<'ast>> {
        let begin = self.current_position();
        if matches!(self.current, Token::Reserved(R::If | R::Elseif)) {
            self.advance();
        } else {
            self.report_parse_error(self.unexpected("if"));
        }

        let condition = self.parse_expression()?;
        let has_then = self.expect_and_consume_keyword(R::Then, "if then else expression");
        let then_position = if has_then {
            self.previous_token_location().begin
        } else {
            Position::missing()
        };
        let then_expression = self.parse_expression()?;
        let else_position = self.current_position();
        let is_else_if = matches!(self.current, Token::Reserved(R::Elseif));
        let (else_expression, has_else) = if is_else_if {
            let old_recursion_counter = self.enter_recursion("expression")?;
            let expression = self.parse_if_expression()?;
            self.contexts.recursion_counter = old_recursion_counter;
            (expression, true)
        } else {
            let has_else = self.expect_and_consume_keyword(R::Else, "if then else expression");
            (self.parse_expression()?, has_else)
        };

        let result = self.alloc_expression_with_cst(
            ExpressionInit::new(
                Location::new(begin, else_expression.location().end),
                ExpressionKind::If {
                    condition,
                    has_then,
                    then_expression,
                    has_else,
                    else_expression,
                },
            ),
            if self.cst.enabled() {
                Some(CstNode::ExprIfElse(CstExprIfElse {
                    then_position,
                    else_position,
                    is_else_if,
                }))
            } else {
                None
            },
        );
        Ok(result)
    }
}

mod call;
mod strings;
mod table;