From f7c1454f132ccd52acb6a79c313daf63dc219c11 Mon Sep 17 00:00:00 2001 From: Andy Yankovsky Date: Sun, 1 Aug 2021 18:21:54 +0200 Subject: [PATCH] Support comments in JSON 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. --- lexers/j/json.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lexers/j/json.go b/lexers/j/json.go index 9a4a3ee..daf6dc3 100644 --- a/lexers/j/json.go +++ b/lexers/j/json.go @@ -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}, @@ -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")}, },