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

Lexer request: add jsonc lexer #1680

Closed
Mesteery opened this issue Jan 13, 2021 · 7 comments
Closed

Lexer request: add jsonc lexer #1680

Mesteery opened this issue Jan 13, 2021 · 7 comments
Labels
A-lexing area: changes to individual lexers

Comments

@Mesteery
Copy link
Contributor

Add a JSONC lexer, a basic JSON with comments (// inline and /* block */).

@Anteru Anteru added good first issue Good for newcomers A-lexing area: changes to individual lexers labels Feb 6, 2021
@Anteru
Copy link
Collaborator

Anteru commented Feb 6, 2021

@kurtmckee Does this sound like something you could look at?

@kurtmckee
Copy link
Contributor

kurtmckee commented Feb 6, 2021 via email

@kurtmckee
Copy link
Contributor

After review, it appears that every JSONC proposal (and there are multiple) is just an attempt to make a subset of Javascript into a superset of JSON.

In other words, Pygments' Javascript lexer appears to be more than sufficient to parse JSON with comments added.

image

  1. The Javascript lexer already parses JSON with comments.
  2. No single proposal going by the name "JSONC" is a clear winner of the term.

For this reason, I recommend closing this ticket and directing users to use the full Javascript lexer.

@Anteru
Copy link
Collaborator

Anteru commented Feb 14, 2021

Perfect, thanks for checking. I wasn't aware you handle comments already -- and yes, it totally makes sense to not cater for various JSONC flavors if all they add are comments.

@alainpannetier
Copy link

Does that work?

{
"a" : "b"
}
Is single colour in javascript and two-colours ("key": "value") in json.

So javascript cannot work for json.

@kurtmckee
Copy link
Contributor

@alainpannetier, you're correct that the object keys and string values are a single color in Javascript. This is because the Javascript lexer interprets anything in double quotes as a String.Double token. However, that does not mean that the Javascript lexer cannot be used for JSON. It just means the Javascript lexer could be improved so that it treats object keys as Name.Tag tokens.

As I wrote above, every specification that introduces comments in JSON (and there are many!) just introduces features that are already handled by the Javascript parser. Whether it's called "JSONC", "JSON-C", or "JSON5", it appears that each specification introduces comments and other things that make them something more than JSON...but something less than Javascript, which already supports all of their proposed features.

┌──────────────────────────────────┐
│            Javascript            │
│                                  │
│  ┌────────────────────────────┐  │
│  │    JSONC, JSON-C, JSON5    │  │
│  │                            │  │
│  │          ┌──────┐          │  │
│  │          │ JSON │          │  │
│  │          └──────┘          │  │
│  │                            │  │
│  └────────────────────────────┘  │
│                                  │
└──────────────────────────────────┘

My recommendation is to target an improvement to the Javascript parser.

@kurtmckee
Copy link
Contributor

I've added comment support to JSON in #2049. It's not part of the JSON spec but adding it doesn't affect parsing of valid JSON documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lexing area: changes to individual lexers
Projects
None yet
Development

No branches or pull requests

4 participants