Loading GDScript classes from multiple local files in headless script mode

Godot Version

4.7.2

Question

Hello! I am trying to make a stand alone GDScript that analyzes the AST of an arbitrary .gd script file. I’m hoping the gdscript-ast asset can get me most the way there. My goal is to FileAccess.get_file_as_string("some_script.gd") then use GDScriptTokenizer.tokenize(content) to tokenize (then parse) the script.

My problem is, I’m having trouble accessing the GDScriptTokenizer class. I downloaded the files and have file gds_tokenizer.gd which contains class_name GDScriptTokenizer in it … but when I run godot --headless --script analyze.gd, it says GDScriptTokenizer is an invalid token. I tried to load("gds_tokenizer.gd") but that complains about the recursive dependencies not being defined.

I am using a text editor and no Godot projects; ideally I want a stand along GDScript I can simply run. The end goal here is to be able to run godot --headless --script generate -- input.gd and have it dump an AST representation.

How do I go about loading and making the classes available to my script?

Why? What are you trying to accomplish? That’s not how GDScript is meant to be used!

I know.

This is for the GDScript on Exercism.org where we terribly misuse languages. We have exercises people can solve using all sorts of “languages” including Python, SQLite, jq, bash and GDScript. We have a working test running that runs a test suite against the solutions. The GDScript test suite runs fine using godot --headless --scriptand a custom test framework. It’s not pretty but it works.

Now I’m trying to figure out with I can generate solution AST to build a representer.

Is this a bad idea? Possibly. Is it how GDScript is meant to be used? Nope! Is it fun and a bit wacky? Yes :smiley:

Is gdscript-ast an editor plugin?

Yes, I believe it is an editor plugin. But I’m trying to use the underlying GDScript code that powers it without using the plugin parts.

More generally, is there an easy way to load other classes in the same directory in a headless GDScript automatically, ie without an explicit load/preload?

Any insights into loading classes from GDScript? Or is that not possible?