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 (
GDScriptTokenizerTextinmodules/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 (
GDScriptParserinmodules/gdscript/gdscript_parser.cpp) that builds a lossless tree.
Structs§
- Position
- A place in the source. Both fields are 1-based;
columncounts characters. - Syntax
Error - An error in the source, with Godot’s message.
- Syntax
Tree - 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_FILEnode 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::errorssays 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’sINVALID_UTF8_MESSAGE, at the first invalid byte.
Type Aliases§
- Syntax
Element - A node or a token.
- Syntax
Node - A node of a lossless GDScript tree.
- Syntax
Token - A token of a lossless GDScript tree.