Skip to content

Commit

Permalink
Support comments in JSON
Browse files Browse the repository at this point in the history
JSON spec doesn't allow comments, but many implementation support them anyway. Having comments is very convenient, for example, when showing code in blog posts and articles.
  • Loading branch information
werat authored and alecthomas committed Sep 11, 2021
1 parent 02951ce commit f7c1454
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lexers/j/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func jsonRules() Rules {
"whitespace": {
{`\s+`, Text, nil},
},
"comment": {
{`//.*?\n`, CommentSingle, nil},
},
"simplevalue": {
{`(true|false|null)\b`, KeywordConstant, nil},
{`-?(0|[1-9]\d*)(\.\d+[eE](\+|-)?\d+|[eE](\+|-)?\d+|\.\d+)`, LiteralNumberFloat, nil},
Expand All @@ -37,18 +40,21 @@ func jsonRules() Rules {
},
"objectvalue": {
Include("whitespace"),
Include("comment"),
{`"(\\\\|\\"|[^"])*"`, NameTag, Push("objectattribute")},
{`\}`, Punctuation, Pop(1)},
},
"arrayvalue": {
Include("whitespace"),
Include("value"),
Include("comment"),
{`,`, Punctuation, nil},
{`\]`, Punctuation, Pop(1)},
},
"value": {
Include("whitespace"),
Include("simplevalue"),
Include("comment"),
{`\{`, Punctuation, Push("objectvalue")},
{`\[`, Punctuation, Push("arrayvalue")},
},
Expand Down

0 comments on commit f7c1454

Please sign in to comment.