Skip to main content

Crate gdsyntax

Crate gdsyntax 

Source
Expand description

GDScript syntax trees and syntax errors, shared by the gdsuite tools.

The parser is a translation of Godot 4.7’s own tokenizer and parser (lexer, parser), so it accepts and rejects what Godot does, with Godot’s error messages. Its tree is lossless: every byte of the file, comments and whitespace included, is in it, so tree.text() is always the source. ast gives typed access to declarations, statements and expressions.

use gdsyntax::ast::Member;

let tree = gdsyntax::parse("extends Node\n\n\nfunc _ready() -> void:\n\tprint(\"hi\")\n");
assert!(tree.errors().is_empty());
for member in tree.file().members() {
    if let Member::Func(func) = member {
        assert_eq!(func.name().unwrap().text(), "_ready");
    }
}

Re-exports§

pub use kind::GDScript;
pub use kind::SyntaxKind;
pub use rowan;

Modules§

ast
Typed access to the tree: one wrapper per kind of declaration, statement and expression, with accessors for their parts.
kind
Every kind of token and node in a GDScript syntax tree.
lexer
A port of Godot 4.7’s GDScript tokenizer (GDScriptTokenizerText in modules/gdscript/gdscript_tokenizer.cpp) that keeps every byte.
literal
The values of literal tokens, decoded the way Godot’s tokenizer does.
parser
A port of Godot 4.7’s GDScript parser (GDScriptParser in modules/gdscript/gdscript_parser.cpp) that builds a lossless tree.

Structs§

Position
A place in the source. Both fields are 1-based; column counts characters.
SyntaxError
An error in the source, with Godot’s message.
SyntaxTree
A parsed GDScript file: its source, its tree and its errors.

Constants§

INVALID_UTF8_MESSAGE
The message Godot gives for a script that is not valid UTF-8.

Functions§

lossless_tree
The source as a flat tree of tokens, without parsing: one SOURCE_FILE node holding every token, with the tokenizer’s errors.
parse
Parse GDScript source. Parsing never fails: code that is not valid GDScript still ends up in the tree, and SyntaxTree::errors says what is wrong, in the order Godot reports it.
parse_bytes
Parse a file’s bytes. Valid UTF-8 parses as with parse. Godot does not load a script that is not valid UTF-8; here each invalid sequence is replaced with U+FFFD so the rest can still be parsed, and the first error is Godot’s INVALID_UTF8_MESSAGE, at the first invalid byte.

Type Aliases§

SyntaxElement
A node or a token.
SyntaxNode
A node of a lossless GDScript tree.
SyntaxToken
A token of a lossless GDScript tree.