parser: record scanned comments and stamp table/column positions - #46
Merged
Merged
Conversation
Record the comments the lexer skips — '-- ' and '#' line comments and '/* */' block comments — as byte-offset spans on the Scanner, exposed through Scanner.Comments and Parser.Comments. Executable comments and optimizer hints are lexed as SQL, so they are not recorded, and recording is gated on strictly increasing positions because the scanner's lookaheads re-read source text. Stamp OriginTextPosition on FROM-clause table factors, INSERT/REPLACE table references and INSERT column lists, which goyacc left unstamped (it stamped only <expr> symbols). Consumers placing comments or errors need these positions; only Accept-reachable nodes are stamped so the restore round-trip tests can keep resetting them. This gives sqlc's MySQL engine what its ParseFile needs to power sqlc fmt: statements and comments from one lexer pass, with enough positions to anchor comments and the author's line breaks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C48orUYrL7xE989UGLTVqi
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.
This gives sqlc's MySQL engine what its
ParseFileneeds to powersqlc fmt: statements and comments from one lexer pass, with enough positions to anchor comments and the author's line breaks.Comment recording
The lexer now records every comment it skips —
--and#line comments (span excludes the terminating newline) and/* */block comments (span includes the closing*/) — as byte-offset spans on theScanner, exposed throughScanner.Comments()and a newParser.Comments()that returns the comments from the most recent parse./*! ... */, recognized/*T[...] ... */) and optimizer hints (/*+ ... */) are lexed as SQL, so they are not recorded; a misplaced hint, which MySQL treats as a plain comment, is.getNextTokenand friends) save and restore the reader, so the same comment can be scanned more than once; recording is gated on strictly increasing positions to keep one entry per comment.Position stamping
OriginTextPositionis now stamped on FROM-clause table factors, INSERT/REPLACE table references, and INSERT column lists, which goyacc left unstamped (it stamped only<expr>symbols). Consumers placing comments or preserving author line breaks need these positions, and 0 (the unstamped value) is indistinguishable from the start of the input.Only
Accept-reachable nodes are stamped, so the restore round-trip tests can keep resetting positions;parseTableNameandparseColumnNameListthemselves stay unstamped because other call sites hang their nodes off structuresAcceptskips (ALTER…UNION options,FOR UPDATE OFlists, procedure names).Both changes are additive: no existing node, field, or
Restoreoutput changes, and stamping respectsSkipPositionRecording.Testing
New
parser/comments_test.gocovers every comment syntax, the lookahead-dedup case, the not-a-comment cases (--x, executable comments, hints), and per-parse reset.go test ./... -count=1 -timeout 120sis green, and the sqlc side (sqlc-dev/sqlc branchclaude/parsefile-sqlc-fmt-marino-x6v056) passes sqlc's full--tags=examplessuite against this commit.🤖 Generated with Claude Code
https://claude.ai/code/session_01C48orUYrL7xE989UGLTVqi
Generated by Claude Code