gdsyntax
A GDScript parser for the gdsuite tools, translated from Godot 4.7's own tokenizer and parser. It accepts and rejects what Godot does, with Godot's error messages, and its tree is lossless: every byte of the file, comments and whitespace included, is in the tree.
use Member;
let tree = parse;
assert!;
assert_eq!;
for member in tree.file.members
parsereturns the tree and the errors, in the order Godot reports them. Lines and columns are 1-based, and columns count characters, not bytes.positionandoffsetconvert between byte offsets and positions, andlines,line_textandline_rangegive each line without its line break.astgives typed access to the whole tree: declarations, statements, expressions, match patterns and annotations, with typed operators. Every accessor returns anOption, because a file with errors still has a tree; code the parser could not read is kept inErrornodes.literaldecodes literal values the way Godot does: string escapes, raw strings,&"name",^"path", and hexadecimal, binary and1_000numbers.- The tree is a
rowantree;gdsyntax::rowanre-exports the version it uses. parse_bytesreads a file's bytes: a script that is not valid UTF-8 gets Godot's error, and the rest is still parsed.
What it checks
Syntax only. Godot's parser also reports some errors that need scope information, such as a name declared twice. Those are left to Godot itself; tests/godot/semantic-errors.txt lists them. Godot's check for identifiers that look like keywords is not ported either. Nesting deeper than 256 levels is reported as an error, so deeply nested input cannot overflow the stack.
How it is tested
- Godot's own test scripts for 4.7 (in
tests/godot): every script Godot parses parses with no error, and every script Godot rejects gets Godot's first error message, apart from the cases above. - Real projects:
GDSYNTAX_CORPUS=<folders> cargo test -p gdsyntax --test corpuschecks that every file round-trips and parses without errors. - Typed tree: on every valid script, every node of the tree is reachable through
astalone (tests/typed_tree.rs; it also runs onGDSYNTAX_CORPUS). - Broken input: random edits to valid files must never make the parser panic, hang or lose a byte.
Keeping up with Godot
godot.toml pins the Godot release gdsyntax follows (now 4.7.2-stable). just godot-sync check fails when the copied test scripts or identifier tables differ from that release, or when a newer stable release changed Godot's tokenizer, parser, identifier tables or test scripts; CI runs it every week. just godot-sync update copies a newer release's test scripts and tables, moves the pin, and shows what changed in the files to port.
License
Licensed under either of MIT or Apache-2.0, at your option. The tokenizer and parser follow Godot's, which is MIT-licensed (Godot's license).