Repository navigation
treesitter: lex comments trailing code on a line - #107
Merged
Merged
Conversation
Deploying dang with
|
| Latest commit: |
0e026bd
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://97d5a071.dang-3kk.pages.dev |
| Branch Preview URL: | https://block-comment-highlighting.dang-3kk.pages.dev |
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
force-pushed
the
block-comment-highlighting
branch
from
June 11, 2026 01:14
ae835d4 to
bda8d9a
Compare
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]>
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.
Addresses docs feedback on
/blocks: comments trailing code on the same line weren't highlighted. While verifying, a sweep of every fenceddangsnippet 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#, socomment_tokennever 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_spacebefore}, 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), whicharg_typecouldn't shift, killing the declaration's GLR fork. The generatedarg_typenow accepts an optional sep there....was treated as a method-chain continuation (leading.); the scanner now looks one char further so spreads get their separator.block_arg/object_selection/arg_valuescan't shift (their separators sit between elements). Now plain whitespace.Docs fixes (invalid placeholder syntax).
...is not a Dang expression, so snippets likegreet: 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, barebreak, 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@commentdocs/go/lexer_test.go— docs-site lexer test asserting trailing comments tokenize aschroma.Commentdangblock underdocs/lit/now parses clean🤖 Generated with Claude Code