Skip to content

[GDScript] refactor, extract the AST from the Parser - #125641

Open
hogoww wants to merge 3 commits into
godotengine:masterfrom
hogoww:extract_ast
Open

hogoww wants to merge 3 commits into
godotengine:masterfrom
hogoww:extract_ast

Conversation

@hogoww

@hogoww hogoww commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What problem(s) does this PR solve?

  • Extracting the AST from the Parser
  • Step toward a more manageable compiler architecture, as mentionned by @HolonProduction in this PR

Additional information

@HolonProduction expressed their interest for this change that I had done in a discord post.

The moving the AST out of GDScriptParser part, hell yeah (with naming tweaks).

Currently, the references to GDScriptParser are everywhere in the compiler architecture.
Extracting the AST doesn't reduce much those dependencies at the moment, but it should in the future.
The AST is in the new directory module/gdscript/toolchain as suggested in this PR.

The diff is huge because nodes are everywhere in the GDScript files, but there are very few actual changes, and it's mostly renaming to the new namespace.

To try to ease the review:

  • I did the smallest step I could think of to get to an AST independent from the Parser;
  • I tried to keep formatting & ordering as close to the initial state as possible to ease the review.
  • I added a review for every files > 10 lines to try to ease the burden of reviewers.
    I highlighted whether the file had only namespaces changes, and the actual semantic variations.
    (Sorry there are multiple reviews github doesn't seem to allow edits (that I found), and it took me a couple of hours to check again every file in github's interface).

Cliffnotes:

  • This PR extracts from the gdscript_parser.* files to the gdscript_ast.* files every node of the AST as well as the structs DataType, AnnotationInfo, ClassDocData and MemberDocData in the new namespace GDScriptAST, reduced to AST in files using it heavily.
    Although (Class|Member)DocData are not strictly nodes, I think they make sense with the AST.
    DataType makes more sense in the AST than in the parser, but would make outside the AST too.
    See last point for AnnotationInfo details.
    Node that, GDScriptAST is a class rather than a namespace (even though it has no state) because using a namespace would result in partial type errors, due to methods defined in the header.

  • This PR extracts the TreePrinter from gdscript_parser.* to the gdscript_ast.* files.
    The TreePrinter is also currently outside the GDScriptAST class.
    It's pretty independent already, and could be moved anywhere trivially, so it's a matter of deciding where it should be.

  • About the naming, @HolonProduction told me to use the AST alias like they started doing in this PR.

Namespace should be named GDScriptAST but aliased to AST in GDScript specific cpp files for brevity.

  • The crux of the dependencies is around the annotations code.
    There's a circular dependency between AnnotationInfo, AnnotationAction and GDScriptParser.
    Current solution is to have GDScriptParser as a friend of GDScriptAST to keep AnnotationInfo private except from the parser (which is similar semantics to the current code).
    I removed a member function from AnnotationNode to reduce its dependency on the parser, as it doesn't seem to need the double dispatch.
    Having the annotation methods on GDScriptParser creates a dependency the AST in the AST, and maybe they shouldn't be there, but I don't know whether the GDScriptParser state is required in these methods.
    I'm still untangling that part a bit, trying to find a better solution.

Please let me know if you think something else should be extracted.

Note

I'm fairly sure that GDScriptParser holds information that should be somewhere in the AST (e.g., is_tool) but that should be tackled in a separate PR, as this is already a huge step.

Edit: add a precision from Holon about naming.
Edit2: Try to clear up description, update some information.

@hogoww
hogoww requested review from a team as code owners November 19, 2026 14:54

@hogoww hogoww left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubled check about half the file so far, and indicated what's relevant for each file, to try to ease other people's review.
I'll finish a bit later

Comment thread modules/gdscript/gdscript.cpp Resolved
Comment thread modules/gdscript/gdscript_analyzer.cpp Resolved
Comment thread modules/gdscript/gdscript_analyzer.h Resolved

@hogoww hogoww left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished the triple checking.
I highlighted "important things", I hope it'll help.
I also caught a couple of difference in formatting that I'm gonna fix.

Comment thread modules/gdscript/gdscript_compiler.cpp Resolved
Comment thread modules/gdscript/gdscript_editor.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
Comment thread modules/gdscript/gdscript_parser.h Resolved
Comment thread modules/gdscript/gdscript_parser.h Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
@hogoww
hogoww marked this pull request as draft November 19, 2026 18:05

@hogoww hogoww left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Updated the initial description, to try to make it clearer.
  • Change the dependency between the GDScriptParser and AnnotationNode
  • Updated all the review comments to reflect current state.

Comment thread modules/gdscript/gdscript_parser.h Resolved
Comment thread modules/gdscript/gdscript_parser.cpp Resolved
@hogoww
hogoww marked this pull request as ready for review November 20, 2026 15:52

@HolonProduction HolonProduction left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As already mentioned I'm generally in favor of this change. At the moment GDScriptParser both holds parsing results (which are passed on to analyzer, compiler, etc.) and intermediate state relevant during parsing (which no other component should rely on). Holding them together doesn't make all that much sense.

  • Keeping annotation functions in the parsers, is fine.
  • GDScriptParser still owning the tree is also fine for now. This PR is about a structural split. The ownership split can come later.

Given the size of the patch we need to think a bit about git blame. To me the changes here can clearly be split into two categories:

  1. Refactoring (moving stuff, extracting functions)
  2. Qualifying (changing the qualification of things from GDScriptParser (or no qualification) to AST/GDScriptAST.

The second kind is boring, trivial and self contained and touches a lot of complex code. As such I'd like to exclude those changes from git blame.

To achieve that you'll need to follow a strict commit structure:

  1. commit:
    • Do refactoring changes (move stuff to gdscript_ast.*) and extract the functions.
    • Create aliases for all the types in GDScriptParser (e.g using ClassNode = GDScriptAST::ClassNode;)
    • Already create the using AST = GDScriptAST aliases.
  2. commit:
    • Do all the qualification changes.
  3. commit:
    • add the hash of the second commit to .git-blame-ignore-revs
    • Remove the aliases from GDScriptParser

With that structure the GDScriptParser -> AST changes won't appear in git blame, while the other refactoring things do. Doing the additional aliases in the analyzer ensures that each commit is well formed C++ which IMO is a good rule of thumb to follow.

(You don't need to rewrite all this stuff. You can just use git reset (WITHOUT --hard) to undo your commits and then redistribute the changes into new commits.)

If you want feel free to do the 1. commit as dedicated PR since applying changes with this commit structure might be annoying. For the stuff I already checked I don't expect many change requests (beyond what is included here). I haven't checked the code moved between the .h files yet though.

Comment thread modules/gdscript/gdscript.cpp Resolved
Comment thread modules/gdscript/gdscript_linter.cpp Resolved
Comment thread modules/gdscript/gdscript_linter.h Resolved
@hogoww
hogoww force-pushed the extract_ast branch 3 times, most recently from 07babc6 to 76050db Compare November 24, 2026 10:41
@AThousandShips AThousandShips changed the title [gdscript] refactor, extract the AST from the Parser [GDScript] refactor, extract the AST from the Parser Nov 24, 2026
@hogoww

hogoww commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Done.
Sorry for the flood, I had to test all 3 commits.
CI step 1
CI step 2
Nothing else to report, I think.

If there are a lot of comments, I'll open another pull request for step 1, as you proposed.
It was indeed a pain, but quite worthwhile one.

@hogoww

hogoww commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

While working on a different thing, I had a weird error.
Basically, that's a missing include, that should be in commit 1 that becomes necessary in a special case that I don't know.
I'll fix it if necessary when it becomes relevant, I guess 4.9, to not rerun the CI on all 3 commits now.

(Ref<Script>, Script wasn't defined, but that's in the template only, which wasn't needed to be resolved apparently, until i added some other irrelevant code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants