Skip to content

treesitter: lex comments trailing code on a line - #107

Merged
vito merged 4 commits into
mainfrom
block-comment-highlighting
Jun 11, 2026
Merged

vito merged 4 commits into
mainfrom
block-comment-highlighting

Conversation

@vito

@vito vito commented Jun 11, 2026 •

Copy link
Copy Markdown
Owner

Addresses docs feedback on /blocks: comments trailing code on the same line weren't highlighted. While verifying, a sweep of every fenced dang snippet in the docs surfaced more parse failures, which split into two buckets: invalid placeholder syntax in the docs, and valid Dang that the tree-sitter grammar rejected. This PR fixes all of them.

Scanner fix (trailing comments). When a comment trails code (twice { 21 } # note), the external scanner was consulted while the lookahead still sat on the spaces before the #, so comment_token never matched and the comment became an ERROR node. The scanner now skips the space run once, then decides what follows (comment vs. inline-space vs. newline).

Grammar gaps (valid Dang that tree-sitter rejected). Common theme: pigeon backtracks where tree-sitter's lexer must commit.

  • for { break } — the scanner emitted _inline_space before }, committing the parser to break-with-value. Closers/commas can never start a value, so refuse inline space there.
  • """docstring""" before a parameter — the newline after the docstring lexes as a sep (kept valid by the call interpretation), which arg_type couldn't shift, killing the declaration's GLR fork. The generated arg_type now accepts an optional sep there.
  • Multi-line inline fragments — a line starting with ... was treated as a method-chain continuation (leading .); the scanner now looks one char further so spreads get their separator.
  • Newline before a closing delimiter — wrongly emitted as a separator, which block_arg/object_selection/arg_values can't shift (their separators sit between elements). Now plain whitespace.

Docs fixes (invalid placeholder syntax). ... is not a Dang expression, so snippets like greet: String! { ... } lost highlighting entirely. Each placeholder now has a small real body ({ "hi!" }, try { risky() }, real interface fields). Inline fragments (... on User) are real syntax and stay.

Tests

  • treesitter/test/corpus/ — new cases for trailing comments, bare break, docstring params, multi-line fragments, and trailing newlines in block args (34/34 pass)
  • editors/highlights/corpus/comments.txt — Neovim highlight corpus mirroring the /blocks snippet; all trailing comments capture as @comment
  • docs/go/lexer_test.go — docs-site lexer test asserting trailing comments tokenize as chroma.Comment
  • a sweep of every fenced dang block under docs/lit/ now parses clean

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

vito added 2 commits June 11, 2026 01:14
A comment after code on the same line (`twice { 21 } # note`) parsed
as an ERROR node, so the docs site and editors dropped its
highlighting. The external scanner owns the comment_token extra, but
it was only ever consulted while the lookahead still sat on the
spaces before the `#`; scan_comment_token refused anything that
isn't a `#`, and the internal lexer cannot lex an external token
after skipping those spaces itself. Comments at the start of a line
worked only because there was no leading space at lex time.

Skip the space/tab run once in the scanner dispatch, then decide by
what follows: a `#` becomes a comment, and an expected _inline_space
is emitted as a zero-width token at the value's position (folding
the old scan_inline_space into the dispatch). Cover the fix with a
parse corpus case, a Neovim highlight corpus case, and a docs lexer
test mirroring the /blocks snippet from the docs feedback.

Signed-off-by: Alex Suraci <[email protected]>
The playground ships the tree-sitter grammar compiled to wasm, which
embeds the external scanner; rebuild it (via dagger generate) so it
picks up the trailing-comment lexing fix.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the block-comment-highlighting branch from ae835d4 to bda8d9a Compare June 11, 2026 01:14
vito added 2 commits June 11, 2026 01:36
Several constructs that the real parser accepts produced ERROR nodes
in tree-sitter, killing highlighting for whole docs snippets. The
common theme is that pigeon backtracks where tree-sitter's lexer
must commit.

Bare `break`/`return` inside a one-line block (`for { break }`)
failed because the scanner emitted _inline_space before `}`, forcing
the parser into the take-a-value branch. A closer or comma can never
start a value, so refuse inline space there.

A `"""docstring"""` before a parameter failed because the newline
after it lexes as the sep the call interpretation keeps valid, which
arg_type couldn't shift; the GLR fork for the declaration died.
Patch the generated arg_type to accept an optional sep after the
docstring.

Multi-line inline fragments (`... on User { ... }` on its own line)
failed because a leading `.` was treated as method-chain
continuation; look one character further so `...` spreads still get
their separator.

A newline before a closing delimiter also wrongly became a
separator, which block_arg/object_selection/arg_values (separators
between elements, unlike Block) cannot shift. Treat it as plain
whitespace — no rule requires a separator there.

Regenerate the grammar and playground wasm, extend the corpus with
each construct, and update expectations (trailing seps before
closers no longer appear in trees).

Signed-off-by: Alex Suraci <[email protected]>
`...` is not a Dang expression, so snippets using it as a body
placeholder fail to parse and lose syntax highlighting entirely
(reported via docs feedback on /fields). Give each placeholder a
small real body instead: `greet` returns "hi!", `try` wraps a
`risky()` call, and the implements examples declare the fields their
interfaces require. Inline fragments (`... on User`) are real syntax
and stay as they were.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 7dec185 into main Jun 11, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant