Make AST spans and visitors reliable for source transformations - #231
Merged
Conversation
someone235
previously approved these changes
Aug 29, 2026
someone235
force-pushed
the
source-aware-ast-api
branch
from
August 29, 2026 06:17
ec41d86 to
7f1fa35
Compare
Build infix and postfix spans from parser-pair extents instead of normalized child AST spans. This retains grouping delimiters and prevents syntactically incomplete source ranges. Cover casts, field and index access, suffix operations, chained postfix expressions, and grouped infix operands while keeping identifier-name spans exact.
Add exact target-struct and typed-array type spans to the public AST for source transformations. Capture the spans during parsing and preserve them through compiler rewrites. Include the new spans in mutable visitor traversal and cover exact source ranges plus span mutation in regression tests.
Expose a parser entry point for exactly one Sil statement without wrapping it in a synthetic function. Returned nodes retain spans into the caller's original source. Cover required terminators, surrounding comments, supported statement shapes, and rejection of incomplete or multiple statements.
Add a uniform visit_type callback across declarations, bindings, typed arrays, and struct-oriented syntax. String-backed struct references are exposed as temporary scalar custom TypeRefs, while their source spans remain anchored at the authored base name. Complete contract traversal through structs and classify struct declaration names. Cover all type-bearing AST shapes, leading trivia, span mutation order, and nested expression traversal.
Drive return-type and attribute-path traversal from semantic vectors, supplying synthetic spans when deserialized AST metadata is absent. Cover both callbacks with a serialized AST regression.
someone235
force-pushed
the
source-aware-ast-api
branch
from
August 29, 2026 06:22
7f1fa35 to
77c9837
Compare
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.
Context
Argent uses the Sil parser and mutable AST visitor to transform embedded Sil source.
It uses the AST to identify semantic nodes. It then edits their ranges in the original source, reparses the result, and checks that the transformation is complete.
This process requires correct source spans and complete AST traversal.
Problem
Some composed expressions had incomplete spans.
For example:
Sil removed the grouping node before it built the outer expression. The resulting span could contain the closing parenthesis but omit the opening parenthesis.
Other API gaps required consumers to:
Change
This PR:
parse_statement_astfor exactly one standalone statement;visit_typefor AST-classified type occurrences;Plain parentheses remain normalized away. Exact inner spans remain available. For example, the identifier inside
((value))still has the name spanvalue.Sil remains a semantic AST. This PR does not make it a concrete syntax tree and does not preserve comments or formatting through AST rendering.
Consumer
Argent PR #52 uses these APIs to remove source-transformation workarounds.
Argent will update its Sil revision to the merged master commit before it merges.
Validation
Tests cover: