fix(parsers/zig): align function_extractor with the real tree-sitter-zig grammar + lazy import#87
Open
gadievron wants to merge 1 commit into
Open
Conversation
…zig grammar + lazy import
Pinned against the actually installed grammar (tree-sitter-zig 1.1.2) by
AST-probing it directly; several AST node-type names had drifted. Three defects
in libs/openant-core/parsers/zig/function_extractor.py:
1. Struct extraction was dead and fields could be emitted as functions. Struct
handling gated on node types the 1.1.2 grammar never emits (VarDecl /
container_decl / ContainerDecl); the grammar emits variable_declaration and
struct_declaration, so total_classes was always 0 and the literal
container_field->method path was unreachable. Corrected the node-type names;
methods now flow through the recursive walk which only treats
function_declaration as a unit, so container_field (struct FIELDS) are never
emitted. The dedicated _extract_struct_methods helper (home of the original
container_field bug) is removed as superseded.
2. A struct method was emitted twice (qualified Type.method by the eager scan,
then bare by an unconditional recursion that never threaded current_struct).
Now thread the struct name into the recursion so a method produces the same
func_id and the dict de-duplicates instead of producing a bare twin. Also fix
a name-capture bug in _extract_function (it grabbed the last identifier,
recording `fn init(...) Point {` under the return type `Point`); it now
captures the first identifier and stops. Regression test pins that the de-dup
does not drop nested struct / method / @import.
3. tree_sitter_zig was imported at module top level and called at class-body
time, raising ImportError in any clean install lacking the package, which was
declared in neither pyproject.toml nor requirements.txt. Load the grammar
lazily on first FunctionExtractor construction with a clear error message, and
declare tree-sitter-zig>=1.1.2 in pyproject.toml + requirements.txt.
(call_graph_builder.py carries the same top-level import but is out of this
change's scope; the dependency declarations cover it too.)
New tests/parsers/zig/test_zig_extractor_followup.py (loaded via importlib unique
name because function_extractor.py recurs across parser packages). RED at base
6 failed / 1 passed; GREEN 7 passed. Full suite 183 passed, 63 skipped, 0 failed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Pinned against the actually installed grammar (tree-sitter-zig 1.1.2) by
AST-probing it directly; several AST node-type names had drifted. Three defects
in libs/openant-core/parsers/zig/function_extractor.py:
Struct extraction was dead and fields could be emitted as functions. Struct
handling gated on node types the 1.1.2 grammar never emits (VarDecl /
container_decl / ContainerDecl); the grammar emits variable_declaration and
struct_declaration, so total_classes was always 0 and the literal
container_field->method path was unreachable. Corrected the node-type names;
methods now flow through the recursive walk which only treats
function_declaration as a unit, so container_field (struct FIELDS) are never
emitted. The dedicated _extract_struct_methods helper (home of the original
container_field bug) is removed as superseded.
A struct method was emitted twice (qualified Type.method by the eager scan,
then bare by an unconditional recursion that never threaded current_struct).
Now thread the struct name into the recursion so a method produces the same
func_id and the dict de-duplicates instead of producing a bare twin. Also fix
a name-capture bug in _extract_function (it grabbed the last identifier,
recording
fn init(...) Point {under the return typePoint); it nowcaptures the first identifier and stops. Regression test pins that the de-dup
does not drop nested struct / method / @import.
tree_sitter_zig was imported at module top level and called at class-body
time, raising ImportError in any clean install lacking the package, which was
declared in neither pyproject.toml nor requirements.txt. Load the grammar
lazily on first FunctionExtractor construction with a clear error message, and
declare tree-sitter-zig>=1.1.2 in pyproject.toml + requirements.txt.
(call_graph_builder.py carries the same top-level import but is out of this
change's scope; the dependency declarations cover it too.)
New tests/parsers/zig/test_zig_extractor_followup.py (loaded via importlib unique
name because function_extractor.py recurs across parser packages). RED at base
6 failed / 1 passed; GREEN 7 passed. Full suite 183 passed, 63 skipped, 0 failed.
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com