Skip to content

fix: read comment markers in a text block as its content - #238

Closed
knutwannheden wants to merge 1 commit into
tree-sitter:masterfrom
knutwannheden:fix/text-block-comment-markers
Closed

knutwannheden wants to merge 1 commit into
tree-sitter:masterfrom
knutwannheden:fix/text-block-comment-markers

Conversation

@knutwannheden

Copy link
Copy Markdown

A text block whose content holds // or /* can lose its closing delimiter to a comment. This valid Java does not parse:

class C {
    String s = """
        //comment""";
}
(program
  (ERROR
    (identifier)
    (type_identifier)
    name: (identifier)
    (line_comment)
    (multiline_string_fragment)))

Where the comment stops short of the delimiter, the file parses, but the string holds a comment node:

    String s = """
        /* "quoted" */
        """;
(string_literal
  (block_comment)
  (multiline_string_fragment))

Comments are extras, so the lexer may match one wherever a text block expects a fragment. The fragment /[^"\\]+/ and the comments all have precedence 0, so the longer match wins. A comment runs to the end of its line or to its */, while the fragment stops at the next " or \.

The [^"\\]+ alternative of _multiline_string_fragment takes prec(1), as string_fragment already does. That is why "//c\"x\"" parses: lexical precedence outranks match length. The alternative that starts with " keeps precedence 0. No comment can start at a ", and at precedence 1 it would beat the closing """.

The new corpus test text block with comment markers holds both inputs and fails without the change. All corpus tests pass with it. src/ is not regenerated here.

A comment is an extra, so the lexer may match one wherever the text block
expects a fragment. With equal precedence the longer match wins, and a
comment runs to the end of its line or its `*/` where a fragment stops at
the next `"` or `\`. So `//comment"""` read as a line comment that took the
closing delimiter, and `/* "quoted" */` became a block comment inside the
string.

The fragment that excludes `"` and `\` takes precedence 1, as string_fragment
does. The fragment that starts with `"` keeps precedence 0: a comment cannot
start where it does, and at precedence 1 it would win over the closing `"""`.
@wetneb

wetneb commented Nov 27, 2026

Copy link
Copy Markdown

Hi @knutwannheden,

Thanks for working on this. This repository isn't actively maintained, so we have been maintaining a fork which has already integrated many improvements, meaning that the example you provide above already parses correctly there.

If you encounter any issue switching to our fork, I would be happy to know what needs improving on our side.

@knutwannheden

Copy link
Copy Markdown
Author

@wetneb Thanks for letting me know!

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.

2 participants