json.go

 1package j
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// JSON lexer.
 9var JSON = internal.Register(MustNewLexer(
10	&Config{
11		Name:         "JSON",
12		Aliases:      []string{"json"},
13		Filenames:    []string{"*.json"},
14		MimeTypes:    []string{"application/json"},
15		NotMultiline: true,
16		DotAll:       true,
17	},
18	Rules{
19		"whitespace": {
20			{`\s+`, Text, nil},
21		},
22		"simplevalue": {
23			{`(true|false|null)\b`, KeywordConstant, nil},
24			{`-?(0|[1-9]\d*)(\.\d+[eE](\+|-)?\d+|[eE](\+|-)?\d+|\.\d+)`, LiteralNumberFloat, nil},
25			{`-?(0|[1-9]\d*)`, LiteralNumberInteger, nil},
26			{`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
27		},
28		"objectattribute": {
29			Include("value"),
30			{`:`, Punctuation, nil},
31			{`,`, Punctuation, Pop(1)},
32			{`\}`, Punctuation, Pop(2)},
33		},
34		"objectvalue": {
35			Include("whitespace"),
36			{`"(\\\\|\\"|[^"])*"`, NameTag, Push("objectattribute")},
37			{`\}`, Punctuation, Pop(1)},
38		},
39		"arrayvalue": {
40			Include("whitespace"),
41			Include("value"),
42			{`,`, Punctuation, nil},
43			{`\]`, Punctuation, Pop(1)},
44		},
45		"value": {
46			Include("whitespace"),
47			Include("simplevalue"),
48			{`\{`, Punctuation, Push("objectvalue")},
49			{`\[`, Punctuation, Push("arrayvalue")},
50		},
51		"root": {
52			Include("value"),
53		},
54	},
55))