Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "JSON with Comments" grammar #1050

Open
savetheclocktower opened this issue Jul 6, 2024 · 1 comment
Open

Add a "JSON with Comments" grammar #1050

savetheclocktower opened this issue Jul 6, 2024 · 1 comment

Comments

@savetheclocktower
Copy link
Contributor

savetheclocktower commented Jul 6, 2024

Copied from a draft issue on the WASM/Modern tree-sitter project.

This is an interesting idea because tree-sitter-json properly parses JavaScript-style comments. They aren't automatically errors, which means that tree-sitter-json could just as easily support a JSONC grammar (i.e., “JSON with comments,” used in the VS Code sense) as it does a JSON grammar.

Right now, inserting a comment into a JSON file (using its modern Tree-sitter grammar) has no effect; the comment is highlighted neither as a comment nor as an invalid token.

We could do the following:

  • Create another grammar called “JSONC” (or “JSON (with comments)” or something similar) that's nearly identical to the JSON grammar. They can share the same tree-sitter-json WASM files.
  • Both grammars should use the same existing highlights.scm, but each one should import its own highlights.scm that's unique from the other one:
    • The JSON grammar's unique SCM file would mark comment nodes as invalid.illegal.
    • The JSONC grammar's unique SCM file would mark comment nodes as comment.line or comment.block as appropriate.
  • Since some usages of JSON-with-comments seem to reuse the .json file extension, we could also add an “Allow comments in JSON files” setting to the language-json config schema. If that setting is enabled, the JSON grammar's behavior with comments would be reversed.
@savetheclocktower
Copy link
Contributor Author

There's another draft issue about adding a JSON5 grammar. That would take more effort, since some of JSON5's enhancements aren't understood by tree-sitter-json. There is a community JSON5 tree-sitter parser (though I haven't investigated it too deeply), but since it'd need a different parser, it might as well be a community package unless people clamor for it. (I think JSON5 is a good idea, but it's not exactly catching on in my corners of the development world.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant