Add source positional information to the AST - #63
Merged
Merged
Conversation
- TsqlAstParser now emits StartOffset/FragmentLength/StartLine/StartColumn for every fragment with real tokens; golden ast.json files regenerated - New ast.Fragment struct embedded in every AST node - Lexer/parser track UTF-16 code-unit offsets and line/column via a source map - Parse functions record node spans; marshaler emits position fields and derives spans for wrapper nodes synthesized during marshaling - Legacy handwritten goldens (PhaseOne_*) compared without positions Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…and literals Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
… filters Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ns, collation Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…o columns Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
… paths Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ration Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…argets Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ages Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
… options Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…tions Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ames Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…cope options Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
… options Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…, openjson Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…njson items Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ool options Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…rals Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…as string Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…oint defaults Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ortance Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
… in derived tables Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…..) selects Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…erences Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…ubqueries Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
…comparisons All 882 comparable positional tests now pass. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011oXvuZCsJPD7Mbk6yNdT3y
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.
Every AST node now carries source position information —
StartOffset,FragmentLength,StartLine, andStartColumn— matching what Microsoft's ScriptDom emits for the same input.Approach
ScriptDom already computes positions for every
TSqlFragment;TsqlAstParserwas explicitly discarding them via aSkipPropertiesset. Removing those four properties from that set gave us authoritative expected values, so no golden file was hand-authored. All 957 regenerableast.jsonfiles were regenerated with the official parser using the loop documented inCLAUDE.md.Regeneration is reproducible: re-running it over the committed goldens produces zero diffs, and
git diffofparser/testdatabetween the regeneration commit and the branch tip is empty — every subsequent commit touched only Go code.Changes
TsqlAstParser/Program.cs— emits the four position properties for fragments with real tokens (StartOffset >= 0); still skipsScriptTokenStreamand token indices, which would bloat the JSON with ScriptDom-internal numbering.ast/fragment.go(new) — aFragmentstruct embedded in all 860 node structs, giving every node uniform position fields and accessors. APin()mechanism marks spans that reproduce ScriptDom quirks narrower than a node's full token extent, so enclosing parse functions don't widen them.parser/source_map.go(new) — converts the lexer's byte offsets into ScriptDom's coordinate system: UTF-16 code-unit offsets and 1-based line/column. This is load-bearing rather than theoretical — 149 test inputs contain non-ASCII text where Go byte offsets and .NET string indices diverge. Lone\rstarts a new line, matching ScriptDom.parser/parser.go— span-recording helpers (spanned,spanFrom,tokSpan,spanTokens,spanFromChild,unspan,trimTrailingSemicolon, and others) plus token-end tracking.parser/parse_*.go,parser/marshal.go— parse functions record spans; the marshaler emits them and derives spans for wrapper nodes synthesized during marshaling.parser/parser_test.go— the 75 handwrittenPhaseOne_*goldens can't be regenerated (the official parser rejects their intentionally partial SQL), so tests whose golden lacks positions are compared with position fields stripped from both sides.Positional quirks
Most of the work was matching ScriptDom's spans, which are not simply "first token through last token":
CREATE VIEWbody'sSELECTdoes notEXISTSpredicates exclude theEXISTSkeyword;ORDERbulk-insert options excludeUNIQUEBUCKET_COUNTindex hints span only their value;COMPRESSION_DELAYexcludes its time unitCLUSTERED/COLUMNSTOREkeywordsBeyond positions
Reaching exact golden parity required a few output corrections worth reviewing separately, since they change AST output beyond position fields:
$(var)database names now emitSqlCommandIdentifierALTER ENDPOINTandCREATE MESSAGE TYPEalways emit their defaulted enum fieldsCONNECTIONis treated as a user data type rather than a SQL data typeThese also fixed 5 pre-existing
PhaseOnetest failures.Testing
go test ./parser/passes (fresh run, 1023 tests)PhaseOnetests pass — 5 were failing before this branch🤖 Generated with Claude Code
Generated by Claude Code