feat: add ASTTransformer for in-place AST node replacement#80
Merged
Conversation
Adds ASTTransformer, an iterative depth-first visitor that supports replacing AST nodes during traversal. Unlike the existing ASTVisitor, the transformer propagates replacements back to parent nodes and supports root replacement. Key design decisions: - Stack-based iterative traversal (no recursion limit issues) - Direct parent tracking via (key, container) tuples (O(1) replacement) - Replacement node's children are visited (not original's) - Identity check (replacement is not node) avoids unnecessary writes - Returns new root from visit(), enabling full tree transformation Includes 15 comprehensive tests covering: - Value replacement, statement replacement, deep nesting - Root replacement, recursive folding, round-trip identity - None/no-op semantics, list child iteration, parent class dispatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The existing
ASTVisitorandASTRecursiveVisitoronly support read-only traversal. There is no way to transform or replace AST nodes during traversal, which is needed for tasks like:Solution
Adds
ASTTransformer, a production-grade AST visitor that supports in-place node replacement.Design
(key, container)tuples — O(1) replacement, no brute-force key searchreplacement is not nodeavoids unnecessary mutationvisit()Usage
Comparison with PR #74
This implementation improves on the original PR by:
Tests
15 tests covering:
Closes #74