gdsyntax 0.1.3

GDScript syntax trees and syntax errors, shared by the gdsuite tools
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! A port of Godot 4.7's GDScript parser (`GDScriptParser` in
//! `modules/gdscript/gdscript_parser.cpp`) that builds a lossless tree.
//!
//! Functions keep Godot's names (`parse_class_body`, `parse_statement`,
//! `parse_precedence`, ...) and follow its control flow, so a change in Godot
//! maps to one change here. What differs:
//!
//! - **The output is a concrete syntax tree** ([`rowan`]) holding every token,
//!   trivia included, instead of Godot's abstract syntax tree.
//! - **Only syntax is checked.** Godot's parser also tracks scopes and reports
//!   names declared twice, for example. Those checks, and every warning, are
//!   left to Godot itself (gdtypecheck). `tests/godot/semantic-errors.txt`
//!   lists the parser errors this leaves out.
//! - **Nesting is bounded**, so deeply nested input reports an error instead
//!   of overflowing the stack.

mod class;
mod expr;
mod stmt;

use rowan::{Checkpoint, GreenNode, GreenNodeBuilder};

use crate::SyntaxKind::{self, *};
use crate::lexer::{Lexer, Token};

/// An error found while parsing, with Godot's message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub message: String,
    /// Byte offsets of the error in the source.
    pub start: usize,
    pub end: usize,
}

/// A parsed file: its lossless tree and the errors found, in the order Godot
/// reports them.
#[derive(Debug, Clone)]
pub struct Parse {
    pub green: GreenNode,
    pub errors: Vec<ParseError>,
}

/// Parse GDScript source. Parsing never fails: code that is not valid
/// GDScript ends up in the tree all the same, with errors.
pub fn parse(source: &str) -> Parse {
    let mut parser = Parser::new(source);
    parser.parse();
    parser.finish()
}

/// Godot's `AnnotationInfo::TargetKind`.
pub(crate) mod target {
    pub const SCRIPT: u32 = 1 << 0;
    pub const CLASS: u32 = 1 << 1;
    pub const VARIABLE: u32 = 1 << 2;
    pub const CONSTANT: u32 = 1 << 3;
    pub const SIGNAL: u32 = 1 << 4;
    pub const FUNCTION: u32 = 1 << 5;
    pub const STATEMENT: u32 = 1 << 6;
    pub const STANDALONE: u32 = 1 << 7;
    pub const CLASS_LEVEL: u32 = CLASS | VARIABLE | CONSTANT | SIGNAL | FUNCTION;
}

/// The annotations Godot 4.7 registers, with what each may apply to.
fn annotation_targets(name: &str) -> Option<u32> {
    use target::*;
    Some(match name {
        "@tool" | "@icon" | "@static_unload" => SCRIPT,
        "@abstract" => SCRIPT | CLASS | FUNCTION,
        "@onready"
        | "@export"
        | "@export_enum"
        | "@export_file"
        | "@export_file_path"
        | "@export_dir"
        | "@export_global_file"
        | "@export_global_dir"
        | "@export_multiline"
        | "@export_placeholder"
        | "@export_range"
        | "@export_exp_easing"
        | "@export_color_no_alpha"
        | "@export_node_path"
        | "@export_flags"
        | "@export_flags_2d_render"
        | "@export_flags_2d_physics"
        | "@export_flags_2d_navigation"
        | "@export_flags_3d_render"
        | "@export_flags_3d_physics"
        | "@export_flags_3d_navigation"
        | "@export_flags_avoidance"
        | "@export_storage"
        | "@export_custom"
        | "@export_tool_button" => VARIABLE,
        "@export_category" | "@export_group" | "@export_subgroup" => STANDALONE,
        "@warning_ignore" => CLASS_LEVEL | STATEMENT,
        "@warning_ignore_start" | "@warning_ignore_restore" => STANDALONE,
        "@rpc" => FUNCTION,
        _ => return None,
    })
}

/// An annotation read but not yet attached to what follows it.
#[derive(Clone)]
pub(crate) struct PendingAnnotation {
    name: String,
    targets: u32,
    checkpoint: Checkpoint,
    /// The node the annotation was read in; it can only attach to a
    /// declaration in the same node.
    parent: u32,
    start: usize,
    end: usize,
}

/// Nesting deeper than this is reported instead of followed.
const MAX_DEPTH: u32 = 256;

pub(crate) struct Parser<'s> {
    src: &'s str,
    lexer: Lexer<'s>,
    /// Tokens read but not yet in the tree: trivia, the current token at
    /// `current_index`, and trivia read past it.
    buf: Vec<Token>,
    current_index: usize,
    /// Byte offset of `buf[0]`.
    buf_start: usize,
    current: SyntaxKind,
    current_start: usize,
    current_end: usize,
    previous: SyntaxKind,
    previous_start: usize,
    previous_end: usize,
    builder: GreenNodeBuilder<'static>,
    errors: Vec<ParseError>,
    lexer_errors_seen: usize,
    /// Tokenizer errors Godot reports at the next token.
    deferred_lexer_errors: Vec<ParseError>,
    multiline_stack: Vec<bool>,
    panic_mode: bool,
    lambda_ended: bool,
    in_lambda: bool,
    can_break: bool,
    can_continue: bool,
    /// Godot's `current_function`: whether we are inside a function, and
    /// whether it is static.
    function: Option<FunctionContext>,
    annotation_stack: Vec<PendingAnnotation>,
    /// Where a `static` before a declaration was read, and in which node.
    static_checkpoint: Option<(Checkpoint, u32)>,
    /// Godot's `current_class->extends_used`.
    class_extends_used: bool,
    /// Ids of the open nodes, innermost last.
    open_nodes: Vec<u32>,
    next_node_id: u32,
    depth: u32,
    too_deep_reported: bool,
}

#[derive(Clone, Copy)]
pub(crate) struct FunctionContext {
    is_static: bool,
    /// `_init` or `_static_init`, which cannot return a value.
    is_constructor: bool,
}

impl<'s> Parser<'s> {
    fn new(src: &'s str) -> Self {
        Parser {
            src,
            lexer: Lexer::new(src),
            buf: Vec::new(),
            current_index: 0,
            buf_start: 0,
            current: EOF,
            current_start: 0,
            current_end: 0,
            previous: EOF,
            previous_start: 0,
            previous_end: 0,
            builder: GreenNodeBuilder::new(),
            errors: Vec::new(),
            lexer_errors_seen: 0,
            deferred_lexer_errors: Vec::new(),
            multiline_stack: Vec::new(),
            panic_mode: false,
            lambda_ended: false,
            in_lambda: false,
            can_break: false,
            can_continue: false,
            function: None,
            annotation_stack: Vec::new(),
            static_checkpoint: None,
            class_extends_used: false,
            open_nodes: Vec::new(),
            next_node_id: 0,
            depth: 0,
            too_deep_reported: false,
        }
    }

    /// Godot's `parse`.
    fn parse(&mut self) {
        self.open_node();
        self.builder.start_node(SOURCE_FILE.into());
        self.scan();
        // Leading line breaks are not statements.
        while self.current == NEWLINE {
            self.demote_current();
        }
        self.push_multiline(false); // Keep one for the whole parsing.
        self.parse_program();
        self.pop_multiline();
        // Whatever is left (trivia, and tokens after an error) stays in the tree.
        while self.current != EOF {
            self.advance();
        }
        self.flush_before_current();
        self.builder.finish_node();
    }

    fn finish(mut self) -> Parse {
        self.collect_lexer_errors();
        self.errors.append(&mut self.deferred_lexer_errors);
        let builder = std::mem::replace(&mut self.builder, GreenNodeBuilder::new());
        Parse {
            green: builder.finish(),
            errors: self.errors,
        }
    }

    // ---- Tokens ----

    /// `tokenizer->scan()`: read the next significant token into `current`,
    /// skipping error tokens as Godot's `advance` does.
    fn scan(&mut self) {
        for error in std::mem::take(&mut self.deferred_lexer_errors) {
            self.panic_mode = true;
            self.errors.push(error);
        }
        loop {
            let kind = self.lexer.scan(&mut self.buf);
            self.collect_lexer_errors();
            if kind == ERROR_TOKEN {
                continue;
            }
            self.current = kind;
            self.current_index = self.lexer.significant_index().min(self.buf.len());
            let before: usize = self.buf[..self.current_index]
                .iter()
                .map(|t| t.len as usize)
                .sum();
            self.current_start = self.buf_start + before;
            self.current_end = self.current_start
                + self
                    .buf
                    .get(self.current_index)
                    .map_or(0, |t| t.len as usize);
            return;
        }
    }

    fn collect_lexer_errors(&mut self) {
        let new = &self.lexer.errors()[self.lexer_errors_seen..];
        self.lexer_errors_seen += new.len();
        for error in new {
            let error_out = ParseError {
                message: error.message.clone(),
                start: error.start,
                end: error.end,
            };
            if error.deferred {
                self.deferred_lexer_errors.push(error_out);
            } else {
                self.panic_mode = true;
                self.errors.push(error_out);
            }
        }
    }

    /// Put the tokens before the current one into the tree.
    fn flush_before_current(&mut self) {
        let n = if self.current == EOF {
            self.buf.len()
        } else {
            self.current_index
        };
        self.flush(n);
    }

    fn flush(&mut self, n: usize) {
        for token in self.buf.drain(..n) {
            let end = self.buf_start + token.len as usize;
            self.builder
                .token(token.kind.into(), &self.src[self.buf_start..end]);
            self.buf_start = end;
        }
        self.current_index = self.current_index.saturating_sub(n);
    }

    /// Godot's `advance`: the current token goes into the tree and the next
    /// one is read.
    fn advance(&mut self) -> SyntaxKind {
        self.lambda_ended = false;
        if self.current == EOF {
            return EOF;
        }
        // The current token goes in; trivia read past it waits for the next.
        let n = (self.current_index + 1).min(self.buf.len());
        self.flush(n);
        self.previous = self.current;
        self.previous_start = self.current_start;
        self.previous_end = self.current_end;
        self.scan();
        self.previous
    }

    /// Replace a `NEWLINE`, `INDENT` or `DEDENT` the parser decides to skip
    /// (Godot's `current = tokenizer->scan()`): a line break becomes trivia
    /// and a zero-width token disappears.
    fn demote_current(&mut self) {
        if self.current == EOF {
            return;
        }
        let index = self.current_index;
        if let Some(token) = self.buf.get_mut(index) {
            if token.len == 0 {
                self.buf.remove(index);
            } else {
                token.kind = LINE_BREAK;
            }
        }
        self.scan();
    }

    /// Godot's `check`. `IDENTIFIER` also matches the keywords Godot accepts
    /// as names.
    fn check(&self, kind: SyntaxKind) -> bool {
        match kind {
            IDENTIFIER => is_identifier(self.current),
            _ => self.current == kind,
        }
    }

    fn check_literal(&self) -> bool {
        self.current.is_literal()
    }

    /// Godot's `match`.
    fn eat(&mut self, kind: SyntaxKind) -> bool {
        if !self.check(kind) {
            return false;
        }
        self.advance();
        true
    }

    /// Godot's `consume`.
    fn consume(&mut self, kind: SyntaxKind, message: &str) -> bool {
        if self.eat(kind) {
            return true;
        }
        self.push_error(message);
        false
    }

    fn is_at_end(&self) -> bool {
        self.current == EOF
    }

    /// The source text of the current token.
    fn current_text(&self) -> &'s str {
        &self.src[self.current_start..self.current_end]
    }

    fn previous_text(&self) -> &'s str {
        &self.src[self.previous_start..self.previous_end]
    }

    // ---- Errors ----

    /// Godot's `push_error`: at the previous token.
    fn push_error(&mut self, message: impl Into<String>) {
        let (start, end) = (self.previous_start, self.previous_end);
        self.push_error_at(message, start, end);
    }

    fn push_error_at(&mut self, message: impl Into<String>, start: usize, end: usize) {
        self.panic_mode = true;
        self.errors.push(ParseError {
            message: message.into(),
            start,
            end,
        });
    }

    /// Godot's `synchronize`: after an error, skip to the next likely start
    /// of a statement or declaration.
    fn synchronize(&mut self) {
        self.panic_mode = false;
        // The tokens skipped go in one ERROR node, so a tool reading the tree
        // can tell unreadable code from code that is not there.
        let mut skipping = false;
        while !self.is_at_end() {
            if matches!(self.previous, NEWLINE | SEMICOLON) {
                break;
            }
            if matches!(
                self.current,
                CLASS_KW
                    | FUNC_KW
                    | STATIC_KW
                    | VAR_KW
                    | CONST_KW
                    | SIGNAL_KW
                    | FOR_KW
                    | WHILE_KW
                    | MATCH_KW
                    | RETURN_KW
                    | ANNOTATION
            ) {
                break;
            }
            if !skipping {
                self.start(ERROR);
                skipping = true;
            }
            self.advance();
        }
        if skipping {
            self.finish_node();
        }
    }

    // ---- Modes ----

    /// Godot's `push_multiline`.
    fn push_multiline(&mut self, state: bool) {
        self.multiline_stack.push(state);
        self.lexer.set_multiline_mode(state);
        if state {
            // Skip whitespace tokens already waiting.
            while matches!(self.current, NEWLINE | INDENT | DEDENT) {
                self.demote_current();
            }
        }
    }

    /// Godot's `pop_multiline`.
    fn pop_multiline(&mut self) {
        self.multiline_stack.pop();
        let state = self.multiline_stack.last().copied().unwrap_or(false);
        self.lexer.set_multiline_mode(state);
    }

    fn is_statement_end_token(&self) -> bool {
        matches!(self.current, NEWLINE | SEMICOLON | EOF)
    }

    fn is_statement_end(&self) -> bool {
        self.lambda_ended || self.in_lambda || self.is_statement_end_token()
    }

    /// Godot's `end_statement`.
    fn end_statement(&mut self, context: &str) {
        let mut found = false;
        while self.is_statement_end() && !self.is_at_end() {
            if self.is_statement_end_token() {
                self.advance();
            } else if self.lambda_ended {
                self.lambda_ended = false;
                found = true;
                break;
            } else {
                if !found {
                    self.lambda_ended = true;
                    found = true;
                }
                break;
            }
            found = true;
        }
        if !found && !self.is_at_end() {
            let name = token_name(self.current);
            let (start, end) = (self.previous_start, self.previous_end);
            self.push_error_at(
                format!("Expected end of statement after {context}, found \"{name}\" instead."),
                start,
                end,
            );
        }
    }

    // ---- Tree ----

    fn open_node(&mut self) {
        self.next_node_id += 1;
        self.open_nodes.push(self.next_node_id);
    }

    /// The id of the innermost open node.
    fn node_id(&self) -> u32 {
        self.open_nodes.last().copied().unwrap_or(0)
    }

    fn start(&mut self, kind: SyntaxKind) {
        self.flush_before_current();
        self.open_node();
        self.builder.start_node(kind.into());
    }

    fn checkpoint(&mut self) -> Checkpoint {
        self.flush_before_current();
        self.builder.checkpoint()
    }

    fn start_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind) {
        self.flush_before_current();
        self.open_node();
        self.builder.start_node_at(checkpoint, kind.into());
    }

    fn finish_node(&mut self) {
        self.open_nodes.pop();
        self.builder.finish_node();
    }

    /// Enter one level of nesting. Returns false, after reporting it once,
    /// when the input is nested too deeply to follow.
    fn enter(&mut self) -> bool {
        if self.depth >= MAX_DEPTH {
            self.report_too_deep();
            return false;
        }
        self.depth += 1;
        true
    }

    fn report_too_deep(&mut self) {
        if !self.too_deep_reported {
            self.too_deep_reported = true;
            let (start, end) = (self.current_start, self.current_end);
            self.push_error_at("The code is nested too deeply to parse.", start, end);
        }
    }

    fn leave(&mut self) {
        self.depth -= 1;
    }
}

/// Godot's `Token::is_identifier`: names, and the keywords Godot still
/// accepts as names.
pub(crate) fn is_identifier(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        IDENTIFIER | MATCH_KW | WHEN_KW | CONST_PI | CONST_INF | CONST_NAN | CONST_TAU
    )
}

/// Godot's `Token::is_node_name`: what may follow `$` or `.`.
pub(crate) fn is_node_name(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        IDENTIFIER
            | AND_KW
            | AS_KW
            | ASSERT_KW
            | AWAIT_KW
            | BREAK_KW
            | BREAKPOINT_KW
            | CLASS_NAME_KW
            | CLASS_KW
            | CONST_KW
            | CONST_PI
            | CONST_INF
            | CONST_NAN
            | CONST_TAU
            | CONTINUE_KW
            | ELIF_KW
            | ELSE_KW
            | ENUM_KW
            | EXTENDS_KW
            | FOR_KW
            | FUNC_KW
            | IF_KW
            | IN_KW
            | IS_KW
            | MATCH_KW
            | NAMESPACE_KW
            | NOT_KW
            | OR_KW
            | PASS_KW
            | PRELOAD_KW
            | RETURN_KW
            | SELF_KW
            | SIGNAL_KW
            | STATIC_KW
            | SUPER_KW
            | TRAIT_KW
            | UNDERSCORE
            | VAR_KW
            | VOID_KW
            | WHILE_KW
            | WHEN_KW
            | YIELD_KW
    )
}

/// Godot's `Token::get_name`, used in error messages.
pub(crate) fn token_name(kind: SyntaxKind) -> &'static str {
    match kind {
        ANNOTATION => "Annotation",
        IDENTIFIER => "Identifier",
        INT | FLOAT | STRING | STRING_NAME | NODE_PATH | TRUE_KW | FALSE_KW | NULL_KW => "Literal",
        LESS => "<",
        LESS_EQUAL => "<=",
        GREATER => ">",
        GREATER_EQUAL => ">=",
        EQUAL_EQUAL => "==",
        BANG_EQUAL => "!=",
        AND_KW => "and",
        OR_KW => "or",
        NOT_KW => "not",
        AMPERSAND_AMPERSAND => "&&",
        PIPE_PIPE => "||",
        BANG => "!",
        AMPERSAND => "&",
        PIPE => "|",
        TILDE => "~",
        CARET => "^",
        LESS_LESS => "<<",
        GREATER_GREATER => ">>",
        PLUS => "+",
        MINUS => "-",
        STAR => "*",
        STAR_STAR => "**",
        SLASH => "/",
        PERCENT => "%",
        EQUAL => "=",
        PLUS_EQUAL => "+=",
        MINUS_EQUAL => "-=",
        STAR_EQUAL => "*=",
        STAR_STAR_EQUAL => "**=",
        SLASH_EQUAL => "/=",
        PERCENT_EQUAL => "%=",
        LESS_LESS_EQUAL => "<<=",
        GREATER_GREATER_EQUAL => ">>=",
        AMPERSAND_EQUAL => "&=",
        PIPE_EQUAL => "|=",
        CARET_EQUAL => "^=",
        IF_KW => "if",
        ELIF_KW => "elif",
        ELSE_KW => "else",
        FOR_KW => "for",
        WHILE_KW => "while",
        BREAK_KW => "break",
        CONTINUE_KW => "continue",
        PASS_KW => "pass",
        RETURN_KW => "return",
        MATCH_KW => "match",
        WHEN_KW => "when",
        AS_KW => "as",
        ASSERT_KW => "assert",
        AWAIT_KW => "await",
        BREAKPOINT_KW => "breakpoint",
        CLASS_KW => "class",
        CLASS_NAME_KW => "class_name",
        CONST_KW => "const",
        ENUM_KW => "enum",
        EXTENDS_KW => "extends",
        FUNC_KW => "func",
        IN_KW => "in",
        IS_KW => "is",
        NAMESPACE_KW => "namespace",
        PRELOAD_KW => "preload",
        SELF_KW => "self",
        SIGNAL_KW => "signal",
        STATIC_KW => "static",
        SUPER_KW => "super",
        TRAIT_KW => "trait",
        VAR_KW => "var",
        VOID_KW => "void",
        YIELD_KW => "yield",
        BRACKET_OPEN => "[",
        BRACKET_CLOSE => "]",
        BRACE_OPEN => "{",
        BRACE_CLOSE => "}",
        PARENTHESIS_OPEN => "(",
        PARENTHESIS_CLOSE => ")",
        COMMA => ",",
        SEMICOLON => ";",
        PERIOD => ".",
        PERIOD_PERIOD => "..",
        PERIOD_PERIOD_PERIOD => "...",
        COLON => ":",
        DOLLAR => "$",
        FORWARD_ARROW => "->",
        UNDERSCORE => "_",
        NEWLINE => "Newline",
        INDENT => "Indent",
        DEDENT => "Dedent",
        CONST_PI => "PI",
        CONST_TAU => "TAU",
        CONST_INF => "INF",
        CONST_NAN => "NaN",
        VCS_CONFLICT_MARKER => "VCS conflict marker",
        BACKTICK => "`",
        QUESTION_MARK => "?",
        ERROR_TOKEN => "Error",
        EOF => "End of file",
        _ => "Empty",
    }
}

#[cfg(test)]
mod tests;